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.components;
029
030import org.opencms.ui.CmsVaadinUtils;
031import org.opencms.ui.FontOpenCms;
032
033import java.util.ArrayList;
034import java.util.List;
035import java.util.Map;
036import java.util.function.Supplier;
037
038import com.vaadin.ui.Button;
039import com.vaadin.ui.Component;
040import com.vaadin.ui.UI;
041import com.vaadin.ui.Window;
042import com.vaadin.ui.themes.ValoTheme;
043import com.vaadin.v7.shared.ui.label.ContentMode;
044import com.vaadin.v7.ui.Label;
045import com.vaadin.v7.ui.VerticalLayout;
046
047/**
048 * Class for the info button used in toolbar.<p>
049 */
050public class CmsInfoButton extends Button {
051
052    /**
053     * Bean holding further Vaadin elements and theire position.<p>
054     */
055    class InfoElementBean {
056
057        /**Position in layout. */
058        private int m_pos;
059
060        /**Component to add.*/
061        private Component m_component;
062
063        /**
064         * public constructor.<p>
065         *
066         * @param pos position of element
067         * @param comp vaadin component
068         */
069        InfoElementBean(int pos, Component comp) {
070
071            m_pos = pos;
072            m_component = comp;
073        }
074
075        /**
076         * Gets component.
077         *
078         * @return Vaadin component
079         */
080        protected Component getComponent() {
081
082            return m_component;
083        }
084
085        /**
086         * Gets position of component.
087         *
088         * @return int position
089         */
090        protected int getPos() {
091
092            return m_pos;
093        }
094
095    }
096
097    /**vaadin serial id.*/
098    private static final long serialVersionUID = 5718515094289838271L;
099
100    /**Icon of button.*/
101    private static final FontOpenCms ICON = FontOpenCms.INFO;
102
103    /**Caption for information window.*/
104    protected String m_windowCaption;
105
106    Button m_addButton;
107
108    /**Html lines to be shown in label.*/
109    private List<String> m_htmlLines;
110
111    /**List with additional vaadin elements to display and their position in VerticalLayout.*/
112    private List<InfoElementBean> m_additionalElements = new ArrayList<InfoElementBean>();
113
114    /**Clicklistener for the button. */
115    private ClickListener m_clickListener;
116
117    /** Optional provider for information which should only be calculated when the button is first clicked. */
118    private Supplier<Map<String, String>> m_infoSupplier;
119
120    /**
121     * public constructor.<p>
122     */
123    public CmsInfoButton() {
124
125        super(ICON);
126        ini(new ArrayList<String>());
127    }
128
129    /**
130     * public constructor.<p>
131     *
132     * @param htmlLines lines to show
133     */
134    public CmsInfoButton(final List<String> htmlLines) {
135
136        super(ICON);
137        m_htmlLines = htmlLines;
138        ini(htmlLines);
139    }
140
141    /**
142     * public constructor.<p>
143     *
144     * @param infos map with information to display
145     */
146    public CmsInfoButton(Map<String, String> infos) {
147
148        super(ICON);
149
150        ini(getHtmlLines(infos));
151    }
152
153    /**
154     * Creates an info button whose data is calculated when the button is first clicked.<p>
155     *
156     * @param infoSupplier the information supplier
157     * @return the lazily initialized info button
158     */
159    public static CmsInfoButton createLazy(Supplier<Map<String, String>> infoSupplier) {
160
161        CmsInfoButton result = new CmsInfoButton();
162        result.m_infoSupplier = infoSupplier;
163        return result;
164    }
165
166    /**
167     * Adds a vaadin element to window at last position.<p>
168     *
169     * @param component to be added
170     */
171    public void addAdditionalElement(Component component) {
172
173        m_additionalElements.add(new InfoElementBean(m_additionalElements.size() + 1, component));
174        removeClickListener(m_clickListener);
175        m_clickListener = getClickListener(m_htmlLines, m_additionalElements);
176        addClickListener(m_clickListener);
177    }
178
179    /**
180     * Adds a vaadin element to window.<p>
181     *
182     * @param component to be added
183     * @param pos position in vertical layout
184     */
185    public void addAdditionalElement(Component component, int pos) {
186
187        m_additionalElements.add(new InfoElementBean(pos, component));
188        removeClickListener(m_clickListener);
189        m_clickListener = getClickListener(m_htmlLines, m_additionalElements);
190        addClickListener(m_clickListener);
191    }
192
193    /**
194     * Get the info layout.<p>
195     *
196     * @return VerticalLayout
197     */
198    public VerticalLayout getInfoLayout() {
199
200        return getLayout(m_htmlLines, m_additionalElements);
201    }
202
203    /**
204     * Replaces current Map with new map.<p>
205     *
206     * @param data to replace the old map
207     */
208    public void replaceData(Map<String, String> data) {
209
210        removeClickListener(m_clickListener);
211        m_clickListener = getClickListener(getHtmlLines(data), m_additionalElements);
212        addClickListener(m_clickListener);
213
214    }
215
216    public void setAdditionalButton(Button button) {
217
218        m_addButton = button;
219    }
220
221    /**
222     * Sets the caption of the information window.<p>
223     *
224     * @param caption to be set
225     */
226    public void setWindowCaption(String caption) {
227
228        m_windowCaption = caption;
229    }
230
231    /**
232     * The layout which is shown in window by triggering onclick event of button.<p>
233     *
234     * @param htmlLines to be shown
235     * @param additionalElements further vaadin elements
236     * @return vertical layout
237     */
238    protected VerticalLayout getLayout(final List<String> htmlLines, final List<InfoElementBean> additionalElements) {
239
240        VerticalLayout layout = new VerticalLayout();
241        Label label = new Label();
242        label.setWidthUndefined();
243        layout.setMargin(true);
244        label.setContentMode(ContentMode.HTML);
245        layout.addStyleName(OpenCmsTheme.INFO);
246        String htmlContent = "";
247        for (String line : htmlLines) {
248            htmlContent += line;
249        }
250        label.setValue(htmlContent);
251
252        layout.addComponent(label);
253        for (InfoElementBean infoElement : additionalElements) {
254            layout.addComponent(infoElement.getComponent(), infoElement.getPos());
255        }
256        layout.setWidthUndefined();
257        return layout;
258    }
259
260    /**
261     * Clicklistener for the button.<p>
262     *
263     * @param htmlLines to be shown in Label
264     * @param additionalElements to be placed in the verticalllayout which holds the label
265     * @return ClickListener
266     */
267    private ClickListener getClickListener(
268        final List<String> htmlLines,
269        final List<InfoElementBean> additionalElements) {
270
271        return new Button.ClickListener() {
272
273            private static final long serialVersionUID = -553128629431329217L;
274
275            public void buttonClick(ClickEvent event) {
276
277                if (m_infoSupplier != null) {
278                    m_htmlLines = getHtmlLines(m_infoSupplier.get());
279                    m_infoSupplier = null;
280                }
281                final Window window = CmsBasicDialog.prepareWindow(CmsBasicDialog.DialogWidth.content);
282                window.setCaption(
283                    m_windowCaption == null
284                    ? CmsVaadinUtils.getMessageText(Messages.GUI_INFO_BUTTON_CAPTION_0)
285                    : m_windowCaption);
286                window.setResizable(false);
287                CmsBasicDialog dialog = new CmsBasicDialog();
288                if (m_addButton != null) {
289                    dialog.addButton(m_addButton, false);
290                }
291                VerticalLayout layout = getLayout(m_htmlLines, additionalElements);
292                dialog.setContent(layout);
293
294                Button button = new Button(CmsVaadinUtils.messageClose());
295                button.addClickListener(new Button.ClickListener() {
296
297                    private static final long serialVersionUID = 5789436407764072884L;
298
299                    public void buttonClick(ClickEvent event1) {
300
301                        window.close();
302
303                    }
304                });
305                dialog.addButton(button);
306
307                window.setContent(dialog);
308
309                UI.getCurrent().addWindow(window);
310            }
311        };
312
313    }
314
315    /**
316     * Creates html code from given map.<p>
317     *
318     * @param infos to be displayed
319     * @return List of html lines
320     */
321    private List<String> getHtmlLines(Map<String, String> infos) {
322
323        List<String> htmlLines = new ArrayList<String>();
324
325        for (String key : infos.keySet()) {
326            htmlLines.add(
327                "<div style=\"display:flex;align-items:flex-end;\"><div class=\""
328                    + OpenCmsTheme.INFO_ELEMENT_NAME
329                    + "\">"
330                    + key
331                    + " :</div><div style=\"width:140px;\" class=\""
332                    + OpenCmsTheme.INFO_ELEMENT_VALUE
333                    + "\">"
334                    + infos.get(key)
335                    + "</div></div>");
336        }
337        m_htmlLines = htmlLines;
338        return htmlLines;
339    }
340
341    /**
342     * initializes the button.<p>
343     *
344     * @param htmlLines to show
345     */
346    private void ini(final List<String> htmlLines) {
347
348        m_htmlLines = htmlLines;
349        addStyleName(ValoTheme.BUTTON_BORDERLESS);
350        addStyleName(OpenCmsTheme.TOOLBAR_BUTTON);
351
352        m_clickListener = getClickListener(htmlLines, m_additionalElements);
353        addClickListener(m_clickListener);
354    }
355}