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
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
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
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
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 }