Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
book-of-vaadin.pdf
Скачиваний:
88
Добавлен:
24.03.2015
Размер:
13.43 Mб
Скачать

Writing a Server-Side Web Application

shown in a large tooltip box, as illustrated in Figure 4.10, “Uncaught Exception in Component Error Indicator”.

Figure 4.10. Uncaught Exception in Component Error Indicator

You can customize the default error handling by implementing a custom ErrorHandler and enabling it with setErrorHandler() in any of the components in the component hierarchy, including the UI, or in the VaadinSession object. You can either implement the ErrorHandler or extend the DefaultErrorHandler. In the following example, we modify the behavior of the default handler.

//Here's some code that produces an uncaught exception final VerticalLayout layout = new VerticalLayout(); final Button button = new Button("Click Me!",

new Button.ClickListener() {

public void buttonClick(ClickEvent event) { ((String)null).length(); // Null-pointer exception

}

});

layout.addComponent(button);

//Configure the error handler for the UI

UI.getCurrent().setErrorHandler(new DefaultErrorHandler() { @Override

public void error(com.vaadin.server.ErrorEvent event) { // Find the final cause

String cause = "<b>The click failed because:</b><br/>"; for (Throwable t = event.getThrowable(); t != null;

t = t.getCause())

if (t.getCause() == null) // We're at final cause cause += t.getClass().getName() + "<br/>";

//Display the error message in a custom fashion layout.addComponent(new Label(cause, ContentMode.HTML));

//Do the default error handling (optional) doDefault(event);

}

});

The above example also demonstrates how to dig up the final cause from the cause stack.

4.6. Notifications

Notifications are error or information boxes that appear briefly, typically at the center of the screen. A notification box has a caption and an optional description and icon.The box stays on the screen either for a preset time or until the user clicks it. The notification type defines the default appearance and behaviour of a notification.

76

Notifications

Writing a Server-Side Web Application

There are two ways to create a notification. The easiest is to use a static shorthand Notification.show() method, which takes the caption of the notification as a parameter, and an optional description and notification type, and displays it in the current page.

Notification.show("This is the caption", "This is the description",

Notification.Type.WARNING_MESSAGE);

Figure 4.11. Notification

For more control, you can create a Notification object. Different constructors exist for taking just the caption, and optionally the description, notification type, and whether HTML is allowed or not. Notifications are shown in a Page, typically the current page.

new Notification("This is a warning", "<br/>This is the <i>last</i> warning", Notification.TYPE_WARNING_MESSAGE, true)

.show(Page.getCurrent());

The caption and description are by default written on the same line. If you want to have a line break between them, use the XHTML line break markup "<br/>" if HTML is enabled, or "\n" if not. HTML is disabled by default, but can be enabled with setHtmlContentAllowed(true). When enabled, you can use any XHTML markup in the caption and description of a notification. If it is in any way possible to get the notification content from user input, you should either disallow HTML or sanitize the content carefully, as noted in Section 11.8.1, “Sanitizing User Input to Prevent Cross-Site Scripting”.

Figure 4.12. Notification with HTML Formatting

4.6.1. Notification Type

The notification type defines the overall default style and behaviour of a notification. If no notification type is given, the "humanized" type is used as the default. The notification types, listed below, are defined in the Notification.Type class.

TYPE_HUMANIZED_MESSAGE

A user-friendly message that does not annoy too much: it does not require confirmation by clicking and disappears quickly. It is centered and has a neutral gray color.

Notification Type

77

Writing a Server-Side Web Application

TYPE_WARNING_MESSAGE

Warnings are messages of medium importance. They are displayed with colors that are neither neutral nor too distractive. A warning is displayed for 1.5 seconds, but the user can click the message box to dismiss it. The user can continue to interact with the application while the warning is displayed.

TYPE_ERROR_MESSAGE

Error messages are notifications that require the highest user attention, with alert colors, and they require the user to click the message to dismiss it. The error message box does not itself include an instruction to click the message, although the close box in the upper right corner indicates it visually. Unlike with other notifications, the user can not interact with the application while the error message is displayed.

TYPE_TRAY_NOTIFICATION

Tray notifications are displayed in the "system tray" area, that is, in the lower-right corner of the browser view. As they do not usually obscure any user interface, they are displayed longer than humanized or warning messages, 3 seconds by default.The user can continue to interact with the application normally while the tray notification is displayed.

4.6.2. Customizing Notifications

All of the features of specific notification types can be controlled with the Notification properties. Once configured, you need to show it in the current page.

//Notification with default settings for a warning Notification notif = new Notification(

"Warning",

"<br/>Area of reindeer husbandry", Notification.TYPE_WARNING_MESSAGE);

//Customize it

notif.setDelayMsec(20000); notif.setPosition(Position.BOTTOM_RIGHT); notif.setStyleName("mystyle");

notif.setIcon(new ThemeResource("img/reindeer.png"));

// Show it in the page notif.show(Page.getCurrent());

The setPosition() method allows setting the positioning of the notification. The position can be specified by any of the constants defined in the Position enum.

The setDelayMSec() allows setting the time for how long the notification is displayed in milliseconds. Parameter value -1 means that the message is displayed until the user clicks the message box. It also prevents interaction with other parts of the application window, which is the default behaviour for error notifications. It does not, however, add a close box that the error notification has.

78

Customizing Notifications

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]