Package com.alkacon.geranium.client.ui

Source Code of com.alkacon.geranium.client.ui.FrameDialog

/*
* This library is part of Geranium -
* an open source UI library for GWT.
*
* Copyright (c) Alkacon Software GmbH (http://www.alkacon.com)-
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software, please see the
* company website: http://www.alkacon.com
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package com.alkacon.geranium.client.ui;

import com.alkacon.geranium.client.ui.css.I_LayoutBundle;
import com.alkacon.geranium.client.util.DomUtil;
import com.alkacon.geranium.client.util.DomUtil.Method;

import java.util.Map;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.dom.client.FormElement;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

/**
* Frame dialog utility class.<p>
*
* Use to render the dialog content within an iFrame on top of a regular {@link com.alkacon.geranium.client.ui.Popup}.
* May also be used to wrap the popup if no iFrame is needed.<p>
*
* Provides function to show an iFrame dialog.<p>
*/
public class FrameDialog {

    /** The name of the close function. */
    public static final String CLOSE_FUNCTION = "cmsDialogClose";

    /** The dialog height. */
    public static final int DIALOG_HEIGHT = 300;

    /** The dialog width. */
    public static final int DIALOG_WIDTH = 200;

    /** The name of the enable dialog close function. */
    public static final String ENABLE_CLOSE_FUNCTION = "cmsEnableDialogClose";

    /** The name of the dialog height function. */
    public static final String HEIGHT_FUNCTION = "cmsDialogHeight";

    /** The name of the IFrame used for displaying the upload hook page. */
    public static final String IFRAME_NAME = "upload_hook";

    /** The name of the dialog title function. */
    public static final String TITLE_FUNCTION = "cmsDialogTitle";

    /** The name of the dialog width function. */
    public static final String WIDTH_FUNCTION = "cmsUploadHookDialogWidth";

    /** The button panel. */
    private FlowPanel m_buttonPanel;

    /** The content widget. */
    private Widget m_content;

    /** The panel holding the content widget. */
    private SimplePanel m_contentPanel;

    /** Flag indicating if this dialog is displayed within an iFrame on top of a popup. */
    private boolean m_isFrame;

    /** Flag indicating that the dialog is showing.< */
    private boolean m_isShowing;

    /** The main panel. */
    private FlowPanel m_main;

    /** The popup. */
    private Popup m_popup;

    /**
     * Constructor.<p>
     */
    public FrameDialog() {

        m_isFrame = hasParentFrame();
        if (m_isFrame) {
            m_main = new FlowPanel();
            m_main.addStyleName(I_LayoutBundle.INSTANCE.dialogCss().frameDialog());
            m_contentPanel = new SimplePanel();
            m_contentPanel.addStyleName(I_LayoutBundle.INSTANCE.dialogCss().popupMainContent());
            m_contentPanel.addStyleName(I_LayoutBundle.INSTANCE.dialogCss().contentPadding());
            m_main.add(m_contentPanel);

        } else {
            m_popup = new Popup();
            m_popup.setGlassEnabled(true);
        }
    }

    /**
     * Returns if this dialog has a parent frame.<p>
     *
     * @return <code>true</code> if the parent frame is available
     */
    public static native boolean hasParentFrame() /*-{
        if ($wnd.parent[@com.alkacon.geranium.client.ui.FrameDialog::CLOSE_FUNCTION]) {
            return true;
        }
        return false;

    }-*/;

    /**
     * Shows an iFrame dialog popup.<p>
     *
     * @param title the dialog title
     * @param dialogUri the dialog URI
     * @param parameters the dialog post parameters
     * @param closeHandler the dialog close handler
     *
     * @return the opened popup
     */
    public static Popup showFrameDialog(
        String title,
        String dialogUri,
        Map<String, String> parameters,
        CloseHandler<PopupPanel> closeHandler) {

        Popup popup = new Popup(title);
        popup.removePadding();
        popup.addStyleName(I_LayoutBundle.INSTANCE.contentEditorCss().contentEditor());
        popup.setGlassEnabled(true);
        IFrame editorFrame = new IFrame(IFRAME_NAME, "");
        popup.add(editorFrame);
        final FormElement formElement = DomUtil.generateHiddenForm(dialogUri, Method.post, IFRAME_NAME, parameters);
        RootPanel.getBodyElement().appendChild(formElement);
        exportDialogFunctions(popup);
        popup.addCloseHandler(new CloseHandler<PopupPanel>() {

            public void onClose(CloseEvent<PopupPanel> event) {

                formElement.removeFromParent();
                removeExportedFunctions();
            }
        });
        if (closeHandler != null) {
            popup.addCloseHandler(closeHandler);
        }
        popup.center();
        formElement.submit();
        return popup;
    }

