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.markup.html.form;
 
 import java.util.List;
 
An adapted CheckBoxMultipleChoice that renders like Bootstrap's buttons.

Parameters:
<T> Model object type
 
 public class ButtonMultipleChoice<T> extends CheckBoxMultipleChoice<T> {
     private static final long serialVersionUID = 1L;
 
     public ButtonMultipleChoice(String id) {
         super(id);
     }
 
     public ButtonMultipleChoice(String idList<? extends T> choices) {
         super(idchoices);
     }
 
     public ButtonMultipleChoice(String idList<? extends T> choicesIChoiceRenderer<? super T> renderer) {
         super(idchoicesrenderer);
     }
 
     public ButtonMultipleChoice(String idIModel<? extends Collection<T>> modelList<? extends T> choices) {
         super(idmodelchoices);
     }
 
     public ButtonMultipleChoice(String idIModel<? extends Collection<T>> modelList<? extends T> choices,
             IChoiceRenderer<? super T> renderer) {
         super(idmodelchoicesrenderer);
     }
 
     public ButtonMultipleChoice(String idIModel<? extends List<? extends T>> choices) {
         super(idchoices);
     }
 
     public ButtonMultipleChoice(String idIModel<? extends Collection<T>> model,
             IModel<? extends List<? extends T>> choices) {
         super(idmodelchoices);
     }
 
     public ButtonMultipleChoice(String idIModel<? extends List<? extends T>> choices,
             IChoiceRenderer<? super T> renderer) {
         super(idchoicesrenderer);
     }
 
     public ButtonMultipleChoice(String idIModel<? extends Collection<T>> model,
             IModel<? extends List<? extends T>> choicesIChoiceRenderer<? super T> renderer) {
         super(idmodelchoicesrenderer);
     }
 
     @Override
     protected void onInitialize() {
         super.onInitialize();
 
         add(new CssClassNameAppender("btn-group"));
     }
 
     @Override
     protected void onComponentTag(ComponentTag tag) {
         super.onComponentTag(tag);
 
         tag.put("data-toggle""buttons");
     }
 
     // CHECKSTYLE:OFF this method was copied and marginally adapted from Wicket's CheckBoxMultipleChoice
     @SuppressWarnings("unchecked")
     @Override
     protected void appendOptionHtml(final AppendingStringBuffer bufferfinal T choiceint index,
         final String selected) {
         Object displayValue = getChoiceRenderer().getDisplayValue(choice);
        Class<?> objectClass = displayValue == null ? null : displayValue.getClass();
        // Get label for choice
        String label = "";
        if (objectClass != null && objectClass != String.class) {
            @SuppressWarnings("rawtypes")
            IConverter converter = getConverter(objectClass);
            label = converter.convertToString(displayValuegetLocale());
        } else if (displayValue != null) {
            label = displayValue.toString();
        }
        // If there is a display value for the choice, then we know that the
        // choice is automatic in some way. If label is /null/ then we know
        // that the choice is a manually created checkbox tag at some random
        // location in the page markup!
        if (label != null) {
            String id = getChoiceRenderer().getIdValue(choiceindex);
            final String idAttr = getCheckBoxMarkupId(id);
            boolean isSlected = isSelected(choiceindexselected);
            // Append option suffix
            buffer.append(getPrefix(indexchoice));
            Tag tag = new Tag("label").attr("for"idAttr);
            List<StringlabelClasses = new ArrayList<>();
            labelClasses.add("btn");
            if (isSlected) {
                labelClasses.add("active");
                labelClasses.add(getActiveLabelClass(choiceindex));
            } else {
                labelClasses.add(getLabelClass(choiceindex));
            }
            tag.attr("class"labelClasses);
            // Add checkbox element
            Tag input = new Tag("input").attr("name"getInputName()).attr("type""checkbox");
            input.attr("value"id).attr("id"idAttr);
            input.attr("checked""checked"isSlected);
            input.attr("disabled""disabled"isDisabled(choiceindexselected) || !isEnabledInHierarchy());
            // Allows user to add attributes to the <input..> tag
            {
                IValueMap attrs = getAdditionalAttributes(indexchoice);
                if (attrs != null) {
                    input.getAttributes().putAll(attrs);
                }
            }
            if (getApplication().getDebugSettings().isOutputComponentPath()) {
                CharSequence path = getPageRelativePath();
                path = Strings.replaceAll(path"_""__");
                path = Strings.replaceAll(path":""_");
                input.attr("wicketpath"path + "_input_" + index);
            }
            tag.add(input);
            // Add label for checkbox
            tag.add(getLabelText(label));
            buffer.append(tag.toString());
            // Append option suffix
            buffer.append(getSuffix(indexchoice));
        }
        // CHECKSTYLE:ON
    }
    private CharSequence getLabelText(String label) {
        String display = label;
        if (localizeDisplayValues()) {
            display = getLocalizer().getString(labelthislabel);
        }
        return getEscapeModelStrings() ? Strings.escapeMarkup(display) : display;
    }

    
Returns the css class added to the label when the checkbox is not selected.

Parameters:
choice the choice being rendered
index the index of the label in the list
Returns:
a css class
    protected String getLabelClass(T choiceint index) {
        return "btn-default";
    }

    
Returns the css class added to the label when the checkbox is selected.

Parameters:
choice the choice being rendered
index the index of the label in the list
Returns:
a css class
    protected String getActiveLabelClass(T choiceint index) {
        return getLabelClass(choiceindex);
    }
    @Override
    protected String getSuffix(int index, T choice) {
        return "";
    }
New to GrepCode? Check out our FAQ X