001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.ade.sitemap.client.toolbar;
029
030import org.opencms.ade.sitemap.client.CmsSitemapView;
031import org.opencms.gwt.client.CmsCoreProvider;
032import org.opencms.gwt.client.I_CmsDisableable;
033import org.opencms.gwt.client.ui.A_CmsToolbarHandler;
034import org.opencms.gwt.client.ui.I_CmsToolbarButton;
035import org.opencms.gwt.client.ui.contenteditor.I_CmsContentEditorHandler;
036import org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuCommand;
037import org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuCommandInitializer;
038import org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuEntry;
039import org.opencms.gwt.client.util.I_CmsSimpleCallback;
040import org.opencms.gwt.shared.CmsContextMenuEntryBean;
041import org.opencms.gwt.shared.CmsCoreData.AdeContext;
042import org.opencms.gwt.shared.CmsGwtConstants;
043import org.opencms.util.CmsUUID;
044
045import java.util.ArrayList;
046import java.util.List;
047import java.util.Map;
048
049import com.google.gwt.core.client.GWT;
050import com.google.gwt.user.client.Window;
051
052/**
053 * The toolbar handler used for the sitemap toolbar context menu.<p>
054 */
055public class CmsSitemapToolbarHandler extends A_CmsToolbarHandler {
056
057    /** The currently active button. */
058    private I_CmsToolbarButton m_activeButton;
059
060    /** The available context menu commands. */
061    private Map<String, I_CmsContextMenuCommand> m_contextMenuCommands;
062
063    /** The context menu entries. */
064    private List<I_CmsContextMenuEntry> m_contextMenuEntries;
065
066    /** The content editor handler. */
067    private I_CmsContentEditorHandler m_editorHandler;
068
069    /**
070     * Constructor.<p>
071     *
072     * @param menuBeans the context menu entry beans
073     */
074    public CmsSitemapToolbarHandler(List<CmsContextMenuEntryBean> menuBeans) {
075
076        m_contextMenuEntries = transformEntries(menuBeans, null);
077        m_editorHandler = new I_CmsContentEditorHandler() {
078
079            public void onClose(String sitePath, CmsUUID structureId, boolean isNew, boolean hasChangedSettings) {
080
081                CmsSitemapView.getInstance().getController().updateEntry(sitePath);
082            }
083        };
084    }
085
086    /**
087     * @see org.opencms.gwt.client.ui.I_CmsToolbarHandler#activateSelection()
088     */
089    public void activateSelection() {
090
091        // do nothing
092    }
093
094    /**
095     * @see org.opencms.gwt.client.ui.I_CmsToolbarHandler#deactivateCurrentButton()
096     */
097    public void deactivateCurrentButton() {
098
099        if (m_activeButton != null) {
100            m_activeButton.setActive(false);
101            m_activeButton = null;
102        }
103    }
104
105    /**
106     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#ensureLockOnResource(org.opencms.util.CmsUUID, org.opencms.gwt.client.util.I_CmsSimpleCallback)
107     */
108    public void ensureLockOnResource(CmsUUID structureId, I_CmsSimpleCallback<Boolean> callback) {
109
110        CmsCoreProvider.get().lock(structureId, callback);
111    }
112
113    /**
114     * @see org.opencms.gwt.client.ui.I_CmsToolbarHandler#getActiveButton()
115     */
116    public I_CmsToolbarButton getActiveButton() {
117
118        return m_activeButton;
119    }
120
121    /**
122     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#getContextMenuCommands()
123     */
124    public Map<String, I_CmsContextMenuCommand> getContextMenuCommands() {
125
126        if (m_contextMenuCommands == null) {
127            I_CmsContextMenuCommandInitializer initializer = GWT.create(I_CmsContextMenuCommandInitializer.class);
128            m_contextMenuCommands = initializer.initCommands();
129            List<String> toRemove = new ArrayList<String>();
130            for (Map.Entry<String, I_CmsContextMenuCommand> entry : m_contextMenuCommands.entrySet()) {
131                I_CmsContextMenuCommand command = entry.getValue();
132                if ((command != null)
133                    && (command instanceof I_CmsDisableable)
134                    && ((I_CmsDisableable)command).isDisabled()) {
135                    toRemove.add(entry.getKey());
136                }
137            }
138            for (String removeKey : toRemove) {
139                m_contextMenuCommands.remove(removeKey);
140            }
141        }
142        return m_contextMenuCommands;
143    }
144
145    /**
146     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#getContextType()
147     */
148    public String getContextType() {
149
150        return CmsGwtConstants.CONTEXT_TYPE_SITEMAP_TOOLBAR;
151    }
152
153    /**
154     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#getEditorHandler()
155     */
156    public I_CmsContentEditorHandler getEditorHandler() {
157
158        return m_editorHandler;
159    }
160
161    /**
162     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#leavePage(java.lang.String)
163     */
164    public void leavePage(String targetUri) {
165
166        Window.Location.assign(targetUri);
167    }
168
169    /**
170     * @see org.opencms.gwt.client.ui.I_CmsToolbarHandler#loadContextMenu(org.opencms.util.CmsUUID, org.opencms.gwt.shared.CmsCoreData.AdeContext)
171     */
172    public void loadContextMenu(CmsUUID structureId, AdeContext context) {
173
174        CmsSitemapView.getInstance().getToolbar().getContextMenuButton().showMenu(m_contextMenuEntries);
175    }
176
177    /**
178     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#onSiteOrProjectChange(java.lang.String, java.lang.String)
179     */
180    public void onSiteOrProjectChange(String sitePath, String serverLink) {
181
182        CmsSitemapView.getInstance().getController().openSiteMap(sitePath, true);
183    }
184
185    /**
186     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#refreshResource(org.opencms.util.CmsUUID)
187     */
188    public void refreshResource(CmsUUID structureId) {
189
190        Window.Location.reload();
191    }
192
193    /**
194     * Sets the currently active tool-bar button.<p>
195     *
196     * @param button the button
197     */
198    public void setActiveButton(I_CmsToolbarButton button) {
199
200        m_activeButton = button;
201    }
202
203    /**
204     * @see org.opencms.gwt.client.ui.A_CmsToolbarHandler#transformEntries(java.util.List, org.opencms.util.CmsUUID)
205     */
206    @Override
207    public List<I_CmsContextMenuEntry> transformEntries(List<CmsContextMenuEntryBean> menuBeans, CmsUUID structureId) {
208
209        List<I_CmsContextMenuEntry> result = super.transformEntries(menuBeans, structureId);
210        return result;
211    }
212
213    /**
214     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#unlockResource(org.opencms.util.CmsUUID)
215     */
216    public void unlockResource(CmsUUID structureId) {
217
218        CmsCoreProvider.get().unlock(structureId);
219    }
220
221}