View Javadoc

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