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";
int DELAY_FOREVER = -1;
int DELAY_NONE = 0;
int DELAY_DEFAULT = Integer.MIN_VALUE;
/**
* Creates empty notification object.
*
* @return notification
*/
Notification create();
/**
* Notification object.
*/
interface Notification {
/**
* Sets notification caption.
*
* @param caption caption
* @return this
*/
Notification setCaption(String caption);
/**
* @return caption
*/
String getCaption();
/**
* Sets notification description.
*
* @param description description
* @return this
*/
Notification setDescription(String description);
/**
* @return description
*/
String getDescription();
Notification setType(NotificationType notificationType);
/**
* Sets type of notification.
*
* @param type type
* @return this
*/
Notification setType(NotificationType type);
/**
* @return type
*/
NotificationType getType();
/**
* Sets content mode for caption and description of notification.
*
* @param contentMode content mode
* @return this
*/
Notification setContentMode(ContentMode contentMode);
/**
* @return content mode
*/
ContentMode getContentMode();
/**
* Sets CSS class name for notification DOM element.
*
* @param styleName CSS class name
* @return this
*/
Notification setStyleName(String styleName);
/**
* @return CSS class name
*/
String getStyleName();
/**
* Sets position of notification.
*
* @param position position
* @return this
*/
Notification setPosition(Position position);
/**
* @return position
*/
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.
*/
@ -70,6 +145,11 @@ public interface Notifications {
* Popup notification position.
*/
enum Position {
/**
* Set position by notification type
*/
DEFAULT,
TOP_LEFT, TOP_CENTER, TOP_RIGHT,
MIDDLE_LEFT, MIDDLE_CENTER, MIDDLE_RIGHT,
BOTTOM_LEFT, BOTTOM_CENTER, BOTTOM_RIGHT

View File

@ -30,8 +30,8 @@ import javax.inject.Inject;
@Scope(UIScope.NAME)
public class WebNotifications implements Notifications {
public static final int HUMANIZED_NOTIFICATION_DELAY_MSEC = 3000;
public static final int WARNING_NOTIFICATION_DELAY_MSEC = -1;
protected static final int HUMANIZED_NOTIFICATION_DELAY_MSEC = 3000;
protected static final int WARNING_NOTIFICATION_DELAY_MSEC = -1;
protected AppUI ui;
@ -58,7 +58,8 @@ public class WebNotifications implements Notifications {
protected String description;
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 NotificationType notificationType = NotificationType.HUMANIZED;
@ -92,10 +93,6 @@ public class WebNotifications implements Notifications {
public Notification setType(NotificationType notificationType) {
this.notificationType = notificationType;
// todo set default position here
// todo set default timeout here
return this;
}
@ -106,6 +103,10 @@ public class WebNotifications implements Notifications {
@Override
public Notification setContentMode(ContentMode contentMode) {
if (contentMode == ContentMode.PREFORMATTED) {
throw new UnsupportedOperationException("ContentMode.PREFORMATTED unsupported for Notification");
}
this.contentMode = contentMode;
return this;
}
@ -137,6 +138,17 @@ public class WebNotifications implements Notifications {
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) {
switch (notificationType) {
case TRAY:
@ -172,12 +184,20 @@ public class WebNotifications implements Notifications {
com.vaadin.ui.Notification vNotification =
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.setStyleName(styleName);
vNotification.setPosition(com.vaadin.shared.Position.valueOf(position.name()));
if (styleName != null) {
vNotification.setStyleName(styleName);
}
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.Position.BOTTOM_CENTER
class NotificationsSpec extends Specification {
class NotificationsTest extends Specification {
@SuppressWarnings("GroovyPointlessBoolean")
def "Notification can be show"() {
@ -81,4 +81,22 @@ class NotificationsSpec extends Specification {
vNotification.styleName == 'open-notification'
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
}
}