summaryrefslogtreecommitdiffstats
path: root/vaadin-app/src/ch/asynk/helloworld/HelloWorldApp.java
blob: badf22302c5a2c678a69d724e747fa45c13db183 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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<Double> property = new ObjectProperty<Double>(trouble);
        final TextField tf2 = new TextField("ObjectProperty + Validator", 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);
        //
        final TextField tf3 = new TextField("No spaces");
        addComponent(tf3);
        tf3.setComponentError(null); // (actually the default)
        final Button b1 = new Button("Ok!");
        addComponent(b1);
        b1.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                if (! ((String) tf3.getValue()).matches("^\\w*$")) {
                    tf3.setComponentError(new UserError("Must be letters and numbers"));
                } else {
                    tf3.setComponentError(null);
                }
            }
        });
        //
        class IntegerObjectProperty implements Property {
            Integer data = 0;
            boolean readOnly = false;
            public Class<?> getType() {
                return Integer.class;
            }
            public Object getValue() {
                return data;
            }
            public boolean isReadOnly() {
                return readOnly;
            }
            public void setReadOnly(boolean newStatus) {
                readOnly = newStatus;
            }
            public void setValue(Object newValue) throws ReadOnlyException, ConversionException {
                if (readOnly)
                    throw new ReadOnlyException();
                if (newValue instanceof Integer)
                    data = (Integer) newValue;
                else if (newValue instanceof String)
                    try {
                        data = Integer.parseInt((String) newValue, 16);
                    } catch (NumberFormatException e) {
                        throw new ConversionException("Must be an integer (from property)");
                    }
                else
                    throw new ConversionException("Unknown entry type (from property)");
            }
            @Override
            public String toString() {
                return Integer.toHexString(data)+" (hex)";
            }
        };
        final IntegerObjectProperty intProperty = new IntegerObjectProperty();
        intProperty.setValue(666);
        final TextField tf4 = new TextField("MyObjectProperty",intProperty);
        tf4.setImmediate(true);
        tf4.setErrorHandler(new ComponentErrorHandler() {
            @Override
            public boolean handleComponentError(ComponentErrorEvent event) {
                tf4.setComponentError(new UserError("Must be an integer (from handler)"));
                return true;
            }
        });
        addComponent(tf4);
        //
        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(), "<i>italic</i> <b>bold</b> 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);
        }
    }
}