
de.agilecoders.wicket.markup.html.bootstrap.behavior.CssClassNameAppender Maven / Gradle / Ivy
package de.agilecoders.wicket.markup.html.bootstrap.behavior;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.string.Strings;
import java.util.List;
/**
* A CssClassNameAppender appends the given value, rather than replace it. This is especially useful
* for adding CSS classes to markup elements.
*
*
* <span class="className" wicket:id="foo">
*
*
* can be modified with these CssClassNameAppender:
*
*
* link.add(new CssClassNameAppender("className2"));
* link.add(new CssClassNameAppender(Arrays.asList("className2","className3")));
*
*
* this will result in the following markup:
*
*
* <span class="className className2 className3" wicket:id="foo" >
*
*
* @author miha
* @version 1.0
*/
public class CssClassNameAppender extends AttributeAppender {
/**
* Separator between the current value and the concatenated value.
*/
protected static final String SEPARATOR = " ";
/**
* The name of the html class attribute name.
*/
protected static final String ATTRIBUTE_NAME = "class";
/**
* @return separator between the current value and the concatenated value.
*/
public static String separator() {
return SEPARATOR;
}
/**
* Creates an AttributeModifier that appends the appendModel's value to the current value of the
* class attribute, and will add the attribute when it is not there already.
*
* @param appendModel the model supplying a single value to append
*/
public CssClassNameAppender(IModel appendModel) {
super(ATTRIBUTE_NAME, appendModel, SEPARATOR);
}
/**
* Constructor.
* {@link CssClassNameAppender#CssClassNameAppender(org.apache.wicket.model.IModel)}
*
* @param appendValue one or more values to append
*/
public CssClassNameAppender(String... appendValue) {
this(Lists.newArrayList(appendValue));
}
/**
* Constructor.
* {@link CssClassNameAppender#CssClassNameAppender(org.apache.wicket.model.IModel)}
*
* @param appendValueList a list of values to append
*/
public CssClassNameAppender(List appendValueList) {
this(Model.of(Joiner.on(SEPARATOR).skipNulls().join(appendValueList)));
}
/**
* Constructor.
* {@link CssClassNameAppender#CssClassNameAppender(org.apache.wicket.model.IModel)}
*
* @param cssClassNameProvider a css class name provider
*/
public CssClassNameAppender(CssClassNameProvider cssClassNameProvider) {
this(Model.of(cssClassNameProvider.cssClassName()));
}
@Override
protected String newValue(String currentValue, String appendValue) {
// Short circuit when one of the values is empty: return the other value.
if (Strings.isEmpty(currentValue)) {
return appendValue != null ? appendValue : null;
} else if (Strings.isEmpty(appendValue)) {
return currentValue != null ? currentValue : null;
}
List values = Lists.newArrayList(Splitter.on(separator()).trimResults().split(currentValue));
List appendValues = Lists.newArrayList(Splitter.on(separator()).trimResults().split(appendValue));
StringBuilder sb = new StringBuilder(currentValue);
for (String append : appendValues) {
if (!values.contains(append) && append != null && !"".equals(append)) {
sb.append(separator());
sb.append(append);
}
}
return sb.toString().trim();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy