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.containerpage.client.ui;
029
030import org.opencms.ade.containerpage.client.CmsContainerpageController;
031import org.opencms.ade.containerpage.client.CmsContainerpageHandler;
032import org.opencms.ade.containerpage.client.CmsFavoritesDNDController;
033import org.opencms.ade.containerpage.client.Messages;
034import org.opencms.ade.containerpage.client.ui.css.I_CmsLayoutBundle;
035import org.opencms.gwt.client.rpc.CmsRpcAction;
036import org.opencms.gwt.client.ui.A_CmsToolbarMenu;
037import org.opencms.gwt.client.ui.CmsListItem;
038import org.opencms.gwt.client.ui.CmsTabbedPanel;
039import org.opencms.gwt.client.ui.CmsToolbarPopup;
040import org.opencms.gwt.client.ui.I_CmsButton;
041import org.opencms.gwt.client.util.CmsDebugLog;
042
043import java.util.ArrayList;
044import java.util.Iterator;
045import java.util.List;
046
047import com.google.gwt.core.client.Scheduler;
048import com.google.gwt.core.client.Scheduler.ScheduledCommand;
049import com.google.gwt.dom.client.Document;
050import com.google.gwt.event.logical.shared.SelectionEvent;
051import com.google.gwt.event.logical.shared.SelectionHandler;
052import com.google.gwt.user.client.ui.FlowPanel;
053import com.google.gwt.user.client.ui.SimplePanel;
054import com.google.gwt.user.client.ui.Widget;
055
056/**
057 * The clip-board tool-bar menu.<p>
058 *
059 * @since 8.0.0
060 */
061public class CmsToolbarClipboardMenu extends A_CmsToolbarMenu<CmsContainerpageHandler> {
062
063    /** The favorite list widget. */
064    protected CmsFavoriteTab m_favorites;
065
066    /** Flag to indicate if the favorites are being edited. */
067    protected boolean m_isEditingFavorites;
068
069    /** The main content widget. */
070    private FlowPanel m_content;
071
072    /** The favorites editing drag and drop controller. */
073    private CmsFavoritesDNDController m_dndController;
074
075    /** The recent list widget. */
076    private CmsRecentTab m_recent;
077
078    /** The favorite and recent list tabs. */
079    CmsTabbedPanel<A_CmsClipboardTab> m_tabs;
080
081    /**
082     * Constructor.<p>
083     *
084     * @param handler the container-page handler
085     */
086    public CmsToolbarClipboardMenu(CmsContainerpageHandler handler) {
087
088        super(I_CmsButton.ButtonData.CLIPBOARD_BUTTON, handler);
089
090        m_content = new FlowPanel();
091        m_tabs = new CmsTabbedPanel<A_CmsClipboardTab>();
092        m_favorites = new CmsFavoriteTab(this);
093        m_recent = new CmsRecentTab();
094
095        m_tabs.add(m_favorites, Messages.get().key(Messages.GUI_TAB_FAVORITES_TITLE_0));
096        m_tabs.add(m_recent, Messages.get().key(Messages.GUI_TAB_RECENT_TITLE_0));
097        m_tabs.addSelectionHandler(new SelectionHandler<Integer>() {
098
099            /**
100             * @see com.google.gwt.event.logical.shared.SelectionHandler#onSelection(com.google.gwt.event.logical.shared.SelectionEvent)
101             */
102            public void onSelection(SelectionEvent<Integer> event) {
103
104                if (m_isEditingFavorites) {
105                    m_favorites.saveFavorites();
106                }
107                CmsContainerpageController.get().saveClipboardTab(event.getSelectedItem().intValue());
108                Scheduler.get().scheduleDeferred(new ScheduledCommand() {
109
110                    public void execute() {
111
112                        updateSize();
113                    }
114                });
115
116            }
117        });
118
119        SimplePanel tabsContainer = new SimplePanel();
120        tabsContainer.addStyleName(I_CmsLayoutBundle.INSTANCE.containerpageCss().menuTabContainer());
121        int dialogHeight = CmsToolbarPopup.getAvailableHeight();
122        int dialogWidth = CmsToolbarPopup.getAvailableWidth();
123        tabsContainer.setHeight(dialogHeight + "px");
124        getPopup().setWidth(dialogWidth);
125        tabsContainer.add(m_tabs);
126        m_content.add(tabsContainer);
127        setMenuWidget(m_content);
128        m_dndController = new CmsFavoritesDNDController();
129    }
130
131    /**
132     * Adds an element to the favorite list widget.<p>
133     *
134     * @param listItem the item widget
135     */
136    public void addToFavorites(CmsListItem listItem) {
137
138        m_favorites.addListItem(listItem);
139    }
140
141    /**
142     * Adds an element to the recent list widget.<p>
143     *
144     * @param listItem the item widget
145     */
146    public void addToRecent(CmsListItem listItem) {
147
148        m_recent.addListItem(listItem);
149    }
150
151    /**
152     * Clears the contents of the favorite list widget.<p>
153     */
154    public void clearFavorites() {
155
156        m_favorites.clearList();
157    }
158
159    /**
160     * Clears the contents of the recent list widget.<p>
161     */
162    public void clearRecent() {
163
164        m_recent.clearList();
165    }
166
167    /**
168     * Enables the favorite list editing.<p>
169     */
170    public void enableFavoritesEdit() {
171
172        m_isEditingFavorites = true;
173        getHandler().enableFavoriteEditing(true, m_dndController);
174        Iterator<Widget> it = m_favorites.iterator();
175        while (it.hasNext()) {
176
177            CmsMenuListItem element = (CmsMenuListItem)it.next();
178            element.hideEditButton();
179            element.showRemoveButton();
180        }
181    }
182
183    /**
184     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#onToolbarActivate()
185     */
186    public void onToolbarActivate() {
187
188        Document.get().getBody().addClassName(I_CmsButton.ButtonData.CLIPBOARD_BUTTON.getIconClass());
189        getHandler().loadFavorites();
190        getHandler().loadRecent();
191        CmsRpcAction<Integer> tabAction = new CmsRpcAction<Integer>() {
192
193            @Override
194            public void execute() {
195
196                start(1, false);
197                CmsContainerpageController.get().getContainerpageService().loadClipboardTab(this);
198            }
199
200            @Override
201            protected void onResponse(Integer result) {
202
203                stop(false);
204                m_tabs.selectTab(result.intValue(), false);
205                updateSize();
206            }
207
208        };
209        tabAction.execute();
210    }
211
212    /**
213     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#onToolbarDeactivate()
214     */
215    public void onToolbarDeactivate() {
216
217        if (m_isEditingFavorites) {
218            m_favorites.saveFavorites();
219        }
220        Document.get().getBody().removeClassName(I_CmsButton.ButtonData.CLIPBOARD_BUTTON.getIconClass());
221    }
222
223    /**
224     * Reloads the favorite list.<p>
225     */
226    public void reloadFavorites() {
227
228        m_isEditingFavorites = false;
229        getHandler().enableFavoriteEditing(false, m_dndController);
230        getHandler().loadFavorites();
231    }
232
233    /**
234     * Replaces old versions of the given item with the new one.<p>
235     *
236     * @param listItem the list item
237     */
238    public void replaceFavoriteItem(CmsListItem listItem) {
239
240        m_favorites.replaceItem(listItem);
241    }
242
243    /**
244     * Replaces old versions of the given item with the new one.<p>
245     *
246     * @param listItem the list item
247     */
248    public void replaceRecentItem(CmsListItem listItem) {
249
250        m_recent.replaceItem(listItem);
251    }
252
253    /**
254     * Saves the favorite list.<p>
255     */
256    public void saveFavorites() {
257
258        m_isEditingFavorites = false;
259        getHandler().enableFavoriteEditing(false, m_dndController);
260        List<String> clientIds = new ArrayList<String>();
261        Iterator<Widget> it = m_favorites.iterator();
262        while (it.hasNext()) {
263            try {
264                CmsMenuListItem element = (CmsMenuListItem)it.next();
265                element.hideRemoveButton();
266                element.showEditButton();
267                clientIds.add(element.getId());
268            } catch (ClassCastException e) {
269                CmsDebugLog.getInstance().printLine("Could not cast widget");
270            }
271        }
272        getHandler().saveFavoriteList(clientIds);
273    }
274
275    /**
276     * Updates the popup size according to the tab contents.<p>
277     */
278    public void updateSize() {
279
280        int availableHeight = CmsToolbarPopup.getAvailableHeight();
281        int dialogWidth = CmsToolbarPopup.getAvailableWidth();
282
283        A_CmsClipboardTab tab = m_tabs.getWidget(m_tabs.getSelectedIndex());
284        int requiredHeight = tab.getRequiredHeight() + 31;
285        int dialogHeight = availableHeight > requiredHeight ? requiredHeight : availableHeight;
286        m_tabs.getParent().setHeight(dialogHeight + "px");
287        getPopup().setWidth(dialogWidth);
288        tab.getList().truncate("CLIPBOARD_TM", dialogWidth - 40);
289        tab.getScrollPanel().onResizeDescendant();
290    }
291}