Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.cdlflex.ui.behavior;
 
 import java.util.Set;
 
A CssClassNameAppender is an AttributeAppender that appends given strings to the class html attribute. It keeps classes unique within one attribute, making the operation idempotent.

Example:
The markup

 <span class="className" wicket:id="foo">
 
Using the following CssClassNameAppenders
 label.add(new CssClassNameAppender("className2", "className3"));
 label.add(new CssClassNameAppender("className2"));
 
will yield
 <span class="className className2 className3" wicket:id="foo" >
 
(Not necessarily in that order)
 
 public class CssClassNameAppender extends AttributeAppender {
 
     private static final long serialVersionUID = 1L;

    
The name of the html class attribute name.
 
     public static final String ATTRIBUTE_NAME = "class";
 
     public CssClassNameAppender(IModel<?> replaceModel) {
         this(replaceModel" ");
     }
 
     public CssClassNameAppender(String... values) {
         this(Strings.join(" "values));
     }
 
     public CssClassNameAppender(Serializable value) {
         this(, Model.of(value), " ");
     }
 
     protected CssClassNameAppender(String attributeIModel<?> appendModelString separator) {
         super(attributeappendModelseparator);
     }
 
     @Override
     protected String newValue(String currentValueString appendValue) {
         return normalize(getSeparator(), currentValueappendValue);
     }

    
Takes a string of separated values, interprets them as a collection, retains unique values and joins them using the separator again.

Example: A string "a b a", using a whitespace as separator, would yield "a b".

Parameters:
separator the separator
values the values to normalize together
Returns:
a normalized string
 
     protected static String normalize(String separatorString... values) {
         Set<Stringset = new HashSet<>(values.length);
 
         for (String value : values) {
             if (Strings.isEmpty(value)) {
                continue;
            }
            String[] split = value.trim().split(separator);
            set.addAll(Arrays.asList(split));
        }
        return Strings.join(separatorset);
    }

    
Appends the given CSS classes to the given ComponentTag. It preserves the class attributes set in the ComponentTag. CSS classes are never duplicated.

Parameters:
tag the component tag
cssClasses the classes to append
    public static void append(ComponentTag tagString... cssClasses) {
        String currentValue = tag.getAttribute("class");
        String appendValue = Strings.join(" "cssClasses);
        tag.put("class"normalize(" "currentValueappendValue));
    }

    
Appends the css class the given ICssClassNameProvider provides to the given COmponentTag. It preserves the class attributes set in the ComponentTag. CSS classes are never duplicated.

Parameters:
tag the component tag
classNameProvider the class name provider
    public static void append(ComponentTag tagICssClassNameProvider classNameProvider) {
        append(tagclassNameProvider.getCssClassName());
    }

    
Appends the css class the given ICssClassNameProvider provides to the given COmponentTag. It preserves the class attributes set in the ComponentTag. CSS classes are never duplicated.

Parameters:
tag the component tag
classNameProvider the class name provider
    public static void append(ComponentTag tagIModel<? extends ICssClassNameProviderclassNameProvider) {
        append(tagclassNameProvider.getObject());
    }
New to GrepCode? Check out our FAQ X