Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.uberfire.client.common;
  
  import java.util.HashSet;
  import java.util.Set;
  
 
 //@formatter:off
 
Popup dialog with optional header and footer.

By default, all other Modals are closed once a new one is opened. This setting can be overridden.

UiBinder Usage:

 <b:Modal title="My Modal" backdrop="STATIC">
     <g:Label>Modal Content!</g:Label>
     <b:ModalFooter>
         <b:Button icon="FILE">Save</b:Button>
     </b:ModalFooter>
 </b:Modal>
 
 

All arguments are optional.

Author(s):
Carlos Alexandro Becker
Dominik Mayer
Since:
2.0.4.0
See also:
Bootstrap documentation
com.google.gwt.user.client.ui.PopupPanel
 
 // @formatter:on
 public class Modal extends DivWidget implements HasVisibility,
                                                 HasVisibleHandlers,
                                                 IsAnimated {
 
     private static Set<ModalcurrentlyShown = new HashSet<Modal>();
 
     private final DivWidget header = new DivWidget();
 
     private final DivWidget body = new DivWidget"modal-body" );
 
     private boolean keyboard = true;
 
 
     private boolean show = false;
 
     private boolean hideOthers = true;
 
     private boolean configured = false;
 
     private Close close = new Close. );
 
     private String title;

    
Creates an empty, hidden widget.
 
     public Modal() {
         super"modal" );
         super.add );
         super.add );
         setVisiblefalse );
     }

    
Creates an empty, hidden widget with specified show behavior.

Parameters:
animated true if the widget should be animated.
    public Modalboolean animated ) {
        thisanimatedfalse );
    }

    
Creates an empty, hidden widget with specified show behavior.

Parameters:
animated true if the widget should be animated.
dynamicSafe true removes from RootPanel when hidden
    public Modalboolean animated,
                  boolean dynamicSafe ) {
        this();
        setAnimationanimated );
        setDynamicSafedynamicSafe );
    }

    
Setup the modal to prevent memory leaks. When modal is hidden, will remove all event handlers, and them remove the modal DOM from document DOM.

Default is false.

Parameters:
dynamicSafe
    public void setDynamicSafeboolean dynamicSafe ) {
        if ( dynamicSafe ) {
            addHiddenHandlernew HiddenHandler() {
                @Override
                public void onHiddenHiddenEvent hiddenEvent ) {
                    unsetHandlerFunctionsgetElement() );
                    Modal.this.removeFromParent();
                }
            } );
        }
    }

    
Sets the title of the Modal.

Parameters:
title the title of the Modal
    @Override
    public void setTitleString title ) {
        this. = title;
        .clear();
        if ( title == null || title.isEmpty() ) {
            showHeaderfalse );
        } else {
            .add );
            .addnew Heading( 3, title ) );
            showHeadertrue );
        }
    }
    private void showHeaderboolean show ) {
        if ( show ) {
            .setStyleName. );
        } else {
            .removeStyleName. );
        }
    }

    
    public void setAnimationboolean animated ) {
        if ( animated ) {
            addStyleName. );
        } else {
            removeStyleName. );
        }
    }

    
    public boolean getAnimation() {
        return getStyleName().contains. );
    }

    
Sets whether this Modal appears on top of others or is the only one visible on screen.

Parameters:
hideOthers true to make sure that this modal is the only one shown. All others will be hidden. Default: true
    public void setHideOthersboolean hideOthers ) {
        this. = hideOthers;
    }

    
Sets whether the Modal is closed when the ESC is pressed.

Parameters:
keyboard true if the Modal is closed by ESC key. Default: true
    public void setKeyboardboolean keyboard ) {
        this. = keyboard;
        reconfigure();
    }

    
Get Keyboard enable state

Returns:
true:enable false:disable
    public boolean isKeyboardEnable() {
        return this.;
    }

    
Sets the type of the backdrop.

