* @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. */
}
// Configures the field
- field.setPropertyDataSource(property);
+ bindPropertyToField(id, property, field);
// Register and attach the created field
addField(id, field);
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.
*
// 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;
}
}
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.
--- /dev/null
+package com.vaadin.tests.components.form;\r
+\r
+import com.vaadin.data.Item;\r
+import com.vaadin.data.util.BeanItem;\r
+import com.vaadin.data.util.PropertyFormatter;\r
+import com.vaadin.tests.components.TestBase;\r
+import com.vaadin.ui.AbstractField;\r
+import com.vaadin.ui.Button;\r
+import com.vaadin.ui.Component;\r
+import com.vaadin.ui.DefaultFieldFactory;\r
+import com.vaadin.ui.Field;\r
+import com.vaadin.ui.Form;\r
+import com.vaadin.ui.FormFieldFactory;\r
+\r
+public class FormWithPropertyFormatterConnected extends TestBase {\r
+ @Override\r
+ protected void setup() {\r
+ Form form2 = new Form();\r
+ form2.setFormFieldFactory(new FormFieldFactory() {\r
+\r
+ public Field createField(Item item, Object propertyId,\r
+ Component uiContext) {\r
+ AbstractField f = (AbstractField) DefaultFieldFactory.get()\r
+ .createField(item, propertyId, uiContext);\r
+ if (propertyId.equals("age")) {\r
+ f.setPropertyDataSource(new PropertyFormatter() {\r
+\r
+ @Override\r
+ public Object parse(String formattedValue)\r
+ throws Exception {\r
+ String str = formattedValue\r
+ .replaceAll("[^0-9.]", "");\r
+ if (formattedValue.toLowerCase().contains("months")) {\r
+ return Double.parseDouble(str) / 12;\r
+ }\r
+ return Double.parseDouble(str);\r
+ }\r
+\r
+ @Override\r
+ public String format(Object value) {\r
+ Double dValue = (Double) value;\r
+ if (dValue < 1) {\r
+ return ((int)(dValue * 12)) + " months";\r
+ }\r
+ return dValue + " years";\r
+ }\r
+ });\r
+ f.setImmediate(true);\r
+ }\r
+ return f;\r
+ }\r
+ });\r
+ form2.setItemDataSource(createItem());\r
+ \r
+ addComponent(form2);\r
+ addComponent(new Button("B"));\r
+ }\r
+\r
+ private Item createItem() {\r
+ return new BeanItem<Person>(new Person(0.5));\r
+ }\r
+\r
+ public class Person {\r
+ public Person(double age) {\r
+ super();\r
+ this.age = age;\r
+ }\r
+\r
+ public double getAge() {\r
+ return age;\r
+ }\r
+\r
+ public void setAge(double age) {\r
+ this.age = age;\r
+ }\r
+\r
+ private double age;\r
+ }\r
+\r
+ @Override\r
+ protected String getDescription() {\r
+ 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)";\r
+ }\r
+\r
+ @Override\r
+ protected Integer getTicketNumber() {\r
+ return null;\r
+ }\r
+\r
+}\r