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.control.CmsSitemapController;
031import org.opencms.ade.sitemap.client.ui.css.I_CmsSitemapLayoutBundle;
032import org.opencms.gwt.client.ui.CmsList;
033import org.opencms.gwt.client.ui.CmsMenuButton;
034import org.opencms.gwt.client.ui.CmsTabbedPanel;
035import org.opencms.gwt.client.ui.CmsToolbarPopup;
036import org.opencms.gwt.client.ui.I_CmsListItem;
037
038import com.google.gwt.core.client.Scheduler;
039import com.google.gwt.core.client.Scheduler.ScheduledCommand;
040import com.google.gwt.event.dom.client.ClickEvent;
041import com.google.gwt.event.dom.client.ClickHandler;
042import com.google.gwt.event.logical.shared.SelectionEvent;
043import com.google.gwt.event.logical.shared.SelectionHandler;
044import com.google.gwt.user.client.ui.FlowPanel;
045import com.google.gwt.user.client.ui.SimplePanel;
046import com.google.gwt.user.client.ui.Widget;
047
048/**
049 * A menu button with list tabs.<p>
050 *
051 * @since 8.0.0
052 */
053public abstract class A_CmsToolbarListMenuButton extends CmsMenuButton implements I_CmsToolbarActivatable {
054
055    /** The dialog width. */
056    public static final int DIALOG_WIDTH = 600;
057
058    /** Text metrics key for truncation. */
059    public static final String TM_LITST_MENU = "TM_LITST_MENU";
060
061    /** Flag to indicate if the menu tabs have been initialized. */
062    protected boolean m_initialized;
063
064    /** The controller instance. */
065    private CmsSitemapController m_controller;
066
067    /** The tab panel. */
068    protected CmsTabbedPanel<CmsListTab> m_tabs;
069
070    /** The toolbar instance. */
071    private CmsSitemapToolbar m_toolbar;
072
073    /**
074     * Constructor.<p>
075     *
076     * @param title the button title
077     * @param iconClass the icon CSS class
078     * @param toolbar the toolbar instance
079     * @param controller the controller instance
080     */
081    public A_CmsToolbarListMenuButton(
082        String title,
083        String iconClass,
084        CmsSitemapToolbar toolbar,
085        CmsSitemapController controller) {
086
087        super(null, iconClass);
088        m_toolbar = toolbar;
089        m_controller = controller;
090        setTitle(title);
091        setToolbarMode(true);
092        setOpenRight(true);
093        if (!m_controller.isEditable()) {
094            setEnabled(false);
095        }
096
097        m_tabs = new CmsTabbedPanel<CmsListTab>();
098        m_tabs.addSelectionHandler(new SelectionHandler<Integer>() {
099
100            public void onSelection(SelectionEvent<Integer> event) {
101
102                Scheduler.get().scheduleDeferred(new ScheduledCommand() {
103
104                    public void execute() {
105
106                        updateSize();
107                    }
108                });
109            }
110        });
111        SimplePanel tabsContainer = new SimplePanel();
112        tabsContainer.addStyleName(I_CmsSitemapLayoutBundle.INSTANCE.clipboardCss().menuTabContainer());
113        int dialogHeight = CmsToolbarPopup.getAvailableHeight();
114        int dialogWidth = CmsToolbarPopup.getAvailableWidth();
115        tabsContainer.setHeight(dialogHeight + "px");
116        getPopup().setWidth(dialogWidth);
117        tabsContainer.add(m_tabs);
118        FlowPanel content = new FlowPanel();
119        content.add(tabsContainer);
120        setMenuWidget(content);
121        addClickHandler(new ClickHandler() {
122
123            /**
124             * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent)
125             */
126            public void onClick(ClickEvent event) {
127
128                if (!isOpen()) {
129                    getToolbar().onButtonActivation(A_CmsToolbarListMenuButton.this);
130                    if (!m_initialized) {
131                        // lazy initialization
132                        m_initialized = initContent();
133                    }
134
135                    openMenu();
136                    updateSize();
137                } else {
138                    closeMenu();
139                }
140            }
141        });
142    }
143
144    /**
145     * Adds a new tab to the tab-panel.<p>
146     *
147     * @param tab the tab
148     * @param title the tab title
149     */
150    public void addTab(CmsListTab tab, String title) {
151
152        m_tabs.add(tab, title);
153    }
154
155    /**
156     * Creates a new tab.<p>
157     *
158     * @param list list of items
159     *
160     * @return the created tab widget
161     */
162    public CmsListTab createTab(CmsList<? extends I_CmsListItem> list) {
163
164        return new CmsListTab(list);
165    }
166
167    /**
168     * @see org.opencms.ade.sitemap.client.toolbar.I_CmsToolbarActivatable#onActivation(com.google.gwt.user.client.ui.Widget)
169     */
170    public void onActivation(Widget widget) {
171
172        closeMenu();
173    }
174
175    /**
176     * Updates the dialog size according to the current tab content.<p>
177     */
178    public void updateSize() {
179
180        int width = CmsToolbarPopup.getAvailableWidth();
181        getPopup().setWidth(width);
182        CmsListTab tab = m_tabs.getWidget(m_tabs.getSelectedIndex());
183        tab.truncate(TM_LITST_MENU, width);
184        int availableHeight = CmsToolbarPopup.getAvailableHeight();
185        int requiredHeight = tab.getRequiredHeight() + 44;
186        int height = (availableHeight > requiredHeight) && (requiredHeight > 50) ? requiredHeight : availableHeight;
187        m_tabs.getParent().setHeight(height + "px");
188        tab.getScrollPanel().onResizeDescendant();
189    }
190
191    /**
192     * Returns the controller.<p>
193     *
194     * @return the controller
195     */
196    protected CmsSitemapController getController() {
197
198        return m_controller;
199    }
200
201    /**
202     * Returns the toolbar.<p>
203     *
204     * @return the toolbar
205     */
206    protected CmsSitemapToolbar getToolbar() {
207
208        return m_toolbar;
209    }
210
211    /**
212     * Initializes the menu tabs.<p>
213     *
214     * @return true if the content does not need to be initialized the next time the menu is opened
215     */
216    protected abstract boolean initContent();
217}