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.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            m_html = iconHtml.substring(FONT_ICON_PREFIX.length());
162        }
163
164        /**
165         * @see com.google.gwt.safehtml.shared.SafeHtml#asString()
166         */
167        public String asString() {
168
169            return m_html;
170        }
171
172    }
173
174    /**
175     * The button HTML generator templates.<p>
176     */
177    protected interface I_ButtonTemplates extends SafeHtmlTemplates {
178
179        /**
180         * Generates the icon button HTML.<p>
181         *
182         * @param iconHtml the icon HTML
183         * @param titel the button title
184         *
185         * @return the HTML
186         */
187        @Template("<span class=\"v-button-wrap\">{0}<span class=\"v-button-caption\">{1}</span></span>")
188        SafeHtml iconButtonHtml(SafeHtml iconHtml, String titel);
189
190        /**
191         * Generates the image button HTML.<p>
192         *
193         * @param imageUri the image URI
194         * @param titel the button title
195         *
196         * @return the HTML
197         */
198        @Template("<span class=\"v-button-wrap\"><img src=\"{0}\" class=\"v-icon\"/><span class=\"v-button-caption\">{1}</span></span>")
199        SafeHtml imageButtonHtml(String imageUri, String titel);
200    }
201
202    /** Html for the menu button. */
203    public static final String BUTTON_HTML = "<span class='"
204        + I_CmsLayoutBundle.INSTANCE.toolbarCss().toolbarFontButton()
205        + "'>\ue617</span>";
206
207    /** The font icon HTML prefix. */
208    protected static final String FONT_ICON_PREFIX = "fonticon:";
209
210    /** The button template generator instance. */
211    static final I_ButtonTemplates BUTTON_TEMPLATES = GWT.create(I_ButtonTemplates.class);
212
213    /** The quick launch handler. */
214    I_QuickLaunchHandler m_quickLaunchHandler;
215
216    /** The handler instance. */
217    private I_CmsToolbarHandler m_handler;
218
219    /** The panel containing the individual quick launch buttons. */
220    private Panel m_itemContainer;
221
222    /**
223     * Constructor.<p>
224     */
225    public CmsQuickLauncher() {
226
227        super();
228        setVisible(false); // only turn visible once the handler is set
229        getPopup().addStyleName(I_CmsLayoutBundle.INSTANCE.dialogCss().contextMenu());
230        getPopup().setWidth(0);
231        m_button.getUpFace().setHTML(BUTTON_HTML);
232
233        m_button.setTitle(Messages.get().key(Messages.GUI_QUICK_LAUCNH_0));
234        m_button.getElement().getStyle().setBottom(1, Unit.PX);
235
236        setToolbarMode(true);
237
238        FlowPanel panel = new FlowPanel();
239        panel.addStyleName(I_CmsLayoutBundle.INSTANCE.toolbarCss().quickLaunchContainer());
240        m_itemContainer = panel;
241        setMenuWidget(panel);
242        addClickHandler(new ClickHandler() {
243
244            public void onClick(ClickEvent event) {
245
246                onToolbarClick();
247            }
248        });
249    }
250
251    /**
252     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#isActive()
253     */
254    public boolean isActive() {
255
256        return isOpen();
257    }
258
259    /**
260     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#onToolbarActivate()
261     */
262    public void onToolbarActivate() {
263
264        CmsRpcAction<List<CmsQuickLaunchData>> action = new CmsRpcAction<List<CmsQuickLaunchData>>() {
265
266            @Override
267            public void execute() {
268
269                start(150, false);
270                CmsCoreProvider.getVfsService().loadQuickLaunchItems(m_quickLaunchHandler.getParameters(), this);
271            }
272
273            @Override
274            protected void onResponse(List<CmsQuickLaunchData> result) {
275
276                stop(false);
277                fillItems(result);
278                openMenu();
279            }
280
281        };
282        action.execute();
283    }
284
285    /**
286     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#onToolbarClick()
287     */
288    public void onToolbarClick() {
289
290        boolean active = isActive();
291
292        setActive(!active);
293
294    }
295
296    /**
297     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#onToolbarDeactivate()
298     */
299    public void onToolbarDeactivate() {
300
301        // nothing to do
302    }
303
304    /**
305     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#setActive(boolean)
306     */
307    public void setActive(boolean active) {
308
309        if (active) {
310            if (m_handler != null) {
311                m_handler.deactivateCurrentButton();
312                m_handler.setActiveButton(this);
313            }
314            m_popup.catchNotifications();
315            onToolbarActivate();
316            openMenu();
317        } else {
318            onToolbarDeactivate();
319            closeMenu();
320            if (m_handler != null) {
321                m_handler.setActiveButton(null);
322                m_handler.activateSelection();
323            }
324        }
325    }
326
327    /**
328     * Sets the button handler.<p>
329     *
330     * @param handler the button handler
331     */
332    public void setHandler(I_CmsToolbarHandler handler) {
333
334        m_handler = handler;
335    }
336
337    /**
338     * Sets the quick launch handler and makes the button visible.<p>
339     *
340     * @param handler the quick launch handler
341     */
342    public void setQuicklaunchHandler(I_QuickLaunchHandler handler) {
343
344        m_quickLaunchHandler = handler;
345        setVisible(true);
346    }
347
348    /**
349     * @see org.opencms.gwt.client.ui.CmsMenuButton#autoClose()
350     */
351    @Override
352    protected void autoClose() {
353
354        super.autoClose();
355        onToolbarDeactivate();
356        if (m_handler != null) {
357            m_handler.setActiveButton(null);
358            m_handler.activateSelection();
359        }
360    }
361
362    /**
363     * Fills the quick launch menu with buttons corresponding to the given quick launch beans.<p>
364     *
365     * @param quickLaunchData the list of quick launch beans
366     */
367    protected void fillItems(Collection<CmsQuickLaunchData> quickLaunchData) {
368
369        Panel container = getItemContainer();
370        container.clear();
371        for (CmsQuickLaunchData item : quickLaunchData) {
372            container.add(new QuickLaunchButton(item));
373        }
374    }
375
376    /**
377     * Returns the container-page handler.<p>
378     *
379     * @return the container-page handler
380     */
381    protected I_CmsToolbarHandler getHandler() {
382
383        return m_handler;
384    }
385
386    /**
387     * Gets the item container.<p>
388     *
389     * @return the item container
390     */
391    private Panel getItemContainer() {
392
393        return m_itemContainer;
394    }
395}