001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.gwt.client.ui.input;
029
030import org.opencms.gwt.client.ui.CmsPopup;
031
032import com.google.gwt.core.client.Scheduler;
033import com.google.gwt.core.client.Scheduler.ScheduledCommand;
034import com.google.gwt.user.client.ui.Frame;
035
036/**
037 * This class represents a popup which displays an IFrame.<p>
038 *
039 * It also exports a Javascript function to close the popup when given the id of the popup.<p>
040 *
041 * @since 8.0.0
042 */
043public class CmsFramePopup extends CmsPopup {
044
045    /** The handler which is called when the popup closes itself. */
046    protected Runnable m_closeHandler;
047
048    /** The iframe for the popup content. */
049    Frame m_frame;
050
051    /** The id of this popup. */
052    private String m_id;
053
054    /**
055     * Constructor.<p>
056     *
057     * @param title the title of the popup dialog
058     * @param url the URL which should be opened in the popup
059     */
060
061    public CmsFramePopup(String title, String url) {
062
063        super(title);
064        m_frame = new Frame();
065        add(m_frame);
066        m_frame.setUrl(url);
067    }
068
069    /**
070     * @see org.opencms.gwt.client.ui.CmsPopup#center()
071     */
072    @Override
073    public void center() {
074
075        exportCloseFunction();
076        super.center();
077    }
078
079    /**
080     * Returns the frame contained in this popup.<p>
081     *
082     * @return a frame
083     */
084    public Frame getFrame() {
085
086        return m_frame;
087    }
088
089    /**
090     * Hide the popup, but only after the current event has been processed.<p>
091     */
092    public void hideDelayed() {
093
094        // The reason for using this function, instead of calling hide directly, is that
095        // the latter leads to harmless but annoying Javascript errors when called from
096        // Javascript inside the IFrame, since the IFrame is closed before the function returns.
097        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
098
099            /**
100             * @see com.google.gwt.core.client.Scheduler.ScheduledCommand#execute()
101             */
102            public void execute() {
103
104                if (m_closeHandler != null) {
105                    m_closeHandler.run();
106                }
107
108                hide();
109            }
110        });
111    }
112
113    /**
114     * Sets the handler which should be called when the popup closes itself.<p>
115     *
116     * @param closeHandler the "close" handler
117     */
118    public void setCloseHandler(Runnable closeHandler) {
119
120        m_closeHandler = closeHandler;
121    }
122
123    /**
124     * Sets the id of this IFrame popup.<p>
125     *
126     * The popup can be closed by calling the cmsCloseDialog Javascript function with the same id as a parameter.<p>
127     *
128     * @param id the new id
129     */
130    public void setId(String id) {
131
132        m_id = id;
133    }
134
135    /**
136     * @see org.opencms.gwt.client.ui.CmsPopup#show()
137     */
138    @Override
139    public void show() {
140
141        exportCloseFunction();
142        super.show();
143    }
144
145    /**
146     * Exports a Javascript function 'cmsCloseDialog', which, when passed the id of a CmsFramePopup as a parameter, will close that dialog.<p>
147     */
148    protected native void exportCloseFunction() /*-{
149                                                var w = $wnd;
150                                                w.CmsFramePopup_instances = w.CmsFramePopup_instances || {};
151                                                // register the current instance under its id
152                                                w.CmsFramePopup_instances[this.@org.opencms.gwt.client.ui.input.CmsFramePopup::m_id] = this;
153                                                if (!w.cmsCloseDialog) {
154                                                w.cmsCloseDialog = function(arg) {
155                                                var instance = w.CmsFramePopup_instances[arg];
156                                                instance.@org.opencms.gwt.client.ui.input.CmsFramePopup::hideDelayed()();
157                                                // remove current instance
158                                                delete w.CmsFramePopup_instances[this.@org.opencms.gwt.client.ui.input.CmsFramePopup::m_id];
159                                                } // cmsCloseDialog
160                                                } // if
161                                                }-*/;
162
163    /**
164     * test.<p>
165     */
166    protected native void setGroupFormValue() /*-{
167                                              var w = $wnd;
168                                              w.CmsFramePopup_instances = w.CmsFramePopup_instances || {};
169                                              // register the current instance under its id
170                                              w.CmsFramePopup_instances[this.@org.opencms.gwt.client.ui.input.CmsFramePopup::m_id] = this;
171                                              if (!w.cmsCloseDialog) {
172                                              w.cmsCloseDialog = function(arg) {
173                                              var instance = w.CmsFramePopup_instances[arg];
174                                              instance.@org.opencms.gwt.client.ui.input.CmsFramePopup::hideDelayed()();
175                                              // remove current instance
176                                              delete w.CmsFramePopup_instances[this.@org.opencms.gwt.client.ui.input.CmsFramePopup::m_id];
177                                              } // cmsCloseDialog
178                                              } // if
179                                              }-*/;
180
181}