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