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.ui.css.I_CmsLayoutBundle;
033import org.opencms.ade.galleries.client.CmsGalleryController;
034import org.opencms.ade.galleries.client.CmsGalleryControllerHandler;
035import org.opencms.ade.galleries.client.I_CmsGalleryHandler;
036import org.opencms.ade.galleries.client.ui.CmsGalleryDialog;
037import org.opencms.ade.galleries.client.ui.CmsResultListItem;
038import org.opencms.ade.galleries.shared.CmsGalleryConfiguration;
039import org.opencms.ade.galleries.shared.CmsResultItemBean;
040import org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration;
041import org.opencms.ade.galleries.shared.rpc.I_CmsGalleryServiceAsync;
042import org.opencms.gwt.client.CmsCoreProvider;
043import org.opencms.gwt.client.dnd.CmsDNDHandler;
044import org.opencms.gwt.client.rpc.CmsRpcAction;
045import org.opencms.gwt.client.ui.A_CmsToolbarMenu;
046import org.opencms.gwt.client.ui.CmsPopup;
047import org.opencms.gwt.client.ui.CmsToolbarPopup;
048import org.opencms.gwt.client.ui.I_CmsAutoHider;
049import org.opencms.gwt.client.ui.I_CmsButton;
050import org.opencms.gwt.shared.CmsGwtConstants;
051
052import com.google.gwt.dom.client.Document;
053import com.google.gwt.user.client.ui.FlowPanel;
054import com.google.gwt.user.client.ui.SimplePanel;
055import com.google.gwt.user.client.ui.Widget;
056
057/**
058 * The gallery tool-bar menu.<p>
059 *
060 * This is used to access all galleries in the system (including download and image galleries), but can not
061 * be used to add elements to the container page.<p>
062 *
063 * @since 8.0.0
064 */
065public class CmsToolbarAllGalleriesMenu extends A_CmsToolbarMenu<CmsContainerpageHandler> {
066
067    /** Marker string to distinguish search results from the 'all galleries' dialog from results from other instances of the gallery dialog. */
068    public static final String DND_MARKER = "ALL_GALLERIES_DND_MARKER";
069
070    /** The gallery service instance. */
071    I_CmsGalleryServiceAsync m_galleryService = CmsGalleryController.createGalleryService();
072
073    /** The main content widget. */
074    private FlowPanel m_contentPanel;
075
076    /** The gallery dialog instance. */
077    private CmsGalleryDialog m_dialog;
078
079    /**
080     * Constructor.<p>
081     *
082     * @param handler the container-page handler
083     * @param dragHandler the container-page drag handler
084     */
085    public CmsToolbarAllGalleriesMenu(CmsContainerpageHandler handler, CmsDNDHandler dragHandler) {
086
087        super(I_CmsButton.ButtonData.GALLERY_BUTTON, handler);
088        m_contentPanel = new FlowPanel();
089        setMenuWidget(m_contentPanel);
090    }
091
092    /**
093     * Creates a new gallery dialog and instantiates the controller for it.<p>
094     *
095     * @param configuration the gallery configuration
096     * @param galleryHandler the gallery handler
097     *
098     * @return the gallery dialog instance
099     */
100    @SuppressWarnings("unused")
101    protected static CmsGalleryDialog createDialog(
102        I_CmsGalleryConfiguration configuration,
103        I_CmsGalleryHandler galleryHandler) {
104
105        CmsGalleryDialog result = new CmsGalleryDialog(galleryHandler);
106        new CmsGalleryController(new CmsGalleryControllerHandler(result), configuration);
107        return result;
108    }
109
110    /**
111     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#onToolbarActivate()
112     */
113    public void onToolbarActivate() {
114
115        loadConfiguration();
116    }
117
118    /**
119     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#onToolbarDeactivate()
120     */
121    public void onToolbarDeactivate() {
122
123        Document.get().getBody().removeClassName(I_CmsButton.ButtonData.WAND_BUTTON.getIconClass());
124    }
125
126    /**
127     * @see org.opencms.gwt.client.ui.CmsMenuButton#getPopup()
128     */
129    @Override
130    protected CmsPopup getPopup() {
131
132        return super.getPopup();
133    }
134
135    /**
136     * Opens the dialog given the loaded gallery configuration.<p>
137     *
138     * @param configuration the gallery configuration
139     */
140    protected void openDialog(I_CmsGalleryConfiguration configuration) {
141
142        Document.get().getBody().addClassName(I_CmsButton.ButtonData.WAND_BUTTON.getIconClass());
143        if (m_dialog == null) {
144            SimplePanel tabsContainer = new SimplePanel();
145            tabsContainer.addStyleName(I_CmsLayoutBundle.INSTANCE.containerpageCss().menuTabContainer());
146            int dialogHeight = CmsToolbarPopup.getAvailableHeight();
147            int dialogWidth = CmsToolbarPopup.getAvailableWidth();
148            m_dialog = createDialog(configuration, new I_CmsGalleryHandler() {
149
150                public boolean filterDnd(CmsResultItemBean resultBean) {
151
152                    return CmsGwtConstants.TYPE_IMAGE.equals(resultBean.getType());
153                }
154
155                public Widget getAdditionalTypeTabControl() {
156
157                    return null;
158                }
159
160                public I_CmsAutoHider getAutoHideParent() {
161
162                    return getPopup();
163                }
164
165                public CmsDNDHandler getDndHandler() {
166
167                    return CmsContainerpageController.get().getDndHandler();
168                }
169
170                public void processResultItem(CmsResultListItem item) {
171
172                    item.setData(DND_MARKER);
173                    item.setDndHelperClass("imagedrag");
174                    item.setDndParentClass(
175                        "imageparent "
176                            + org.opencms.ade.galleries.client.ui.css.I_CmsLayoutBundle.INSTANCE.galleryResultItemCss().tilingList());
177                    //item.setDragHelperTransformer(DRAG_HELPER_TRANSFORMER);
178                }
179
180            });
181            m_dialog.setDialogSize(dialogWidth, dialogHeight);
182            getPopup().setWidth(dialogWidth);
183            tabsContainer.add(m_dialog);
184            m_contentPanel.add(tabsContainer);
185        } else {
186            int dialogWidth = CmsToolbarPopup.getAvailableWidth();
187            getPopup().setWidth(dialogWidth);
188            m_dialog.truncate("GALLERY_DIALOG_TM", dialogWidth);
189            m_dialog.updateSizes();
190        }
191    }
192
193    /**
194     * Loads the configuration from the server.<p>
195     */
196    private void loadConfiguration() {
197
198        CmsRpcAction<CmsGalleryConfiguration> action = new CmsRpcAction<CmsGalleryConfiguration>() {
199
200            @Override
201            public void execute() {
202
203                start(200, false);
204                m_galleryService.getAdeViewModeConfiguration(this);
205            }
206
207            @Override
208            protected void onResponse(CmsGalleryConfiguration result) {
209
210                stop(false);
211                if (result.getReferencePath() == null) {
212                    result.setReferencePath(CmsCoreProvider.get().getUri());
213                }
214                openDialog(result);
215
216            }
217        };
218        if (m_dialog != null) {
219            openDialog(null);
220        } else {
221            action.execute();
222        }
223
224    }
225
226}