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.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    public static Component createAppFolderButton(CmsObject cms, final CmsAppCategoryNode node, final Locale locale) {
105
106        Button button = createAppFolderIconButton((I_CmsFolderAppCategory)node.getCategory(), locale);
107        button.addClickListener(new ClickListener() {
108
109            private static final long serialVersionUID = 1L;
110            private static final int DEFAULT_WIDTH = 855;
111            private static final int DEFAULT_MAX_APP_PER_ROW = 5;
112            private static final int MARGIN = 10;
113
114            public void buttonClick(ClickEvent event) {
115
116                CmsAppHierarchyPanel panel = new CmsAppHierarchyPanel(new CmsDefaultAppButtonProvider());
117                //                panel.setCaption(((I_CmsFolderAppCategory)node.getCategory()).getName(locale));
118                panel.setCaption("Test caption");
119
120                panel.fill(node, locale);
121
122                Panel realPanel = new Panel();
123                realPanel.setContent(panel);
124                realPanel.setCaption(((I_CmsFolderAppCategory)node.getCategory()).getName(locale));
125                int browtherWidth = A_CmsUI.get().getPage().getBrowserWindowWidth();
126                if (node.getAppConfigurations().size() <= DEFAULT_MAX_APP_PER_ROW) {
127                    panel.setComponentAlignment(panel.getComponent(0), com.vaadin.ui.Alignment.MIDDLE_CENTER);
128                }
129                if (browtherWidth < DEFAULT_WIDTH) {
130                    realPanel.setWidth((browtherWidth - (2 * MARGIN)) + "px");
131                } else {
132                    realPanel.setWidth(DEFAULT_WIDTH + "px");
133                }
134                final Window window = CmsBasicDialog.prepareWindow(DialogWidth.content);
135                window.setResizable(false);
136                window.setContent(realPanel);
137                window.setClosable(true);
138                window.addStyleName("o-close-on-background");
139                window.setModal(true);
140                window.setDraggable(false);
141
142                CmsAppWorkplaceUi.get().addWindow(window);
143
144            }
145        });
146        return button;
147    }
148
149    /**
150     * Creates a properly styled button for the given app, without adding a click handler or checking visibility settings.<p>
151     *
152     * @param appCat the app category
153     * @param locale the locale
154     *
155     * @return the button component
156     */
157    public static Button createAppFolderIconButton(I_CmsFolderAppCategory appCat, Locale locale) {
158
159        return createIconButton(
160            appCat.getName(locale),
161            appCat.getHelpText(locale),
162            appCat.getIcon(),
163            appCat.getButtonStyle());
164    }
165
166    /**
167     * Creates a properly styled button for the given app, without adding a click handler or checking visibility settings.<p>
168     *
169     * @param appConfig the app configuration
170     * @param locale the locale
171     *
172     * @return the button component
173     */
174    public static Button createAppIconButton(I_CmsWorkplaceAppConfiguration appConfig, Locale locale) {
175
176        return createIconButton(
177            appConfig.getName(locale),
178            appConfig.getHelpText(locale),
179            appConfig.getIcon(),
180            appConfig.getButtonStyle());
181    }
182
183    /**
184     * Creates an icon button.<p>
185     *
186     * @param name the name
187     * @param description the description
188     * @param icon the icon
189     *
190     * @return the created button
191     */
192    public static Button createIconButton(String name, String description, Resource icon) {
193
194        return createIconButton(name, description, icon, I_CmsAppButtonProvider.BUTTON_STYLE_TRANSPARENT);
195    }
196
197    /**
198     * Creates an icon button.<p>
199     *
200     * @param name the name
201     * @param description the description
202     * @param icon the icon
203     * @param buttonStyle the button style
204     *
205     * @return the created button
206     */
207    public static Button createIconButton(String name, String description, Resource icon, String buttonStyle) {
208
209        Button button = new Button(name);
210        button.setIcon(icon, name);
211        button.setDescription(description);
212        button.addStyleName(OpenCmsTheme.APP_BUTTON);
213        button.addStyleName(ValoTheme.BUTTON_BORDERLESS);
214        button.addStyleName(ValoTheme.BUTTON_ICON_ALIGN_TOP);
215        if (buttonStyle != null) {
216            button.addStyleName(buttonStyle);
217        }
218        if ((icon instanceof CmsCssIcon) && ((CmsCssIcon)icon).hasAdditionalButtonStyle()) {
219            button.addStyleName(((CmsCssIcon)icon).getAdditionalButtonStyle());
220        }
221        return button;
222    }
223
224    /**
225     * @see org.opencms.ui.apps.I_CmsAppButtonProvider#createAppButton(org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration)
226     */
227    public Component createAppButton(I_CmsWorkplaceAppConfiguration appConfig) {
228
229        return createAppButton(A_CmsUI.getCmsObject(), appConfig, UI.getCurrent().getLocale());
230    }
231
232    /**
233     * @see org.opencms.ui.apps.I_CmsAppButtonProvider#createAppButton(org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration)
234     *
235     */
236    public Component createAppFolderButton(CmsAppCategoryNode node) {
237
238        if (node.getAppConfigurations().size() == 1) {
239            return createAppButton(
240                A_CmsUI.getCmsObject(),
241                node.getAppConfigurations().get(0),
242                UI.getCurrent().getLocale());
243        }
244        return createAppFolderButton(A_CmsUI.getCmsObject(), node, UI.getCurrent().getLocale());
245    }
246}