package ch.asynk.helloworld; import java.util.Date; import com.vaadin.Application; import com.vaadin.terminal.UserError; import com.vaadin.terminal.Terminal; import com.vaadin.terminal.SystemError; import com.vaadin.terminal.ErrorMessage; import com.vaadin.terminal.VariableOwner; import com.vaadin.terminal.URIHandler; import com.vaadin.terminal.ParameterHandler; import com.vaadin.terminal.gwt.server.ChangeVariablesErrorEvent; import com.vaadin.data.Property; import com.vaadin.data.Property.ValueChangeEvent; import com.vaadin.data.util.ObjectProperty; import com.vaadin.data.validator.DoubleValidator; import com.vaadin.ui.FormLayout; import com.vaadin.ui.VerticalLayout; import com.vaadin.ui.Window; import com.vaadin.ui.Label; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.TextField; import com.vaadin.ui.AbstractComponent; class DataMappingLayout extends FormLayout { public DataMappingLayout() { // final TextField tf1 = new TextField("Property.ValueChangeListener"); tf1.setImmediate(true); addComponent(tf1); final Label lb1 = new Label(); lb1.setCaption("The Value"); addComponent(lb1); tf1.addListener( new Property.ValueChangeListener() { @Override public void valueChange(ValueChangeEvent event) { String value = (String) tf1.getValue(); lb1.setValue(value); } }); // Double trouble = 66.6; final ObjectProperty property = new ObjectProperty(trouble); final TextField tf2 = new TextField("ObjectProperty", property); tf2.setImmediate(true); tf2.addValidator( new DoubleValidator("It should be a double") ); //tf2.setValidationVisible(false); // nothing will happen addComponent(tf2); final Label lb2 = new Label(property); lb2.setCaption("The Value"); addComponent(lb2); // Button closeButton = new Button("close the application"); closeButton.setDescription("This will close the application"); addComponent(closeButton ); closeButton.addListener( new Button.ClickListener() { @Override public void buttonClick(ClickEvent event) { getApplication().getMainWindow().getApplication().close(); } }); } } class MainLayout extends VerticalLayout { public MainLayout() { Label label = new Label("Hello world"); addComponent(label); // addComponent( new Button("What is the time?", new Button.ClickListener() { @Override public void buttonClick(ClickEvent event) { getApplication().getMainWindow().showNotification("The time is " + new Date(), "italic bold description",Window.Notification.TYPE_WARNING_MESSAGE); event.getButton().setComponentError( new UserError("Stop pressing this button !!") ); } })); /// Button exceptionButton = new Button("throw an exception"); addComponent(exceptionButton ); exceptionButton.addListener( new Button.ClickListener() { @Override public void buttonClick(ClickEvent event) { throw new RuntimeException("Wahou !! : exception"); } }); // Button nextButton = new Button("next layout"); nextButton.setDescription("Go to next layout"); addComponent(nextButton ); nextButton.addListener( new Button.ClickListener() { @Override public void buttonClick(ClickEvent event) { getApplication().getMainWindow().setContent( new DataMappingLayout() ); } }); } } public class HelloWorldApp extends Application { private static final long serialVersionUID = 1L; @Override public void init() { final Window mainWindow = new Window("Hello World Application"); mainWindow.setContent( new MainLayout() ); setMainWindow(mainWindow); setLogoutURL("http://asynk.ch"); } @Override public void terminalError(Terminal.ErrorEvent event) { // Call the default implementation. //super.terminalError(event); // http://dev.vaadin.com/browser/releases/6.6.6/src/com/vaadin/Application.java#L1190 final Throwable t = event.getThrowable(); Object owner = null; if (event instanceof VariableOwner.ErrorEvent) { owner = ((VariableOwner.ErrorEvent) event).getVariableOwner(); } else if (event instanceof URIHandler.ErrorEvent) { owner = ((URIHandler.ErrorEvent) event).getURIHandler(); } else if (event instanceof ParameterHandler.ErrorEvent) { owner = ((ParameterHandler.ErrorEvent) event).getParameterHandler(); } else if (event instanceof ChangeVariablesErrorEvent) { owner = ((ChangeVariablesErrorEvent) event).getComponent(); } if (owner instanceof AbstractComponent) { if (t instanceof ErrorMessage) { ((AbstractComponent) owner).setComponentError((ErrorMessage)t); } else { //((AbstractComponent) owner).setComponentError(new SystemError(t)); ((AbstractComponent) owner).setComponentError(new UserError("User Error!")); } } // Some custom behaviour. if (getMainWindow() != null) { getMainWindow().showNotification("An unchecked exception occured!", event.getThrowable().toString(), Window.Notification.TYPE_ERROR_MESSAGE); } } }