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