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;
 
Bootstrap button rendering of a RadioChoice.

Parameters:
<T> The model object type
 
 public class ButtonRadioChoice<T> extends RadioChoice<T> {
     private static final long serialVersionUID = 1L;
 
     private IModel<Buttons.TypebuttonTypeModel = new Model<>(..);
     private IModel<Buttons.SizebuttonSizeModel = new Model<>(..);
 
     public ButtonRadioChoice(String id) {
         super(id);
     }
 
     public ButtonRadioChoice(String idList<? extends T> choices) {
         super(idchoices);
     }
 
     public ButtonRadioChoice(String idList<? extends T> choicesIChoiceRenderer<? super T> renderer) {
         super(idchoicesrenderer);
     }
 
     public ButtonRadioChoice(String idIModel<T> modelList<? extends T> choices) {
         super(idmodelchoices);
     }
 
     public ButtonRadioChoice(String idIModel<T> modelList<? extends T> choices,
             IChoiceRenderer<? super T> renderer) {
         super(idmodelchoicesrenderer);
     }
 
     public ButtonRadioChoice(String idIModel<? extends List<? extends T>> choices) {
         super(idchoices);
     }
 
     public ButtonRadioChoice(String idIModel<T> modelIModel<? extends List<? extends T>> choices) {
         super(idmodelchoices);
     }
 
     public ButtonRadioChoice(String idIModel<? extends List<? extends T>> choices,
             IChoiceRenderer<? super T> renderer) {
         super(idchoicesrenderer);
     }
 
     public ButtonRadioChoice(String idIModel<T> modelIModel<? extends List<? extends T>> choices,
             IChoiceRenderer<? super T> renderer) {
         super(idmodelchoicesrenderer);
     }
 
     public IModel<Buttons.TypegetButtonTypeModel() {
         return ;
     }
 
     public void setButtonTypeModel(IModel<Buttons.TypebuttonTypeModel) {
         this. = buttonTypeModel;
     }
 
     public IModel<Buttons.SizegetButtonSizeModel() {
         return ;
     }
 
     public void setButtonSizeModel(IModel<Buttons.SizebuttonSizeModel) {
         this. = buttonSizeModel;
     }

    
Returns the button type used.

Returns:
the button type
    public Buttons.Type getButtonType() {
        return getButtonTypeModel().getObject();
    }

    
Sets the button type used.

Parameters:
buttonType the button type
Returns:
this for chaining
    public ButtonRadioChoice setButtonType(Buttons.Type buttonType) {
        getButtonTypeModel().setObject(buttonType);
        return this;
    }

    
Returns the button size used.

Returns:
the button size
    public Buttons.Size getButtonSize() {
        return getButtonSizeModel().getObject();
    }

    
Sets the button type used.

Parameters:
buttonSize the button size
Returns:
this for chaining
    public ButtonRadioChoice setButtonSize(Buttons.Size buttonSize) {
        getButtonSizeModel().setObject(buttonSize);
        return this;
    }
    @Override
    protected void onInitialize() {
        super.onInitialize();
        setSuffix("");
        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 adapted from Wicket's RadioChoice
    @Override
    protected void appendOptionHtml(AppendingStringBuffer buffer, T choiceint indexString selected) {
        // CHECKSTYLE:ON
        String label = generateLabelForChoice(choice);
        // 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) {
            return;
        }
        String id = getChoiceRenderer().getIdValue(choiceindex);
        String idAttr = getMarkupId() + "-" + id;
        boolean isSelected = isSelected(choiceindexselected);
        boolean enabled = isEnabledInHierarchy() && !isDisabled(choiceindexselected);
        Tag inputTag =
            new Tag("input").attr("name"getInputName()).attr("type""radio")
                    .attr("checked""checked"isSelected).attr("disabled""disabled", !enabled).attr("value"id)
                    .attr("id"idAttr).attr("style""display:none");
        // Should a roundtrip be made (have onSelectionChanged called) when the option is clicked?
        if (wantOnSelectionChangedNotifications()) {
            inputTag.attr("onclick"buildOnClickUrl(id));
        }
            inputTag.attr("wicketpath"buildWicketPath(index));
        }
        // Add label for radio button
        Tag labelTag =
            new Tag("label").attr("for"idAttr).attr("class"generateLabelTagClass(choiceindexisSelected));
        inputTag.attrs(getAdditionalAttributes(indexchoice));
        inputTag.attrs(getAdditionalAttributesForLabel(indexchoice));
        labelTag.add(inputTag);
        labelTag.add(generateLabelBody(label).toString());
        buffer.append(getPrefix(indexchoice));
        buffer.append(labelTag);
        buffer.append(getSuffix(indexchoice));
    }

    
Builds the round-trip url added as "onclick" attribute to the radio choice button for the given id.

Parameters:
id the id value of the choice renderer
Returns:
a url usable in an "onclick" attribute
    protected String buildOnClickUrl(String id) {
        Form<?> form = findParent(Form.class);
        String onclick;
        if (form != null) {
            onclick = form.getJsForInterfaceUrl(url) + ";";
        } else {
            onclick = "window.location.href='" + url;
            onclick += (url.toString().indexOf('?') > -1 ? '&' : '?') + getInputName();
            onclick += "=" + id + ";";
        }
        return onclick;
    }

    
Generates the initial label that is displayed inside the button for the given choice.

Parameters:
choice the choice being rendered
Returns:
the choice label
    @SuppressWarnings("unchecked")
    protected String generateLabelForChoice(T choice) {
        Object displayValue = getChoiceRenderer().getDisplayValue(choice);
        Class<?> objectClass = displayValue == null ? null : displayValue.getClass();
        String label = "";
        if (objectClass != null && objectClass != String.class) {
            IConverter converter = getConverter(objectClass);
            label = converter.convertToString(displayValuegetLocale());
        } else if (displayValue != null) {
            label = displayValue.toString();
        }
        return label;
    }

    
Generates the css class used for a specific label tag.

Parameters:
choice the choice being rendered
index the index of the label in the list
isSelected whether the choice is selected or not
Returns:
css classes
    protected String generateLabelTagClass(T choiceint indexboolean isSelected) {
        String css = "btn " + getLabelClass(choiceindex);
        if (isSelected) {
            css += " active";
        }
        return css;
    }

    
Creates the message displayed in the label's body for a choice by localizing and escaping the choice label.

Parameters:
label the label
Returns:
the displayed label
    protected CharSequence generateLabelBody(String label) {
        String display = label;
        if (localizeDisplayValues()) {
            display = getLocalizer().getString(labelthislabel);
        }
        return getEscapeModelStrings() ? Strings.escapeMarkup(display) : display;
    }

    
Builds the wicketpath attribute value for the given choice index.

Parameters:
index the index of the choice being rendered
Returns:
wicketpath attribute value
    protected CharSequence buildWicketPath(int index) {
        CharSequence path = getPageRelativePath();
        path = Strings.replaceAll(path"_""__");
        path = Strings.replaceAll(path":""_");
        return path + "_input_" + index;
    }

    
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 getButtonType().getCssClassName() + " " + getButtonSize().getCssClassName();
    }
New to GrepCode? Check out our FAQ X