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.dialog;
 
 import java.util.List;
 
A configurable, generic, modal dialog.

Parameters:
<T> The model object type.
 
 public abstract class Dialog<T> extends GenericPanel<T> {
 
     private static final long serialVersionUID = 1L;

    
Markup id to be used for footer buttons
 
     public static final String BUTTON_MARKUP_ID = ButtonList.getButtonMarkupId();
 
     private IModel<?> title;
     private IModel<Sizesize;
     private IModel<Booleanfade;

    
Contains buttons shown in footer.
 
     private List<AbstractLinkbuttons;
 
     /* Child components */
     private WebMarkupContainer container;
     private Component titleLabel;
     private WebMarkupContainer body;
     private WebMarkupContainer footer;
 
     public Dialog(String id) {
         this(idnull);
     }
 
     public Dialog(String idIModel<T> model) {
         this(idnew Model<>(), model);
     }
 
     public Dialog(String idIModel<?> titleIModel<T> model) {
         super(idmodel);
         this. = title;
         this. = new Model<>(.);
         this. = new ArrayList<>();
         this. = new Model<>(.);
     }
 
     public IModel<?> getTitle() {
         return ;
     }

    
Sets the display model for the header label.

Parameters:
title the header display model
Returns:
this for chaining
 
     public Dialog<T> setTitle(IModel<?> title) {
         this. = title;
         if ( != null) {
             .setDefaultModel(title);
         }
         return this;
     }
 
     public IModel<SizegetSize() {
         return ;
    }

    
Sets the dialog size.

Parameters:
size the size
Returns:
this for chaining
    public Dialog<T> setSize(IModel<Sizesize) {
        this. = size;
        return this;
    }

    
Sets the dialog size.

Parameters:
size the size
Returns:
this for chaining
    public Dialog<T> setSize(Size size) {
        getSize().setObject(size);
        return this;
    }
    public IModel<BooleangetFade() {
        return ;
    }

    
Sets whether or not to use the modal fade animation.

Parameters:
fade flag
Returns:
this for chaining
    public Dialog<T> setFade(IModel<Booleanfade) {
        this. = fade;
        return this;
    }

    
Sets whether or not to use the modal fade animation.

Parameters:
fade flag
Returns:
this for chaining
    public Dialog<T> setFade(Boolean fade) {
        getFade().setObject(fade);
        return this;
    }
    @Override
    protected void onInitialize() {
        super.onInitialize();
        setOutputMarkupId(true);
         = newContainer("modal-dialog");
         = newTitleLabel("modal-title");
         = newBody("modal-body");
         = newFooter("modal-footer");
        add();
        .add();
        .add();
        .add();
        add(new CssClassNameAppender("modal"));
            private static final long serialVersionUID = 1L;
            @Override
            public String getObject() {
                Boolean flag = getFade().getObject();
                return (flag != null && flag) ? "fade" : "";
            }
        }));
        // adds actions provided by the overrideable createActions method
        List<AbstractLinkactions = createActions();
        if (actions != null && !actions.isEmpty()) {
            for (AbstractLink action : actions) {
                addButton(action);
            }
        }
    }
    @Override
    protected void onConfigure() {
        super.onConfigure();
        if (Strings.isEmpty(.getDefaultModelObjectAsString())) {
            // there must be at least on character inside the header to prevent layout problems.
            .setDefaultModelObject("&nbsp;");
            .setEscapeModelStrings(false);
        }
        .setVisible(.size() > 0);
    }

    
Factory method for creating the outer modal container.

Parameters:
id the component id
Returns:
a new component
    protected WebMarkupContainer newContainer(String id) {
        WebMarkupContainer modal = new TransparentWebMarkupContainer(id);
        modal.add(new CssClassNameAppender(new CssClassNameProvidingModel<>()));
        return modal;
    }

    
Factory method for creating the component showing the dialog title.

Parameters:
id the component id
Returns:
a new component
    protected Component newTitleLabel(String id) {
        return new Label(idgetTitle());
    }

    
Factory method for creating the body container.

Parameters:
id the component id
Returns:
a new component
    protected WebMarkupContainer newBody(String id) {
        return new TransparentWebMarkupContainer(id);
    }

    
Factory method for creating the footer container. By default, it adds a ButtonList to display a variable list of buttons.

