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.ui.NativeButton;
50  import com.vaadin.v7.data.Property.ValueChangeEvent;
51  import com.vaadin.v7.data.Property.ValueChangeListener;
52  import com.vaadin.v7.data.fieldgroup.FieldGroup;
53  import com.vaadin.v7.data.fieldgroup.FieldGroup.CommitException;
54  import com.vaadin.v7.data.util.BeanItem;
55  import com.vaadin.v7.shared.ui.label.ContentMode;
56  import com.vaadin.v7.ui.Field;
57  import com.vaadin.v7.ui.HorizontalLayout;
58  import com.vaadin.v7.ui.Label;
59  import com.vaadin.v7.ui.OptionGroup;
60  import com.vaadin.v7.ui.TextArea;
61  import com.vaadin.v7.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.setNullSelectionAllowed(false);
244         scopes.addItem(MESSAGE_SCOPE_GLOBAL);
245         scopes.setItemCaption(MESSAGE_SCOPE_GLOBAL, i18n.translate("messages-app.app.field.scope.option.global"));
246         scopes.addItem(MESSAGE_SCOPE_LOCAL);
247         scopes.setItemCaption(MESSAGE_SCOPE_LOCAL, i18n.translate("messages-app.app.field.scope.option.local"));
248         scopes.addItem(MESSAGE_SCOPE_USER);
249         scopes.setItemCaption(MESSAGE_SCOPE_USER, i18n.translate("messages-app.app.field.scope.option.user"));
250         scopes.addItem(MESSAGE_SCOPE_GROUP);
251         scopes.setItemCaption(MESSAGE_SCOPE_GROUP, i18n.translate("messages-app.app.field.scope.option.group"));
252         // initial selection
253         scopes.addStyleName("vertical");
254         return scopes;
255     }
256 
257     private Field<String> createSubjectTextField() {
258         final TextField subjectField = new TextField(i18n.translate("messages-app.app.field.messageTitle"));
259         subjectField.addStyleName("required");
260         subjectField.setWidth(100, Unit.PERCENTAGE);
261         subjectField.setRequired(true);
262         // force plain input
263         subjectField.setColumns(0);
264         return subjectField;
265     }
266 
267     /**
268      * The Message POJO.
269      */
270     public class Message implements Serializable {
271 
272         private String title;
273 
274         private String content;
275 
276         private MessageType type;
277 
278         private String scope;
279 
280         private String user;
281 
282         public Message() {
283             reset();
284         }
285 
286         public void reset() {
287             title = "";
288             content = "";
289             type = MessageType.INFO;
290             scope = MESSAGE_SCOPE_LOCAL;
291             user = "";
292         }
293 
294         public String getTitle() {
295             return title;
296         }
297 
298         public void setTitle(String title) {
299             this.title = title;
300         }
301 
302         public String getContent() {
303             return content;
304         }
305 
306         public void setContent(String content) {
307             this.content = content;
308         }
309 
310         public MessageType getType() {
311             return type;
312         }
313 
314         public void setType(MessageType type) {
315             this.type = type;
316         }
317 
318         public String getScope() {
319             return scope;
320         }
321 
322         public void setScope(String scope) {
323             this.scope = scope;
324         }
325 
326         public String getUser() {
327             return user;
328         }
329 
330         public void setUser(String user) {
331             this.user = user;
332         }
333 
334     }
335 }