001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.rpc.CmsRpcAction;
033import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
034import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle.I_CmsToolbarCss;
035import org.opencms.gwt.shared.CmsQuickLaunchData;
036import org.opencms.gwt.shared.CmsQuickLaunchParams;
037import org.opencms.util.CmsStringUtil;
038
039import java.util.Collection;
040import java.util.List;
041
042import com.google.gwt.core.client.GWT;
043import com.google.gwt.dom.client.Style.Unit;
044import com.google.gwt.event.dom.client.ClickEvent;
045import com.google.gwt.event.dom.client.ClickHandler;
046import com.google.gwt.safehtml.client.SafeHtmlTemplates;
047import com.google.gwt.safehtml.shared.SafeHtml;
048import com.google.gwt.user.client.Window;
049import com.google.gwt.user.client.ui.FlowPanel;
050import com.google.gwt.user.client.ui.HTML;
051import com.google.gwt.user.client.ui.Panel;
052
053/**
054 * The user info toolbar button.<p>
055 */
056public class CmsQuickLauncher extends CmsMenuButton implements I_CmsToolbarButton {
057
058    /**
059     * Abstract class for standard handling of quick launh items.<p>
060     */
061    public abstract static class A_QuickLaunchHandler implements I_QuickLaunchHandler {
062
063        /**
064         * @see org.opencms.gwt.client.ui.CmsQuickLauncher.I_QuickLaunchHandler#handleQuickLaunch(org.opencms.gwt.shared.CmsQuickLaunchData)
065         */
066        public void handleQuickLaunch(CmsQuickLaunchData data) {
067
068            if (data.getErrorMessage() != null) {
069                CmsAlertDialog alert = new CmsAlertDialog("" + data.getErrorTitle(), data.getErrorMessage());
070                alert.center();
071            } else if (data.isReload()) {
072                Window.Location.reload();
073            } else {
074                Window.Location.assign(data.getDefaultUrl());
075            }
076        }
077    }
078
079    /**
080     * The quick launch handler interface.<p>
081     */
082    public static interface I_QuickLaunchHandler {
083
084        /**
085         * Gets the quick launch parameters.<p>
086         *
087         * @return the quick launch parameters
088         */
089        CmsQuickLaunchParams getParameters();
090
091        /**
092         * Processes a click on a quick launch item.
093         *
094         * @param data the bean representing the quick launch item
095         */
096        void handleQuickLaunch(CmsQuickLaunchData data);
097    }
098
099    /**
100     * Button for an individual entry in the quick launch menu.<p>
101     */
102    public class QuickLaunchButton extends HTML {
103
104        /**
105         * Creates a new button instance for the given bean.<p>
106         *
107         * @param data the quick launch data bean
108         */
109        public QuickLaunchButton(final CmsQuickLaunchData data) {
110
111            super();
112            I_CmsToolbarCss toolbarCss = I_CmsLayoutBundle.INSTANCE.toolbarCss();
113            SafeHtml html;
114            if (data.getIconUrl().startsWith(FONT_ICON_PREFIX)) {
115                SafeHtml iconHtml = new FontIconHtml(data.getIconUrl());
116                html = BUTTON_TEMPLATES.iconButtonHtml(iconHtml, data.getTitle());
117
118            } else {
119                html = BUTTON_TEMPLATES.imageButtonHtml(data.getIconUrl(), data.getTitle());
120            }
121
122            setHTML(html);
123            setStyleName(toolbarCss.quickButton());
124            addStyleName("v-button-o-app-button");
125            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(data.getButtonStyle())) {
126                addStyleName(data.getButtonStyle());
127            }
128
129            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(data.getErrorMessage())) {
130                setTitle(data.getErrorMessage());
131                addStyleName(toolbarCss.quickButtonDeactivated());
132            } else {
133                addDomHandler(new ClickHandler() {
134
135                    public void onClick(ClickEvent event) {
136
137                        closeMenu();
138                        m_quickLaunchHandler.handleQuickLaunch(data);
139
140                    }
141                }, ClickEvent.getType());
142            }
143        }
144    }
145
146    /** The font icon HTML. */
147    protected class FontIconHtml implements SafeHtml {
148
149        /** The serial version id. */
150        private static final long serialVersionUID = 1L;
151
152        /** The font icon HTML. */
153        private String m_html;
154
155        /**
156         * Constructor.<p>
157         *
158         * @param iconHtml the icon HTML prefixed with 'fonticon:'
159         */
160        protected FontIconHtml(String iconHtml) {
161
162            m_html = iconHtml.substring(FONT_ICON_PREFIX.length());
163        }
164
165        /**
166         * @see com.google.gwt.safehtml.shared.SafeHtml#asString()
167         */
168        public String asString() {
169
170            return m_html;
171        }
172
173    }
174
175    /**
176     * The button HTML generator templates.<p>
177     */
178    protected interface I_ButtonTemplates extends SafeHtmlTemplates {
179
180        /**
181         * Generates the icon button HTML.<p>
182         *
183         * @param iconHtml the icon HTML
184         * @param titel the button title
185         *
186         * @return the HTML
187         */
188        @Template("<span class=\"v-button-wrap\">{0}<span class=\"v-button-caption\">{1}</span></span>")
189        SafeHtml iconButtonHtml(SafeHtml iconHtml, String titel);
190
191        /**
192         * Generates the image button HTML.<p>
193         *
194         * @param imageUri the image URI
195         * @param titel the button title
196         *
197         * @return the HTML
198         */
199        @Template("<span class=\"v-button-wrap\"><img src=\"{0}\" class=\"v-icon\"/><span class=\"v-button-caption\">{1}</span></span>")
200        SafeHtml imageButtonHtml(String imageUri, String titel);
201    }
202
203    /** Html for the menu button. */
204    public static final String BUTTON_HTML = "<span class='"
205        + I_CmsLayoutBundle.INSTANCE.toolbarCss().toolbarFontButton()
206        + "'>\ue617</span>";
207
208    /** The font icon HTML prefix. */
209    protected static final String FONT_ICON_PREFIX = "fonticon:";
210
211    /** The button template generator instance. */
212    static final I_ButtonTemplates BUTTON_TEMPLATES = GWT.create(I_ButtonTemplates.class);
213
214    /** The quick launch handler. */
215    I_QuickLaunchHandler m_quickLaunchHandler;
216
217    /** The handler instance. */
218    private I_CmsToolbarHandler m_handler;
219
220    /** The panel containing the individual quick launch buttons. */
221    private Panel m_itemContainer;
222
223    /**
224     * Constructor.<p>
225     */
226    public CmsQuickLauncher() {
227
228        super();
229        setVisible(false); // only turn visible once the handler is set
230        getPopup().addStyleName(I_CmsLayoutBundle.INSTANCE.dialogCss().contextMenu());
231        getPopup().setWidth(0);
232        m_button.getUpFace().setHTML(BUTTON_HTML);
233
234        m_button.setTitle(Messages.get().key(Messages.GUI_QUICK_LAUCNH_0));
235        m_button.getElement().getStyle().setBottom(1, Unit.PX);
236
237        setToolbarMode(true);
238
239        FlowPanel panel = new FlowPanel();
240        panel.addStyleName(I_CmsLayoutBundle.INSTANCE.toolbarCss().quickLaunchContainer());
241        m_itemContainer = panel;
242        setMenuWidget(panel);
243        addClickHandler(new ClickHandler() {
244
245            public void onClick(ClickEvent event) {
246
247                onToolbarClick();
248            }
249        });
250    }
251
252    /**
253     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#isActive()
254     */
255    public boolean isActive() {
256
257        return isOpen();
258    }
259
260    /**
261     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#onToolbarActivate()
262     */
263    public void onToolbarActivate() {
264
265        CmsRpcAction<List<CmsQuickLaunchData>> action = new CmsRpcAction<List<CmsQuickLaunchData>>() {
266
267            @Override
268            public void execute() {
269
270                start(150, false);
271                CmsCoreProvider.getVfsService().loadQuickLaunchItems(m_quickLaunchHandler.getParameters(), this);
272            }
273
274            @Override
275            protected void onResponse(List<CmsQuickLaunchData> result) {
276
277                stop(false);
278                fillItems(result);
279                openMenu();
280            }
281
282        };
283        action.execute();
284    }
285
286    /**
287     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#onToolbarClick()
288     */
289    public void onToolbarClick() {
290
291        boolean active = isActive();
292
293        setActive(!active);
294
295    }
296
297    /**
298     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#onToolbarDeactivate()
299     */
300    public void onToolbarDeactivate() {
301
302        // nothing to do
303    }
304
305    /**
306     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#setActive(boolean)
307     */
308    public void setActive(boolean active) {
309
310        if (active) {
311            if (m_handler != null) {
312                m_handler.deactivateCurrentButton();
313                m_handler.setActiveButton(this);
314            }
315            m_popup.catchNotifications();
316            onToolbarActivate();
317            openMenu();
318        } else {
319            onToolbarDeactivate();
320            closeMenu();
321            if (m_handler != null) {
322                m_handler.setActiveButton(null);
323                m_handler.activateSelection();
324            }
325        }
326    }
327
328    /**
329     * Sets the button handler.<p>
330     *
331     * @param handler the button handler
332     */
333    public void setHandler(I_CmsToolbarHandler handler) {
334
335        m_handler = handler;
336    }
337
338    /**
339     * Sets the quick launch handler and makes the button visible.<p>
340     *
341     * @param handler the quick launch handler
342     */
343    public void setQuicklaunchHandler(I_QuickLaunchHandler handler) {
344
345        m_quickLaunchHandler = handler;
346        setVisible(true);
347    }
348
349    /**
350     * @see org.opencms.gwt.client.ui.CmsMenuButton#autoClose()
351     */
352    @Override
353    protected void autoClose() {
354
355        super.autoClose();
356        onToolbarDeactivate();
357        if (m_handler != null) {
358            m_handler.setActiveButton(null);
359            m_handler.activateSelection();
360        }
361    }
362
363    /**
364     * Fills the quick launch menu with buttons corresponding to the given quick launch beans.<p>
365     *
366     * @param quickLaunchData the list of quick launch beans
367     */
368    protected void fillItems(Collection<CmsQuickLaunchData> quickLaunchData) {
369
370        Panel container = getItemContainer();
371        container.clear();
372        for (CmsQuickLaunchData item : quickLaunchData) {
373            container.add(new QuickLaunchButton(item));
374        }
375    }
376
377    /**
378     * Returns the container-page handler.<p>
379     *
380     * @return the container-page handler
381     */
382    protected I_CmsToolbarHandler getHandler() {
383
384        return m_handler;
385    }
386
387    /**
388     * Gets the item container.<p>
389     *
390     * @return the item container
391     */
392    private Panel getItemContainer() {
393
394        return m_itemContainer;
395    }
396}