Parameters:
id the component id
Returns:
a new component
    protected WebMarkupContainer newFooter(String id) {
        WebMarkupContainer footer = new WebMarkupContainer(id);
        footer.add(new ButtonList("buttons"));
        return footer;
    }

    
Returns the dialog container component.

Returns:
a component
    protected WebMarkupContainer getContainer() {
        return ;
    }

    
Returns the body component.

Returns:
a component
    protected WebMarkupContainer getBody() {
        return ;
    }

    
Returns the footer component.

Returns:
a component
    protected WebMarkupContainer getFooter() {
        return ;
    }
    @Override
    protected void onComponentTag(ComponentTag tag) {
        super.onComponentTag(tag);
        modifyContainerTag(tag);
    }

    
Adds the attributes required by bootstrap for activating the modal behavior to the panel tag.

Parameters:
tag Tag to modify
    protected void modifyContainerTag(ComponentTag tag) {
        tag.put("tabindex""-1");
        tag.put("role""dialog");
        tag.put("aria-hidden""true");
        tag.put("aria-labelledby".getMarkupId());
    }

    
Adds the given link to the footer panel. The component is required to use the BUTTON_MARKUP_ID constant as component id.

Parameters:
button the link to add
Returns:
this for chaining
Throws:
java.lang.IllegalArgumentException if the component does not use the correct component id
    public Dialog<T> addButton(AbstractLink buttonthrows IllegalArgumentException {
        if (!.equals(button.getId())) {
            throw new IllegalArgumentException(String.format("Invalid button component id. Must be '%s', was '%s'.",
                    button.getId()));
        }
        .add(button);
        return this;
    }

    
Adds the given link to the footer panel to the given position. The component is required to use the BUTTON_MARKUP_ID constant as component id.

Parameters:
button the link to add
position index at which the specified element is to be inserted
Returns:
this for chaining
Throws:
java.lang.IllegalArgumentException if the component does not use the correct component id
    public Dialog<T> addButton(int positionAbstractLink buttonthrows IllegalArgumentException {
        if (!.equals(button.getId())) {
            throw new IllegalArgumentException(String.format("Invalid button component id. Must be '%s', was '%s'.",
                    button.getId()));
        }
        .add(positionbutton);
        return this;
    }

    
Adds the given link to the footer panel at the front of the list. The component is required to use the BUTTON_MARKUP_ID constant as component id.

Parameters:
button the link to add
Returns:
this for chaining
Throws:
java.lang.IllegalArgumentException if the component does not use the correct component id
    public Dialog<T> prependButton(AbstractLink buttonthrows IllegalArgumentException {
        return addButton(0, button);
    }

    
Adds a DialogCloseLink with specific label.

Parameters:
label The label of close button
Returns:
this instance
    @SuppressWarnings("unchecked")
    public Dialog<T> addCloseButton(final IModel<Stringlabel) {
        AbstractLink button = newCloseLink(label);
        return (button == null) ? this : addButton(button);
    }

    
Prepends a DialogCloseLink with specific label.

Parameters:
label The label of close button
Returns:
this instance
    @SuppressWarnings("unchecked")
    public Dialog<T> prependCloseButton(final IModel<Stringlabel) {
        AbstractLink button = newCloseLink(label);
        return (button == null) ? this : prependButton(button);
    }

    
Creates a new link that opens this dialog.

Parameters:
id the component id
Returns:
a new DialogOpenLink that opens this dialog
    public DialogOpenLink createOpenLink(String id) {
        return new DialogOpenLink(idthis);
    }

    
Factory called in #addCloseButton(IModel) to create a new link that closes the dialog.

Parameters:
id the component id
label the body label
Returns:
a new component
    public AbstractLink newCloseLink(String idIModel<Stringlabel) {
        return new DialogCloseLink(id).setBody(label);
    }

    
Hook to create new action links.

Parameters:
id the button id
Returns:
a list of actions
    protected List<AbstractLinkcreateActions(String id) {
        return null;
    }

    
Dialog size.
    public static enum Size implements ICssClassNameProvider {
        NORMAL(""),
        LARGE("modal-lg"),
        SMALL("modal-sm");
        private final String cssClassName;
        private Size(String cssClassName) {
            this. = cssClassName;
        }
        @Override
        public String getCssClassName() {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X