From 59a2dafdcb1333b278484bdd65b3ca3cc7bb87f8 Mon Sep 17 00:00:00 2001 From: Artur Date: Tue, 9 Jan 2018 13:11:02 +0200 Subject: Add Notification.close() to hide a notification from the server (#10483) Also fixes the problem that notifications were never removed on the server side Fixes #2114, fixes #10481 --- .../src/main/java/com/vaadin/ui/Notification.java | 52 ++++++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) (limited to 'server') diff --git a/server/src/main/java/com/vaadin/ui/Notification.java b/server/src/main/java/com/vaadin/ui/Notification.java index 24327a8cc4..9a3bc8198a 100644 --- a/server/src/main/java/com/vaadin/ui/Notification.java +++ b/server/src/main/java/com/vaadin/ui/Notification.java @@ -20,6 +20,7 @@ import java.io.Serializable; import java.lang.reflect.Method; import com.vaadin.event.ConnectorEvent; +import com.vaadin.event.HasUserOriginated; import com.vaadin.server.AbstractExtension; import com.vaadin.server.Page; import com.vaadin.server.Resource; @@ -125,8 +126,9 @@ public class Notification extends AbstractExtension { * * @since 8.2 */ - private NotificationServerRpc rpc = () -> fireEvent( - new CloseEvent(Notification.this)); + private NotificationServerRpc rpc = () -> { + close(true); + }; /** * Creates a "humanized" notification message. @@ -391,6 +393,37 @@ public class Notification extends AbstractExtension { extend(page.getUI()); } + /** + * Closes (hides) the notification. + *

+ * If the notification is not shown, does nothing. + * + * @since + */ + public void close() { + close(false); + } + + /** + * Closes (hides) the notification. + *

+ * If the notification is not shown, does nothing. + * + * @param userOriginated + * true if the notification was closed because the + * user clicked on it, false if the notification was + * closed from the server + * @since + */ + protected void close(boolean userOriginated) { + if (!isAttached()) { + return; + } + + remove(); + fireEvent(new CloseEvent(this, userOriginated)); + } + @Override protected NotificationState getState() { return (NotificationState) super.getState(); @@ -498,13 +531,21 @@ public class Notification extends AbstractExtension { * * @since 8.2 */ - public static class CloseEvent extends ConnectorEvent { + public static class CloseEvent extends ConnectorEvent + implements HasUserOriginated { + + private boolean userOriginated; /** * @param source */ public CloseEvent(Notification source) { + this(source, true); + } + + public CloseEvent(Notification source, boolean userOriginated) { super(source); + this.userOriginated = userOriginated; } /** @@ -515,6 +556,11 @@ public class Notification extends AbstractExtension { public Notification getNotification() { return (Notification) getSource(); } + + @Override + public boolean isUserOriginated() { + return userOriginated; + } } /** -- cgit v1.2.3