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