Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package de.agilecoders.wicket.core.markup.html.bootstrap.button.dropdown;
  
 
 import java.util.List;
 
 import static de.agilecoders.wicket.core.markup.html.bootstrap.button.DropDownJqueryFunction.dropdown;
 import static de.agilecoders.wicket.jquery.JQuery.$;

Use any button to trigger a dropdown menu by placing it within a .btn-group and providing the proper menu markup.

Author(s):
miha
 
 public abstract class DropDownButton extends AbstractLink implements Activatable {
 
     private final IModel<Buttons.SizebuttonSize = Model.of(..);
     private final IModel<Buttons.TypebuttonType = Model.of(..);
     private final IModel<BooleandropUp = Model.of(false);
     private final ButtonList buttonListView;
     private final WebMarkupContainer baseButton;
     private final String script;
     private final Icon icon;
     private final IModel<AlignmentBehavior.Alignmentalignment = Model.of(..);

    
Construct.

Parameters:
id The markup id
model The label of the main button
 
     public DropDownButton(final String idfinal IModel<Stringmodel) {
         this(idmodel, Model.of((IconTypenull));
     }

    
Construct.

Parameters:
markupId The markup id
model The label of the main button
iconTypeModel the type of the icon
 
     public DropDownButton(final String markupIdfinal IModel<Stringmodelfinal IModel<IconTypeiconTypeModel) {
         super(markupIdmodel);
 
         this. = newInitializerScript();
 
         add( = newButton("btn"modeliconTypeModel));
         WebMarkupContainer dropdownMenu = new WebMarkupContainer("dropdown-menu");
         dropdownMenu.add(new AlignmentBehavior());
         add(dropdownMenu);
         dropdownMenu.add( = newButtonList("buttons"));
 
         this. = newButtonIcon("icon"iconTypeModel);
 
         BootstrapResourcesBehavior.addTo(this);
 
         addIconToBaseButton();
     }

    

Returns:
base css class name of button container element
 
     protected String createCssClassName() {
         return "dropdown";
     }

    
adds an icon to the base button

Parameters:
icon The icon to add
 
     protected void addIconToBaseButton(final Icon icon) {
         .add(icon);
     }

    
adds a org.apache.wicket.behavior.Behavior to the base button

Parameters:
behavior to add
Returns:
this instance for chaining
    public final DropDownButton addToButton(final Behavior behavior) {
        .add(behavior);
        return this;
    }

    

Returns:
new initializer script
    protected String newInitializerScript() {
        return $(this".dropdown-toggle").chain(dropdown()).get();
    }

    
appends a toggle menu script to a given org.apache.wicket.ajax.AjaxRequestTarget.

Parameters:
target the current target
    public final void appendToggleMenuScript(final AjaxRequestTarget target) {
        target.appendJavaScript($(this".dropdown-toggle").chain(dropdown("toggle")).get());
    }

    
sets the icon of the base button element

Parameters:
iconType The de.agilecoders.wicket.core.markup.html.bootstrap.image.IconType of the icon
Returns:
this element instance
    public final DropDownButton setIconType(final IconType iconType) {
        .setType(iconType);
        return this;
    }

    
creates a new button instance

Parameters:
markupId The component's id
labelModel The label text as model
iconTypeModel The icon type as model
Returns:
a new button component
    protected WebMarkupContainer newButton(final String markupIdfinal IModel<StringlabelModelfinal IModel<IconTypeiconTypeModel) {
        final WebMarkupContainer baseButton = new WebMarkupContainer(markupId);
        baseButton.setOutputMarkupId(true);
        baseButton.add(newButtonLabel("label"labelModel));
        return baseButton;
    }

    
creates a new icon component with given de.agilecoders.wicket.core.markup.html.bootstrap.image.IconType.

Parameters:
markupId The component' id
labelModel The label text as model
Returns:
new label component
    protected Component newButtonLabel(final String markupIdfinal IModel<?> labelModel) {
        final Label label = new Label(markupIdlabelModel);
        label.setRenderBodyOnly(true);
        return label;
    }

    
creates a new icon component with given de.agilecoders.wicket.core.markup.html.bootstrap.image.IconType.

Parameters:
markupId The component' id
iconTypeModel The icon type as model
Returns:
new icon component
    protected Icon newButtonIcon(final String markupIdfinal IModel<IconTypeiconTypeModel) {
        return new Icon(markupIdiconTypeModel);
    }
    @Override
    public void renderHead(IHeaderResponse response) {
        super.renderHead(response);
        response.render(OnDomReadyHeaderItem.forScript());
    }

    
adds the button behavior to the base button of this dropdown component

Parameters:
buttonType mandatory parameter
buttonSize mandatory parameter
    protected void addButtonBehavior(final IModel<Buttons.TypebuttonTypefinal IModel<Buttons.SizebuttonSize) {
        .add(new ButtonBehavior(buttonTypebuttonSize));
    }

    
creates a list of sub menu buttons which will be shown if sub menu will be opened.

Parameters:
buttonMarkupId the markup id that all sub menu buttons must be use.
Returns:
list of sub menu buttons
    protected abstract List<AbstractLinknewSubMenuButtons(final String buttonMarkupId);

    
    private ButtonList newButtonList(final String markupId) {
        final ButtonList buttonList = new ButtonList(markupIdnewSubMenuButtons(ButtonList.getButtonMarkupId()));
        buttonList.setRenderBodyOnly(true);
        return buttonList;
    }

    
whether to use default dropdown behavior (default value is false which means default behavior) or to open the dropdown menu at the top of dropdown button, also the button icon will be rotated.

Parameters:
dropUp true, to use a 180deg rotated button and open menu on top
Returns:
this instance for chaining
    public DropDownButton setDropUp(final boolean dropUp) {
        this..setObject(dropUp);
        return this;
    }

    
sets the size of the button

Parameters:
size mandatory parameter
Returns:
this instance for chaining
    public DropDownButton setSize(final Buttons.Size size) {
        this..setObject(size);
        return this;
    }

    
sets the type of the button

Parameters:
type mandatory parameter
Returns:
this instance for chaining
    public DropDownButton setType(final Buttons.Type type) {
        this..setObject(type);
        return this;
    }

    
sets the dropdown menu alignment

Parameters:
alignment
Returns:
    public DropDownButton setAlignment(final AlignmentBehavior.Alignment alignment) {
        this..setObject(alignment);
        return this;
    }
    @Override
        return new PanelMarkupSourcingStrategy(false);
    }
    @Override
    protected void onComponentTag(final ComponentTag tag) {
        if (!Components.hasTagName(tag"div""li")) {
            tag.setName("div");
        }
        super.onComponentTag(tag);
        if (.getObject()) {
            Attributes.addClass(tag"dropup");
        }
        Attributes.addClass(tagcreateCssClassName());
    }
    @Override
    public boolean isActive(final Component item) {
        return .hasActiveButton(item);
    }
New to GrepCode? Check out our FAQ X