diff options
author | Henri Sara <hesara@vaadin.com> | 2011-11-08 18:02:55 +0200 |
---|---|---|
committer | Henri Sara <hesara@vaadin.com> | 2011-11-08 18:02:55 +0200 |
commit | 0ab1d01c8da1f280370452fa2427708fd6de513e (patch) | |
tree | 4bf67b8a478da3b25af35c33c031c837ef2cc2e7 /src/com/vaadin/ui | |
parent | a61302616430cbbfc97cbdbb85fe294842b3409c (diff) | |
download | vaadin-framework-0ab1d01c8da1f280370452fa2427708fd6de513e.tar.gz vaadin-framework-0ab1d01c8da1f280370452fa2427708fd6de513e.zip |
Parameterize Property and Field with the value type.
The interfaces Property and Field and most of their implementations are
now parameterized with the type of their value. The method getValue()
returns the Property type but setValue() takes Object as its parameter.
No implicit conversions between value type and strings are performed in
most locations (fields).
Among others, AbstractTextField, DateField and RichTextArea not have
specific value types and do not accept arbitrary values.
Most locations requiring migration will be visible as compilation
errors, with the exception of some cases where a non-parameterized
Property or Field (or one parametrized with Object) is used.
Not yet done:
- Label
- converters
- setValue() parameterization (causes much more migration effort)
Diffstat (limited to 'src/com/vaadin/ui')
-rw-r--r-- | src/com/vaadin/ui/AbstractField.java | 49 | ||||
-rw-r--r-- | src/com/vaadin/ui/AbstractSelect.java | 3 | ||||
-rw-r--r-- | src/com/vaadin/ui/AbstractTextField.java | 10 | ||||
-rw-r--r-- | src/com/vaadin/ui/Button.java | 13 | ||||
-rw-r--r-- | src/com/vaadin/ui/DateField.java | 12 | ||||
-rw-r--r-- | src/com/vaadin/ui/Field.java | 8 | ||||
-rw-r--r-- | src/com/vaadin/ui/Form.java | 4 | ||||
-rw-r--r-- | src/com/vaadin/ui/Label.java | 1 | ||||
-rw-r--r-- | src/com/vaadin/ui/ProgressIndicator.java | 11 | ||||
-rw-r--r-- | src/com/vaadin/ui/RichTextArea.java | 6 | ||||
-rw-r--r-- | src/com/vaadin/ui/Slider.java | 9 |
11 files changed, 69 insertions, 57 deletions
diff --git a/src/com/vaadin/ui/AbstractField.java b/src/com/vaadin/ui/AbstractField.java index 7b686fd397..0f209ce04d 100644 --- a/src/com/vaadin/ui/AbstractField.java +++ b/src/com/vaadin/ui/AbstractField.java @@ -53,8 +53,8 @@ import com.vaadin.terminal.PaintTarget; * @since 3.0 */ @SuppressWarnings("serial") -public abstract class AbstractField extends AbstractComponent implements Field, - Property.ReadOnlyStatusChangeListener, +public abstract class AbstractField<T> extends AbstractComponent implements + Field<T>, Property.ReadOnlyStatusChangeListener, Property.ReadOnlyStatusChangeNotifier, Action.ShortcutNotifier { /* Private members */ @@ -62,12 +62,12 @@ public abstract class AbstractField extends AbstractComponent implements Field, /** * Value of the abstract field. */ - private Object value; + private T value; /** * Connected data-source. */ - private Property dataSource = null; + private Property<?> dataSource = null; /** * The list of validators. @@ -189,7 +189,7 @@ public abstract class AbstractField extends AbstractComponent implements Field, * Gets the field type Don't add a JavaDoc comment here, we use the default * documentation from the implemented interface. */ - public abstract Class<?> getType(); + public abstract Class<? extends T> getType(); /** * The abstract field is read only also if the data source is in read only @@ -237,13 +237,14 @@ public abstract class AbstractField extends AbstractComponent implements Field, public void commit() throws Buffered.SourceException, InvalidValueException { if (dataSource != null && !dataSource.isReadOnly()) { if ((isInvalidCommitted() || isValid())) { - final Object newValue = getValue(); + final T newValue = getValue(); try { // Commits the value to datasource. valueWasModifiedByDataSourceDuringCommit = false; committingValueToDataSource = true; - dataSource.setValue(newValue); + // TODO cast required until conversions applied + ((Property) dataSource).setValue(newValue); } catch (final Throwable e) { @@ -325,6 +326,7 @@ public abstract class AbstractField extends AbstractComponent implements Field, // If the new value differs from the previous one if ((newValue == null && value != null) || (newValue != null && !newValue.equals(value))) { + // TODO use converter setInternalValue(newValue); fireValueChange(false); } @@ -393,6 +395,7 @@ public abstract class AbstractField extends AbstractComponent implements Field, if (String.class == getType() && newValue != null) { newValue = newValue.toString(); } + // TODO use converter setInternalValue(newValue); fireValueChange(false); } @@ -416,12 +419,11 @@ public abstract class AbstractField extends AbstractComponent implements Field, } /** - * Returns the (UI type) value of the field converted to a String using - * toString(). + * Returns the (UI type) value of the field converted to a String. * * This method exists to help migration from the use of Property.toString() - * to get the field value - for new applications, access getValue() - * directly. This method may disappear in future Vaadin versions. + * to get the field value. For new applications, it is often better to + * access getValue() directly. * * @return string representation of the field value or null if the value is * null @@ -440,27 +442,25 @@ public abstract class AbstractField extends AbstractComponent implements Field, * * <p> * This is the visible, modified and possible invalid value the user have - * entered to the field. In the read-through mode, the abstract buffer is - * also updated and validation is performed. + * entered to the field. * </p> * * <p> * Note that the object returned is compatible with getType(). For example, * if the type is String, this returns Strings even when the underlying - * datasource is of some other type. In order to access the datasources - * native type, use getPropertyDatasource().getValue() instead. + * datasource is of some other type. In order to access the native type of + * the datasource, use getPropertyDatasource().getValue() instead. * </p> * * <p> - * Note that when you extend AbstractField, you must reimplement this method - * if datasource.getValue() is not assignable to class returned by getType() - * AND getType() is not String. In case of Strings, getValue() calls - * datasource.toString() instead of datasource.getValue(). + * Since Vaadin 7.0, no implicit conversions between other data types and + * String are performed, but the converter is used if set. * </p> * * @return the current value of the field. + * @throws Property.ConversionException */ - public Object getValue() { + public T getValue() { // Give the value from abstract buffers if the field if possible if (dataSource == null || !isReadThrough() || isModified()) { @@ -468,10 +468,11 @@ public abstract class AbstractField extends AbstractComponent implements Field, } Object result = dataSource.getValue(); + // TODO perform correct conversion, no cast or toString() if (String.class == getType() && result != null) { result = result.toString(); } - return result; + return (T) result; } /** @@ -537,7 +538,7 @@ public abstract class AbstractField extends AbstractComponent implements Field, // Commits the value to datasource committingValueToDataSource = true; - dataSource.setValue(newValue); + ((Property) dataSource).setValue(newValue); // The buffer is now unmodified modified = false; @@ -643,6 +644,7 @@ public abstract class AbstractField extends AbstractComponent implements Field, if (String.class == getType() && newValue != null) { newValue = newValue.toString(); } + // TODO use converter setInternalValue(newValue); } modified = false; @@ -1070,6 +1072,7 @@ public abstract class AbstractField extends AbstractComponent implements Field, } private void readValueFromProperty(Property.ValueChangeEvent event) { + // TODO use converter or check type otherwise setInternalValue(event.getProperty().getValue()); } @@ -1134,7 +1137,7 @@ public abstract class AbstractField extends AbstractComponent implements Field, * the new value to be set. */ protected void setInternalValue(Object newValue) { - value = newValue; + value = (T) newValue; if (validators != null && !validators.isEmpty()) { requestRepaint(); } diff --git a/src/com/vaadin/ui/AbstractSelect.java b/src/com/vaadin/ui/AbstractSelect.java index eaac3bb4c5..d4bf600651 100644 --- a/src/com/vaadin/ui/AbstractSelect.java +++ b/src/com/vaadin/ui/AbstractSelect.java @@ -55,7 +55,8 @@ import com.vaadin.terminal.gwt.client.ui.dd.VerticalDropLocation; * @since 5.0 */ @SuppressWarnings("serial") -public abstract class AbstractSelect extends AbstractField implements +// TODO currently cannot specify type more precisely in case of multi-select +public abstract class AbstractSelect extends AbstractField<Object> implements Container, Container.Viewer, Container.PropertySetChangeListener, Container.PropertySetChangeNotifier, Container.ItemSetChangeNotifier, Container.ItemSetChangeListener { diff --git a/src/com/vaadin/ui/AbstractTextField.java b/src/com/vaadin/ui/AbstractTextField.java index 8d775c862e..1bcb794eba 100644 --- a/src/com/vaadin/ui/AbstractTextField.java +++ b/src/com/vaadin/ui/AbstractTextField.java @@ -20,7 +20,7 @@ import com.vaadin.terminal.PaintException; import com.vaadin.terminal.PaintTarget; import com.vaadin.terminal.gwt.client.ui.VTextField; -public abstract class AbstractTextField extends AbstractField implements +public abstract class AbstractTextField extends AbstractField<String> implements BlurNotifier, FocusNotifier, TextChangeNotifier { /** @@ -171,8 +171,8 @@ public abstract class AbstractTextField extends AbstractField implements } @Override - public Object getValue() { - Object v = super.getValue(); + public String getValue() { + String v = super.getValue(); if (format == null || v == null) { return v; } @@ -250,7 +250,7 @@ public abstract class AbstractTextField extends AbstractField implements } @Override - public Class getType() { + public Class<String> getType() { return String.class; } @@ -513,7 +513,7 @@ public abstract class AbstractTextField extends AbstractField implements * case. AbstractField optimizes value change if the existing value is * reset. Also we need to force repaint if the flag is on. */ - if(lastKnownTextContent != null) { + if (lastKnownTextContent != null) { lastKnownTextContent = null; requestRepaint(); } diff --git a/src/com/vaadin/ui/Button.java b/src/com/vaadin/ui/Button.java index 16314f94c3..2c8e17f071 100644 --- a/src/com/vaadin/ui/Button.java +++ b/src/com/vaadin/ui/Button.java @@ -36,8 +36,9 @@ import com.vaadin.ui.themes.BaseTheme; */ @SuppressWarnings("serial") @ClientWidget(value = VButton.class, loadStyle = LoadStyle.EAGER) -public class Button extends AbstractField implements FieldEvents.BlurNotifier, - FieldEvents.FocusNotifier { +// FIXME Button should not be a Field, but CheckBox should +public class Button extends AbstractField<Boolean> implements + FieldEvents.BlurNotifier, FieldEvents.FocusNotifier { /* Private members */ @@ -125,7 +126,7 @@ public class Button extends AbstractField implements FieldEvents.BlurNotifier, * @deprecated use {@link CheckBox} instead of Button in "switchmode" */ @Deprecated - public Button(String caption, Property dataSource) { + public Button(String caption, Property<Boolean> dataSource) { setCaption(caption); setSwitchMode(true); setPropertyDataSource(dataSource); @@ -178,7 +179,7 @@ public class Button extends AbstractField implements FieldEvents.BlurNotifier, if (!isReadOnly() && variables.containsKey("state")) { // Gets the new and old button states final Boolean newValue = (Boolean) variables.get("state"); - final Boolean oldValue = (Boolean) getValue(); + final Boolean oldValue = getValue(); if (isSwitchMode()) { @@ -265,7 +266,7 @@ public class Button extends AbstractField implements FieldEvents.BlurNotifier, * @return True iff the button is pressed down or checked. */ public boolean booleanValue() { - Boolean value = (Boolean) getValue(); + Boolean value = getValue(); return (null == value) ? false : value.booleanValue(); } @@ -286,7 +287,7 @@ public class Button extends AbstractField implements FieldEvents.BlurNotifier, * @see com.vaadin.data.Property#getType() */ @Override - public Class getType() { + public Class<Boolean> getType() { return Boolean.class; } diff --git a/src/com/vaadin/ui/DateField.java b/src/com/vaadin/ui/DateField.java index b6e3d1e8cb..040a2a4dd4 100644 --- a/src/com/vaadin/ui/DateField.java +++ b/src/com/vaadin/ui/DateField.java @@ -48,7 +48,7 @@ import com.vaadin.terminal.gwt.client.ui.VPopupCalendar; */ @SuppressWarnings("serial") @ClientWidget(VPopupCalendar.class) -public class DateField extends AbstractField implements +public class DateField extends AbstractField<Date> implements FieldEvents.BlurNotifier, FieldEvents.FocusNotifier { /* Private members */ @@ -228,7 +228,7 @@ public class DateField extends AbstractField implements // Gets the calendar final Calendar calendar = getCalendar(); - final Date currentDate = (Date) getValue(); + final Date currentDate = getValue(); for (int r = resolution; r <= largestModifiable; r++) { switch (r) { @@ -298,10 +298,10 @@ public class DateField extends AbstractField implements || variables.containsKey("min") || variables.containsKey("sec") || variables.containsKey("msec") || variables - .containsKey("dateString"))) { + .containsKey("dateString"))) { // Old and new dates - final Date oldDate = (Date) getValue(); + final Date oldDate = getValue(); Date newDate = null; // this enables analyzing invalid input on the server @@ -469,7 +469,7 @@ public class DateField extends AbstractField implements * the default documentation from implemented interface. */ @Override - public Class<?> getType() { + public Class<Date> getType() { return Date.class; } @@ -642,7 +642,7 @@ public class DateField extends AbstractField implements final Calendar newCal = (Calendar) calendar.clone(); // Assigns the current time tom calendar. - final Date currentDate = (Date) getValue(); + final Date currentDate = getValue(); if (currentDate != null) { newCal.setTime(currentDate); } diff --git a/src/com/vaadin/ui/Field.java b/src/com/vaadin/ui/Field.java index 1f9bea311f..5cc81a9e92 100644 --- a/src/com/vaadin/ui/Field.java +++ b/src/com/vaadin/ui/Field.java @@ -9,9 +9,15 @@ import com.vaadin.data.Property; import com.vaadin.ui.Component.Focusable; /** + * TODO document + * + * @param T + * the type of values in the field, which might not be the same type + * as that of the data source if converters are used + * * @author IT Mill Ltd. */ -public interface Field extends Component, BufferedValidatable, Property, +public interface Field<T> extends Component, BufferedValidatable, Property<T>, Property.ValueChangeNotifier, Property.ValueChangeListener, Property.Editor, Focusable { diff --git a/src/com/vaadin/ui/Form.java b/src/com/vaadin/ui/Form.java index 8ee702bbb4..b00a34ad42 100644 --- a/src/com/vaadin/ui/Form.java +++ b/src/com/vaadin/ui/Form.java @@ -61,8 +61,8 @@ import com.vaadin.terminal.gwt.client.ui.VForm; */ @SuppressWarnings("serial") @ClientWidget(VForm.class) -public class Form extends AbstractField implements Item.Editor, Buffered, Item, - Validatable, Action.Notifier { +public class Form extends AbstractField<Object> implements Item.Editor, + Buffered, Item, Validatable, Action.Notifier { private Object propertyValue; diff --git a/src/com/vaadin/ui/Label.java b/src/com/vaadin/ui/Label.java index 1249f20a62..9bae47dbae 100644 --- a/src/com/vaadin/ui/Label.java +++ b/src/com/vaadin/ui/Label.java @@ -40,6 +40,7 @@ import com.vaadin.ui.ClientWidget.LoadStyle; */ @SuppressWarnings("serial") @ClientWidget(value = VLabel.class, loadStyle = LoadStyle.EAGER) +// TODO generics for interface Property public class Label extends AbstractComponent implements Property, Property.Viewer, Property.ValueChangeListener, Property.ValueChangeNotifier, Comparable<Object> { diff --git a/src/com/vaadin/ui/ProgressIndicator.java b/src/com/vaadin/ui/ProgressIndicator.java index 6f2758f323..7bf4bc56af 100644 --- a/src/com/vaadin/ui/ProgressIndicator.java +++ b/src/com/vaadin/ui/ProgressIndicator.java @@ -26,7 +26,7 @@ import com.vaadin.terminal.gwt.client.ui.VProgressIndicator; */ @SuppressWarnings("serial") @ClientWidget(VProgressIndicator.class) -public class ProgressIndicator extends AbstractField implements Property, +public class ProgressIndicator extends AbstractField<Number> implements Property.Viewer, Property.ValueChangeListener { /** @@ -125,11 +125,12 @@ public class ProgressIndicator extends AbstractField implements Property, * @see com.vaadin.ui.AbstractField#getValue() */ @Override - public Object getValue() { + public Number getValue() { if (dataSource == null) { throw new IllegalStateException("Datasource must be set"); } - return dataSource.getValue(); + // TODO conversions to eliminate cast + return (Number) dataSource.getValue(); } /** @@ -138,7 +139,7 @@ public class ProgressIndicator extends AbstractField implements Property, * * @param newValue * the New value of the ProgressIndicator. - * @see com.vaadin.ui.AbstractField#setValue(java.lang.Object) + * @see com.vaadin.ui.AbstractField#setValue() */ @Override public void setValue(Object newValue) { @@ -163,7 +164,7 @@ public class ProgressIndicator extends AbstractField implements Property, * @see com.vaadin.ui.AbstractField#getType() */ @Override - public Class<?> getType() { + public Class<? extends Number> getType() { if (dataSource == null) { throw new IllegalStateException("Datasource must be set"); } diff --git a/src/com/vaadin/ui/RichTextArea.java b/src/com/vaadin/ui/RichTextArea.java index 612cc24e8b..0f7a2b376c 100644 --- a/src/com/vaadin/ui/RichTextArea.java +++ b/src/com/vaadin/ui/RichTextArea.java @@ -21,7 +21,7 @@ import com.vaadin.ui.ClientWidget.LoadStyle; * into length of field. */ @ClientWidget(value = VRichTextArea.class, loadStyle = LoadStyle.LAZY) -public class RichTextArea extends AbstractField { +public class RichTextArea extends AbstractField<String> { /** * Value formatter used to format the string contents. @@ -175,8 +175,8 @@ public class RichTextArea extends AbstractField { } @Override - public Object getValue() { - Object v = super.getValue(); + public String getValue() { + String v = super.getValue(); if (format == null || v == null) { return v; } diff --git a/src/com/vaadin/ui/Slider.java b/src/com/vaadin/ui/Slider.java index c07913d5fe..ae10e91e22 100644 --- a/src/com/vaadin/ui/Slider.java +++ b/src/com/vaadin/ui/Slider.java @@ -48,7 +48,7 @@ import com.vaadin.terminal.gwt.client.ui.VSlider; */ @SuppressWarnings("serial") @ClientWidget(VSlider.class) -public class Slider extends AbstractField { +public class Slider extends AbstractField<Number> { public static final int ORIENTATION_HORIZONTAL = 0; @@ -408,10 +408,9 @@ public class Slider extends AbstractField { target.addAttribute("resolution", resolution); if (resolution > 0) { - target.addVariable(this, "value", - ((Double) getValue()).doubleValue()); + target.addVariable(this, "value", getValue().doubleValue()); } else { - target.addVariable(this, "value", ((Double) getValue()).intValue()); + target.addVariable(this, "value", getValue().intValue()); } if (orientation == ORIENTATION_VERTICAL) { @@ -493,7 +492,7 @@ public class Slider extends AbstractField { } @Override - public Class getType() { + public Class<Double> getType() { return Double.class; } |