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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.HUMANIZED_MESSAGE);
  22. ----
  23. [[figure.notification.example1]]
  24. .Notification
  25. image::img/notification-example2.png[width=50%, scaledwidth=80%]
  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. "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. <<../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[width=30%, scaledwidth=60%]
  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]#HUMANIZED_MESSAGE#::
  57. image:img/notification-humanized.png[width=30%, scaledwidth=50%]
  58. +
  59. A user-friendly message that does not annoy too much: it does not require
  60. confirmation by clicking and disappears quickly. It is centered and has a
  61. neutral gray color.
  62. [parameter]#WARNING_MESSAGE#::
  63. image:img/notification-warning.png[width=30%, scaledwidth=50%]
  64. +
  65. Warnings are messages of medium importance. They are displayed with colors that
  66. are neither neutral nor too distractive. A warning is displayed for 1.5 seconds,
  67. but the user can click the message box to dismiss it. The user can continue to
  68. interact with the application while the warning is displayed.
  69. [parameter]#ERROR_MESSAGE#::
  70. image:img/notification-error.png[width=30%, scaledwidth=50%]
  71. +
  72. Error messages are notifications that require the highest user attention, with
  73. alert colors, and they require the user to click the message to dismiss it. The
  74. error message box does not itself include an instruction to click the message,
  75. although the close icon on the right side indicates it visually. Unlike
  76. with other notifications, the user can not interact with the application while
  77. the error message is displayed. If you don't want to show the close icon,
  78. you can hide by adding the style constant
  79. [literal]#ValoTheme.NOTIFICATION_CRITICAL_ERROR# to the [classname]#Notification#.
  80. [parameter]#TRAY_NOTIFICATION#::
  81. image:img/notification-tray.png[width=30%, scaledwidth=50%]
  82. +
  83. Tray notifications are displayed in the "system tray" area, that is, in the
  84. lower-right corner of the browser view. As they do not usually obscure any user
  85. interface, they are displayed longer than humanized or warning messages, 3
  86. seconds by default. The user can continue to interact with the application
  87. normally while the tray notification is displayed.
  88. [[application.notifications.customization]]
  89. == Customizing Notifications
  90. All of the features of specific notification types can be controlled with the
  91. [classname]#Notification# properties. Once configured, you need to show it in
  92. the current page.
  93. [source, java]
  94. ----
  95. // Notification with default settings for a warning
  96. Notification notif = new Notification(
  97. "Warning",
  98. "Area of reindeer husbandry",
  99. Notification.TYPE_WARNING_MESSAGE);
  100. // Customize it
  101. notif.setDelayMsec(20000);
  102. notif.setPosition(Position.BOTTOM_RIGHT);
  103. notif.setStyleName("mystyle");
  104. notif.setIcon(new ThemeResource("img/reindeer.png"));
  105. // Show it in the page
  106. notif.show(Page.getCurrent());
  107. ----
  108. The [methodname]#setPosition()# method allows setting the positioning of the
  109. notification. The position can be specified by any of the constants defined in
  110. the [classname]#Position# enum.
  111. The [methodname]#setDelayMSec()# allows setting the time for how long the
  112. notification is displayed in milliseconds. Parameter value [literal]#++-1++#
  113. means that the message is displayed until the user clicks the message box. It
  114. also prevents interaction with other parts of the application window, which is
  115. the default behaviour for error notifications. It does not, however, add a close
  116. box that the error notification has.
  117. [[application.notifications.css]]
  118. == Styling with CSS
  119. [source, css]
  120. ----
  121. .v-Notification {}
  122. .popupContent {}
  123. .gwt-HTML {}
  124. h1 {}
  125. p {}
  126. ----
  127. The notification box is a floating [literal]#++div++# element under the
  128. [literal]#++body++# element of the page. It has an overall
  129. [literal]#++v-Notification++# style. The content is wrapped inside an element
  130. with [literal]#++popupContent++# style. The caption is enclosed within an
  131. [literal]#++h1++# element and the description in a [literal]#++p++# element.
  132. To customize it, add a style for the [classname]#Notification# object with
  133. [methodname]#setStyleName("mystyle")#, and make the settings in the theme, for
  134. example as follows:
  135. [source, css]
  136. ----
  137. .v-Notification.mystyle {
  138. background: #FFFF00;
  139. border: 10px solid #C00000;
  140. color: black;
  141. }
  142. ----
  143. The result is shown, with the icon set earlier in the customization example, in
  144. <<figure.application.errors.notifications.css>>.
  145. [[figure.application.errors.notifications.css]]
  146. .A Styled Notification
  147. image::img/notification-customization.png[width=40%, scaledwidth=60%]