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.rpc.CmsRpcAction;
032import org.opencms.gwt.client.ui.contenteditor.I_CmsContentEditorHandler;
033import org.opencms.gwt.client.util.I_CmsSimpleCallback;
034import org.opencms.gwt.shared.CmsContextMenuEntryBean;
035import org.opencms.gwt.shared.CmsCoreData.AdeContext;
036import org.opencms.gwt.shared.CmsGwtConstants;
037import org.opencms.util.CmsStringUtil;
038import org.opencms.util.CmsUUID;
039
040import java.util.ArrayList;
041import java.util.List;
042import java.util.Map;
043
044import com.google.gwt.core.client.GWT;
045
046/**
047 * The context menu handler for the search result tab.<p>
048 */
049public class CmsContextMenuHandler implements I_CmsContextMenuHandler {
050
051    /** The available context menu commands. */
052    private static Map<String, I_CmsContextMenuCommand> m_contextMenuCommands;
053
054    /** the content editor handler. */
055    private I_CmsContentEditorHandler m_editorHandler;
056
057    /**
058     * Constructor.<p>
059     */
060    public CmsContextMenuHandler() {
061
062    }
063
064    /**
065     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#ensureLockOnResource(org.opencms.util.CmsUUID, org.opencms.gwt.client.util.I_CmsSimpleCallback)
066     */
067    public void ensureLockOnResource(CmsUUID structureId, I_CmsSimpleCallback<Boolean> callback) {
068
069        CmsCoreProvider.get().lock(structureId, callback);
070    }
071
072    /**
073     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#getContextMenuCommands()
074     */
075    public Map<String, I_CmsContextMenuCommand> getContextMenuCommands() {
076
077        if (m_contextMenuCommands == null) {
078            I_CmsContextMenuCommandInitializer initializer = GWT.create(I_CmsContextMenuCommandInitializer.class);
079            m_contextMenuCommands = initializer.initCommands();
080        }
081        return m_contextMenuCommands;
082    }
083
084    /**
085     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#getContextType()
086     */
087    public String getContextType() {
088
089        return CmsGwtConstants.CONTEXT_TYPE_FILE_TABLE;
090    }
091
092    /**
093     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#getEditorHandler()
094     */
095    public I_CmsContentEditorHandler getEditorHandler() {
096
097        if (m_editorHandler == null) {
098            m_editorHandler = new I_CmsContentEditorHandler() {
099
100                public void onClose(
101                    String sitePath,
102                    CmsUUID structureId,
103                    boolean isNew,
104                    boolean hasChangedSettings,
105                    boolean usedPublishDialog) {
106
107                    // do nothing
108                }
109            };
110        }
111        return m_editorHandler;
112    }
113
114    /**
115     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#leavePage(java.lang.String)
116     */
117    public void leavePage(String targetUri) {
118
119        // not supported within galleries
120    }
121
122    /**
123     * Loads the context menu.<p>
124     *
125     * @param structureId the resource structure id
126     * @param context the context
127     * @param menuButton the menu button
128     */
129    public void loadContextMenu(
130        final CmsUUID structureId,
131        final AdeContext context,
132        final CmsContextMenuButton menuButton) {
133
134        CmsRpcAction<List<CmsContextMenuEntryBean>> action = new CmsRpcAction<List<CmsContextMenuEntryBean>>() {
135
136            @Override
137            public void execute() {
138
139                CmsCoreProvider.getService().getContextMenuEntries(structureId, context, this);
140            }
141
142            @Override
143            protected void onResponse(List<CmsContextMenuEntryBean> result) {
144
145                menuButton.showMenu(transformEntries(result, structureId));
146            }
147        };
148        action.execute();
149    }
150
151    /**
152     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#onSiteOrProjectChange(java.lang.String, java.lang.String)
153     */
154    public void onSiteOrProjectChange(String sitePath, String serverLink) {
155
156        leavePage(serverLink);
157    }
158
159    /**
160     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#refreshResource(org.opencms.util.CmsUUID)
161     */
162    public void refreshResource(CmsUUID structureId) {
163
164        // do nothing
165    }
166
167    /**
168     * Sets the editor handler.<p>
169     *
170     * @param editorHandler the editor handler
171     */
172    public void setEditorHandler(I_CmsContentEditorHandler editorHandler) {
173
174        m_editorHandler = editorHandler;
175    }
176
177    /**
178     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#unlockResource(org.opencms.util.CmsUUID)
179     */
180    public void unlockResource(CmsUUID structureId) {
181
182        CmsCoreProvider.get().unlock(structureId);
183    }
184
185    /**
186     * Transforms a list of context menu entry beans to a list of context menu entries.<p>
187     *
188     * @param menuBeans the list of context menu entry beans
189     * @param structureId the id of the resource for which to transform the context menu entries
190     *
191     * @return a list of context menu entries
192     */
193    protected List<I_CmsContextMenuEntry> transformEntries(
194        List<CmsContextMenuEntryBean> menuBeans,
195        final CmsUUID structureId) {
196
197        List<I_CmsContextMenuEntry> menuEntries = new ArrayList<I_CmsContextMenuEntry>();
198        for (CmsContextMenuEntryBean bean : menuBeans) {
199            I_CmsContextMenuEntry entry = transformSingleEntry(bean, structureId);
200            if (entry != null) {
201                menuEntries.add(entry);
202            }
203        }
204        return menuEntries;
205    }
206
207    /**
208     * Creates a single context menu entry from a context menu entry bean.<p>
209     *
210     * @param bean the menu entry bean
211     * @param structureId the structure id
212     *
213     * @return the context menu for the given entry
214     */
215    protected I_CmsContextMenuEntry transformSingleEntry(CmsContextMenuEntryBean bean, CmsUUID structureId) {
216
217        String name = bean.getName();
218        I_CmsContextMenuCommand command = null;
219        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(name)) {
220            command = getContextMenuCommands().get(name);
221        }
222        if (command instanceof I_CmsValidatingContextMenuCommand) {
223            boolean ok = ((I_CmsValidatingContextMenuCommand)command).validate(bean);
224            if (!ok) {
225                return null;
226            }
227        }
228        CmsContextMenuEntry entry = new CmsContextMenuEntry(this, structureId, command);
229        entry.setBean(bean);
230        if (bean.hasSubMenu()) {
231            entry.setSubMenu(transformEntries(bean.getSubMenu(), structureId));
232        }
233        return entry;
234    }
235}