View Javadoc
1   /**
2    * This file Copyright (c) 2013-2015 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.ui.framework.overlay;
35  
36  import info.magnolia.objectfactory.Classes;
37  import info.magnolia.ui.api.overlay.AlertCallback;
38  import info.magnolia.ui.api.overlay.ConfirmationCallback;
39  import info.magnolia.ui.api.overlay.MessageStyleType;
40  import info.magnolia.ui.api.overlay.NotificationCallback;
41  import info.magnolia.ui.api.overlay.OverlayCloser;
42  import info.magnolia.ui.api.overlay.OverlayLayer;
43  import info.magnolia.ui.api.view.View;
44  import info.magnolia.ui.vaadin.dialog.BaseDialog;
45  import info.magnolia.ui.vaadin.dialog.ConfirmationDialog;
46  import info.magnolia.ui.vaadin.dialog.ConfirmationDialog.ConfirmationEvent;
47  import info.magnolia.ui.vaadin.dialog.LightDialog;
48  import info.magnolia.ui.vaadin.dialog.Notification;
49  import info.magnolia.ui.vaadin.icon.CompositeIcon;
50  
51  import com.vaadin.event.LayoutEvents;
52  import com.vaadin.event.ShortcutAction;
53  import com.vaadin.event.ShortcutListener;
54  import com.vaadin.server.Sizeable.Unit;
55  import com.vaadin.shared.ui.label.ContentMode;
56  import com.vaadin.ui.Button;
57  import com.vaadin.ui.Button.ClickEvent;
58  import com.vaadin.ui.Button.ClickListener;
59  import com.vaadin.ui.CssLayout;
60  import com.vaadin.ui.HorizontalLayout;
61  import com.vaadin.ui.Label;
62  import com.vaadin.ui.Panel;
63  import com.vaadin.ui.themes.BaseTheme;
64  
65  /**
66   * Provides implementations for most OverlayLayer methods.
67   */
68  public abstract class OverlayPresenter implements OverlayLayer {
69  
70      public static final int TIMEOUT_SECONDS_DEFAULT = 3;
71  
72      /**
73       * Opens an overlay with default strong modality level.
74       */
75      @Override
76      public OverlayCloser openOverlay(View view) {
77          return openOverlay(view, ModalityLevel.STRONG);
78      }
79  
80      @Override
81      public void openAlert(MessageStyleType type, View body, String okButton, AlertCallback callback) {
82          openAlert(type, null, body, okButton, callback);
83      }
84  
85      @Override
86      public void openAlert(MessageStyleType type, String title, String body, String okButton, AlertCallback callback) {
87          openAlert(type, title, new ViewAdapter(new Label(body, ContentMode.HTML)), okButton, callback);
88      }
89  
90      @Override
91      public void openAlert(MessageStyleType type, String title, View body, String okButton, final AlertCallback callback) {
92  
93          final BaseDialog dialog = new LightDialog();
94          dialog.addStyleName(type.getCssClass());
95          dialog.addStyleName("alert");
96  
97          dialog.setCaption(title);
98          CompositeIcon icon = (CompositeIcon) Classes.getClassFactory().newInstance(type.getIconClass());
99          icon.setStyleName("dialog-icon");
100         dialog.setHeaderToolbar(icon);
101         dialog.showCloseButton();
102 
103         dialog.setContent(body.asVaadinComponent());
104 
105         Panel shortcutPanel = new Panel();
106         shortcutPanel.setStyleName("shortcut-panel");
107         shortcutPanel.setHeight(100, Unit.PERCENTAGE);
108         shortcutPanel.setWidth(100, Unit.PERCENTAGE);
109         shortcutPanel.setContent(dialog);
110 
111         final OverlayCloser overlayCloser = openOverlay(new ViewAdapter(shortcutPanel), ModalityLevel.LIGHT);
112         final ShortcutListener escapeShortcut = new ShortcutListener("Escape shortcut", ShortcutAction.KeyCode.ESCAPE, null) {
113             @Override
114             public void handleAction(Object sender, Object target) {
115                 callback.onOk();
116                 dialog.closeSelf();
117             }
118         };
119         shortcutPanel.addShortcutListener(escapeShortcut);
120         addOkHandler(dialog, okButton, overlayCloser, callback);
121         dialog.addDialogCloseHandler(createCloseHandler(overlayCloser));
122     }
123 
124     private void addOkHandler(BaseDialog dialog, String okButtonText, final OverlayCloser overlayCloser, final AlertCallback cb) {
125         CssLayout footer = new CssLayout();
126         footer.setWidth(100, Unit.PERCENTAGE);
127         footer.addStyleName("v-align-right");
128         Button okButton = new Button(okButtonText, new ClickListener() {
129             @Override
130             public void buttonClick(ClickEvent event) {
131                 cb.onOk();
132                 overlayCloser.close();
133             }
134         });
135         okButton.focus();
136         footer.addComponent(okButton);
137         dialog.setFooterToolbar(footer);
138     }
139 
140     @Override
141     public void openConfirmation(MessageStyleType type, View body, String confirmButton, String cancelButton, boolean cancelIsDefault, ConfirmationCallback callback) {
142         openConfirmation(type, null, body, confirmButton, cancelButton, cancelIsDefault, callback);
143     }
144 
145     @Override
146     public void openConfirmation(MessageStyleType type, String title, String body, String confirmButton, String cancelButton, boolean cancelIsDefault, ConfirmationCallback callback) {
147         openConfirmation(type, title, new ViewAdapter(new Label(body, ContentMode.HTML)), confirmButton, cancelButton, cancelIsDefault, callback);
148     }
149 
150     @Override
151     public void openConfirmation(MessageStyleType type, String title, View body, String confirmButton, String cancelButton, boolean cancelIsDefault, final ConfirmationCallback callback) {
152         final ConfirmationDialog dialog = new ConfirmationDialog(body.asVaadinComponent(), confirmButton, cancelButton, cancelIsDefault);
153         dialog.addStyleName(type.getCssClass());
154         dialog.addStyleName("confirmation");
155 
156         dialog.setCaption(title);
157         CompositeIcon icon = (CompositeIcon) Classes.getClassFactory().newInstance(type.getIconClass());
158         icon.setStyleName("dialog-icon");
159         dialog.setHeaderToolbar(icon);
160         dialog.showCloseButton();
161 
162         dialog.setContent(body.asVaadinComponent());
163 
164         Panel shortcutPanel = new Panel();
165         shortcutPanel.setStyleName("shortcut-panel");
166         shortcutPanel.setHeight(100, Unit.PERCENTAGE);
167         shortcutPanel.setWidth(100, Unit.PERCENTAGE);
168         shortcutPanel.setContent(dialog);
169 
170         final OverlayCloser overlayCloser = openOverlay(new ViewAdapter(shortcutPanel), ModalityLevel.LIGHT);
171 
172         final ShortcutListener escapeShortcut = new ShortcutListener("Escape shortcut", ShortcutAction.KeyCode.ESCAPE, null) {
173             @Override
174             public void handleAction(Object sender, Object target) {
175                 callback.onCancel();
176                 dialog.closeSelf();
177             }
178         };
179         shortcutPanel.addShortcutListener(escapeShortcut);
180         dialog.addConfirmationHandler(
181                 new ConfirmationDialog.ConfirmationEvent.Handler() {
182                     @Override
183                     public void onConfirmation(ConfirmationEvent event) {
184                         if (event.isConfirmed()) {
185                             callback.onSuccess();
186                         } else {
187                             callback.onCancel();
188                         }
189                         overlayCloser.close();
190                     }
191                 });
192         dialog.addDialogCloseHandler(createCloseHandler(overlayCloser));
193     }
194 
195     private BaseDialog.DialogCloseEvent.Handler createCloseHandler(final OverlayCloser overlayCloser) {
196         return new BaseDialog.DialogCloseEvent.Handler() {
197             @Override
198             public void onClose(BaseDialog.DialogCloseEvent event) {
199                 overlayCloser.close();
200             }
201         };
202     }
203 
204     /**
205      * Opens a notification of given {@link MessageStyleType type}, with given body; it can close automatically after a timeout.
206      */
207     @Override
208     public void openNotification(final MessageStyleType type, boolean doesTimeout, View viewToShow) {
209         final Notification notification = new Notification(type);
210         notification.setContent(viewToShow.asVaadinComponent());
211 
212         Panel shortcutPanel = new Panel();
213         shortcutPanel.setStyleName("shortcut-panel");
214         shortcutPanel.setWidth(null);
215         shortcutPanel.setContent(notification.asVaadinComponent());
216         final OverlayCloser closer = openOverlay(new ViewAdapter(shortcutPanel), ModalityLevel.NON_MODAL);
217 
218         final ShortcutListener escapeShortcut = new ShortcutListener("Escape shortcut", ShortcutAction.KeyCode.ESCAPE, null) {
219             @Override
220             public void handleAction(Object sender, Object target) {
221                 closer.close();
222             }
223         };
224         shortcutPanel.addShortcutListener(escapeShortcut);
225 
226         notification.addCloseButtonListener(new ClickListener() {
227             @Override
228             public void buttonClick(ClickEvent clickEvent) {
229                 closer.close();
230             }
231         });
232 
233         notification.addNotificationBodyClickListener(new LayoutEvents.LayoutClickListener() {
234             @Override
235             public void layoutClick(LayoutEvents.LayoutClickEvent layoutClickEvent) {
236                 closer.setCloseTimeout(-1);
237             }
238         });
239 
240         if (doesTimeout) {
241             closer.setCloseTimeout(TIMEOUT_SECONDS_DEFAULT);
242         }
243 
244     }
245 
246     /**
247      * Opens a notification of given {@link MessageStyleType type}, with given body text; it can close automatically after a timeout.
248      */
249     @Override
250     public void openNotification(MessageStyleType type, boolean doesTimeout, final String title) {
251         openNotification(type, doesTimeout, new ViewAdapter(new Label(title, ContentMode.HTML)));
252 
253     }
254 
255     /**
256      * Convenience method for presenting notification indicator with string content.
257      */
258     @Override
259     public void openNotification(MessageStyleType type, boolean doesTimeout, String title, String linkText, final NotificationCallback callback) {
260         HorizontalLayout layout = new HorizontalLayout();
261         layout.setSpacing(true);
262         layout.addComponent(new Label(title, ContentMode.HTML));
263 
264         Button button = new Button(linkText, new ClickListener() {
265             @Override
266             public void buttonClick(ClickEvent event) {
267                 callback.onLinkClicked();
268             }
269         });
270         button.setStyleName(BaseTheme.BUTTON_LINK);
271 
272         layout.addComponent(button);
273         openNotification(type, doesTimeout, new ViewAdapter(layout));
274     }
275 
276 }