|
All components have a built-in error indicator that can be set explicitly with The following example shows how you can set the component error explicitly. The example essentially validates field value without using an actual validator. // Create a field.
final TextField textfield = new TextField("Enter code");
main.addComponent(textfield);
// Let the component error be initially clear.
textfield.setComponentError(null); // (actually the default)
// Have a button right of the field (and align it properly).
final Button button = new Button("Ok!");
main.addComponent(button);
((VerticalLayout)main.getLayout())
.setComponentAlignment(button, Alignment.BOTTOM_LEFT);
// Handle button clicks
button.addListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
// If the field value is bad, set its error.
// (Allow only alphanumeric characters.)
if (! ((String) textfield.getValue()).matches("^\\w*$")) {
// Put the component in error state and
// set the error message.
textfield.setComponentError(
new UserError("Must be letters and numbers"));
} else {
// Otherwise clear it.
textfield.setComponentError(null);
}
}
}); The Notifications are error or information boxes that appear typically at the center of the screen. A notification box has a caption and optional description and icon. The box stays on the screen either for a defined time or until the user clicks it. The notification type defines the default appearance and behaviour of a notification. Notifications are always associated with a window object, which can be a child window (the positioning is always relative to the entire browser view). The mainwindow.showNotification("This is the caption",
"This is the description"); 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 " main.showNotification("This is a warning",
"<br/>This is the <i>last</i> warning",
Window.Notification.TYPE_WARNING_MESSAGE); 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
All of the features of specific notification types can be controlled with the attributes of // Create a notification with default settings for a warning.
Window.Notification notif = new Window.Notification(
"Be warned!",
"This message lurks in the top-left corner!",
Window.Notification.TYPE_WARNING_MESSAGE);
// Set the position.
notif.setPosition(Window.Notification.POSITION_TOP_LEFT);
// Let it stay there until the user clicks it
notif.setDelayMsec(-1);
// Show it in the main window.
main.showNotification(notif); The
The Application development with Vaadin follows the event-driven programming model. Mouse and keyboard events in the client cause (usually higher-level) events on the server-side, which can be handled with listeners, and that is how most of the application logic works. Handling the events can result in exceptions either in the application logic or in the framework itself, but some of them may not be caught properly. For example, in the following code excerpt, we throw an error in an event listener but do not catch it, so it falls to the framework. final Button button = new Button ("Fail Me");
button.addListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
// Throw some exception.
throw new RuntimeException("You can't catch this.");
}
});
Any such exceptions that occur in the call chain, but are not caught at
any other level, are eventually caught by the terminal adapter in
The reason for this error-handling logic lies in the logic that handles component state synchronization between the client and the server. We want to handle all the serialized variable changes in the client request, because otherwise the client-side and server-side component states would become unsynchronized very easily, which could put the entire application in an invalid state. The default implementation of the In UI, the component error is shown with a small red "!" -sign (in the default theme). If you hover the mouse pointer over it, you will see the entire backtrace of the exception in a large tooltip box, as illustrated in Figure 4.9, “Uncaught Exception in Component Error Indicator” for the above code example. You can change the logic of handling the terminal errors easily by overriding the @Override
public void terminalError(Terminal.ErrorEvent event) {
// Call the default implementation.
super.terminalError(event);
// Some custom behaviour.
if (getMainWindow() != null) {
getMainWindow().showNotification(
"An unchecked exception occured!",
event.getThrowable().toString(),
Notification.TYPE_ERROR_MESSAGE);
}
} Handling other exceptions works in the usual way for Java Servlets. Uncaught exceptions are finally caught and handled by the application server. |
Table of Contents
|