From a98b9fcc14b8fc8a9a03320e298e16c0467f1457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Mon, 26 Sep 2011 15:42:25 +0200 Subject: vaadin-app: split source code => 1 class / file --- .../src/ch/asynk/helloworld/DataMappingLayout.java | 140 +++++++++++++++ vaadin-app/src/ch/asynk/helloworld/EndLayout.java | 26 +++ .../src/ch/asynk/helloworld/HelloWorldApp.java | 196 +-------------------- vaadin-app/src/ch/asynk/helloworld/MainLayout.java | 47 +++++ 4 files changed, 214 insertions(+), 195 deletions(-) create mode 100644 vaadin-app/src/ch/asynk/helloworld/DataMappingLayout.java create mode 100644 vaadin-app/src/ch/asynk/helloworld/EndLayout.java create mode 100644 vaadin-app/src/ch/asynk/helloworld/MainLayout.java diff --git a/vaadin-app/src/ch/asynk/helloworld/DataMappingLayout.java b/vaadin-app/src/ch/asynk/helloworld/DataMappingLayout.java new file mode 100644 index 0000000..ee0382e --- /dev/null +++ b/vaadin-app/src/ch/asynk/helloworld/DataMappingLayout.java @@ -0,0 +1,140 @@ +package ch.asynk.helloworld; + +import com.vaadin.terminal.UserError; +import com.vaadin.data.Property; +import com.vaadin.data.Property.ValueChangeEvent; +import com.vaadin.data.util.ObjectProperty; +import com.vaadin.data.Validator; +import com.vaadin.data.validator.DoubleValidator; +import com.vaadin.ui.FormLayout; +import com.vaadin.ui.Label; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.TextField; + +public 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 + 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); + } + } + }); + // + // final Validator intValidator = new Validator() { ... } + class MyIntValidator implements Validator { + public boolean isValid(Object value) { + if (value == null || !(value instanceof String)) { + return false; + } + return ((String) value).matches("[1-9][0-9]{4}"); + } + public void validate(Object value) throws InvalidValueException { + if (!isValid(value)) { + if (value != null && value.toString().startsWith("0")) { + throw new InvalidValueException("Must not start with a zero."); + } else { + throw new InvalidValueException("Must be a number 10000-99999."); + } + } + } + }; + final TextField tf5 = new TextField("MyIntValidator"); + tf5.addValidator(new MyIntValidator()); + tf5.setImmediate(true); + addComponent(tf5); + // + 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 nextButton = new Button("end layout"); + nextButton.setDescription("Go to end layout"); + addComponent(nextButton ); + nextButton.addListener( new Button.ClickListener() { + @Override + public void buttonClick(ClickEvent event) { + getApplication().getMainWindow().setContent( new EndLayout() ); + } + }); + } +} diff --git a/vaadin-app/src/ch/asynk/helloworld/EndLayout.java b/vaadin-app/src/ch/asynk/helloworld/EndLayout.java new file mode 100644 index 0000000..a88de35 --- /dev/null +++ b/vaadin-app/src/ch/asynk/helloworld/EndLayout.java @@ -0,0 +1,26 @@ +package ch.asynk.helloworld; + +import com.vaadin.ui.VerticalLayout; +import com.vaadin.ui.Label; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; + +public class EndLayout extends VerticalLayout { + + public EndLayout() { + // + final Label lb1 = new Label(); + lb1.setCaption("That's all folks"); + addComponent(lb1); + // + 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(); + } + }); + } +}; diff --git a/vaadin-app/src/ch/asynk/helloworld/HelloWorldApp.java b/vaadin-app/src/ch/asynk/helloworld/HelloWorldApp.java index 19f9fbd..dbd133d 100644 --- a/vaadin-app/src/ch/asynk/helloworld/HelloWorldApp.java +++ b/vaadin-app/src/ch/asynk/helloworld/HelloWorldApp.java @@ -3,211 +3,17 @@ 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.UserError; 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; -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 EndLayout extends VerticalLayout { - - public EndLayout() { - // - final Label lb1 = new Label(); - lb1.setCaption("That's all folks"); - addComponent(lb1); - // - 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 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 + 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); - } - } - }); - // - // final Validator intValidator = new Validator() { ... } - class MyIntValidator implements Validator { - public boolean isValid(Object value) { - if (value == null || !(value instanceof String)) { - return false; - } - return ((String) value).matches("[1-9][0-9]{4}"); - } - public void validate(Object value) throws InvalidValueException { - if (!isValid(value)) { - if (value != null && value.toString().startsWith("0")) { - throw new InvalidValueException("Must not start with a zero."); - } else { - throw new InvalidValueException("Must be a number 10000-99999."); - } - } - } - }; - final TextField tf5 = new TextField("MyIntValidator"); - tf5.addValidator(new MyIntValidator()); - tf5.setImmediate(true); - addComponent(tf5); - // - 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 nextButton = new Button("end layout"); - nextButton.setDescription("Go to end layout"); - addComponent(nextButton ); - nextButton.addListener( new Button.ClickListener() { - @Override - public void buttonClick(ClickEvent event) { - getApplication().getMainWindow().setContent( new EndLayout() ); - } - }); - } -} - -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; diff --git a/vaadin-app/src/ch/asynk/helloworld/MainLayout.java b/vaadin-app/src/ch/asynk/helloworld/MainLayout.java new file mode 100644 index 0000000..e5e364e --- /dev/null +++ b/vaadin-app/src/ch/asynk/helloworld/MainLayout.java @@ -0,0 +1,47 @@ +package ch.asynk.helloworld; + + +import java.util.Date; + +import com.vaadin.terminal.UserError; +import com.vaadin.ui.VerticalLayout; +import com.vaadin.ui.Label; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Window.Notification; + +public 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",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() ); + } + }); + } + +} -- cgit v1.1-2-g2b99