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, String title, String body, String okButton, AlertCallback callback) {
86          openAlert(type, title, new ViewAdapter(new Label(body, ContentMode.HTML)), okButton, callback);
87      }
88  
89      @Override
90      public void openAlert(MessageStyleType type, String title, View body, String okButton, final AlertCallback callback) {
91  
92          final BaseDialog dialog = new LightDialog();
93          dialog.addStyleName(type.getCssClass());
94          dialog.addStyleName("alert");
95  
96          dialog.setCaption(title);
97          CompositeIcon icon = (CompositeIcon) Classes.getClassFactory().newInstance(type.getIconClass());
98          icon.setStyleName("dialog-icon");
99          dialog.setHeaderToolbar(icon);
100         dialog.showCloseButton();
101 
102         dialog.setContent(body.asVaadinComponent());
103 
104         Panel shortcutPanel = new Panel();
105         shortcutPanel.setStyleName("shortcut-panel");
106         shortcutPanel.setHeight(100, Unit.PERCENTAGE);
107         shortcutPanel.setWidth(100, Unit.PERCENTAGE);
108         shortcutPanel.setContent(dialog);
109 
110         final OverlayCloser overlayCloser = openOverlay(new ViewAdapter(shortcutPanel), ModalityLevel.LIGHT);
111         final ShortcutListener escapeShortcut = new ShortcutListener("Escape shortcut", ShortcutAction.KeyCode.ESCAPE, null) {
112             @Override
113             public void handleAction(Object sender, Object target) {
114                 callback.onOk();
115                 dialog.closeSelf();
116             }
117         };
118         shortcutPanel.addShortcutListener(escapeShortcut);
119         addOkHandler(dialog, okButton, overlayCloser, callback);
120         dialog.addDialogCloseHandler(createCloseHandler(overlayCloser));
121     }
122 
123     private void addOkHandler(BaseDialog dialog, String okButtonText, final OverlayCloser overlayCloser, final AlertCallback cb) {
124         CssLayout footer = new CssLayout();
125         footer.setWidth(100, Unit.PERCENTAGE);
126         footer.addStyleName("v-align-right");
127         Button okButton = new Button(okButtonText, new ClickListener() {
128             @Override
129             public void buttonClick(ClickEvent event) {
130                 cb.onOk();
131                 overlayCloser.close();
132             }
133         });
134         okButton.focus();
135         footer.addComponent(okButton);
136         dialog.setFooterToolbar(footer);
137     }
138 
139     @Override
140     public void openConfirmation(MessageStyleType type, String title, String body, String confirmButton, String cancelButton, boolean cancelIsDefault, ConfirmationCallback callback) {
141         openConfirmation(type, title, new ViewAdapter(new Label(body, ContentMode.HTML)), confirmButton, cancelButton, cancelIsDefault, callback);
142     }
143 
144     @Override
145     public void openConfirmation(MessageStyleType type, String title, View body, String confirmButton, String cancelButton, boolean cancelIsDefault, final ConfirmationCallback callback) {
146         final ConfirmationDialog dialog = new ConfirmationDialog(body.asVaadinComponent(), confirmButton, cancelButton, cancelIsDefault);
147         dialog.addStyleName(type.getCssClass());
148         dialog.addStyleName("confirmation");
149 
150         dialog.setCaption(title);
151         CompositeIcon icon = (CompositeIcon) Classes.getClassFactory().newInstance(type.getIconClass());
152         icon.setStyleName("dialog-icon");
153         dialog.setHeaderToolbar(icon);
154         dialog.showCloseButton();
155 
156         dialog.setContent(body.asVaadinComponent());
157 
158         Panel shortcutPanel = new Panel();
159         shortcutPanel.setStyleName("shortcut-panel");
160         shortcutPanel.setHeight(100, Unit.PERCENTAGE);
161         shortcutPanel.setWidth(100, Unit.PERCENTAGE);
162         shortcutPanel.setContent(dialog);
163 
164         final OverlayCloser overlayCloser = openOverlay(new ViewAdapter(shortcutPanel), ModalityLevel.LIGHT);
165 
166         final ShortcutListener escapeShortcut = new ShortcutListener("Escape shortcut", ShortcutAction.KeyCode.ESCAPE, null) {
167             @Override
168             public void handleAction(Object sender, Object target) {
169                 callback.onCancel();
170                 dialog.closeSelf();
171             }
172         };
173         shortcutPanel.addShortcutListener(escapeShortcut);
174         dialog.addConfirmationHandler(
175                 new ConfirmationDialog.ConfirmationEvent.Handler() {
176                     @Override
177                     public void onConfirmation(ConfirmationEvent event) {
178                         if (event.isConfirmed()) {
179                             callback.onSuccess();
180                         } else {
181                             callback.onCancel();
182                         }
183                         overlayCloser.close();
184                     }
185                 });
186         dialog.addDialogCloseHandler(createCloseHandler(overlayCloser));
187     }
188 
189     private BaseDialog.DialogCloseEvent.Handler createCloseHandler(final OverlayCloser overlayCloser) {
190         return new BaseDialog.DialogCloseEvent.Handler() {
191             @Override
192             public void onClose(BaseDialog.DialogCloseEvent event) {
193                 overlayCloser.close();
194             }
195         };
196     }
197 
198     /**
199      * Opens a notification of given {@link MessageStyleType type}, with given body; it can close automatically after a timeout.
200      */
201     @Override
202     public void openNotification(final MessageStyleType type, boolean doesTimeout, View viewToShow) {
203         final Notification notification = new Notification(type);
204         notification.setContent(viewToShow.asVaadinComponent());
205 
206         Panel shortcutPanel = new Panel();
207         shortcutPanel.setStyleName("shortcut-panel");
208         shortcutPanel.setWidth(null);
209         shortcutPanel.setContent(notification.asVaadinComponent());
210         final OverlayCloser closer = openOverlay(new ViewAdapter(shortcutPanel), ModalityLevel.NON_MODAL);
211 
212         final ShortcutListener escapeShortcut = new ShortcutListener("Escape shortcut", ShortcutAction.KeyCode.ESCAPE, null) {
213             @Override
214             public void handleAction(Object sender, Object target) {
215                 closer.close();
216             }
217         };
218         shortcutPanel.addShortcutListener(escapeShortcut);
219 
220         notification.addCloseButtonListener(new ClickListener() {
221             @Override
222             public void buttonClick(ClickEvent clickEvent) {
223                 closer.close();
224             }
225         });
226 
227         notification.addNotificationBodyClickListener(new LayoutEvents.LayoutClickListener() {
228             @Override
229             public void layoutClick(LayoutEvents.LayoutClickEvent layoutClickEvent) {
230                 closer.setCloseTimeout(-1);
231             }
232         });
233 
234         if (doesTimeout) {
235             closer.setCloseTimeout(TIMEOUT_SECONDS_DEFAULT);
236         }
237 
238     }
239 
240     /**
241      * Opens a notification of given {@link MessageStyleType type}, with given body text; it can close automatically after a timeout.
242      */
243     @Override
244     public void openNotification(MessageStyleType type, boolean doesTimeout, final String title) {
245         openNotification(type, doesTimeout, new ViewAdapter(new Label(title, ContentMode.HTML)));
246 
247     }
248 
249     /**
250      * Convenience method for presenting notification indicator with string content.
251      */
252     @Override
253     public void openNotification(MessageStyleType type, boolean doesTimeout, String title, String linkText, final NotificationCallback callback) {
254         HorizontalLayout layout = new HorizontalLayout();
255         layout.setSpacing(true);
256         layout.addComponent(new Label(title, ContentMode.HTML));
257 
258         Button button = new Button(linkText, new ClickListener() {
259             @Override
260             public void buttonClick(ClickEvent event) {
261                 callback.onLinkClicked();
262             }
263         });
264         button.setStyleName(BaseTheme.BUTTON_LINK);
265 
266         layout.addComponent(button);
267         openNotification(type, doesTimeout, new ViewAdapter(layout));
268     }
269 
270 }