Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Notification.java 16KB

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