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.ui.vaadin.gwt.client.magnoliashell.shellmessage;
35
36 import info.magnolia.ui.vaadin.gwt.client.jquerywrapper.JQueryCallback;
37 import info.magnolia.ui.vaadin.gwt.client.jquerywrapper.JQueryWrapper;
38 import info.magnolia.ui.vaadin.gwt.client.magnoliashell.shell.MagnoliaShellView;
39
40 import com.google.gwt.dom.client.Style.Display;
41 import com.google.gwt.event.shared.HandlerRegistration;
42 import com.google.gwt.user.client.DOM;
43 import com.google.gwt.user.client.Element;
44 import com.google.gwt.user.client.Event;
45 import com.google.gwt.user.client.Event.NativePreviewEvent;
46 import com.google.gwt.user.client.Event.NativePreviewHandler;
47 import com.google.gwt.user.client.ui.HTML;
48 import info.magnolia.ui.vaadin.gwt.client.magnoliashell.viewport.animation.FadeAnimation;
49
50
51
52
53 public abstract class ShellMessageWidget extends HTML {
54
55
56
57
58 public enum MessageType {
59 WARNING, ERROR, INFO
60 }
61
62 private static final String STYLE_NAME = "v-shell-notification";
63
64 private HandlerRegistration eventPreviewReg = null;
65
66 private final MagnoliaShellView shell;
67
68 private Element messageTypeEl = DOM.createElement("b");
69
70 private Element header = DOM.createDiv();
71
72 private Element topicEl = DOM.createSpan();
73
74 private Element closeEl = DOM.createDiv();
75
76 private final String id;
77
78 private final String message;
79
80 private final String topic;
81
82 public ShellMessageWidget(MagnoliaShellView shell, String topic, String message, String id) {
83 super();
84 this.topic = topic;
85 this.shell = shell;
86 this.message = message;
87 this.id = id;
88 eventPreviewReg = Event.addNativePreviewHandler(new NativePreviewHandler() {
89 @Override
90 public void onPreviewNativeEvent(NativePreviewEvent event) {
91 if (event.getTypeInt() == Event.ONCLICK) {
92 final Element targetEl = event.getNativeEvent().getEventTarget().cast();
93 if (getElement().isOrHasChild(targetEl)) {
94 onMessageClicked(targetEl);
95 }
96 }
97 }
98 });
99 }
100
101 @Override
102 protected void onLoad() {
103 super.onLoad();
104 sinkEvents(Event.MOUSEEVENTS);
105 construct();
106 show();
107 }
108
109 protected void onMessageClicked(Element targetEl) {
110 close();
111 }
112
113 protected void close() {
114 hide();
115 getShell().closeMessageEager(getId());
116 }
117
118 protected void construct() {
119 topicEl.setInnerText(topic);
120 header.appendChild(messageTypeEl);
121 header.appendChild(topicEl);
122
123 addStyleName(STYLE_NAME);
124
125 header.setClassName("header");
126 getElement().appendChild(header);
127
128 applyCloseIconStyles(closeEl);
129 header.appendChild(closeEl);
130
131 messageTypeEl.setInnerHTML(getMessageTypeCaption());
132 }
133
134 protected void applyCloseIconStyles(Element element) {
135 element.setClassName("close");
136 }
137
138 protected abstract String getMessageTypeCaption();
139
140 protected int getHeaderHeight() {
141 return JQueryWrapper.select(header).cssInt("height");
142 }
143
144 public void show() {
145 getElement().getStyle().setDisplay(Display.NONE);
146 JQueryWrapper.select(this).slideDown(300, null);
147 }
148
149 public void hide() {
150
151
152
153
154
155
156 FadeAnimationient/magnoliashell/viewport/animation/FadeAnimation.html#FadeAnimation">FadeAnimation fadeAnimation = new FadeAnimation(0,true);
157 fadeAnimation.addCallback(new JQueryCallback() {
158 @Override
159 public void execute(JQueryWrapper query) {
160 removeFromParent();
161 }
162 });
163 fadeAnimation.run(500,this.getElement());
164 }
165
166 public void hideWithoutTransition() {
167 removeFromParent();
168 }
169
170 @Override
171 public void onBrowserEvent(Event event) {
172 super.onBrowserEvent(event);
173 int eventCode = event.getTypeInt();
174 if (eventCode == Event.ONTOUCHEND) {
175 final Element target = event.getEventTarget().cast();
176 if (target == closeEl) {
177 close();
178 }
179 }
180 }
181
182 @Override
183 protected void onUnload() {
184 super.onUnload();
185 if (eventPreviewReg != null) {
186 eventPreviewReg.removeHandler();
187 }
188 }
189
190 public Element getHeader() {
191 return header;
192 }
193
194 protected final MagnoliaShellView getShell() {
195 return shell;
196 }
197
198 public String getMessage() {
199 return message;
200 }
201
202 public String getId() {
203 return id;
204 }
205
206 public String getTopic() {
207 return topic;
208 }
209
210 }