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