From a614a722e050163c30190f7d811deb23ae5b81f8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Thu, 22 Dec 2011 14:00:02 +0200 Subject: [PATCH] Code for some of the mini tutorials --- .../tests/minitutorials/FormatTableValue.java | 60 ++++++++++ .../IntegerTextFieldDataSource.java | 69 +++++++++++ .../IntegerTextFieldStandalone.java | 57 +++++++++ .../minitutorials/StringMyTypeConverter.java | 108 ++++++++++++++++++ 4 files changed, 294 insertions(+) create mode 100644 tests/testbench/com/vaadin/tests/minitutorials/FormatTableValue.java create mode 100644 tests/testbench/com/vaadin/tests/minitutorials/IntegerTextFieldDataSource.java create mode 100644 tests/testbench/com/vaadin/tests/minitutorials/IntegerTextFieldStandalone.java create mode 100644 tests/testbench/com/vaadin/tests/minitutorials/StringMyTypeConverter.java diff --git a/tests/testbench/com/vaadin/tests/minitutorials/FormatTableValue.java b/tests/testbench/com/vaadin/tests/minitutorials/FormatTableValue.java new file mode 100644 index 0000000000..aba1447d2c --- /dev/null +++ b/tests/testbench/com/vaadin/tests/minitutorials/FormatTableValue.java @@ -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 index 0000000000..5c0efea3ea --- /dev/null +++ b/tests/testbench/com/vaadin/tests/minitutorials/IntegerTextFieldDataSource.java @@ -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 beanItem = new BeanItem(myBean); + + final Property integerProperty = (Property) 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 + + "
Property value (Integer): " + + propertyValue + + "
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 index 0000000000..45bc49ba72 --- /dev/null +++ b/tests/testbench/com/vaadin/tests/minitutorials/IntegerTextFieldStandalone.java @@ -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 + + "
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 index 0000000000..e28788f743 --- /dev/null +++ b/tests/testbench/com/vaadin/tests/minitutorials/StringMyTypeConverter.java @@ -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() + + "
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 { + 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 getModelType() { + return Name.class; + } + + public Class 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; + } +} -- 2.39.5