Notifications API #999

This commit is contained in:
Yuriy Artamonov 2018-08-17 19:23:30 +04:00
parent f2d0b56d03
commit 6a34a9b640
3 changed files with 131 additions and 13 deletions

View File

@ -26,30 +26,105 @@ public interface Notifications {
String NAME = "cuba_Notifications"; String NAME = "cuba_Notifications";
int DELAY_FOREVER = -1;
int DELAY_NONE = 0;
int DELAY_DEFAULT = Integer.MIN_VALUE;
/**
* Creates empty notification object.
*
* @return notification
*/
Notification create(); Notification create();
/** /**
* Notification object. * Notification object.
*/ */
interface Notification { interface Notification {
/**
* Sets notification caption.
*
* @param caption caption
* @return this
*/
Notification setCaption(String caption); Notification setCaption(String caption);
/**
* @return caption
*/
String getCaption(); String getCaption();
/**
* Sets notification description.
*
* @param description description
* @return this
*/
Notification setDescription(String description); Notification setDescription(String description);
/**
* @return description
*/
String getDescription(); String getDescription();
Notification setType(NotificationType notificationType); /**
* Sets type of notification.
*
* @param type type
* @return this
*/
Notification setType(NotificationType type);
/**
* @return type
*/
NotificationType getType(); NotificationType getType();
/**
* Sets content mode for caption and description of notification.
*
* @param contentMode content mode
* @return this
*/
Notification setContentMode(ContentMode contentMode); Notification setContentMode(ContentMode contentMode);
/**
* @return content mode
*/
ContentMode getContentMode(); ContentMode getContentMode();
/**
* Sets CSS class name for notification DOM element.
*
* @param styleName CSS class name
* @return this
*/
Notification setStyleName(String styleName); Notification setStyleName(String styleName);
/**
* @return CSS class name
*/
String getStyleName(); String getStyleName();
/**
* Sets position of notification.
*
* @param position position
* @return this
*/
Notification setPosition(Position position); Notification setPosition(Position position);
/**
* @return position
*/
Position getPosition(); Position getPosition();
/**
* Sets the delay before the notification disappears.
*
* @param hideDelayMs the desired delay in milliseconds, {@value #DELAY_FOREVER} to
* require the user to click the message
*/
Notification setHideDelayMs(int hideDelayMs);
/**
* @return the delay before the notification disappears in milliseconds
*/
int getHideDelayMs();
/** /**
* Shows notification. * Shows notification.
*/ */
@ -70,6 +145,11 @@ public interface Notifications {
* Popup notification position. * Popup notification position.
*/ */
enum Position { enum Position {
/**
* Set position by notification type
*/
DEFAULT,
TOP_LEFT, TOP_CENTER, TOP_RIGHT, TOP_LEFT, TOP_CENTER, TOP_RIGHT,
MIDDLE_LEFT, MIDDLE_CENTER, MIDDLE_RIGHT, MIDDLE_LEFT, MIDDLE_CENTER, MIDDLE_RIGHT,
BOTTOM_LEFT, BOTTOM_CENTER, BOTTOM_RIGHT BOTTOM_LEFT, BOTTOM_CENTER, BOTTOM_RIGHT

View File

@ -30,8 +30,8 @@ import javax.inject.Inject;
@Scope(UIScope.NAME) @Scope(UIScope.NAME)
public class WebNotifications implements Notifications { public class WebNotifications implements Notifications {
public static final int HUMANIZED_NOTIFICATION_DELAY_MSEC = 3000; protected static final int HUMANIZED_NOTIFICATION_DELAY_MSEC = 3000;
public static final int WARNING_NOTIFICATION_DELAY_MSEC = -1; protected static final int WARNING_NOTIFICATION_DELAY_MSEC = -1;
protected AppUI ui; protected AppUI ui;
@ -58,7 +58,8 @@ public class WebNotifications implements Notifications {
protected String description; protected String description;
protected String styleName; protected String styleName;
protected Position position = Position.MIDDLE_CENTER; protected Position position = Position.DEFAULT;
protected int hideDelayMs = Integer.MIN_VALUE;
protected ContentMode contentMode = ContentMode.TEXT; protected ContentMode contentMode = ContentMode.TEXT;
protected NotificationType notificationType = NotificationType.HUMANIZED; protected NotificationType notificationType = NotificationType.HUMANIZED;
@ -92,10 +93,6 @@ public class WebNotifications implements Notifications {
public Notification setType(NotificationType notificationType) { public Notification setType(NotificationType notificationType) {
this.notificationType = notificationType; this.notificationType = notificationType;
// todo set default position here
// todo set default timeout here
return this; return this;
} }
@ -106,6 +103,10 @@ public class WebNotifications implements Notifications {
@Override @Override
public Notification setContentMode(ContentMode contentMode) { public Notification setContentMode(ContentMode contentMode) {
if (contentMode == ContentMode.PREFORMATTED) {
throw new UnsupportedOperationException("ContentMode.PREFORMATTED unsupported for Notification");
}
this.contentMode = contentMode; this.contentMode = contentMode;
return this; return this;
} }
@ -137,6 +138,17 @@ public class WebNotifications implements Notifications {
return position; return position;
} }
@Override
public Notification setHideDelayMs(int hideDelayMs) {
this.hideDelayMs = hideDelayMs;
return this;
}
@Override
public int getHideDelayMs() {
return hideDelayMs;
}
protected com.vaadin.ui.Notification.Type convertType(NotificationType notificationType) { protected com.vaadin.ui.Notification.Type convertType(NotificationType notificationType) {
switch (notificationType) { switch (notificationType) {
case TRAY: case TRAY:
@ -172,12 +184,20 @@ public class WebNotifications implements Notifications {
com.vaadin.ui.Notification vNotification = com.vaadin.ui.Notification vNotification =
new com.vaadin.ui.Notification(caption, description, convertType(notificationType)); new com.vaadin.ui.Notification(caption, description, convertType(notificationType));
setNotificationDelayMsec(vNotification, notificationType); if (hideDelayMs != DELAY_DEFAULT) {
vNotification.setDelayMsec(hideDelayMs);
} else {
setNotificationDelayMsec(vNotification, notificationType);
}
if (position != Position.DEFAULT) {
vNotification.setPosition(com.vaadin.shared.Position.valueOf(position.name()));
}
vNotification.setHtmlContentAllowed(contentMode == ContentMode.HTML); vNotification.setHtmlContentAllowed(contentMode == ContentMode.HTML);
vNotification.setStyleName(styleName); if (styleName != null) {
vNotification.setStyleName(styleName);
vNotification.setPosition(com.vaadin.shared.Position.valueOf(position.name())); }
vNotification.show(ui.getPage()); vNotification.show(ui.getPage());
} }

View File

@ -27,7 +27,7 @@ import spock.lang.Specification
import static com.haulmont.cuba.gui.Notifications.NotificationType.WARNING import static com.haulmont.cuba.gui.Notifications.NotificationType.WARNING
import static com.haulmont.cuba.gui.Notifications.Position.BOTTOM_CENTER import static com.haulmont.cuba.gui.Notifications.Position.BOTTOM_CENTER
class NotificationsSpec extends Specification { class NotificationsTest extends Specification {
@SuppressWarnings("GroovyPointlessBoolean") @SuppressWarnings("GroovyPointlessBoolean")
def "Notification can be show"() { def "Notification can be show"() {
@ -81,4 +81,22 @@ class NotificationsSpec extends Specification {
vNotification.styleName == 'open-notification' vNotification.styleName == 'open-notification'
vNotification.htmlContentAllowed == true vNotification.htmlContentAllowed == true
} }
def "Notification does not support ContentMode.PREFORMATTED"() {
def testBackgroundWorker = Mock(BackgroundWorker)
def notifications = new WebNotifications(new AppUI()) {
{
backgroundWorker = testBackgroundWorker
}
}
when:
def notification = notifications.create()
notification.setContentMode(ContentMode.PREFORMATTED)
then:
thrown UnsupportedOperationException
}
} }