Parameters:
type the backdrop type
    public void setBackdropBackdropType type ) {
         = type;
        reconfigure();
    }

    
Get backdrop type.

Returns:
backdrop type.
    public BackdropType getBackdropType() {
        return this.;
    }

    
Reconfigures the modal with changed settings.
    protected void reconfigure() {
        if (  ) {
            reconfigure );
        }
    }

    
    @Override
    public void addWidget w ) {
        if ( w instanceof ModalFooter ) {
            super.addw );
        } else {
            .addw );
        }
    }

    
    @Override
    public void insertWidget w,
                        int beforeIndex ) {
        .insertwbeforeIndex );
    }

    
    public void show() {
        if ( !this.isAttached() ) {
            RootPanel.get().addthis );
        }
        changeVisibility"show" );
        centerVerticallygetElement() );
    }
    @Override
    protected void onAttach() {
        super.onAttach();
        configure );
        setHandlerFunctionsgetElement() );
         = true;
    }

    
    public void hide() {
        changeVisibility"hide" );
    }

    
    public void toggle() {
        changeVisibility"toggle" );
    }
    private void changeVisibilityString visibility ) {
        changeVisibilitygetElement(), visibility );
    }

    
This method is called immediately when the widget's hide() method is executed.
    protected void onHideEvent e ) {
        fireEventnew HideEvente ) );
    }

    
This method is called once the widget is completely hidden.
    protected void onHiddenEvent e ) {
        fireEventnew HiddenEvente ) );
        .removethis );
    }

    
This method is called immediately when the widget's show() method is executed.
    protected void onShowEvent e ) {
        if (  ) {
            hideShownModals();
        }
        fireEventnew ShowEvente ) );
    }
    private void hideShownModals() {
        for ( Modal m :  ) {
            if ( !m.equalsthis ) ) {
                m.hide();
            }
        }
    }

    
This method is called once the widget is completely shown.
    protected void onShownEvent e ) {
        fireEventnew ShownEvente ) );
        .addthis );
    }
    private void reconfigureboolean keyboard,
                              BackdropType backdropType,
                              boolean show ) {
        if ( backdropType == . ) {
            reconfiguregetElement(), keyboardtrueshow );
        } else if ( backdropType == . ) {
            reconfiguregetElement(), keyboardfalseshow );
        } else if ( backdropType == . ) {
            reconfiguregetElement(), keyboard..get(), show );
        }
    }
    private void configureboolean keyboard,
                            BackdropType backdropType,
                            boolean show ) {
        if ( backdropType == . ) {
            configuregetElement(), keyboardtrueshow );
        } else if ( backdropType == . ) {
            configuregetElement(), keyboardfalseshow );
        } else if ( backdropType == . ) {
            configuregetElement(), keyboard..get(), show );
        }
    }
    //@formatter:off
    private native void reconfigureElement e,
                                     boolean k,
                                     boolean b,
                                     boolean s ) /*-{
        var modal = null;
        if ($wnd.jQuery(e).data('modal')) {
            modal = $wnd.jQuery(e).data('modal');
            $wnd.jQuery(e).removeData('modal');
        }
        $wnd.jQuery(e).modal({
            keyboard: k,
            backdrop: b,
            show: s
        });
        if (modal) {
            $wnd.jQuery(e).data('modal').isShown = modal.isShown;
        }
    }-*/;
    private native void reconfigureElement e,
                                     boolean k,
                                     String b,
                                     boolean s ) /*-{
        var modal = null;
        if ($wnd.jQuery(e).data('modal')) {
            modal = $wnd.jQuery(e).data('modal');
            $wnd.jQuery(e).removeData('modal');
        }
        $wnd.jQuery(e).modal({
            keyboard: k,
            backdrop: b,
            show: s
        });
        if (modal) {
            $wnd.jQuery(e).data('modal').isShown = modal.isShown;
        }
    }-*/;
    private native void configureElement e,
                                   boolean k,
                                   boolean b,
                                   boolean s ) /*-{
        $wnd.jQuery(e).modal({
            keyboard: k,
            backdrop: b,
            show: s
        });
    }-*/;
    private native void configureElement e,
                                   boolean k,
                                   String b,
                                   boolean s ) /*-{
        $wnd.jQuery(e).modal({
            keyboard: k,
            backdrop: b,
            show: s
        });
    }-*/;
    private native void changeVisibilityElement e,
                                          String visibility ) /*-{
        $wnd.jQuery(e).modal(visibility);
    }-*/;

    
