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.ui.apps;
029
030import org.opencms.file.CmsObject;
031import org.opencms.ui.A_CmsUI;
032import org.opencms.ui.CmsCssIcon;
033import org.opencms.ui.components.CmsBasicDialog;
034import org.opencms.ui.components.CmsBasicDialog.DialogWidth;
035import org.opencms.ui.components.OpenCmsTheme;
036
037import java.util.Locale;
038
039import com.vaadin.server.Resource;
040import com.vaadin.ui.Button;
041import com.vaadin.ui.Button.ClickEvent;
042import com.vaadin.ui.Button.ClickListener;
043import com.vaadin.ui.Component;
044import com.vaadin.ui.Panel;
045import com.vaadin.ui.UI;
046import com.vaadin.ui.Window;
047import com.vaadin.ui.themes.ValoTheme;
048
049/**
050 * The default app button provider.<p>
051 */
052public class CmsDefaultAppButtonProvider implements I_CmsAppButtonProvider {
053
054    /**
055     * Creates a properly styled button for the given app.<p>
056     *
057     * @param cms the cms context
058     * @param appConfig the app configuration
059     * @param locale the locale
060     *
061     * @return the button component
062     */
063    public static Component createAppButton(
064        CmsObject cms,
065        final I_CmsWorkplaceAppConfiguration appConfig,
066        Locale locale) {
067
068        Button button = createAppIconButton(appConfig, locale);
069        button.addClickListener(new ClickListener() {
070
071            private static final long serialVersionUID = 1L;
072
073            public void buttonClick(ClickEvent event) {
074
075                if ((appConfig instanceof I_CmsHasAppLaunchCommand)
076                    && (((I_CmsHasAppLaunchCommand)appConfig).getAppLaunchCommand() != null)) {
077                    ((I_CmsHasAppLaunchCommand)appConfig).getAppLaunchCommand().run();
078                } else {
079                    CmsAppWorkplaceUi ui = (CmsAppWorkplaceUi)A_CmsUI.get();
080                    ui.showApp(appConfig);
081                }
082            }
083        });
084        CmsAppVisibilityStatus status = appConfig.getVisibility(cms);
085        if (!status.isActive()) {
086            button.setEnabled(false);
087            button.setDescription(status.getHelpText());
088        } else {
089            String helpText = appConfig.getHelpText(locale);
090            button.setDescription(helpText);
091        }
092        return button;
093    }
094
095    /**
096     * Creates a properly styled button for the given app.<p>
097     *
098     * @param cms the cms context
099     * @param node the node to display a buttom for
100     * @param locale the locale
101     *
102     * @return the button component
103     *
104     *                         (I_CmsFolderAppCategory)childNode.getCategory(),
105                        childNode.getAppConfigurations())
106     */
107    public static Component createAppFolderButton(CmsObject cms, final CmsAppCategoryNode node, final Locale locale) {
108
109        Button button = createAppFolderIconButton((I_CmsFolderAppCategory)node.getCategory(), locale);
110        button.addClickListener(new ClickListener() {
111
112            private static final long serialVersionUID = 1L;
113            private static final int DEFAULT_WIDTH = 855;
114            private static final int DEFAULT_MAX_APP_PER_ROW = 5;
115            private static final int MARGIN = 10;
116
117            public void buttonClick(ClickEvent event) {
118
119                CmsAppHierarchyPanel panel = new CmsAppHierarchyPanel(new CmsDefaultAppButtonProvider());
120                //                panel.setCaption(((I_CmsFolderAppCategory)node.getCategory()).getName(locale));
121                panel.setCaption("Test caption");
122
123                panel.fill(node, locale);
124
125                Panel realPanel = new Panel();
126                realPanel.setContent(panel);
127                realPanel.setCaption(((I_CmsFolderAppCategory)node.getCategory()).getName(locale));
128                int browtherWidth = A_CmsUI.get().getPage().getBrowserWindowWidth();
129                if (node.getAppConfigurations().size() <= DEFAULT_MAX_APP_PER_ROW) {
130                    panel.setComponentAlignment(panel.getComponent(0), com.vaadin.ui.Alignment.MIDDLE_CENTER);
131                }
132                if (browtherWidth < DEFAULT_WIDTH) {
133                    realPanel.setWidth((browtherWidth - (2 * MARGIN)) + "px");
134                } else {
135                    realPanel.setWidth(DEFAULT_WIDTH + "px");
136                }
137                final Window window = CmsBasicDialog.prepareWindow(DialogWidth.content);
138                window.setResizable(false);
139                window.setContent(realPanel);
140                window.setClosable(true);
141                window.addStyleName("o-close-on-background");
142                window.setModal(true);
143                window.setDraggable(false);
144
145                CmsAppWorkplaceUi.get().addWindow(window);
146
147            }
148        });
149        return button;
150    }
151
152    /**
153     * Creates a properly styled button for the given app, without adding a click handler or checking visibility settings.<p>
154     *
155     * @param appCat the app category
156     * @param locale the locale
157     *
158     * @return the button component
159     */
160    public static Button createAppFolderIconButton(I_CmsFolderAppCategory appCat, Locale locale) {
161
162        return createIconButton(
163            appCat.getName(locale),
164            appCat.getHelpText(locale),
165            appCat.getIcon(),
166            appCat.getButtonStyle());
167    }
168
169    /**
170     * Creates a properly styled button for the given app, without adding a click handler or checking visibility settings.<p>
171     *
172     * @param appConfig the app configuration
173     * @param locale the locale
174     *
175     * @return the button component
176     */
177    public static Button createAppIconButton(I_CmsWorkplaceAppConfiguration appConfig, Locale locale) {
178
179        return createIconButton(
180            appConfig.getName(locale),
181            appConfig.getHelpText(locale),
182            appConfig.getIcon(),
183            appConfig.getButtonStyle());
184    }
185
186    /**
187     * Creates an icon button.<p>
188     *
189     * @param name the name
190     * @param description the description
191     * @param icon the icon
192     *
193     * @return the created button
194     */
195    public static Button createIconButton(String name, String description, Resource icon) {
196
197        return createIconButton(name, description, icon, I_CmsAppButtonProvider.BUTTON_STYLE_TRANSPARENT);
198    }
199
200    /**
201     * Creates an icon button.<p>
202     *
203     * @param name the name
204     * @param description the description
205     * @param icon the icon
206     * @param buttonStyle the button style
207     *
208     * @return the created button
209     */
210    public static Button createIconButton(String name, String description, Resource icon, String buttonStyle) {
211
212        Button button = new Button(name);
213        button.setIcon(icon, name);
214        button.setDescription(description);
215        button.addStyleName(OpenCmsTheme.APP_BUTTON);
216        button.addStyleName(ValoTheme.BUTTON_BORDERLESS);
217        button.addStyleName(ValoTheme.BUTTON_ICON_ALIGN_TOP);
218        if (buttonStyle != null) {
219            button.addStyleName(buttonStyle);
220        }
221        if ((icon instanceof CmsCssIcon) && ((CmsCssIcon)icon).hasAdditionalButtonStyle()) {
222            button.addStyleName(((CmsCssIcon)icon).getAdditionalButtonStyle());
223        }
224        return button;
225    }
226
227    /**
228     * @see org.opencms.ui.apps.I_CmsAppButtonProvider#createAppButton(org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration)
229     */
230    public Component createAppButton(I_CmsWorkplaceAppConfiguration appConfig) {
231
232        return createAppButton(A_CmsUI.getCmsObject(), appConfig, UI.getCurrent().getLocale());
233    }
234
235    /**
236     * @see org.opencms.ui.apps.I_CmsAppButtonProvider#createAppButton(org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration)
237     *
238     */
239    public Component createAppFolderButton(CmsAppCategoryNode node) {
240
241        if (node.getAppConfigurations().size() == 1) {
242            return createAppButton(
243                A_CmsUI.getCmsObject(),
244                node.getAppConfigurations().get(0),
245                UI.getCurrent().getLocale());
246        }
247        return createAppFolderButton(A_CmsUI.getCmsObject(), node, UI.getCurrent().getLocale());
248    }
249}