From 47adbf4e327896791d7adfd7e8c270d524d754ac Mon Sep 17 00:00:00 2001 From: Matti Tahvonen Date: Wed, 16 Nov 2011 13:17:56 +0000 Subject: [PATCH] Form and Table now sets property to fields Viewer property if one exists. fixes #7942 svn changeset:22019/svn branch:6.7 --- .../vaadin/data/util/PropertyFormatter.java | 2 +- src/com/vaadin/ui/Form.java | 30 ++++++- src/com/vaadin/ui/Table.java | 29 +++++- .../FormWithPropertyFormatterConnected.java | 90 +++++++++++++++++++ 4 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 tests/testbench/com/vaadin/tests/components/form/FormWithPropertyFormatterConnected.java diff --git a/src/com/vaadin/data/util/PropertyFormatter.java b/src/com/vaadin/data/util/PropertyFormatter.java index 3afb1cede8..18e1890259 100644 --- a/src/com/vaadin/data/util/PropertyFormatter.java +++ b/src/com/vaadin/data/util/PropertyFormatter.java @@ -33,7 +33,7 @@ import com.vaadin.data.Property; * @since 5.3.0 */ @SuppressWarnings("serial") -public abstract class PropertyFormatter extends AbstractProperty implements +public abstract class PropertyFormatter extends AbstractProperty implements Property.Viewer, Property.ValueChangeListener, Property.ReadOnlyStatusChangeListener { /** Datasource that stores the actual value. */ diff --git a/src/com/vaadin/ui/Form.java b/src/com/vaadin/ui/Form.java index 8ee702bbb4..1a28c679f9 100644 --- a/src/com/vaadin/ui/Form.java +++ b/src/com/vaadin/ui/Form.java @@ -491,7 +491,7 @@ public class Form extends AbstractField implements Item.Editor, Buffered, Item, } // Configures the field - field.setPropertyDataSource(property); + bindPropertyToField(id, property, field); // Register and attach the created field addField(id, field); @@ -755,13 +755,39 @@ public class Form extends AbstractField implements Item.Editor, Buffered, Item, final Field f = fieldFactory.createField(itemDatasource, id, this); if (f != null) { - f.setPropertyDataSource(property); + bindPropertyToField(id, property, f); addField(id, f); } } } } + /** + * Binds an item property to a field. The default behavior is to bind + * property straight to Field. If Property.Viewer type property (e.g. + * PropertyFormatter) is already set for field, the property is bound to + * that Property.Viewer. + * + * @param propertyId + * @param property + * @param field + * @since 6.7.3 + */ + protected void bindPropertyToField(final Object propertyId, + final Property property, final Field field) { + // check if field has a property that is Viewer set. In that case we + // expect developer has e.g. PropertyFormatter that he wishes to use and + // assign the property to the Viewer instead. + boolean hasFilterProperty = field.getPropertyDataSource() != null + && (field.getPropertyDataSource() instanceof Property.Viewer); + if (hasFilterProperty) { + ((Property.Viewer) field.getPropertyDataSource()) + .setPropertyDataSource(property); + } else { + field.setPropertyDataSource(property); + } + } + /** * Gets the layout of the form. * diff --git a/src/com/vaadin/ui/Table.java b/src/com/vaadin/ui/Table.java index 5efb2545e0..c16505e57f 100644 --- a/src/com/vaadin/ui/Table.java +++ b/src/com/vaadin/ui/Table.java @@ -3405,7 +3405,7 @@ public class Table extends AbstractSelect implements Action.Container, // Remember that we have made this association so we can remove // it when the component is removed associatedProperties.put(f, property); - f.setPropertyDataSource(property); + bindPropertyToField(rowId, colId, property, f); return f; } } @@ -3413,6 +3413,33 @@ public class Table extends AbstractSelect implements Action.Container, return formatPropertyValue(rowId, colId, property); } + /** + * Binds an item property to a field generated by TableFieldFactory. The + * default behavior is to bind property straight to Field. If + * Property.Viewer type property (e.g. PropertyFormatter) is already set for + * field, the property is bound to that Property.Viewer. + * + * @param rowId + * @param colId + * @param property + * @param field + * @since 6.7.3 + */ + protected void bindPropertyToField(Object rowId, Object colId, + Property property, Field field) { + // check if field has a property that is Viewer set. In that case we + // expect developer has e.g. PropertyFormatter that he wishes to use and + // assign the property to the Viewer instead. + boolean hasFilterProperty = field.getPropertyDataSource() != null + && (field.getPropertyDataSource() instanceof Property.Viewer); + if (hasFilterProperty) { + ((Property.Viewer) field.getPropertyDataSource()) + .setPropertyDataSource(property); + } else { + field.setPropertyDataSource(property); + } + } + /** * Formats table cell property values. By default the property.toString() * and return a empty string for null properties. diff --git a/tests/testbench/com/vaadin/tests/components/form/FormWithPropertyFormatterConnected.java b/tests/testbench/com/vaadin/tests/components/form/FormWithPropertyFormatterConnected.java new file mode 100644 index 0000000000..116dd5bcc7 --- /dev/null +++ b/tests/testbench/com/vaadin/tests/components/form/FormWithPropertyFormatterConnected.java @@ -0,0 +1,90 @@ +package com.vaadin.tests.components.form; + +import com.vaadin.data.Item; +import com.vaadin.data.util.BeanItem; +import com.vaadin.data.util.PropertyFormatter; +import com.vaadin.tests.components.TestBase; +import com.vaadin.ui.AbstractField; +import com.vaadin.ui.Button; +import com.vaadin.ui.Component; +import com.vaadin.ui.DefaultFieldFactory; +import com.vaadin.ui.Field; +import com.vaadin.ui.Form; +import com.vaadin.ui.FormFieldFactory; + +public class FormWithPropertyFormatterConnected extends TestBase { + @Override + protected void setup() { + Form form2 = new Form(); + form2.setFormFieldFactory(new FormFieldFactory() { + + public Field createField(Item item, Object propertyId, + Component uiContext) { + AbstractField f = (AbstractField) DefaultFieldFactory.get() + .createField(item, propertyId, uiContext); + if (propertyId.equals("age")) { + f.setPropertyDataSource(new PropertyFormatter() { + + @Override + public Object parse(String formattedValue) + throws Exception { + String str = formattedValue + .replaceAll("[^0-9.]", ""); + if (formattedValue.toLowerCase().contains("months")) { + return Double.parseDouble(str) / 12; + } + return Double.parseDouble(str); + } + + @Override + public String format(Object value) { + Double dValue = (Double) value; + if (dValue < 1) { + return ((int)(dValue * 12)) + " months"; + } + return dValue + " years"; + } + }); + f.setImmediate(true); + } + return f; + } + }); + form2.setItemDataSource(createItem()); + + addComponent(form2); + addComponent(new Button("B")); + } + + private Item createItem() { + return new BeanItem(new Person(0.5)); + } + + public class Person { + public Person(double age) { + super(); + this.age = age; + } + + public double getAge() { + return age; + } + + public void setAge(double age) { + this.age = age; + } + + private double age; + } + + @Override + protected String getDescription() { + return "It should be possible to inject PropertyFormatter and similar classses to fields in form. The test app hooks formatter that displays age in years or months and also accepts value in both (years by default, months if mentioned in the field)"; + } + + @Override + protected Integer getTicketNumber() { + return null; + } + +} -- 2.39.5