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  import info.magnolia.ui.api.message.Message;
38  import info.magnolia.ui.api.message.MessageType;
39  
40  import java.util.Objects;
41  import java.util.Optional;
42  
43  import org.apache.commons.lang3.StringUtils;
44  
45  import com.google.common.base.Preconditions;
46  import com.google.common.base.Strings;
47  import com.vaadin.shared.ui.ContentMode;
48  import com.vaadin.ui.Button;
49  import com.vaadin.ui.CssLayout;
50  import com.vaadin.ui.CustomComponent;
51  import com.vaadin.ui.Label;
52  import com.vaadin.ui.VerticalLayout;
53  
54  /**
55   * Banner component.
56   */
57  public class Banner extends CustomComponent {
58      private final VerticalLayout root;
59      private final CssLayout buttonsLayout;
60  
61  
62      public Banner(Message message){
63          Objects.requireNonNull(message, "Message must not be null");
64          Objects.requireNonNull(message.getId(), "Message Id must not be null.");
65          Objects.requireNonNull(message.getSubject(), "Title must not be empty or null.");
66  
67          //on demand buttons which are added by using addButton() method will be placed to this layout
68          buttonsLayout = createButtonsLayout();
69  
70          root = createRootLayout(message);
71          root.addComponents(createContent(message), buttonsLayout, createCloseButton());
72      }
73  
74      private CssLayout createContent(Message message) {
75          final CssLayout content = new CssLayout();
76          content.addStyleName("content");
77          content.addComponent(createTitle(message.getSubject(), message.getType()));
78          Optional.ofNullable(Strings.emptyToNull(message.getMessage())).map(this::createBody)
79                  .ifPresent(body -> content.addComponent(body));
80          return content;
81      }
82  
83      private CssLayout createTitle(String messageSubject, MessageType messageType){
84          Preconditions.checkArgument(StringUtils.isNotBlank(messageSubject), "Subject must not be null.");
85          Objects.requireNonNull(messageType, "Type most not be null.");
86  
87          Label titleIcon = new Label(getBannerIcon(messageType).getHtml(), ContentMode.HTML);
88          Label titleText = new Label(messageSubject, ContentMode.TEXT);
89          titleText.setWidth(100, Unit.PERCENTAGE);
90  
91          CssLayout title = new CssLayout(titleIcon, titleText);
92          title.addStyleName("title");
93  
94          return title;
95      }
96  
97      private MagnoliaIcons getBannerIcon(MessageType messageType) {
98          switch (messageType) {
99              case ERROR:
100                 return MagnoliaIcons.ERROR_L;
101             case WARNING:
102                 return MagnoliaIcons.WARNING_L;
103             default:
104                 return MagnoliaIcons.INFO_L;
105         }
106     }
107 
108     private CssLayout createBody(String messageContent){
109         Preconditions.checkArgument(StringUtils.isNotBlank(messageContent), "Message content must not be null or empty.");
110         Label bodyText = new Label(messageContent, ContentMode.TEXT);
111         bodyText.setWidth(100, Unit.PERCENTAGE);
112 
113         CssLayout body = new CssLayout(bodyText);
114         body.addStyleName("body");
115 
116         return body;
117     }
118 
119     private CssLayout createButtonsLayout() {
120         final CssLayout buttonsLayout = new CssLayout();
121         buttonsLayout.addStyleName("buttons");
122         return buttonsLayout;
123     }
124 
125     private Button createCloseButton() {
126         final Button closeButton = new Button(MagnoliaIcons.CLOSE, clickEvent -> {
127             this.close(this);
128         });
129         closeButton.addStyleName("close");
130         return closeButton;
131     }
132 
133     private VerticalLayout createRootLayout(Message message) {
134         final VerticalLayout root = new VerticalLayout();
135         root.setId(message.getId());
136         root.setSpacing(false);
137         root.setMargin(false);
138         root.addStyleNames("banner", "slide-down", getMessageTypeStyle(message.getType()));
139         setCompositionRoot(root);
140         return root;
141     }
142 
143     private String getMessageTypeStyle(MessageType type) {
144         switch (type) {
145             case ERROR:
146             case WARNING:
147                 return type.toString().toLowerCase();
148             default:
149                 return MessageType.INFO.toString().toLowerCase();
150         }
151     }
152 
153     void addButton(Button button) {
154         this.buttonsLayout.addComponent(button);
155     }
156 
157     private void close(Banner banner) {
158         ((BannerContainer) getParent()).removeBanner(this);
159     }
160 
161     @Override
162     public String getStyleName() {
163         return root.getStyleName();
164     }
165 }