View Javadoc
1   /**
2    * This file Copyright (c) 2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.admincentral.banner;
35  
36  import info.magnolia.icons.MagnoliaIcons;
37  
38  import java.util.Objects;
39  
40  import com.vaadin.shared.ui.ContentMode;
41  import com.vaadin.ui.Button;
42  import com.vaadin.ui.Component;
43  import com.vaadin.ui.ComponentContainer;
44  import com.vaadin.ui.CssLayout;
45  import com.vaadin.ui.CustomComponent;
46  import com.vaadin.ui.Label;
47  import com.vaadin.ui.VerticalLayout;
48  
49  /**
50   * Banner component.
51   */
52  public class Banner extends CustomComponent {
53  
54      private final CssLayout content;
55  
56      private final CssLayout buttonsLayout;
57  
58      public Banner(String id, String styleName, Title title) {
59          Objects.requireNonNull(id, "Id must not be null.");
60          Objects.requireNonNull(title, "Title must not be empty or null.");
61  
62          VerticalLayout root = new VerticalLayout();
63          setCompositionRoot(root);
64  
65          root.setId(id);
66          root.addStyleNames("banner", styleName);
67          root.setSpacing(false);
68          root.setMargin(false);
69  
70          content = new CssLayout();
71          content.addStyleName("content");
72          content.addComponent(title.asComponent());
73  
74          Button closeButton = new Button(MagnoliaIcons.CLOSE, clickEvent -> close());
75          closeButton.addStyleName("close");
76  
77          buttonsLayout = new CssLayout();
78          buttonsLayout.addStyleName("buttons");
79  
80          root.addComponents(content, buttonsLayout, closeButton);
81      }
82  
83      void addButton(Button button) {
84          buttonsLayout.addComponent(button);
85      }
86  
87      public void display(Body body) {
88          Objects.requireNonNull(body, "Body must not be null.");
89          content.addComponent(body.asComponent());
90      }
91  
92      public void close() {
93          ((ComponentContainer) getParent()).removeComponent(this);
94      }
95  
96      /**
97       * Banner Title.
98       */
99      public static class Title {
100 
101         private final MagnoliaIcons icon;
102         private final String text;
103         private boolean allowHtml = false;
104 
105         public Title(MagnoliaIcons icon, String text) {
106             Objects.requireNonNull(icon, "Icon must not be null.");
107             Objects.requireNonNull(text, "Text most not be null.");
108             this.icon = icon;
109             this.text = text;
110         }
111 
112         public void allowHtml(boolean allowHtml) {
113             this.allowHtml = allowHtml;
114         }
115 
116         private Component asComponent() {
117             Label titleIcon = new Label(icon.getHtml(), ContentMode.HTML);
118 
119             Label titleText = new Label(text, allowHtml ? ContentMode.HTML : ContentMode.TEXT);
120             titleText.setWidth(100, Unit.PERCENTAGE);
121 
122             CssLayout layout = new CssLayout(titleIcon, titleText);
123             layout.addStyleName("title");
124 
125             return layout;
126         }
127     }
128 
129     /**
130      * Banner Body.
131      */
132     public static class Body {
133 
134         private String text;
135         private boolean allowHtml = false;
136 
137         public Body(String text) {
138             Objects.requireNonNull(text, "Text most not be null.");
139             this.text = text;
140         }
141 
142         public void allowHtml(boolean allowHtml) {
143             this.allowHtml = allowHtml;
144         }
145 
146         private Component asComponent() {
147             Label bodyText = new Label(text, allowHtml ? ContentMode.HTML : ContentMode.TEXT);
148             bodyText.setWidth(100, Unit.PERCENTAGE);
149 
150             CssLayout layout = new CssLayout(bodyText);
151             layout.addStyleName("body");
152 
153             return layout;
154         }
155     }
156 }