diff options
author | Ahmed Ashour <asashour@yahoo.com> | 2017-09-27 12:44:01 +0200 |
---|---|---|
committer | Henri Sara <henri.sara@gmail.com> | 2017-09-27 13:44:01 +0300 |
commit | 9f9efe0058397992fda43e104c90b79039d41c0f (patch) | |
tree | 6903fb464d96fb1f8f6a4e92c1308833388fd210 /client | |
parent | 94c57dea3848635e0ae42fae464e23301f9e7c2c (diff) | |
download | vaadin-framework-9f9efe0058397992fda43e104c90b79039d41c0f.tar.gz vaadin-framework-9f9efe0058397992fda43e104c90b79039d41c0f.zip |
Support addCloseListener for Notification (#10027)
Converts Notification to an Extension and adds support for listening to the closing of notifications.
Fixes #888
Diffstat (limited to 'client')
3 files changed, 108 insertions, 48 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/VNotification.java b/client/src/main/java/com/vaadin/client/ui/VNotification.java index 8f98b6bd08..5fc14e7ae2 100644 --- a/client/src/main/java/com/vaadin/client/ui/VNotification.java +++ b/client/src/main/java/com/vaadin/client/ui/VNotification.java @@ -18,6 +18,7 @@ package com.vaadin.client.ui; import java.util.ArrayList; import java.util.EventObject; +import java.util.List; import com.google.gwt.aria.client.Roles; import com.google.gwt.core.client.GWT; @@ -36,12 +37,10 @@ import com.vaadin.client.AnimationUtil; import com.vaadin.client.AnimationUtil.AnimationEndListener; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.BrowserInfo; -import com.vaadin.client.UIDL; import com.vaadin.client.WidgetUtil; import com.vaadin.client.ui.aria.AriaHelper; import com.vaadin.shared.Position; import com.vaadin.shared.ui.ui.NotificationRole; -import com.vaadin.shared.ui.ui.UIConstants; import com.vaadin.shared.ui.ui.UIState.NotificationTypeConfiguration; public class VNotification extends VOverlay { @@ -80,7 +79,7 @@ public class VNotification extends VOverlay { private static final int Z_INDEX_BASE = 20000; public static final String STYLE_SYSTEM = "system"; - private static final ArrayList<VNotification> notifications = new ArrayList<>(); + private static final List<VNotification> notifications = new ArrayList<>(); private boolean infiniteDelay = false; private int hideDelay = 0; @@ -92,7 +91,7 @@ public class VNotification extends VOverlay { private String temporaryStyle; - private ArrayList<EventListener> listeners; + private List<EventListener> listeners; private static final int TOUCH_DEVICE_IDLE_DELAY = 1000; /** @@ -197,8 +196,8 @@ public class VNotification extends VOverlay { && !styleSetup.prefix.isEmpty(); } - public void show(String html, Position position, String style) { - NotificationTypeConfiguration styleSetup = getUiState(style); + public void show(String html, Position position, String styleName) { + NotificationTypeConfiguration styleSetup = getUiState(styleName); String assistiveDeviceOnlyStyle = AriaHelper.ASSISTIVE_DEVICE_ONLY_STYLE; setWaiAriaRole(styleSetup); @@ -217,7 +216,7 @@ public class VNotification extends VOverlay { } setWidget(new HTML(type + html + usage)); - show(position, style); + show(position, styleName); } private NotificationTypeConfiguration getUiState(String style) { @@ -478,54 +477,58 @@ public class VNotification extends VOverlay { } } - public static void showNotification(ApplicationConnection client, - final UIDL notification) { - boolean onlyPlainText = notification.hasAttribute( - UIConstants.NOTIFICATION_HTML_CONTENT_NOT_ALLOWED); + /** + * Creates and shows a {@code Notification} with the specified parameters. + * + * @param client + * The client connection, cannot be {@code null}. + * @param caption + * The Notification caption, can be {@code null}. + * @param description + * The Notification description, can be {@code null}. + * @param htmlContentAllowed + * Whether {@code caption} and {@code description} + * are interpreted as HTML or not. + * @param iconUri + * The icon URI, can be {@code null}. + * @param styleName + * The Notification style name, can be {@code null}. + * @param position + * The desired {@link Position}, can not be {@code null}. + * @param delayMsec + * The delay in milliseconds before disappearing, -1 for forever. + * + * @since + */ + public static VNotification showNotification(ApplicationConnection client, + String caption, String description, boolean htmlContentAllowed, + String iconUri, String styleName, Position position, int delayMsec) { String html = ""; - if (notification - .hasAttribute(UIConstants.ATTRIBUTE_NOTIFICATION_ICON)) { - String iconUri = notification.getStringAttribute( - UIConstants.ATTRIBUTE_NOTIFICATION_ICON); + if (iconUri != null) { html += client.getIcon(iconUri).getElement().getString(); } - if (notification - .hasAttribute(UIConstants.ATTRIBUTE_NOTIFICATION_CAPTION)) { - String caption = notification.getStringAttribute( - UIConstants.ATTRIBUTE_NOTIFICATION_CAPTION); - if (onlyPlainText) { + if (caption != null) { + if (!htmlContentAllowed) { caption = WidgetUtil.escapeHTML(caption); caption = caption.replaceAll("\\n", "<br />"); } html += "<h1 class='" + getDependentStyle(client, CAPTION) + "'>" + caption + "</h1>"; } - if (notification - .hasAttribute(UIConstants.ATTRIBUTE_NOTIFICATION_MESSAGE)) { - String message = notification.getStringAttribute( - UIConstants.ATTRIBUTE_NOTIFICATION_MESSAGE); - if (onlyPlainText) { - message = WidgetUtil.escapeHTML(message); - message = message.replaceAll("\\n", "<br />"); + if (description != null) { + if (!htmlContentAllowed) { + description = WidgetUtil.escapeHTML(description); + description = description.replaceAll("\\n", "<br />"); } html += "<p class='" + getDependentStyle(client, DESCRIPTION) + "'>" - + message + "</p>"; + + description + "</p>"; } - final String style = notification - .hasAttribute(UIConstants.ATTRIBUTE_NOTIFICATION_STYLE) - ? notification.getStringAttribute( - UIConstants.ATTRIBUTE_NOTIFICATION_STYLE) - : null; - - final int pos = notification - .getIntAttribute(UIConstants.ATTRIBUTE_NOTIFICATION_POSITION); - Position position = Position.values()[pos]; + VNotification vNotification = createNotification(delayMsec, + client.getUIConnector().getWidget()); - final int delay = notification - .getIntAttribute(UIConstants.ATTRIBUTE_NOTIFICATION_DELAY); - createNotification(delay, client.getUIConnector().getWidget()) - .show(html, position, style); + vNotification.show(html, position, styleName); + return vNotification; } /** diff --git a/client/src/main/java/com/vaadin/client/ui/notification/NotificationConnector.java b/client/src/main/java/com/vaadin/client/ui/notification/NotificationConnector.java new file mode 100644 index 0000000000..45f72375d6 --- /dev/null +++ b/client/src/main/java/com/vaadin/client/ui/notification/NotificationConnector.java @@ -0,0 +1,64 @@ +/* + * Copyright 2000-2016 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.client.ui.notification; + +import com.google.gwt.event.logical.shared.CloseEvent; +import com.google.gwt.event.logical.shared.CloseHandler; +import com.google.gwt.user.client.ui.PopupPanel; +import com.vaadin.client.ServerConnector; +import com.vaadin.client.extensions.AbstractExtensionConnector; +import com.vaadin.client.ui.VNotification; +import com.vaadin.shared.ui.Connect; +import com.vaadin.shared.ui.notification.NotificationServerRpc; +import com.vaadin.shared.ui.notification.NotificationState; +import com.vaadin.ui.Notification; + +/** + * The client-side connector for the {@code Notification}. + * + * @author Vaadin Ltd. + * + * @since + */ +@Connect(value = Notification.class) +public class NotificationConnector extends AbstractExtensionConnector { + + @Override + public NotificationState getState() { + return (NotificationState) super.getState(); + } + + @Override + protected void extend(ServerConnector target) { + NotificationState state = getState(); + VNotification n = VNotification.showNotification( + target.getConnection(), + state.caption, state.description, + state.htmlContentAllowed, getResourceUrl("icon"), + state.styleName, state.position, state.delay); + + n.addCloseHandler(new CloseHandler<PopupPanel>() { + + @Override + public void onClose(CloseEvent<PopupPanel> event) { + NotificationServerRpc rpc = + getRpcProxy(NotificationServerRpc.class); + rpc.closed(); + } + }); + } + +} diff --git a/client/src/main/java/com/vaadin/client/ui/ui/UIConnector.java b/client/src/main/java/com/vaadin/client/ui/ui/UIConnector.java index 44f0b4253a..c808ea6c9d 100644 --- a/client/src/main/java/com/vaadin/client/ui/ui/UIConnector.java +++ b/client/src/main/java/com/vaadin/client/ui/ui/UIConnector.java @@ -75,7 +75,6 @@ import com.vaadin.client.ui.AbstractConnector; import com.vaadin.client.ui.AbstractSingleComponentContainerConnector; import com.vaadin.client.ui.ClickEventHandler; import com.vaadin.client.ui.ShortcutActionHandler; -import com.vaadin.client.ui.VNotification; import com.vaadin.client.ui.VOverlay; import com.vaadin.client.ui.VUI; import com.vaadin.client.ui.VWindow; @@ -376,12 +375,6 @@ public class UIConnector extends AbstractSingleComponentContainerConnector getWidget().id, client); } getWidget().actionHandler.updateActionMap(childUidl); - } else if (tag == "notifications") { - for (final Iterator<?> it = childUidl.getChildIterator(); it - .hasNext();) { - final UIDL notification = (UIDL) it.next(); - VNotification.showNotification(client, notification); - } } else if (tag == "css-injections") { injectCSS(childUidl); } |