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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
/*
* Copyright 2000-2014 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.ui;
import java.io.Serializable;
import com.vaadin.server.Page;
import com.vaadin.server.Resource;
import com.vaadin.shared.Position;
/**
* A notification message, used to display temporary messages to the user - for
* example "Document saved", or "Save failed".
* <p>
* The notification message can consist of several parts: caption, description
* and icon. It is usually used with only caption - one should be wary of
* filling the notification with too much information.
* </p>
* <p>
* The notification message tries to be as unobtrusive as possible, while still
* drawing needed attention. There are several basic types of messages that can
* be used in different situations:
* <ul>
* <li>TYPE_HUMANIZED_MESSAGE fades away quickly as soon as the user uses the
* mouse or types something. It can be used to show fairly unimportant messages,
* such as feedback that an operation succeeded ("Document Saved") - the kind of
* messages the user ignores once the application is familiar.</li>
* <li>TYPE_WARNING_MESSAGE is shown for a short while after the user uses the
* mouse or types something. It's default style is also more noticeable than the
* humanized message. It can be used for messages that do not contain a lot of
* important information, but should be noticed by the user. Despite the name,
* it does not have to be a warning, but can be used instead of the humanized
* message whenever you want to make the message a little more noticeable.</li>
* <li>TYPE_ERROR_MESSAGE requires to user to click it before disappearing, and
* can be used for critical messages.</li>
* <li>TYPE_TRAY_NOTIFICATION is shown for a while in the lower left corner of
* the window, and can be used for "convenience notifications" that do not have
* to be noticed immediately, and should not interfere with the current task -
* for instance to show "You have a new message in your inbox" while the user is
* working in some other area of the application.</li>
* </ul>
* </p>
* <p>
* In addition to the basic pre-configured types, a Notification can also be
* configured to show up in a custom position, for a specified time (or until
* clicked), and with a custom stylename. An icon can also be added.
* </p>
*
*/
public class Notification implements Serializable {
public enum Type {
HUMANIZED_MESSAGE("humanized"), WARNING_MESSAGE(
"warning"), ERROR_MESSAGE("error"), TRAY_NOTIFICATION("tray"),
/**
* @since 7.2
*/
ASSISTIVE_NOTIFICATION("assistive");
private String style;
Type(String style) {
this.style = style;
}
/**
* @since 7.2
*
* @return the style name for this notification type.
*/
public String getStyle() {
return style;
}
}
@Deprecated
public static final Type TYPE_HUMANIZED_MESSAGE = Type.HUMANIZED_MESSAGE;
@Deprecated
public static final Type TYPE_WARNING_MESSAGE = Type.WARNING_MESSAGE;
@Deprecated
public static final Type TYPE_ERROR_MESSAGE = Type.ERROR_MESSAGE;
@Deprecated
public static final Type TYPE_TRAY_NOTIFICATION = Type.TRAY_NOTIFICATION;
@Deprecated
public static final Position POSITION_CENTERED = Position.MIDDLE_CENTER;
@Deprecated
public static final Position POSITION_CENTERED_TOP = Position.TOP_CENTER;
@Deprecated
public static final Position POSITION_CENTERED_BOTTOM = Position.BOTTOM_CENTER;
@Deprecated
public static final Position POSITION_TOP_LEFT = Position.TOP_LEFT;
@Deprecated
public static final Position POSITION_TOP_RIGHT = Position.TOP_RIGHT;
@Deprecated
public static final Position POSITION_BOTTOM_LEFT = Position.BOTTOM_LEFT;
@Deprecated
public static final Position POSITION_BOTTOM_RIGHT = Position.BOTTOM_RIGHT;
public static final int DELAY_FOREVER = -1;
public static final int DELAY_NONE = 0;
private String caption;
private String description;
private Resource icon;
private Position position = Position.MIDDLE_CENTER;
private int delayMsec = 0;
private String styleName;
private boolean htmlContentAllowed;
/**
* Creates a "humanized" notification message.
*
* The caption is rendered as plain text with HTML automatically escaped.
*
* @param caption
* The message to show
*/
public Notification(String caption) {
this(caption, null, TYPE_HUMANIZED_MESSAGE);
}
/**
* Creates a notification message of the specified type.
*
* The caption is rendered as plain text with HTML automatically escaped.
*
* @param caption
* The message to show
* @param type
* The type of message
*/
public Notification(String caption, Type type) {
this(caption, null, type);
}
/**
* Creates a "humanized" notification message with a bigger caption and
* smaller description.
*
* The caption and description are rendered as plain text with HTML
* automatically escaped.
*
* @param caption
* The message caption
* @param description
* The message description
*/
public Notification(String caption, String description) {
this(caption, description, TYPE_HUMANIZED_MESSAGE);
}
/**
* Creates a notification message of the specified type, with a bigger
* caption and smaller description.
*
* The caption and description are rendered as plain text with HTML
* automatically escaped.
*
* @param caption
* The message caption
* @param description
* The message description
* @param type
* The type of message
*/
public Notification(String caption, String description, Type type) {
this(caption, description, type, false);
}
/**
* Creates a notification message of the specified type, with a bigger
* caption and smaller description.
*
* Care should be taken to to avoid XSS vulnerabilities if html is allowed.
*
* @param caption
* The message caption
* @param description
* The message description
* @param type
* The type of message
* @param htmlContentAllowed
* Whether html in the caption and description should be
* displayed as html or as plain text
*/
public Notification(String caption, String description, Type type,
boolean htmlContentAllowed) {
this.caption = caption;
this.description = description;
this.htmlContentAllowed = htmlContentAllowed;
setType(type);
}
private void setType(Type type) {
styleName = type.getStyle();
switch (type) {
case WARNING_MESSAGE:
delayMsec = 1500;
break;
case ERROR_MESSAGE:
delayMsec = -1;
break;
case TRAY_NOTIFICATION:
delayMsec = 3000;
position = Position.BOTTOM_RIGHT;
break;
case ASSISTIVE_NOTIFICATION:
delayMsec = 3000;
position = Position.ASSISTIVE;
break;
case HUMANIZED_MESSAGE:
default:
break;
}
}
/**
* Gets the caption part of the notification message.
*
* @return The message caption
*/
public String getCaption() {
return caption;
}
/**
* Sets the caption part of the notification message
*
* @param caption
* The message caption
*/
public void setCaption(String caption) {
this.caption = caption;
}
/**
* Gets the description part of the notification message.
*
* @return The message description.
*/
public String getDescription() {
return description;
}
/**
* Sets the description part of the notification message.
*
* @param description
*/
public void setDescription(String description) {
this.description = description;
}
/**
* Gets the position of the notification message.
*
* @return The position
*/
public Position getPosition() {
return position;
}
/**
* Sets the position of the notification message.
*
* @param position
* The desired notification position
*/
public void setPosition(Position position) {
this.position = position;
}
/**
* Gets the icon part of the notification message.
*
* @return The message icon
*/
public Resource getIcon() {
return icon;
}
/**
* Sets the icon part of the notification message.
*
* @param icon
* The desired message icon
*/
public void setIcon(Resource icon) {
this.icon = icon;
}
/**
* Gets the delay before the notification disappears.
*
* @return the delay in msec, -1 indicates the message has to be clicked.
*/
public int getDelayMsec() {
return delayMsec;
}
/**
* Sets the delay before the notification disappears.
*
* @param delayMsec
* the desired delay in msec, -1 to require the user to click the
* message
*/
public void setDelayMsec(int delayMsec) {
this.delayMsec = delayMsec;
}
/**
* Sets the style name for the notification message.
*
* @param styleName
* The desired style name.
*/
public void setStyleName(String styleName) {
this.styleName = styleName;
}
/**
* Gets the style name for the notification message.
*
* @return
*/
public String getStyleName() {
return styleName;
}
/**
* Sets whether html is allowed in the caption and description. If set to
* true, the texts are passed to the browser as html and the developer is
* responsible for ensuring no harmful html is used. If set to false, the
* texts are passed to the browser as plain text.
*
* @param htmlContentAllowed
* true if the texts are used as html, false if used as plain
* text
*/
public void setHtmlContentAllowed(boolean htmlContentAllowed) {
this.htmlContentAllowed = htmlContentAllowed;
}
/**
* Checks whether caption and description are interpreted as html or plain
* text.
*
* @return true if the texts are used as html, false if used as plain text
* @see #setHtmlContentAllowed(boolean)
*/
public boolean isHtmlContentAllowed() {
return htmlContentAllowed;
}
/**
* Shows this notification on a Page.
*
* @param page
* The page on which the notification should be shown
*/
public void show(Page page) {
// TODO Can avoid deprecated API when Notification extends Extension
page.showNotification(this);
}
/**
* Shows a notification message on the middle of the current page. The
* message automatically disappears ("humanized message").
*
* The caption is rendered as plain text with HTML automatically escaped.
*
* @see #Notification(String)
* @see #show(Page)
*
* @param caption
* The message
*/
public static void show(String caption) {
new Notification(caption).show(Page.getCurrent());
}
/**
* Shows a notification message the current page. The position and behavior
* of the message depends on the type, which is one of the basic types
* defined in {@link Notification}, for instance
* Notification.TYPE_WARNING_MESSAGE.
*
* The caption is rendered as plain text with HTML automatically escaped.
*
* @see #Notification(String, int)
* @see #show(Page)
*
* @param caption
* The message
* @param type
* The message type
*/
public static void show(String caption, Type type) {
new Notification(caption, type).show(Page.getCurrent());
}
/**
* Shows a notification message the current page. The position and behavior
* of the message depends on the type, which is one of the basic types
* defined in {@link Notification}, for instance
* Notification.TYPE_WARNING_MESSAGE.
*
* The caption is rendered as plain text with HTML automatically escaped.
*
* @see #Notification(String, Type)
* @see #show(Page)
*
* @param caption
* The message
* @param description
* The message description
* @param type
* The message type
*/
public static void show(String caption, String description, Type type) {
new Notification(caption, description, type).show(Page.getCurrent());
}
}
|