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.Messages;
032import org.opencms.ade.sitemap.shared.CmsSitemapData.EditorMode;
033import org.opencms.gwt.client.CmsCoreProvider;
034import org.opencms.gwt.client.ui.CmsMenuButton;
035import org.opencms.gwt.client.ui.I_CmsButton;
036import org.opencms.gwt.client.ui.contextmenu.CmsContextMenu;
037import org.opencms.gwt.client.ui.contextmenu.CmsContextMenuCloseHandler;
038import org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuEntry;
039import org.opencms.gwt.client.ui.css.I_CmsInputCss;
040import org.opencms.gwt.client.ui.css.I_CmsInputLayoutBundle;
041import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
042
043import java.util.ArrayList;
044import java.util.List;
045
046import com.google.gwt.event.dom.client.ClickEvent;
047import com.google.gwt.event.dom.client.ClickHandler;
048import com.google.gwt.user.client.ui.FlexTable;
049
050/**
051 * The sitemap toolbar change sitemap editor mode button.<p>
052 *
053 * @since 8.0.0
054 */
055public class CmsToolbarChooseEditorModeButton extends CmsMenuButton {
056
057    /**
058     * Context menu entry used to select a sitemap editor mode.<p>
059     */
060    class EditorModeEntry extends A_CmsSitemapModeEntry {
061
062        /** The sitemap editor mode. */
063        private EditorMode m_mode;
064
065        /**
066         * Creates a new entry.<p>
067         *
068         * @param message the context menu item text
069         * @param mode the editor mode
070         */
071        public EditorModeEntry(String message, EditorMode mode) {
072
073            super(message);
074            m_mode = mode;
075        }
076
077        /**
078         * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuEntry#execute()
079         */
080        public void execute() {
081
082            CmsSitemapView.getInstance().onBeforeSetEditorMode(m_mode);
083        }
084
085        /**
086         * @see org.opencms.ade.sitemap.client.toolbar.A_CmsSitemapModeEntry#getIconClass()
087         */
088        @Override
089        public String getIconClass() {
090
091            I_CmsInputCss inputCss = I_CmsInputLayoutBundle.INSTANCE.inputCss();
092            EditorMode currentMode = CmsSitemapView.getInstance().getEditorMode();
093            return (currentMode == m_mode) ? inputCss.checkBoxImageChecked() : "";
094        }
095
096    }
097
098    /** True if we can edit model pages. */
099    private boolean m_canEditModelPages;
100
101    /** The context menu entries. */
102    private List<I_CmsContextMenuEntry> m_entries;
103
104    /** The main content widget. */
105    private FlexTable m_menuPanel;
106
107    /**
108     * Constructor.<p>
109     *
110     * @param canEditModelPages true if editing model pages should be enabled
111     */
112    public CmsToolbarChooseEditorModeButton(boolean canEditModelPages) {
113
114        super(null, I_CmsButton.ButtonData.SITEMAP_BUTTON.getIconClass());
115        m_canEditModelPages = canEditModelPages;
116        setTitle(Messages.get().key(Messages.GUI_SELECT_VIEW_0));
117        m_menuPanel = new FlexTable();
118        // set a style name for the menu table
119        m_menuPanel.getElement().addClassName(I_CmsLayoutBundle.INSTANCE.contextmenuCss().menuPanel());
120        // set the widget
121        setMenuWidget(m_menuPanel);
122        getPopup().addAutoHidePartner(getElement());
123        getPopup().setWidth(0);
124        CmsContextMenu menu = createContextMenu();
125        m_menuPanel.setWidget(0, 0, menu);
126        // add the close handler for the menu
127        getPopup().addCloseHandler(new CmsContextMenuCloseHandler(menu));
128        addClickHandler(new ClickHandler() {
129
130            /**
131             * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent)
132             */
133            @SuppressWarnings("synthetic-access")
134            public void onClick(ClickEvent event) {
135
136                if (!isOpen()) {
137                    m_menuPanel.setWidget(0, 0, createContextMenu()); // we have to create the menu every time because the active mode may change
138                    openMenu();
139                } else {
140                    closeMenu();
141                }
142            }
143        });
144    }
145
146    /**
147     * Creates the menu widget for this button.<p>
148     *
149     * @return the menu widget
150     */
151    public CmsContextMenu createContextMenu() {
152
153        m_entries = new ArrayList<I_CmsContextMenuEntry>();
154        m_entries.add(
155            new EditorModeEntry(
156                Messages.get().key(Messages.GUI_ONLY_NAVIGATION_BUTTON_TITLE_0),
157                EditorMode.navigation));
158        m_entries.add(
159            new EditorModeEntry(Messages.get().key(Messages.GUI_NON_NAVIGATION_BUTTON_TITLE_0), EditorMode.vfs));
160        m_entries.add(
161            new EditorModeEntry(Messages.get().key(Messages.GUI_ONLY_GALLERIES_BUTTON_TITLE_0), EditorMode.galleries));
162        if (CmsCoreProvider.get().getUserInfo().isCategoryManager()) {
163            m_entries.add(
164                new EditorModeEntry(
165                    Messages.get().key(Messages.GUI_CONTEXTMENU_CATEGORY_MODE_0),
166                    EditorMode.categories));
167        }
168        if (m_canEditModelPages) {
169            m_entries.add(new EditorModeEntry(Messages.get().key(Messages.GUI_MODEL_PAGES_0), EditorMode.modelpages));
170        }
171        if (CmsSitemapView.getInstance().getController().isLocaleComparisonEnabled()) {
172            m_entries.add(
173                new EditorModeEntry(Messages.get().key(Messages.GUI_LOCALECOMPARE_MODE_0), EditorMode.compareLocales));
174        }
175
176        CmsContextMenu menu = new CmsContextMenu(m_entries, false, getPopup());
177        return menu;
178    }
179}