Links the Java functions that fire the events.
    private native void setHandlerFunctionsElement e ) /*-{
        var that = this;
        $wnd.jQuery(e).on('hide', function (e) {
            that.@org.uberfire.client.common.Modal::onHide(Lcom/google/gwt/user/client/Event;)(e);
        });
        $wnd.jQuery(e).on('hidden', function (e) {
            that.@org.uberfire.client.common.Modal::onHidden(Lcom/google/gwt/user/client/Event;)(e);
        });
        $wnd.jQuery(e).on('show', function (e) {
            that.@org.uberfire.client.common.Modal::onShow(Lcom/google/gwt/user/client/Event;)(e);
        });
        $wnd.jQuery(e).on('shown', function (e) {
            that.@org.uberfire.client.common.Modal::onShown(Lcom/google/gwt/user/client/Event;)(e);
        });
    }-*/;

    
Unlinks all the Java functions that fire the events.
    private native void unsetHandlerFunctionsElement e ) /*-{
        $wnd.jQuery(e).off('hide');
        $wnd.jQuery(e).off('hidden');
        $wnd.jQuery(e).off('show');
        $wnd.jQuery(e).off('shown');
    }-*/;
    //@formatter:on

    
    public HandlerRegistration addHideHandlerHideHandler handler ) {
        return addHandlerhandler, HideEvent.getType() );
    }

    
    public HandlerRegistration addHiddenHandlerHiddenHandler handler ) {
        return addHandlerhandler, HiddenEvent.getType() );
    }

    
    public HandlerRegistration addShowHandlerShowHandler handler ) {
        return addHandlerhandler, ShowEvent.getType() );
    }

    
    public HandlerRegistration addShownHandlerShownHandler handler ) {
        return addHandlerhandler, ShownEvent.getType() );
    }

    
Show/Hide close button. The Modal must have a title.

Parameters:
visible true for show and false to hide. Defaults is true.
    public void setCloseVisibleboolean visible ) {
        .getElement().getStyle().setVisibilityvisible
                                                             ? ..
                                                             : .. );
    }

    

Deprecated:
modal do not support setSize method
    @Override
    public void setSizeString width,
                         String height ) {
        throw new UnsupportedOperationException"modal do not support setSize method" );
    }

    
Sets the Modal's width.

Parameters:
width Modal's new width, in px
    public void setWidthint width ) {
        DOM.setStyleAttributethis.getElement(), "width"width + "px" );
        DOM.setStyleAttributethis.getElement(), "marginLeft", ( -width / 2 ) + "px" );
    }

    
Sets the Modal's body height.

Parameters:
height the Modal's body height, in px
    public void setBodyHeigthint height ) {
        DOM.setStyleAttribute.getElement(), "height"height + "px" );
    }

    
Sets the Modal's body maxHeight.

Parameters:
maxHeight the Modal's body new maxHeight, in CSS units (e.g. "10px", "1em")
    public void setMaxHeigthString maxHeight ) {
        DOM.setStyleAttribute.getElement(), "maxHeight"maxHeight );
    }

    
Centers fixed positioned element vertically.

Parameters:
e Element to center vertically
    private native void centerVerticallyElement e ) /*-{
        $wnd.jQuery(e).css("margin-top", (-1 * $wnd.jQuery(e).outerHeight() / 2) + "px");
    }-*/;
New to GrepCode? Check out our FAQ X