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.poc;
35  
36  import info.magnolia.ui.api.overlay.MessageStyleType;
37  import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
38  
39  import javax.servlet.annotation.WebServlet;
40  
41  import com.vaadin.annotations.Theme;
42  import com.vaadin.annotations.Title;
43  import com.vaadin.annotations.VaadinServletConfiguration;
44  import com.vaadin.server.VaadinRequest;
45  import com.vaadin.server.VaadinServlet;
46  import com.vaadin.ui.Button;
47  import com.vaadin.ui.Component;
48  import com.vaadin.ui.HorizontalLayout;
49  import com.vaadin.ui.Label;
50  import com.vaadin.ui.Notification;
51  import com.vaadin.ui.UI;
52  import com.vaadin.ui.VerticalLayout;
53  import com.vaadin.ui.Window;
54  
55  @Theme("poctheme")
56  @Title("Notifications & Alerts - Magnolia 6 Resurface")
57  public class NotificationUI extends UI {
58  
59      @WebServlet(value = "/notification/*", asyncSupported = true)
60      @VaadinServletConfiguration(productionMode = false, ui = NotificationUI.class)
61      public static class Servlet extends VaadinServlet {
62      }
63  
64      @Override
65      protected void init(VaadinRequest request) {
66          final VerticalLayout layout = new VerticalLayout();
67          layout.setSizeFull();
68  
69          Button showInfoNotification = new Button("Show info notification");
70          showInfoNotification.addClickListener(listener -> {
71              Notification notification = Notification.show("Item deleted !", Notification.Type.HUMANIZED_MESSAGE);
72              notification.setDelayMsec(Notification.DELAY_FOREVER);
73          });
74  
75  
76          Button showWarningNotification = new Button("Show warning notification");
77  
78          showWarningNotification.addClickListener(listener -> {
79              Notification notification = Notification.show("Your server is currently low on memory. " +
80                      "You might want to close unused apps in order to free memory. " +
81                      "Please try again later or contact your administrator if this happens frequently.", Notification.Type.WARNING_MESSAGE);
82              notification.setDelayMsec(Notification.DELAY_FOREVER);
83          });
84  
85          Button showErrorNotification = new Button("Show error notification");
86  
87          showErrorNotification.addClickListener(listener -> {
88              Notification notification = Notification.show("Please enter all required fields !", Notification.Type.ERROR_MESSAGE);
89              notification.setDelayMsec(Notification.DELAY_FOREVER);
90          });
91  
92          Button showInfoAlert = new Button("Show info alert");
93          showInfoAlert.addClickListener(listener -> {
94              showAlert("Delete this item?", "The item XY and all its sub items wil be marked for deletion.", MessageStyleTypeEnum.INFO);
95          });
96  
97          Button showWarningAlert = new Button("Show warning alert");
98          showWarningAlert.addClickListener(listener -> {
99              showAlert("Agree with Trapattoni?", "Es gibt im Moment in diese Mannschaft, oh," +
100                     " einige Spieler vergessen ihnen Profi was sie sind." +
101                     " Ich lese nicht sehr viele Zeitungen, aber ich habe gehört viele Situationen." +
102                     " Erstens: wir haben nicht offensiv gespielt. Es gibt keine deutsche Mannschaft spielt offensiv" +
103                     " und die Name offensiv wie Bayern. Letzte Spiel hatten wir in Platz drei Spitzen: Elber, Jancka" +
104                     " und dann Zickler. Wir müssen nicht vergessen Zickler. Zickler ist eine Spitzen mehr, Mehmet" +
105                     " eh mehr Basler.", MessageStyleTypeEnum.WARNING);
106         });
107 
108         Button showErrorAlert = new Button("Show error alert");
109         showErrorAlert.addClickListener(listener -> {
110             showAlert("Resurface has failed", "Why would someone ask a yes no question in the context of an error message?",
111                     MessageStyleTypeEnum.ERROR);
112         });
113 
114         layout.addComponents(showInfoNotification, showWarningNotification, showErrorNotification,
115                 showInfoAlert, showWarningAlert, showErrorAlert);
116 
117         setContent(layout);
118     }
119 
120     private void showAlert(String title, String text, MessageStyleType type) {
121         Label content = new Label(text);
122         content.setStyleName("content");
123 
124         YesNoAlert alert = new YesNoAlert(content);
125 
126         Window window = new Window(title);
127         window.setContent(alert);
128         window.center();
129         window.setModal(true);
130         window.addStyleName("alert");
131         window.addStyleName(type.getCssClass());
132 
133         addWindow(window);
134         window.focus();
135     }
136 
137     @Override
138     public boolean equals(Object obj) {
139         return super.equals(obj);
140     }
141 
142     @Override
143     public int hashCode() {
144         return super.hashCode();
145     }
146 
147     private static class YesNoAlert extends VerticalLayout {
148 
149         YesNoAlert(Component content) {
150             this.addComponent(content);
151 
152             HorizontalLayout buttons = new HorizontalLayout();
153             buttons.addStyleName("buttons");
154             final Button no = new Button("No");
155             no.setStyleName("cancel");
156             final Button yes = new Button("Yes");
157             yes.setStyleName("confirm");
158             buttons.addComponents(no, yes);
159             this.addComponent(buttons);
160         }
161     }
162 }