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.ade.sitemap.client.toolbar;
029
030import org.opencms.ade.sitemap.client.CmsSitemapView;
031import org.opencms.ade.sitemap.client.control.CmsSitemapController;
032import org.opencms.ade.sitemap.shared.CmsGalleryType;
033import org.opencms.ade.sitemap.shared.CmsSitemapData.EditorMode;
034import org.opencms.gwt.client.CmsCoreProvider;
035import org.opencms.gwt.client.ui.CmsPushButton;
036import org.opencms.gwt.client.ui.CmsQuickLauncher.A_QuickLaunchHandler;
037import org.opencms.gwt.client.ui.CmsToggleButton;
038import org.opencms.gwt.client.ui.CmsToolbar;
039import org.opencms.gwt.client.ui.CmsToolbarContextButton;
040import org.opencms.gwt.client.ui.I_CmsToolbarButton;
041import org.opencms.gwt.shared.CmsGwtConstants.QuickLaunch;
042import org.opencms.gwt.shared.CmsQuickLaunchParams;
043
044import java.util.Collection;
045
046import com.google.gwt.dom.client.Style.Display;
047import com.google.gwt.event.dom.client.ClickEvent;
048import com.google.gwt.event.dom.client.ClickHandler;
049import com.google.gwt.user.client.ui.Widget;
050
051/**
052 * Sitemap toolbar.<p>
053 *
054 * @since 8.0.0
055 */
056public class CmsSitemapToolbar extends CmsToolbar {
057
058    /**
059     * Quick launch handler for the sitemap.<p>
060     */
061    public static class SitemapQuickLaunchHandler extends A_QuickLaunchHandler {
062
063        /**
064         * @see org.opencms.gwt.client.ui.CmsQuickLauncher.I_QuickLaunchHandler#getParameters()
065         */
066        public CmsQuickLaunchParams getParameters() {
067
068            return new CmsQuickLaunchParams(
069                QuickLaunch.CONTEXT_SITEMAP,
070                null,
071                null,
072                CmsSitemapView.getInstance().getController().getData().getReturnCode(),
073                CmsCoreProvider.get().getUri());
074        }
075
076    }
077
078    /** The sitemap clipboard button. */
079    private CmsToolbarClipboardButton m_clipboardButton;
080
081    /** The context menu button. */
082    private CmsToolbarContextButton m_contextMenuButton;
083
084    /** The new galleries menu button. */
085    private CmsToolbarNewGalleryButton m_newGalleryMenuButton;
086
087    /** The new menu button. */
088    private CmsToolbarNewButton m_newMenuButton;
089
090    /** The sitemap toolbar handler. */
091    private CmsSitemapToolbarHandler m_toolbarHandler;
092
093    /**
094     * Constructor.<p>
095     *
096     * @param controller the sitemap controller
097     */
098    public CmsSitemapToolbar(CmsSitemapController controller) {
099
100        m_toolbarHandler = new CmsSitemapToolbarHandler(controller.getData().getContextMenuEntries());
101        addLeft(new CmsToolbarPublishButton(this, controller));
102        m_newMenuButton = new CmsToolbarNewButton(this, controller);
103        if (controller.isEditable() && (controller.getData().getDefaultNewElementInfo() != null)) {
104            m_clipboardButton = new CmsToolbarClipboardButton(this, controller);
105            addLeft(m_clipboardButton);
106            addLeft(m_newMenuButton);
107        }
108
109        m_newGalleryMenuButton = new CmsToolbarNewGalleryButton(this, controller);
110        if (controller.isEditable()) {
111            addLeft(m_newGalleryMenuButton);
112        }
113
114        addLeft(new CmsToolbarChooseEditorModeButton(CmsCoreProvider.get().getUserInfo().isDeveloper()));
115        ClickHandler clickHandler = new ClickHandler() {
116
117            /**
118             * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent)
119             */
120            public void onClick(ClickEvent event) {
121
122                I_CmsToolbarButton source = (I_CmsToolbarButton)event.getSource();
123                source.onToolbarClick();
124                if (source instanceof CmsPushButton) {
125                    ((CmsPushButton)source).clearHoverState();
126                }
127            }
128        };
129
130        m_contextMenuButton = new CmsToolbarContextButton(m_toolbarHandler);
131        m_contextMenuButton.addClickHandler(clickHandler);
132        insertRight(m_contextMenuButton, 0);
133        setMode(EditorMode.navigation);
134        setQuickLaunchHandler(new SitemapQuickLaunchHandler());
135    }
136
137    /**
138     * Deactivates all toolbar buttons.<p>
139     */
140    public void deactivateAll() {
141
142        for (Widget button : getAll()) {
143            if (button instanceof I_CmsToolbarActivatable) {
144                ((I_CmsToolbarActivatable)button).setEnabled(false);
145            } else if (button instanceof CmsToggleButton) {
146                ((CmsToggleButton)button).setEnabled(false);
147            }
148        }
149    }
150
151    /**
152     * Gets the context menu button.<p>
153     *
154     * @return the context menu button
155     */
156    public CmsToolbarContextButton getContextMenuButton() {
157
158        return m_contextMenuButton;
159    }
160
161    /**
162     * Returns the toolbar handler.<p>
163     *
164     * @return the toolbar handler
165     */
166    public CmsSitemapToolbarHandler getToolbarHandler() {
167
168        return m_toolbarHandler;
169    }
170
171    /**
172     * Should be executed by every widget when starting an action.<p>
173     *
174     * @param widget the widget that got activated
175     */
176    public void onButtonActivation(Widget widget) {
177
178        for (Widget w : getAll()) {
179            if (!(w instanceof I_CmsToolbarActivatable)) {
180                continue;
181            }
182            ((I_CmsToolbarActivatable)w).onActivation(widget);
183        }
184    }
185
186    /**
187     * Enables/disables the new clipboard button.<p>
188     *
189     * @param enabled <code>true</code> to enable the button
190     * @param disabledReason the reason, why the button is disabled
191     */
192    public void setClipboardEnabled(boolean enabled, String disabledReason) {
193
194        if (m_clipboardButton != null) {
195            if (enabled) {
196                m_clipboardButton.enable();
197            } else {
198                m_clipboardButton.disable(disabledReason);
199            }
200        }
201    }
202
203    /**
204     * Sets the available gallery types.<p>
205     *
206     * @param galleryTypes the gallery types
207     */
208    public void setGalleryTypes(Collection<CmsGalleryType> galleryTypes) {
209
210        m_newGalleryMenuButton.setGalleryTypes(galleryTypes);
211    }
212
213    /**
214     * Sets the galleries mode.<p>
215     *
216     * @param mode the editor mode
217     */
218    public void setMode(EditorMode mode) {
219
220        switch (mode) {
221            case galleries:
222                m_newGalleryMenuButton.getElement().getStyle().clearDisplay();
223                m_newMenuButton.getElement().getStyle().setDisplay(Display.NONE);
224                break;
225            case modelpages:
226            case categories:
227                m_newGalleryMenuButton.getElement().getStyle().setDisplay(Display.NONE);
228                m_newMenuButton.getElement().getStyle().clearDisplay();
229                break;
230            default:
231                m_newMenuButton.getElement().getStyle().clearDisplay();
232                m_newGalleryMenuButton.getElement().getStyle().setDisplay(Display.NONE);
233                break;
234        }
235    }
236
237    /**
238     * Enables/disables the new menu button.<p>
239     *
240     * @param enabled <code>true</code> to enable the button
241     * @param disabledReason the reason, why the button is disabled
242     */
243    public void setNewEnabled(boolean enabled, String disabledReason) {
244
245        if (enabled) {
246            m_newMenuButton.enable();
247        } else {
248            m_newMenuButton.disable(disabledReason);
249        }
250    }
251
252    /**
253     * Enables/disables the new menu button.<p>
254     *
255     * @param enabled <code>true</code> to enable the button
256     * @param disabledReason the reason, why the button is disabled
257     */
258    public void setNewGalleryEnabled(boolean enabled, String disabledReason) {
259
260        if (enabled) {
261            m_newGalleryMenuButton.enable();
262        } else {
263            m_newGalleryMenuButton.disable(disabledReason);
264        }
265    }
266}