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.messages.app;
35
36 import info.magnolia.ui.api.message.MessageType;
37
38 import java.io.Serializable;
39
40 import com.vaadin.data.Property.ValueChangeEvent;
41 import com.vaadin.data.Property.ValueChangeListener;
42 import com.vaadin.data.fieldgroup.FieldGroup;
43 import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
44 import com.vaadin.data.util.BeanItem;
45 import com.vaadin.server.Sizeable.Unit;
46 import com.vaadin.shared.ui.label.ContentMode;
47 import com.vaadin.ui.Button;
48 import com.vaadin.ui.Button.ClickEvent;
49 import com.vaadin.ui.Component;
50 import com.vaadin.ui.CssLayout;
51 import com.vaadin.ui.Field;
52 import com.vaadin.ui.FormLayout;
53 import com.vaadin.ui.HorizontalLayout;
54 import com.vaadin.ui.Label;
55 import com.vaadin.ui.NativeButton;
56 import com.vaadin.ui.OptionGroup;
57 import com.vaadin.ui.TextArea;
58 import com.vaadin.ui.TextField;
59
60
61
62
63 public class MessagesViewImpl implements MessagesView {
64
65 private static final String MESSAGE_SCOPE_GLOBAL = "Global";
66 private static final String MESSAGE_SCOPE_LOCAL = "Local";
67 private static final String MESSAGE_SCOPE_USER = "User";
68 private static final String MESSAGE_SCOPE_GROUP = "Group";
69
70 private Listener listener;
71
72 private final Component component;
73 private Field<String> userOrGroupIdField;
74
75 public MessagesViewImpl() {
76
77
78 final Message message = new Message();
79 BeanItem<Message> messageItem = new BeanItem<Message>(message);
80
81 final FieldGroup form = new FieldGroup();
82 form.setItemDataSource(messageItem);
83
84 Field<String> subjectField = createSubjectTextField();
85 Field<String> messageBodyField = createMessageBodyTextField();
86 Field<?> typeField = createTypeSelectionField();
87 Field<?> scopeField = createScopeSelectionField();
88 userOrGroupIdField = createUserOrGroupIdTextField();
89
90
91 scopeField.addValueChangeListener(new ValueChangeListener() {
92
93 @Override
94 public void valueChange(ValueChangeEvent event) {
95 updateUserOrGroupField((String) event.getProperty().getValue());
96 }
97 });
98
99 form.bind(subjectField, "title");
100 form.bind(messageBodyField, "content");
101 form.bind(typeField, "type");
102 form.bind(scopeField, "scope");
103 form.bind(userOrGroupIdField, "user");
104
105 updateUserOrGroupField(message.getScope());
106
107 FormLayout layout = new FormLayout();
108 layout.addComponent(subjectField);
109 layout.addComponent(messageBodyField);
110 layout.addComponent(typeField);
111 layout.addComponent(scopeField);
112 layout.addComponent(userOrGroupIdField);
113
114 layout.setSpacing(true);
115 layout.setMargin(false);
116 layout.setWidth("100%");
117
118
119 NativeButton sendButton = new NativeButton("Send message", new Button.ClickListener() {
120
121 @Override
122 public void buttonClick(Button.ClickEvent event) {
123 try {
124 form.commit();
125 String subject = message.getTitle();
126 String content = message.getContent();
127 MessageType type = message.getType();
128 String scope = message.getScope();
129
130 if (MESSAGE_SCOPE_LOCAL.equals(scope)) {
131 listener.handleLocalMessage(type, subject, content);
132 } else if (MESSAGE_SCOPE_GLOBAL.equals(scope)) {
133 listener.handleGlobalMessage(type, subject, content);
134 } else if (MESSAGE_SCOPE_GROUP.equals(scope)) {
135
136 final String groupName = message.getUser();
137 listener.handleGroupMessage(groupName, type, subject, content);
138 } else {
139
140 final String userName = message.getUser();
141 listener.handleUserMessage(userName, type, subject, content);
142 }
143 } catch (CommitException e) {
144
145 }
146 }
147 });
148 sendButton.addStyleName("btn-dialog");
149 sendButton.addStyleName("btn-dialog-commit");
150
151
152 NativeButton resetButton = new NativeButton("Reset", new Button.ClickListener() {
153
154 @Override
155 public void buttonClick(ClickEvent event) {
156 message.reset();
157 form.discard();
158 }
159 });
160 resetButton.addStyleName("btn-dialog");
161 resetButton.addStyleName("btn-dialog-cancel");
162
163 HorizontalLayout buttons = new HorizontalLayout();
164 buttons.addStyleName("buttons");
165 buttons.setSpacing(true);
166 buttons.addComponent(sendButton);
167 buttons.addComponent(resetButton);
168 layout.addComponent(buttons);
169
170
171 Label intro = new Label("This app enables to send different types of messages to some or all users on a Magnolia instance.<br />" +
172 "Please note that this app is for testing purposes only and will be removed in the final release.", ContentMode.HTML);
173 intro.addStyleName("intro");
174
175 CssLayout container = new CssLayout();
176 container.setSizeFull();
177 container.addStyleName("small-app-panel");
178 container.addComponent(layout);
179
180 CssLayout root = new CssLayout();
181 root.setSizeFull();
182 root.setWidth("900px");
183 root.setStyleName("small-app");
184 root.addComponent(intro);
185 root.addComponent(container);
186
187 component = root;
188 }
189
190 private Field<String> createUserOrGroupIdTextField() {
191 final TextField userOrGroupField = new TextField("User or group");
192 userOrGroupField.setWidth("360px");
193 return userOrGroupField;
194 }
195
196 private void updateUserOrGroupField(String scope) {
197 if (MESSAGE_SCOPE_GLOBAL.equals(scope)
198 || MESSAGE_SCOPE_LOCAL.equals(scope)) {
199 userOrGroupIdField.setEnabled(false);
200 } else {
201 userOrGroupIdField.setEnabled(true);
202 }
203 }
204
205 @Override
206 public void setListener(Listener listener) {
207 this.listener = listener;
208 }
209
210 @Override
211 public Component asVaadinComponent() {
212 return component;
213 }
214
215 private Field<String> createMessageBodyTextField() {
216 final TextArea messageField = new TextArea("Message");
217 messageField.setWidth(100, Unit.PERCENTAGE);
218 return messageField;
219 }
220
221 private OptionGroup createTypeSelectionField() {
222 final OptionGroup types = new OptionGroup("Type of message");
223 types.setNullSelectionAllowed(false);
224 types.addItem(MessageType.INFO);
225 types.setItemCaption(MessageType.INFO, "Informational");
226 types.addItem(MessageType.WARNING);
227 types.setItemCaption(MessageType.WARNING, "Warning");
228 types.addItem(MessageType.ERROR);
229 types.setItemCaption(MessageType.ERROR, "Error");
230 types.setValue(MessageType.INFO);
231 types.addStyleName("horizontal");
232 return types;
233 }
234
235 private OptionGroup createScopeSelectionField() {
236 final OptionGroup scopes = new OptionGroup("Scope");
237 scopes.setImmediate(true);
238 scopes.setNullSelectionAllowed(false);
239 scopes.addItem(MESSAGE_SCOPE_GLOBAL);
240 scopes.setItemCaption(MESSAGE_SCOPE_GLOBAL, "Send to all users");
241 scopes.addItem(MESSAGE_SCOPE_LOCAL);
242 scopes.setItemCaption(MESSAGE_SCOPE_LOCAL, "Send to yourself only");
243 scopes.addItem(MESSAGE_SCOPE_USER);
244 scopes.setItemCaption(MESSAGE_SCOPE_USER, "Send to user");
245 scopes.addItem(MESSAGE_SCOPE_GROUP);
246 scopes.setItemCaption(MESSAGE_SCOPE_GROUP, "Send to group");
247
248 scopes.addStyleName("vertical");
249 return scopes;
250 }
251
252 private Field<String> createSubjectTextField() {
253 final TextField subjectField = new TextField("Message title");
254 subjectField.addStyleName("required");
255 subjectField.setWidth(100, Unit.PERCENTAGE);
256 subjectField.setRequired(true);
257
258 subjectField.setColumns(0);
259 return subjectField;
260 }
261
262
263
264
265 public class Message implements Serializable {
266
267 private String title;
268
269 private String content;
270
271 private MessageType type;
272
273 private String scope;
274
275 private String user;
276
277 public Message() {
278 reset();
279 }
280
281 public void reset() {
282 title = "";
283 content = "";
284 type = MessageType.INFO;
285 scope = MESSAGE_SCOPE_LOCAL;
286 user = "";
287 }
288
289 public String getTitle() {
290 return title;
291 }
292
293 public void setTitle(String title) {
294 this.title = title;
295 }
296
297 public String getContent() {
298 return content;
299 }
300
301 public void setContent(String content) {
302 this.content = content;
303 }
304
305 public MessageType getType() {
306 return type;
307 }
308
309 public void setType(MessageType type) {
310 this.type = type;
311 }
312
313 public String getScope() {
314 return scope;
315 }
316
317 public void setScope(String scope) {
318 this.scope = scope;
319 }
320
321 public String getUser() {
322 return user;
323 }
324
325 public void setUser(String user) {
326 this.user = user;
327 }
328
329 }
330 }