You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

application-notifications.asciidoc 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ---
  2. title: Notifications
  3. order: 7
  4. layout: page
  5. ---
  6. [[application.notifications]]
  7. = Notifications
  8. Notifications are error or information boxes that appear briefly, typically at
  9. the center of the screen. A notification box has a caption and an optional
  10. description and icon. The box stays on the screen either for a preset time or
  11. until the user clicks it. The notification type defines the default appearance
  12. and behaviour of a notification.
  13. There are two ways to create a notification. The easiest is to use a static
  14. shorthand [methodname]#Notification.show()# method, which takes the caption of
  15. the notification as a parameter, and an optional description and notification
  16. type, and displays it in the current page.
  17. [source, java]
  18. ----
  19. Notification.show("This is the caption",
  20. "This is the description",
  21. Notification.Type.WARNING_MESSAGE);
  22. ----
  23. [[figure.notification.example1]]
  24. .Notification
  25. image::img/notification-example2.png[]
  26. For more control, you can create a [classname]#Notification# object. Different
  27. constructors exist for taking just the caption, and optionally the description,
  28. notification type, and whether HTML is allowed or not. Notifications are shown
  29. in a [classname]#Page#, typically the current page.
  30. [source, java]
  31. ----
  32. new Notification("This is a warning",
  33. "<br/>This is the <i>last</i> warning",
  34. Notification.TYPE_WARNING_MESSAGE, true)
  35. .show(Page.getCurrent());
  36. ----
  37. The caption and description are by default written on the same line. If you want
  38. to have a line break between them, use the HTML line break markup "
  39. [literal]#++<br/>++#" if HTML is enabled, or " [literal]#++\n++#" if not. HTML
  40. is disabled by default, but can be enabled with
  41. [methodname]#setHtmlContentAllowed(true)#. When enabled, you can use any HTML
  42. markup in the caption and description of a notification. If it is in any way
  43. possible to get the notification content from user input, you should either
  44. disallow HTML or sanitize the content carefully, as noted in
  45. <<dummy/../../../framework/advanced/advanced-security#advanced.security.sanitizing,"Sanitizing
  46. User Input to Prevent Cross-Site Scripting">>.
  47. [[figure.notification.example2]]
  48. .Notification with HTML Formatting
  49. image::img/notification-example3.png[]
  50. [[application.notifications.type]]
  51. == Notification Type
  52. The notification type defines the overall default style and behaviour of a
  53. notification. If no notification type is given, the "humanized" type is used as
  54. the default. The notification types, listed below, are defined in the
  55. [classname]#Notification.Type# class.
  56. [parameter]#TYPE_HUMANIZED_MESSAGE# image:[]:: A user-friendly message that does not annoy too much: it does not require
  57. confirmation by clicking and disappears quickly. It is centered and has a
  58. neutral gray color.
  59. [parameter]#TYPE_WARNING_MESSAGE# image:[]:: Warnings are messages of medium importance. They are displayed with colors that
  60. are neither neutral nor too distractive. A warning is displayed for 1.5 seconds,
  61. but the user can click the message box to dismiss it. The user can continue to
  62. interact with the application while the warning is displayed.
  63. [parameter]#TYPE_ERROR_MESSAGE# image:[]:: Error messages are notifications that require the highest user attention, with
  64. alert colors, and they require the user to click the message to dismiss it. The
  65. error message box does not itself include an instruction to click the message,
  66. although the close box in the upper right corner indicates it visually. Unlike
  67. with other notifications, the user can not interact with the application while
  68. the error message is displayed.
  69. [parameter]#TYPE_TRAY_NOTIFICATION# image:[]:: Tray notifications are displayed in the "system tray" area, that is, in the
  70. lower-right corner of the browser view. As they do not usually obscure any user
  71. interface, they are displayed longer than humanized or warning messages, 3
  72. seconds by default. The user can continue to interact with the application
  73. normally while the tray notification is displayed.
  74. ifdef::web[]
  75. [[application.notifications.customization]]
  76. == Customizing Notifications
  77. All of the features of specific notification types can be controlled with the
  78. [classname]#Notification# properties. Once configured, you need to show it in
  79. the current page.
  80. [source, java]
  81. ----
  82. // Notification with default settings for a warning
  83. Notification notif = new Notification(
  84. "Warning",
  85. "<br/>Area of reindeer husbandry",
  86. Notification.TYPE_WARNING_MESSAGE);
  87. // Customize it
  88. notif.setDelayMsec(20000);
  89. notif.setPosition(Position.BOTTOM_RIGHT);
  90. notif.setStyleName("mystyle");
  91. notif.setIcon(new ThemeResource("img/reindeer.png"));
  92. // Show it in the page
  93. notif.show(Page.getCurrent());
  94. ----
  95. The [methodname]#setPosition()# method allows setting the positioning of the
  96. notification. The position can be specified by any of the constants defined in
  97. the [classname]#Position# enum.
  98. The [methodname]#setDelayMSec()# allows setting the time for how long the
  99. notification is displayed in milliseconds. Parameter value [literal]#++-1++#
  100. means that the message is displayed until the user clicks the message box. It
  101. also prevents interaction with other parts of the application window, which is
  102. the default behaviour for error notifications. It does not, however, add a close
  103. box that the error notification has.
  104. endif::web[]
  105. [[application.notifications.css]]
  106. == Styling with CSS
  107. [source, css]
  108. ----
  109. .v-Notification {}
  110. .popupContent {}
  111. .gwt-HTML {}
  112. h1 {}
  113. p {}
  114. ----
  115. The notification box is a floating [literal]#++div++# element under the
  116. [literal]#++body++# element of the page. It has an overall
  117. [literal]#++v-Notification++# style. The content is wrapped inside an element
  118. with [literal]#++popupContent++# style. The caption is enclosed within an
  119. [literal]#++h1++# element and the description in a [literal]#++p++# element.
  120. To customize it, add a style for the [classname]#Notification# object with
  121. [methodname]#setStyleName("mystyle")#, and make the settings in the theme, for
  122. example as follows:
  123. [source, css]
  124. ----
  125. .v-Notification.mystyle {
  126. background: #FFFF00;
  127. border: 10px solid #C00000;
  128. color: black;
  129. }
  130. ----
  131. The result is shown, with the icon set earlier in the customization example, in
  132. <<figure.application.errors.notifications.css>>.
  133. [[figure.application.errors.notifications.css]]
  134. .A Styled Notification
  135. image::img/notification-customization.png[]