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