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;
029
030import org.opencms.gwt.client.CmsCoreProvider;
031import org.opencms.gwt.client.Messages;
032import org.opencms.gwt.client.ui.contextmenu.CmsContextMenu;
033import org.opencms.gwt.client.ui.contextmenu.CmsContextMenuCloseHandler;
034import org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuEntry;
035import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
036import org.opencms.gwt.client.ui.input.CmsLabel;
037import org.opencms.gwt.client.util.CmsClientCollectionUtil;
038import org.opencms.gwt.shared.CmsCoreData.AdeContext;
039
040import java.util.List;
041
042import com.google.gwt.event.logical.shared.CloseEvent;
043import com.google.gwt.event.logical.shared.CloseHandler;
044import com.google.gwt.event.shared.HandlerRegistration;
045import com.google.gwt.user.client.Window;
046import com.google.gwt.user.client.ui.FlexTable;
047import com.google.gwt.user.client.ui.PopupPanel;
048
049/**
050 * The context tool-bar menu button.<p>
051 *
052 * @since 8.0.0
053 */
054public class CmsToolbarContextButton extends A_CmsToolbarMenu<I_CmsToolbarHandler> {
055
056    /** The menu data. */
057    protected List<I_CmsContextMenuEntry> m_menuEntries;
058
059    /** The loading panel. */
060    private CmsLoadingAnimation m_loadingPanel;
061
062    /** The context menu. */
063    private CmsContextMenu m_menu;
064
065    /** The registration for the first close handler. */
066    private HandlerRegistration m_menuCloseHandler;
067
068    /** Context used for loading the context menu entries. */
069    private AdeContext m_menuContext = AdeContext.pageeditor;
070
071    /** The main content widget. */
072    private FlexTable m_menuPanel;
073
074    /** The label which is displayed when no entries are found. */
075    private CmsLabel m_noEntriesLabel;
076
077    /** The registration for the second close handler. */
078    private HandlerRegistration m_popupCloseHandler;
079
080    /**
081     * Constructor.<p>
082     *
083     * @param handler the container-page handler
084     */
085    public CmsToolbarContextButton(final I_CmsToolbarHandler handler) {
086
087        super(I_CmsButton.ButtonData.CONTEXT, handler);
088        m_noEntriesLabel = new CmsLabel(Messages.get().key(Messages.GUI_TOOLBAR_CONTEXT_EMPTY_0));
089        m_noEntriesLabel.addStyleName(I_CmsLayoutBundle.INSTANCE.contextmenuCss().menuInfoLabel());
090        m_noEntriesLabel.addStyleName(I_CmsLayoutBundle.INSTANCE.generalCss().buttonCornerAll());
091        m_loadingPanel = new CmsLoadingAnimation();
092        // create the menu panel (it's a table because of ie6)
093        m_menuPanel = new FlexTable();
094        // set a style name for the menu table
095        m_menuPanel.getElement().addClassName(I_CmsLayoutBundle.INSTANCE.contextmenuCss().menuPanel());
096
097        // set the widget
098        setMenuWidget(m_menuPanel);
099
100        // clear the width of the popup content
101        getPopup().setWidth(0);
102        getPopup().addStyleName(I_CmsLayoutBundle.INSTANCE.dialogCss().contextMenu());
103    }
104
105    /**
106     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#onToolbarActivate()
107     */
108    public void onToolbarActivate() {
109
110        getHandler().loadContextMenu(CmsCoreProvider.get().getStructureId(), m_menuContext);
111    }
112
113    /**
114     * Unregister the resize handler.<p>
115     *
116     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#onToolbarDeactivate()
117     */
118    public void onToolbarDeactivate() {
119
120        if (m_resizeRegistration != null) {
121            m_resizeRegistration.removeHandler();
122            m_resizeRegistration = null;
123        }
124    }
125
126    /**
127     * @see org.opencms.gwt.client.ui.CmsMenuButton#openMenu()
128     */
129    @Override
130    public void openMenu() {
131
132        if (m_menu == null) {
133            m_menuPanel.setWidget(0, 0, m_loadingPanel);
134        }
135        super.openMenu();
136
137    }
138
139    /**
140     * Sets the menu context.<p>
141     *
142     * @param menuContext the new menu context
143     */
144    public void setMenuContext(AdeContext menuContext) {
145
146        m_menuContext = menuContext;
147    }
148
149    /**
150     * Creates the menu and adds it to the panel.<p>
151     *
152     * @param menuEntries the menu entries
153     */
154    public void showMenu(List<I_CmsContextMenuEntry> menuEntries) {
155
156        if (!CmsClientCollectionUtil.isEmptyOrNull(menuEntries)) {
157            // if there were entries found for the menu, create the menu
158            m_menu = new CmsContextMenu(menuEntries, true, getPopup());
159            // add the resize handler for the menu
160            m_resizeRegistration = Window.addResizeHandler(m_menu);
161            // set the menu as widget for the panel
162            m_menuPanel.setWidget(0, 0, m_menu);
163            if (m_menuCloseHandler != null) {
164                m_menuCloseHandler.removeHandler();
165            }
166            if (m_popupCloseHandler != null) {
167                m_popupCloseHandler.removeHandler();
168            }
169            // add the close handler for the menu
170            m_menuCloseHandler = getPopup().addCloseHandler(new CmsContextMenuCloseHandler(m_menu));
171            m_popupCloseHandler = getPopup().addCloseHandler(new CloseHandler<PopupPanel>() {
172
173                public void onClose(CloseEvent<PopupPanel> event) {
174
175                    setActive(false);
176                }
177            });
178            m_popup.positionDeferred();
179        } else {
180            m_menuPanel.setWidget(0, 0, m_noEntriesLabel);
181            m_popup.positionDeferred();
182        }
183    }
184}