1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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 }