com.vaadin.data.util.
Class PropertyFormatter
java.lang.Object
com.vaadin.data.util.AbstractProperty
com.vaadin.data.util.PropertyFormatter
All Implemented Interfaces:
Property, Property.ReadOnlyStatusChangeListener, Property.ReadOnlyStatusChangeNotifier, Property.ValueChangeListener, Property.ValueChangeNotifier, Property.Viewer, Serializable
- extends AbstractProperty
- implements Property.Viewer, Property.ValueChangeListener, Property.ReadOnlyStatusChangeListener
public abstract class PropertyFormatter
Formatting proxy for a Property
.
This class can be used to implement formatting for any type of Property datasources. The idea is to connect this as proxy between UI component and the original datasource.
For example
textfield.setPropertyDataSource(new PropertyFormatter(property) {
public String format(Object value) {
return ((Double) value).toString() + "000000000";
}
public Object parse(String formattedValue) throws Exception {
return Double.parseDouble(formattedValue);
}
});
adds formatter for Double-typed property that extends
standard "1.0" notation with more zeroes.
Since:
5.3.0
Author:
Vaadin Ltd.
See Also:
Nested Class Summary |
---|
Nested classes/interfaces inherited from class com.vaadin.data.util.AbstractProperty |
---|
AbstractProperty.ReadOnlyStatusChangeEvent |
Nested classes/interfaces inherited from interface com.vaadin.data.Property |
---|
Property.ConversionException, Property.Editor, Property.ReadOnlyException, Property.ReadOnlyStatusChangeListener, Property.ReadOnlyStatusChangeNotifier, Property.ValueChangeListener, Property.ValueChangeNotifier, Property.Viewer |
Constructor Summary | |
---|---|
protected |
PropertyFormatter()
Construct a new PropertyFormatter that is not connected to any
data source. |
PropertyFormatter(Property propertyDataSource)
Construct a new formatter that is connected to given data source. |
Method Summary | |
---|---|
abstract String |
format(Object value)
This method must be implemented to format the values received from DataSource. |
Property |
getPropertyDataSource()
Gets the current data source of the formatter, if any. |
Class |
getType()
Returns the type of the Property. |
Object |
getValue()
Get the formatted value. |
boolean |
isReadOnly()
Reflects the read-only status of the datasource. |
abstract Object |
parse(String formattedValue)
Parse string and convert it to format compatible with datasource. |
void |
readOnlyStatusChange(Property.ReadOnlyStatusChangeEvent event)
Listens for changes in the datasource. |
void |
setPropertyDataSource(Property newDataSource)
Sets the specified Property as the data source for the formatter. |
void |
setReadOnly(boolean newStatus)
Sets the Property's read-only mode to the specified status. |
void |
setValue(Object newValue)
Sets the value of the Property. |
String |
toString()
Get the formatted value. |
void |
valueChange(Property.ValueChangeEvent event)
Listens for changes in the datasource. |
Methods inherited from class com.vaadin.data.util.AbstractProperty |
---|
addListener, addListener, fireReadOnlyStatusChange, fireValueChange, getListeners, removeListener, removeListener |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
PropertyFormatter
protected PropertyFormatter()
Construct a new PropertyFormatter
that is not connected to any
data source. Call setPropertyDataSource(Property)
later on to
attach it to a property.
PropertyFormatter
public PropertyFormatter(Property propertyDataSource)
- Parameters:
propertyDataSource
- to connect this property to.
Construct a new formatter that is connected to given data source. Calls
format(Object)
which can be a problem if the formatter has not
yet been initialized.
Method Detail |
---|
getPropertyDataSource
public Property getPropertyDataSource()
- Specified by:
getPropertyDataSource
in interfaceProperty.Viewer
- Returns:
- the current data source as a Property, or
null
if none defined.
Gets the current data source of the formatter, if any.
setPropertyDataSource
public void setPropertyDataSource(Property newDataSource)
- Specified by:
setPropertyDataSource
in interfaceProperty.Viewer
- Parameters:
newDataSource
- the new data source Property.
Sets the specified Property as the data source for the formatter.
Remember that new data sources getValue() must return objects that are compatible with parse() and format() methods.
getType
public Class getType()
- Returns:
- type of the Property
Description copied from interface: Property
Returns the type of the Property. The methods getValue
and
setValue
must be compatible with this type: one must be able
to safely cast the value returned from getValue
to the given
type and pass any variable assignable to this type as an argument to
setValue
.
getValue
public Object getValue()
- Returns:
- If the datasource returns null, this is null. Otherwise this is String given by format().
Get the formatted value.
toString
public String toString()
- Specified by:
toString
in interfaceProperty
- Overrides:
toString
in classAbstractProperty
- Returns:
- If the datasource returns null, this is null. Otherwise this is String given by format().
Get the formatted value.
isReadOnly
public boolean isReadOnly()
- Specified by:
isReadOnly
in interfaceProperty
- Overrides:
isReadOnly
in classAbstractProperty
- Returns:
true
if the Property is in read-only mode,false
if it's not
Reflects the read-only status of the datasource.
format
public abstract String format(Object value)
- Parameters:
value
- Value object got from the datasource. This is guaranteed to be non-null and of the type compatible with getType() of the datasource.- Returns:
This method must be implemented to format the values received from DataSource.
parse
public abstract Object parse(String formattedValue)
throws Exception
- Parameters:
formattedValue
- This is guaranteed to be non-null string.- Returns:
- Non-null value compatible with datasource.
- Throws:
Exception
- Any type of exception can be thrown to indicate that the conversion was not succesful.
Parse string and convert it to format compatible with datasource. The method is required to assure that parse(format(x)) equals x.
setReadOnly
public void setReadOnly(boolean newStatus)
- Specified by:
setReadOnly
in interfaceProperty
- Overrides:
setReadOnly
in classAbstractProperty
- Parameters:
newStatus
- the new read-only status of the Property.
Sets the Property's read-only mode to the specified status.
setValue
public void setValue(Object newValue)
throws Property.ReadOnlyException,
Property.ConversionException
- Parameters:
newValue
- New value of the Property. This should be assignable to the type returned by getType, but also String type should be supported- Throws:
Property.ReadOnlyException
- if the object is in read-only modeProperty.ConversionException
- if newValue can't be converted into the Property's native type directly or through String
Description copied from interface: Property
Sets the value of the Property.
Implementing this functionality is optional. If the functionality is
missing, one should declare the Property to be in read-only mode and
throw Property.ReadOnlyException
in this function.
String
in addition to the native type of the
Property (as given by the getType
method). If the
String
conversion fails or is unsupported, the method should
throw Property.ConversionException
. The string conversion
should at least understand the format returned by the
toString
method of the Property.
valueChange
public void valueChange(Property.ValueChangeEvent event)
- Specified by:
valueChange
in interfaceProperty.ValueChangeListener
- Parameters:
event
- value change event object
Listens for changes in the datasource. This should not be called directly.
readOnlyStatusChange
public void readOnlyStatusChange(Property.ReadOnlyStatusChangeEvent event)
- Specified by:
readOnlyStatusChange
in interfaceProperty.ReadOnlyStatusChangeListener
- Parameters:
event
- Read-only status change event object
Listens for changes in the datasource. This should not be called directly.