View Javadoc

1   /**
2    * This file Copyright (c) 2012-2013 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.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   * View implementation for the Messages app.
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          // create form and data item
78          final Message message = new Message(); // message POJO
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          // disable user/group field if not necessary
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         // FieldGroup overrides fields' own enabled property with its own.
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         // send button
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                         // message is bound to FieldGroup - hence the group name is to be retrieved from the user field of the message
136                         final String groupName = message.getUser();
137                         listener.handleGroupMessage(groupName, type, subject, content);
138                     } else {
139                         // User...
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         // reset button
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         // intro text
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         // initial selection
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         // force plain input
258         subjectField.setColumns(0);
259         return subjectField;
260     }
261 
262     /**
263      * The Message POJO.
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 }