    /**
     * Removes exported functions from the window context.<p>
     */
    protected static native void removeExportedFunctions() /*-{
        $wnd[@com.alkacon.geranium.client.ui.FrameDialog::CLOSE_FUNCTION] = null;
        $wnd[@com.alkacon.geranium.client.ui.FrameDialog::HEIGHT_FUNCTION] = null;
        $wnd[@com.alkacon.geranium.client.ui.FrameDialog::WIDTH_FUNCTION] = null;
        $wnd[@com.alkacon.geranium.client.ui.FrameDialog::TITLE_FUNCTION] = null;
    }-*/;

    /**
     * Installs the Javascript function which should be called by the child iframe when the dialog should be closed.<p>
     *
     * @param popup the popup
     */
    private static native void exportDialogFunctions(final Popup popup) /*-{
        var self = this;
        $wnd[@com.alkacon.geranium.client.ui.FrameDialog::CLOSE_FUNCTION] = function() {
            popup.@com.alkacon.geranium.client.ui.Popup::hide()();
        };
        $wnd[@com.alkacon.geranium.client.ui.FrameDialog::HEIGHT_FUNCTION] = function(
                height) {
            popup.@com.alkacon.geranium.client.ui.Popup::setHeight(I)(height);
            if (popup.@com.alkacon.geranium.client.ui.Popup::isShowing()) {
                popup.@com.alkacon.geranium.client.ui.Popup::center()();
            }
        };
        $wnd[@com.alkacon.geranium.client.ui.FrameDialog::WIDTH_FUNCTION] = function(
                width) {
            popup.@com.alkacon.geranium.client.ui.Popup::setWidth(I)(width);
            if (popup.@com.alkacon.geranium.client.ui.Popup::isShowing()) {
                popup.@com.alkacon.geranium.client.ui.Popup::center()();
            }
        };
        $wnd[@com.alkacon.geranium.client.ui.FrameDialog::TITLE_FUNCTION] = function(
                title) {
            popup.@com.alkacon.geranium.client.ui.Popup::setCaption(Ljava/lang/String;)(title);
        };
        $wnd[@com.alkacon.geranium.client.ui.FrameDialog::ENABLE_CLOSE_FUNCTION] = function(
                title) {
            popup.@com.alkacon.geranium.client.ui.Popup::addDialogClose(Lcom/google/gwt/user/client/Command;)(null);
        };
    }-*/;

    /**
     * Adds a new button to the button bar.<p>
     *
     * @param button the button to add
     */
    public void addButton(Widget button) {

        if (m_isFrame) {
            initButtonPanel();
            m_buttonPanel.add(button);
        } else {
            m_popup.addButton(button);
        }
    }

    /**
     * Adds a new button to the button bar at the specified index position.<p>
     *
     * @param button the button to add
     * @param index the index position
     */
    public void addButton(Widget button, int index) {

        if (m_isFrame) {
            initButtonPanel();
            m_buttonPanel.insert(button, index);
        } else {
            m_popup.addButton(button, index);
        }
    }

    /**
     * Enables the dialog close button on the popup.<p>
     */
    public void enableDialogClose() {

        if (m_isFrame) {
            enableParentDialogClose();
        } else {
            m_popup.addDialogClose(null);
        }
    }

    /**
     * Hides the dialog.<p>
     */
    public void hide() {

        if (m_isFrame) {
            hideParent();
        } else {
            m_popup.hide();
        }
    }

    /**
     * Returns if the popup is showing and the content is rendered.<p>
     *
     * @return <code>true</code> if the popup and content are showing
     */
    public boolean isShowing() {

        if (m_isFrame) {
            return m_isShowing;
        } else {
            return m_popup.isShowing();
        }
    }

