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