]> source.dussan.org Git - vaadin-framework.git/commitdiff
Code for some of the mini tutorials
authorLeif Åstrand <leif@vaadin.com>
Thu, 22 Dec 2011 12:00:02 +0000 (14:00 +0200)
committerLeif Åstrand <leif@vaadin.com>
Thu, 22 Dec 2011 13:01:10 +0000 (15:01 +0200)
tests/testbench/com/vaadin/tests/minitutorials/FormatTableValue.java [new file with mode: 0644]
tests/testbench/com/vaadin/tests/minitutorials/IntegerTextFieldDataSource.java [new file with mode: 0644]
tests/testbench/com/vaadin/tests/minitutorials/IntegerTextFieldStandalone.java [new file with mode: 0644]
tests/testbench/com/vaadin/tests/minitutorials/StringMyTypeConverter.java [new file with mode: 0644]

diff --git a/tests/testbench/com/vaadin/tests/minitutorials/FormatTableValue.java b/tests/testbench/com/vaadin/tests/minitutorials/FormatTableValue.java
new file mode 100644 (file)
index 0000000..aba1447
--- /dev/null
@@ -0,0 +1,60 @@
+package com.vaadin.tests.minitutorials;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+
+import com.vaadin.data.util.converter.StringToNumberConverter;
+import com.vaadin.terminal.WrappedRequest;
+import com.vaadin.tests.components.AbstractTestRoot;
+import com.vaadin.ui.Table;
+
+public class FormatTableValue extends AbstractTestRoot {
+
+    private static final String PERCENT_PROPERTY = "percent";
+    private static final String CURRENCY_PROPERTY = "currency";
+    private static final String DEFAULT_PROPERTY = "default";
+
+    @Override
+    protected void setup(WrappedRequest request) {
+        Table table = new Table();
+        table.setLocale(Locale.FRANCE);
+        table.addContainerProperty(PERCENT_PROPERTY, Double.class, 0);
+        table.addContainerProperty(CURRENCY_PROPERTY, Double.class, 0);
+        table.addContainerProperty(DEFAULT_PROPERTY, Double.class, 0);
+
+        Object itemId = table.addItem();
+        table.getItem(itemId).getItemProperty(PERCENT_PROPERTY)
+                .setValue(3.1415);
+        table.getItem(itemId).getItemProperty(CURRENCY_PROPERTY)
+                .setValue(3.1415);
+        table.getItem(itemId).getItemProperty(DEFAULT_PROPERTY)
+                .setValue(3.1415);
+
+        table.setConverter(PERCENT_PROPERTY, new StringToNumberConverter() {
+            @Override
+            protected NumberFormat getFormat(Locale locale) {
+                return NumberFormat.getPercentInstance(locale);
+            }
+        });
+
+        table.setConverter(CURRENCY_PROPERTY, new StringToNumberConverter() {
+            @Override
+            protected NumberFormat getFormat(Locale locale) {
+                return NumberFormat.getCurrencyInstance(locale);
+            }
+        });
+
+        addComponent(table);
+    }
+
+    @Override
+    protected String getTestDescription() {
+        return "Mini tutorial for https://vaadin.com/wiki/-/wiki/Main/Formatting%20data%20in%20Table";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return null;
+    }
+
+}
diff --git a/tests/testbench/com/vaadin/tests/minitutorials/IntegerTextFieldDataSource.java b/tests/testbench/com/vaadin/tests/minitutorials/IntegerTextFieldDataSource.java
new file mode 100644 (file)
index 0000000..5c0efea
--- /dev/null
@@ -0,0 +1,69 @@
+package com.vaadin.tests.minitutorials;
+
+import com.vaadin.data.Property;
+import com.vaadin.data.util.BeanItem;
+import com.vaadin.terminal.WrappedRequest;
+import com.vaadin.tests.components.AbstractTestRoot;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.Button.ClickListener;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.Root;
+import com.vaadin.ui.TextField;
+
+public class IntegerTextFieldDataSource extends AbstractTestRoot {
+
+    public class MyBean {
+        private int value;
+
+        public int getValue() {
+            return value;
+        }
+
+        public void setValue(int integer) {
+            value = integer;
+        }
+    }
+
+    @Override
+    protected void setup(WrappedRequest request) {
+        final MyBean myBean = new MyBean();
+        BeanItem<MyBean> beanItem = new BeanItem<MyBean>(myBean);
+
+        final Property<Integer> integerProperty = (Property<Integer>) beanItem
+                .getItemProperty("value");
+        final TextField textField = new TextField("Text field", integerProperty);
+
+        Button submitButton = new Button("Submit value", new ClickListener() {
+            public void buttonClick(ClickEvent event) {
+                String uiValue = textField.getValue();
+                Integer propertyValue = integerProperty.getValue();
+                int dataModelValue = myBean.getValue();
+
+                Root.getCurrentRoot().showNotification(
+                        "UI value (String): " + uiValue
+                                + "<br />Property value (Integer): "
+                                + propertyValue
+                                + "<br />Data model value (int): "
+                                + dataModelValue);
+            }
+        });
+
+        addComponent(new Label("Text field type: " + textField.getType()));
+        addComponent(new Label("Text field type: " + integerProperty.getType()));
+        addComponent(textField);
+        addComponent(submitButton);
+    }
+
+    @Override
+    protected String getTestDescription() {
+        return "Mini tutorial for https://vaadin.com/wiki/-/wiki/Main/Creating%20a%20TextField%20for%20Integer%20only%20input%20using%20a%20data%20source";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+}
diff --git a/tests/testbench/com/vaadin/tests/minitutorials/IntegerTextFieldStandalone.java b/tests/testbench/com/vaadin/tests/minitutorials/IntegerTextFieldStandalone.java
new file mode 100644 (file)
index 0000000..45bc49b
--- /dev/null
@@ -0,0 +1,57 @@
+package com.vaadin.tests.minitutorials;
+
+import com.vaadin.data.util.converter.Converter.ConversionException;
+import com.vaadin.data.util.converter.StringToIntegerConverter;
+import com.vaadin.terminal.WrappedRequest;
+import com.vaadin.tests.components.AbstractTestRoot;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.Button.ClickListener;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.Root;
+import com.vaadin.ui.TextField;
+
+public class IntegerTextFieldStandalone extends AbstractTestRoot {
+
+    @Override
+    protected void setup(WrappedRequest request) {
+        final TextField textField = new TextField("Text field");
+        textField.setConverter(new StringToIntegerConverter());
+
+        Button submitButton = new Button("Submit value", new ClickListener() {
+            public void buttonClick(ClickEvent event) {
+                String uiValue = textField.getValue();
+                try {
+                    Integer convertedValue = (Integer) textField
+                            .getConvertedValue();
+                    Root.getCurrentRoot().showNotification(
+                            "UI value (String): " + uiValue
+                                    + "<br />Converted value (Integer): "
+                                    + convertedValue);
+                } catch (ConversionException e) {
+                    e.printStackTrace();
+                    Root.getCurrentRoot().showNotification(
+                            "Could not convert value: " + uiValue);
+                }
+            }
+        });
+
+        addComponent(new Label("Text field type: " + textField.getType()));
+        addComponent(new Label("Converterd text field type: "
+                + textField.getConverter().getModelType()));
+        addComponent(textField);
+        addComponent(submitButton);
+    }
+
+    @Override
+    protected String getTestDescription() {
+        return "Mini tutorial for https://vaadin.com/wiki/-/wiki/Main/Creating%20a%20TextField%20for%20Integer%20only%20input%20when%20not%20using%20a%20data%20source";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+}
diff --git a/tests/testbench/com/vaadin/tests/minitutorials/StringMyTypeConverter.java b/tests/testbench/com/vaadin/tests/minitutorials/StringMyTypeConverter.java
new file mode 100644 (file)
index 0000000..e28788f
--- /dev/null
@@ -0,0 +1,108 @@
+package com.vaadin.tests.minitutorials;
+
+import java.util.Locale;
+
+import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.Converter.ConversionException;
+import com.vaadin.terminal.WrappedRequest;
+import com.vaadin.tests.components.AbstractTestRoot;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.Button.ClickListener;
+import com.vaadin.ui.TextField;
+
+public class StringMyTypeConverter extends AbstractTestRoot {
+
+    @Override
+    protected void setup(WrappedRequest request) {
+        Name name = new Name("Rudolph", "Reindeer");
+
+        final TextField textField = new TextField("Name");
+        textField.setConverter(new StringToNameConverter());
+        textField.setConvertedValue(name);
+
+        addComponent(textField);
+        addComponent(new Button("Submit value", new ClickListener() {
+            public void buttonClick(ClickEvent event) {
+                try {
+                    Name name = (Name) textField.getConvertedValue();
+                    getRoot().showNotification(
+                            "First name: " + name.getFirstName()
+                                    + "<br />Last name: " + name.getLastName());
+                } catch (ConversionException e) {
+                    e.printStackTrace();
+                    getRoot().showNotification(e.getCause().getMessage());
+                }
+            }
+        }));
+    }
+
+    @Override
+    protected String getTestDescription() {
+        return "Mini tutorial for https://vaadin.com/wiki/-/wiki/Main/Creating%20your%20own%20converter%20for%20String%20-%20MyType%20conversion";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return null;
+    }
+
+}
+
+class StringToNameConverter implements Converter<String, Name> {
+    public Name convertToModel(String text, Locale locale)
+            throws ConversionException {
+        if (text == null) {
+            return null;
+        }
+        String[] parts = text.split(" ");
+        if (parts.length != 2) {
+            throw new ConversionException("Can not convert text to a name: "
+                    + text);
+        }
+        return new Name(parts[0], parts[1]);
+    }
+
+    public String convertToPresentation(Name name, Locale locale)
+            throws ConversionException {
+        if (name == null) {
+            return null;
+        } else {
+            return name.getFirstName() + " " + name.getLastName();
+        }
+    }
+
+    public Class<Name> getModelType() {
+        return Name.class;
+    }
+
+    public Class<String> getPresentationType() {
+        return String.class;
+    }
+}
+
+class Name {
+    private String firstName;
+    private String lastName;
+
+    public Name(String firstName, String lastName) {
+        this.firstName = firstName;
+        this.lastName = lastName;
+    }
+
+    public String getFirstName() {
+        return firstName;
+    }
+
+    public void setFirstName(String firstName) {
+        this.firstName = firstName;
+    }
+
+    public String getLastName() {
+        return lastName;
+    }
+
+    public void setLastName(String lastName) {
+        this.lastName = lastName;
+    }
+}