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.contextmenu;
029
030import org.opencms.gwt.client.CmsCoreProvider;
031import org.opencms.gwt.client.ui.CmsFrameDialog;
032import org.opencms.gwt.client.ui.CmsPopup;
033import org.opencms.gwt.client.util.CmsClientStringUtil;
034import org.opencms.gwt.shared.CmsContextMenuEntryBean;
035import org.opencms.gwt.shared.CmsMenuCommandParameters;
036import org.opencms.util.CmsUUID;
037
038import java.util.HashMap;
039import java.util.Map;
040
041import com.google.gwt.core.client.Scheduler;
042import com.google.gwt.core.client.Scheduler.ScheduledCommand;
043import com.google.gwt.user.client.Window;
044
045/**
046 * A context menu entry command to open any dialog within an iFrame.<p>
047 *
048 * The dialog will be called with the parameter {@link #PARAM_CONTENT_STRUCTURE_ID}
049 * containing the structure id of the currently edited content if available.<p>
050 *
051 * To close the dialog call from within the dialog frame context
052 * window.parent[{@link #CLOSING_METHOD_NAME}](boolean reload).<p>
053 */
054public final class CmsContextMenuDialog implements I_CmsHasContextMenuCommand, I_CmsContextMenuCommand {
055
056    /** The name of the dialog close method exported to the window context. */
057    public static final String CLOSING_METHOD_NAME = "closeContextMenuDialog";
058
059    /** The parameter name for the content structure id. */
060    public static final String PARAM_CONTENT_STRUCTURE_ID = "contentStructureId";
061
062    /** The context menu handler for this command instance. */
063    protected I_CmsContextMenuHandler m_menuHandler;
064
065    /**
066     * Constructor.<p>
067     */
068    private CmsContextMenuDialog() {
069
070        // nothing to do
071    }
072
073    /**
074     * Returns the context menu command according to
075     * {@link org.opencms.gwt.client.ui.contextmenu.I_CmsHasContextMenuCommand}.<p>
076     *
077     * @return the context menu command
078     */
079    public static I_CmsContextMenuCommand getContextMenuCommand() {
080
081        return new CmsContextMenuDialog();
082    }
083
084    /**
085     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuCommand#execute(org.opencms.util.CmsUUID, org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler, org.opencms.gwt.shared.CmsContextMenuEntryBean)
086     */
087    public void execute(CmsUUID structureId, I_CmsContextMenuHandler handler, CmsContextMenuEntryBean menuEntryBean) {
088
089        m_menuHandler = handler;
090        int height = 400;
091        int width = CmsPopup.DEFAULT_WIDTH;
092        if (menuEntryBean.getParams().containsKey(CmsMenuCommandParameters.PARAM_DIALOG_HEIGHT)) {
093            height = CmsClientStringUtil.parseInt(
094                menuEntryBean.getParams().get(CmsMenuCommandParameters.PARAM_DIALOG_HEIGHT));
095        }
096        if (menuEntryBean.getParams().containsKey(CmsMenuCommandParameters.PARAM_DIALOG_WIDTH)) {
097            width = CmsClientStringUtil.parseInt(
098                menuEntryBean.getParams().get(CmsMenuCommandParameters.PARAM_DIALOG_WIDTH));
099        }
100        String fileName = menuEntryBean.getParams().get(CmsMenuCommandParameters.PARAM_DIALOG_URI);
101        CmsPopup popup = CmsFrameDialog.showFrameDialog(
102            menuEntryBean.getLabel(),
103            CmsCoreProvider.get().link(fileName),
104            getDialogParameters(structureId, menuEntryBean),
105            null);
106        popup.setHeight(height);
107        popup.setWidth(width);
108        popup.addDialogClose(null);
109        popup.center();
110        exportClosingMethod(popup);
111    }
112
113    /**
114     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuCommand#getItemWidget(org.opencms.util.CmsUUID, org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler, org.opencms.gwt.shared.CmsContextMenuEntryBean)
115     */
116    public A_CmsContextMenuItem getItemWidget(
117        CmsUUID structureId,
118        I_CmsContextMenuHandler handler,
119        CmsContextMenuEntryBean bean) {
120
121        return null;
122    }
123
124    /**
125     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuCommand#hasItemWidget()
126     */
127    public boolean hasItemWidget() {
128
129        return false;
130    }
131
132    /**
133     * Executed on dialog close.<p>
134     * @param reload <code>true</code> if the page should be reloaded
135     */
136    protected void onClose(boolean reload) {
137
138        if (reload) {
139            Scheduler.get().scheduleDeferred(new ScheduledCommand() {
140
141                public void execute() {
142
143                    String url = Window.Location.getHref();
144                    m_menuHandler.leavePage(url);
145
146                }
147            });
148        }
149    }
150
151    /**
152     * Exports the close method to the window object, so it can be accessed from within the content editor iFrame.<p>
153     *
154     * @param popup the popup instance
155     */
156    private native void exportClosingMethod(final CmsPopup popup) /*-{
157        var self = this;
158        $wnd[@org.opencms.gwt.client.ui.contextmenu.CmsContextMenuDialog::CLOSING_METHOD_NAME] = function(reload) {
159            popup.@org.opencms.gwt.client.ui.CmsPopup::hide()();
160            self.@org.opencms.gwt.client.ui.contextmenu.CmsContextMenuDialog::onClose(Z)(reload);
161            $wnd[@org.opencms.gwt.client.ui.contextmenu.CmsContextMenuDialog::CLOSING_METHOD_NAME] = null;
162        };
163    }-*/;
164
165    /**
166     * Generates the dialog parameters.<p>
167     *
168     * @param structureId the structure id of the current content
169     * @param menuEntryBean the context menu entry bean
170     * @return the dialog parameters
171     */
172    private Map<String, String> getDialogParameters(CmsUUID structureId, CmsContextMenuEntryBean menuEntryBean) {
173
174        HashMap<String, String> parameters = new HashMap<String, String>();
175        if (structureId != null) {
176            parameters.put(PARAM_CONTENT_STRUCTURE_ID, structureId.toString());
177        }
178        parameters.putAll(menuEntryBean.getParams());
179        return parameters;
180    }
181}