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