1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
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 }