|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.vaadin.data.fieldgroup.FieldGroup
public class FieldGroup
FieldGroup provides an easy way of binding fields to data and handling commits of these fields.
The functionality of FieldGroup is similar to Form
but
FieldGroup
does not handle layouts in any way. The typical use case
is to create a layout outside the FieldGroup and then use FieldGroup to bind
the fields to a data source.
FieldGroup
is not a UI component so it cannot be added to a layout.
Using the buildAndBind methods FieldGroup
can create fields for you
using a FieldGroupFieldFactory but you still have to add them to the correct
position in your layout.
Nested Class Summary | |
---|---|
static class |
FieldGroup.BindException
|
static class |
FieldGroup.CommitEvent
FIXME javadoc |
static class |
FieldGroup.CommitException
|
static interface |
FieldGroup.CommitHandler
CommitHandlers are used by commit() as part of the
commit transactions. |
static class |
FieldGroup.SearchException
|
Constructor Summary | |
---|---|
FieldGroup()
Constructs a field binder. |
|
FieldGroup(Item itemDataSource)
Constructs a field binder that uses the given data source. |
Method Summary | ||
---|---|---|
void |
addCommitHandler(FieldGroup.CommitHandler commitHandler)
Adds a commit handler. |
|
void |
bind(Field<?> field,
java.lang.Object propertyId)
Binds the field with the given propertyId from the current item. |
|
void |
bindMemberFields(java.lang.Object objectWithMemberFields)
Binds member fields found in the given object. |
|
protected
|
build(java.lang.String caption,
java.lang.Class<?> dataType,
java.lang.Class<T> fieldType)
Creates a field based on the given data type. |
|
Field<?> |
buildAndBind(java.lang.Object propertyId)
Builds a field and binds it to the given property id using the field binder. |
|
Field<?> |
buildAndBind(java.lang.String caption,
java.lang.Object propertyId)
Builds a field using the given caption and binds it to the given property id using the field binder. |
|
|
buildAndBind(java.lang.String caption,
java.lang.Object propertyId,
java.lang.Class<T> fieldType)
Builds a field using the given caption and binds it to the given property id using the field binder. |
|
void |
buildAndBindMemberFields(java.lang.Object objectWithMemberFields)
Binds member fields found in the given object and builds member fields that have not been initialized. |
|
protected void |
buildAndBindMemberFields(java.lang.Object objectWithMemberFields,
boolean buildFields)
Binds member fields found in the given object and optionally builds member fields that have not been initialized. |
|
void |
commit()
Commits all changes done to the bound fields. |
|
protected void |
configureField(Field<?> field)
Configures a field with the settings set for this FieldBinder. |
|
void |
discard()
Discards all changes done to the bound fields. |
|
protected java.lang.Object |
findPropertyId(java.lang.reflect.Field memberField)
Searches for a property id from the current itemDataSource that matches the given memberField. |
|
java.util.Collection<java.lang.Object> |
getBoundPropertyIds()
Returns a collection of all property ids that have been bound to fields. |
|
protected java.util.Collection<FieldGroup.CommitHandler> |
getCommitHandlers()
Returns a list of all commit handlers for this FieldGroup . |
|
Field<?> |
getField(java.lang.Object propertyId)
Returns the field that is bound to the given property id |
|
FieldGroupFieldFactory |
getFieldFactory()
Gets the field factory for the FieldGroup . |
|
java.util.Collection<Field<?>> |
getFields()
Returns a collection of all fields that have been bound. |
|
protected static java.util.List<java.lang.reflect.Field> |
getFieldsInDeclareOrder(java.lang.Class searchClass)
Returns an array containing Field objects reflecting all the fields of the class or interface represented by this Class object. |
|
Item |
getItemDataSource()
Gets the item used by this FieldBinder. |
|
protected Property |
getItemProperty(java.lang.Object propertyId)
Gets the property with the given property id from the item. |
|
java.lang.Object |
getPropertyId(Field<?> field)
Returns the property id that is bound to the given field |
|
protected java.lang.Class<?> |
getPropertyType(java.lang.Object propertyId)
Gets the type of the property with the given property id. |
|
java.util.Collection<java.lang.Object> |
getUnboundPropertyIds()
Returns a collection of all property ids that exist in the item set using setItemDataSource(Item) but have not been bound to fields. |
|
boolean |
isBuffered()
Checks the buffered mode for the bound fields. |
|
boolean |
isEnabled()
Returns the enabled status for the fields. |
|
boolean |
isModified()
Checks if any bound field has been modified. |
|
boolean |
isReadOnly()
Returns the read only status that is used by default with all fields that have a writable data source. |
|
boolean |
isValid()
Checks the validity of the bound fields. |
|
protected static java.lang.String |
minifyFieldName(java.lang.String fieldName)
|
|
void |
removeCommitHandler(FieldGroup.CommitHandler commitHandler)
Removes the given commit handler. |
|
void |
setBuffered(boolean buffered)
Sets the buffered mode for the bound fields. |
|
void |
setEnabled(boolean fieldsEnabled)
Updates the enabled state of all bound fields. |
|
void |
setFieldFactory(FieldGroupFieldFactory fieldFactory)
Sets the field factory for the FieldGroup . |
|
void |
setItemDataSource(Item itemDataSource)
Updates the item that is used by this FieldBinder. |
|
void |
setReadOnly(boolean fieldsReadOnly)
Sets the read only state to the given value for all fields with writable data source. |
|
void |
unbind(Field<?> field)
Detaches the field from its property id and removes it from this FieldBinder. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public FieldGroup()
setItemDataSource(Item)
to set a
data source for the field binder.
public FieldGroup(Item itemDataSource)
itemDataSource
- The data source to bind the fields toMethod Detail |
---|
public void setItemDataSource(Item itemDataSource)
itemDataSource
- The new item to usepublic Item getItemDataSource()
commit()
for the item to be updated unless buffered mode has
been switched off.
setBuffered(boolean)
,
commit()
public boolean isBuffered()
for more details on buffered mode
,
Buffered.isBuffered()
public void setBuffered(boolean buffered)
When buffered mode is on the item will not be updated until
commit()
is called. If buffered mode is off the item will be
updated once the fields are updated.
The default is to use buffered mode.
buffered
- true to turn on buffered mode, false otherwiseBuffered.setBuffered(boolean)
public boolean isEnabled()
Note that this will not accurately represent the enabled status of all
fields if you change the enabled status of the fields through some other
method than setEnabled(boolean)
.
public void setEnabled(boolean fieldsEnabled)
fieldsEnabled
- true to enable all bound fields, false to disable thempublic boolean isReadOnly()
Note that this will not accurately represent the read only status of all
fields if you change the read only status of the fields through some
other method than setReadOnly(boolean)
.
public void setReadOnly(boolean fieldsReadOnly)
fieldsReadOnly
- true to set the fields with writable data source to read only,
false to set them to read writepublic java.util.Collection<Field<?>> getFields()
The fields are not returned in any specific order.
public void bind(Field<?> field, java.lang.Object propertyId) throws FieldGroup.BindException
setItemDataSource(Item)
.
This method also adds validators when applicable.
field
- The field to bindpropertyId
- The propertyId to bind to the field
FieldGroup.BindException
- If the property id is already bound to another field by this
field binderprotected Property getItemProperty(java.lang.Object propertyId) throws FieldGroup.BindException
propertyId
- The id if the property to find
FieldGroup.BindException
- If the property was not found in the item or no item has been
setpublic void unbind(Field<?> field) throws FieldGroup.BindException
Note that the field is not detached from its property data source if it is no longer connected to the same property id it was bound to using this FieldBinder.
field
- The field to detach
FieldGroup.BindException
- If the field is not bound by this field binder or not bound
to the correct property idprotected void configureField(Field<?> field)
By default this updates the buffered, read only and enabled state of the field. Also adds validators when applicable. Fields with read only data source are always configured as read only.
field
- The field to updateprotected java.lang.Class<?> getPropertyType(java.lang.Object propertyId) throws FieldGroup.BindException
propertyId
- The propertyId. Must be find
FieldGroup.BindException
public java.util.Collection<java.lang.Object> getBoundPropertyIds()
Note that this will return property ids even before the item has been set. In that case it returns the property ids that will be bound once the item is set.
No guarantee is given for the order of the property ids
public java.util.Collection<java.lang.Object> getUnboundPropertyIds()
setItemDataSource(Item)
but have not been bound to fields.
Will always return an empty collection before an item has been set using
setItemDataSource(Item)
.
No guarantee is given for the order of the property ids
public void commit() throws FieldGroup.CommitException
Calls all FieldGroup.CommitHandler
s before and after committing the field
changes to the item data source. The whole commit is aborted and state is
restored to what it was before commit was called if any
FieldGroup.CommitHandler
throws a CommitException or there is a problem
committing the fields
FieldGroup.CommitException
- If the commit was abortedpublic void discard()
Only has effect if buffered mode is used.
public Field<?> getField(java.lang.Object propertyId)
propertyId
- The property id to use to lookup the field
public java.lang.Object getPropertyId(Field<?> field)
field
- The field to use to lookup the property id
public void addCommitHandler(FieldGroup.CommitHandler commitHandler)
The commit handler is called before the field values are committed to the
item ( CommitHandler#preCommit(CommitEvent)
) and after the item
has been updated (CommitHandler#postCommit(CommitEvent)
). If a
FieldGroup.CommitHandler
throws a CommitException the whole commit is
aborted and the fields retain their old values.
commitHandler
- The commit handler to addpublic void removeCommitHandler(FieldGroup.CommitHandler commitHandler)
commitHandler
- The commit handler to removeaddCommitHandler(CommitHandler)
protected java.util.Collection<FieldGroup.CommitHandler> getCommitHandlers()
FieldGroup
.
Use addCommitHandler(CommitHandler)
and
removeCommitHandler(CommitHandler)
to register or unregister a
commit handler.
public boolean isValid()
Call the Validatable.validate()
for the fields to get the individual
error messages.
public boolean isModified()
public FieldGroupFieldFactory getFieldFactory()
FieldGroup
. The field factory is
only used when FieldGroup
creates a new field.
public void setFieldFactory(FieldGroupFieldFactory fieldFactory)
FieldGroup
. The field factory is
only used when FieldGroup
creates a new field.
fieldFactory
- The field factory to usepublic void bindMemberFields(java.lang.Object objectWithMemberFields) throws FieldGroup.BindException
This method processes all (Java) member fields whose type extends
Field
and that can be mapped to a property id. Property id
mapping is done based on the field name or on a @PropertyId
annotation on the field. All non-null fields for which a property id can
be determined are bound to the property id.
For example:
public class MyForm extends VerticalLayout { private TextField firstName = new TextField("First name");
objectWithMemberFields
- The object that contains (Java) member fields to bind
FieldGroup.BindException
- If there is a problem binding a fieldpublic void buildAndBindMemberFields(java.lang.Object objectWithMemberFields) throws FieldGroup.BindException
This method processes all (Java) member fields whose type extends
Field
and that can be mapped to a property id. Property ids are
searched in the following order: @PropertyId
annotations, exact
field name matches and the case-insensitive matching that ignores
underscores. Fields that are not initialized (null) are built using the
field factory. All non-null fields for which a property id can be
determined are bound to the property id.
For example:
public class MyForm extends VerticalLayout { private TextField firstName = new TextField("First name");
objectWithMemberFields
- The object that contains (Java) member fields to build and
bind
FieldGroup.BindException
- If there is a problem binding or building a fieldprotected void buildAndBindMemberFields(java.lang.Object objectWithMemberFields, boolean buildFields) throws FieldGroup.BindException
This method processes all (Java) member fields whose type extends
Field
and that can be mapped to a property id. Property ids are
searched in the following order: @PropertyId
annotations, exact
field name matches and the case-insensitive matching that ignores
underscores. Fields that are not initialized (null) are built using the
field factory is buildFields is true. All non-null fields for which a
property id can be determined are bound to the property id.
objectWithMemberFields
- The object that contains (Java) member fields to build and
bind
FieldGroup.BindException
- If there is a problem binding or building a fieldprotected java.lang.Object findPropertyId(java.lang.reflect.Field memberField)
If perfect match is not found, uses a case insensitive search that also ignores underscores. Returns null if no match is found. Throws a SearchException if no item data source has been set.
The propertyId search logic used by
buildAndBindMemberFields
can easily be customized by overriding this
method. No other changes are needed.
memberField
- The field an object id is searched for
protected static java.lang.String minifyFieldName(java.lang.String fieldName)
public Field<?> buildAndBind(java.lang.Object propertyId) throws FieldGroup.BindException
propertyId
- The property id to bind to. Must be present in the field
finder.
FieldGroup.BindException
- If there is a problem while building or bindingpublic Field<?> buildAndBind(java.lang.String caption, java.lang.Object propertyId) throws FieldGroup.BindException
caption
- The caption for the fieldpropertyId
- The property id to bind to. Must be present in the field
finder.
Field
.
FieldGroup.BindException
- If there is a problem while building or bindingpublic <T extends Field> T buildAndBind(java.lang.String caption, java.lang.Object propertyId, java.lang.Class<T> fieldType) throws FieldGroup.BindException
caption
- The caption for the fieldpropertyId
- The property id to bind to. Must be present in the field
finder.
Field
.
FieldGroup.BindException
- If the field could not be createdprotected <T extends Field> T build(java.lang.String caption, java.lang.Class<?> dataType, java.lang.Class<T> fieldType) throws FieldGroup.BindException
The data type is the type that we want to edit using the field. The field
type is the type of field we want to create, can be Field
if any
Field is good.
caption
- The caption for the new fielddataType
- The data model type that we want to edit using the fieldfieldType
- The type of field that we want to create
FieldGroup.BindException
- If the field could not be createdprotected static java.util.List<java.lang.reflect.Field> getFieldsInDeclareOrder(java.lang.Class searchClass)
searchClass
-
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |