View Javadoc
1   /**
2    * This file Copyright (c) 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.admincentral;
35  
36  import info.magnolia.i18nsystem.SimpleTranslator;
37  import info.magnolia.ui.AlertBuilder;
38  import info.magnolia.ui.DialogBuilder;
39  import info.magnolia.ui.api.context.UiContext;
40  import info.magnolia.ui.api.ioc.AdmincentralScoped;
41  import info.magnolia.ui.api.message.Message;
42  import info.magnolia.ui.api.message.MessageType;
43  import info.magnolia.ui.api.overlay.AlertCallback;
44  import info.magnolia.ui.api.overlay.ConfirmationCallback;
45  import info.magnolia.ui.api.overlay.MessageStyleType;
46  import info.magnolia.ui.api.overlay.NotificationCallback;
47  import info.magnolia.ui.api.overlay.OverlayCloser;
48  import info.magnolia.ui.api.overlay.OverlayLayer;
49  import info.magnolia.ui.api.shell.ConfirmationHandler;
50  import info.magnolia.ui.api.shell.Shell;
51  import info.magnolia.ui.api.view.View;
52  import info.magnolia.ui.dialog.DialogView;
53  import info.magnolia.ui.framework.message.MessagesManager;
54  import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
55  
56  import javax.inject.Inject;
57  
58  import com.vaadin.server.Page;
59  import com.vaadin.ui.Component;
60  import com.vaadin.ui.Notification;
61  import com.vaadin.ui.UI;
62  import com.vaadin.ui.Window;
63  
64  /**
65   * The shell implementation for the Resurface Admincentral.
66   *
67   * <p>In comparison with the M5 shell, this implementation favors stock Vaadin usage over GWT customizations.
68   */
69  @AdmincentralScoped
70  public class ResurfaceShell implements Shell, UiContext {
71  
72      private final MessagesManager messagesManager;
73  
74      private final SimpleTranslator i18n;
75  
76      @Inject
77      public ResurfaceShell(MessagesManager messagesManager, SimpleTranslator i18n) {
78          this.messagesManager = messagesManager;
79          this.i18n = i18n;
80      }
81  
82      /* MessagesManager delegation */
83  
84      @Override
85      public void askForConfirmation(String message, ConfirmationHandler listener) {
86          // TODO consider dropping since it was always void in ShellImpl
87          AlertBuilder.confirmDialog("confirm")
88                  .withBody(message)
89                  .withConfirmationHandler(listener::onConfirm)
90                  .withConfirmButtonCaption(i18n.translate("button.confirm"))
91                  .withDeclineButtonCaption(i18n.translate("button.decline"))
92                  .buildAndOpen();
93      }
94  
95      @Override
96      public void showNotification(String message) {
97          showMessage(message, MessageType.INFO);
98      }
99  
100     @Override
101     public void showError(String message, Exception e) {
102         showMessage(message, MessageType.ERROR);
103     }
104 
105     private void showMessage(String messageText, MessageType type) {
106         final Message message = new Message();
107         message.setMessage(messageText);
108         message.setType(type);
109         messagesManager.sendLocalMessage(message);
110     }
111 
112     /* Location control */
113 
114     @Override
115     public String getFragment() {
116         return Page.getCurrent().getUriFragment();
117     }
118 
119     @Override
120     public void setFragment(String fragment) {
121         Page.getCurrent().setUriFragment(fragment);
122     }
123 
124     /* Arbitrary overlays */
125 
126     @Override
127     @Deprecated
128     public OverlayCloser openOverlayOnView(View view, View parent, ModalityDomain modalityLocation, ModalityLevel modalityLevel) {
129         return openOverlay(view, modalityLevel);
130     }
131 
132     @Override
133     @Deprecated
134     public OverlayCloser openOverlay(View view) {
135         return openOverlay(view, ModalityLevel.STRONG);
136     }
137 
138     @Override
139     @Deprecated
140     public OverlayCloser openOverlay(View view, ModalityLevel modalityLevel) {
141         final Component component = view.asVaadinComponent();
142         final Window window;
143         if (component instanceof Window) {
144             window = (Window) component;
145         } else if (view instanceof DialogView) {
146             final DialogView dialogView = (DialogView) view;
147             final Component contentComponent = dialogView.asVaadinComponent();
148             final Component actionAreaComponent = dialogView.getActionAreaView().asVaadinComponent();
149 
150             // re-wrap the view into a dialog (mostly for compatibility reasons)
151             contentComponent.setParent(null);
152             actionAreaComponent.setParent(null);
153 
154             window = new Window(dialogView.getTitle());
155             window.addCloseListener(listener -> dialogView.close());
156 
157             window.setContent(DialogBuilder.dialog()
158                     .withContent(contentComponent)
159                     .withFooter(actionAreaComponent)
160                     .withTitle(dialogView.getTitle())
161                     .build());
162         } else {
163             window = new Window();
164             window.setCaption(component.getCaption());
165             window.setContent(component);
166         }
167 
168         window.addStyleNames("dialog", modalityLevel.name().toLowerCase());
169         window.setDraggable(false);
170         window.setResizable(false);
171         window.setModal(ModalityLevel.STRONG == modalityLevel);
172 
173         UI.getCurrent().addWindow(window);
174 
175         return window::close;
176     }
177 
178     /* Notifications and alerts */
179 
180     @Override
181     public void openNotification(MessageStyleType messageType, boolean doesTimeout, String title) {
182         Notification notification = Notification.show(title, notificationTypeOf(messageType));
183         if (doesTimeout) {
184             notification.setDelayMsec(3600);
185         }
186     }
187 
188     @Override
189     public void openNotification(MessageStyleType messageType, boolean doesTimeout, View viewToShow) {
190         throw new UnsupportedOperationException();
191     }
192 
193     @Override
194     public void openNotification(MessageStyleType messageType, boolean doesTimeout, String title, String linkText, NotificationCallback cb) {
195         throw new UnsupportedOperationException();
196     }
197 
198     @Override
199     public void openAlert(MessageStyleType messageType, String title, String body, String okButton, AlertCallback callback) {
200         Window window = AlertBuilder.alert(title)
201                 .withLevel(notificationTypeOf(messageType))
202                 .withBody(body)
203                 .withConfirmationHandler(callback::onOk)
204                 .withOkButtonCaption(okButton)
205                 .buildAndOpen();
206         // TODO switch or add styling to stock Vaadin notification types
207         window.addStyleName(messageType.getCssClass());
208     }
209 
210     @Override
211     public void openAlert(MessageStyleType messageType, String title, View content, String okButton, AlertCallback callback) {
212         Window window = AlertBuilder.alert(title)
213                 .withLevel(notificationTypeOf(messageType))
214                 .withContent(content.asVaadinComponent())
215                 .withConfirmationHandler(callback::onOk)
216                 .withOkButtonCaption(okButton)
217                 .buildAndOpen();
218         // TODO switch or add styling to stock Vaadin notification types
219         window.addStyleName(messageType.getCssClass());
220     }
221 
222     @Override
223     public void openConfirmation(MessageStyleType messageType, String title, String body, String confirmButton, String cancelButton, boolean cancelIsDefault, ConfirmationCallback callback) {
224         Window confirmDialog = AlertBuilder.confirmDialog(title)
225                 .withLevel(notificationTypeOf(messageType))
226                 .withBody(body)
227                 .withConfirmationHandler(callback::onSuccess)
228                 .withConfirmButtonCaption(confirmButton)
229                 .withDeclineButtonCaption(cancelButton)
230                 .buildAndOpen();
231         // TODO switch or add styling to stock Vaadin notification types
232         confirmDialog.addStyleName(messageType.getCssClass());
233     }
234 
235     @Override
236     public void openConfirmation(MessageStyleType messageType, String title, View content, String confirmButton, String cancelButton, boolean cancelIsDefault, ConfirmationCallback callback) {
237         Window confirmDialog = AlertBuilder.confirmDialog(title)
238                 .withLevel(notificationTypeOf(messageType))
239                 .withContent(content.asVaadinComponent())
240                 .withConfirmationHandler(callback::onSuccess)
241                 .withConfirmButtonCaption(confirmButton)
242                 .withDeclineButtonCaption(cancelButton)
243                 .buildAndOpen();
244         // TODO switch or add styling to stock Vaadin notification types
245         confirmDialog.addStyleName(messageType.getCssClass());
246     }
247 
248     @Override
249     public OverlayLayer getOverlayDelegate() {
250         return this;
251     }
252 
253     /**
254      * Helper method to convert Magnolia message levels into equivalent Vaadin notification types.
255      *
256      * @param messageStyleType the Magnolia {@link MessageStyleType}, usually a member of the {@link MessageStyleTypeEnum}
257      * @return the corresponding Vaadin {@link Notification.Type}
258      */
259     private static Notification.Type notificationTypeOf(MessageStyleType messageStyleType) {
260         if (messageStyleType instanceof MessageStyleTypeEnum) {
261             switch ((MessageStyleTypeEnum) messageStyleType) {
262             case INFO:
263                 return Notification.Type.HUMANIZED_MESSAGE;
264             case WARNING:
265                 return Notification.Type.WARNING_MESSAGE;
266             case ERROR:
267                 return Notification.Type.ERROR_MESSAGE;
268             }
269         }
270         return Notification.Type.HUMANIZED_MESSAGE;
271     }
272 }