    /**
     * Removes the given button from the button bar.<p>
     *
     * @param button the button to remove
     */
    public void removeButton(Widget button) {

        if (m_isFrame) {
            if (m_buttonPanel != null) {
                m_buttonPanel.remove(button);
            }
        } else {
            m_popup.removeButton(button);
        }
    }

    /**
     * Sets the content widget.<p>
     *
     * @param content the content widget
     */
    public void setContent(Widget content) {

        if (m_content != null) {
            m_content.removeFromParent();
        }
        if (m_isFrame) {
            m_contentPanel.setWidget(content);
        } else {
            m_popup.setMainContent(content);
        }
        m_content = content;
    }

    /**
     * Sets the popup height.<p>
     *
     * @param height the height
     */
    public void setHeight(int height) {

        if (m_isFrame) {
            setParentHeight(height);
        } else {
            m_popup.setHeight(height);
            if (m_popup.isShowing()) {
                m_popup.center();
            }
        }
    }

    /**
     * Sets the dialog title.<p>
     *
     * @param title the title
     */
    public void setTitle(String title) {

        if (m_isFrame) {
            setParentTitle(title);
        } else {
            m_popup.setCaption(title);
        }
    }

    /**
     * Sets the popup width.<p>
     *
     * @param width the width
     */
    public void setWidth(int width) {

        if (m_isFrame) {
            setParentWidth(width);
        } else {
            m_popup.setWidth(width);
            if (m_popup.isShowing()) {
                m_popup.center();
            }
        }
    }

    /**
     * Shows the dialog.<p>
     */
    public void show() {

        if (m_isFrame) {
            RootPanel root = RootPanel.get();
            root.getElement().getStyle().setMargin(0, Unit.PX);
            root.getElement().getStyle().setPadding(0, Unit.PX);
            RootPanel.get().add(m_main);
            m_isShowing = true;
            Scheduler.get().scheduleDeferred(new ScheduledCommand() {

                public void execute() {

                    adjustContentSize();
                }
            });
        } else {
            m_popup.center();
        }

    }

    /**
     * Adjusts the content panel size according to the button panel height.<p>
     */
    protected void adjustContentSize() {

        if (m_isFrame && m_isShowing) {
            if (m_buttonPanel != null) {
                m_contentPanel.getElement().getStyle().setBottom(m_buttonPanel.getOffsetHeight() + 6, Unit.PX);
            } else {
                m_contentPanel.getElement().getStyle().clearBottom();
            }
        }
    }

    /**
     * Enables the dialog close button on the parent frame popup.<p>
     */
    private native void enableParentDialogClose() /*-{
        $wnd.parent[@com.alkacon.geranium.client.ui.FrameDialog::ENABLE_CLOSE_FUNCTION]
                ();
    }-*/;

    /**
     * Hides the parent dialog.<p>
     */
    private native void hideParent() /*-{
        $wnd.parent[@com.alkacon.geranium.client.ui.FrameDialog::CLOSE_FUNCTION]
                ();
    }-*/;

    /**
     * Initializes the button panel within frame mode.<p>
     */
    private void initButtonPanel() {

        if ((m_buttonPanel == null) && m_isFrame) {
            m_buttonPanel = new FlowPanel();
            m_buttonPanel.addStyleName(I_LayoutBundle.INSTANCE.dialogCss().popupButtonPanel());
            m_main.add(m_buttonPanel);
        }
    }

    /**
     * Sets the parent dialog height.<p>
     *
     * @param height the height to set
     */
    private native void setParentHeight(int height) /*-{
        $wnd.parent[@com.alkacon.geranium.client.ui.FrameDialog::HEIGHT_FUNCTION]
                (height);
    }-*/;

    /**
     * Sets the title of the parent dialog.<p>
     *
     * @param title the title
     */
    private native void setParentTitle(String title) /*-{
        $wnd.parent[@com.alkacon.geranium.client.ui.FrameDialog::TITLE_FUNCTION]
                (title);
    }-*/;

    /**
     * Sets the parent dialog width.<p>
     *
     * @param width the width to set
     */
    private native void setParentWidth(int width) /*-{
        $wnd.parent[@com.alkacon.geranium.client.ui.FrameDialog::WIDTH_FUNCTION]
                (width);
    }-*/;
}
 
TOP

Related Classes of com.alkacon.geranium.client.ui.FrameDialog

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.