com.vaadin.addon.jpacontainer.fieldfactory
Class FieldFactory

java.lang.Object
  extended by com.vaadin.ui.DefaultFieldFactory
      extended by com.vaadin.addon.jpacontainer.fieldfactory.FieldFactory
All Implemented Interfaces:
com.vaadin.ui.FormFieldFactory, com.vaadin.ui.TableFieldFactory, Serializable

public class FieldFactory
extends com.vaadin.ui.DefaultFieldFactory

A FormFieldFactory and TableFieldFactory implementation suitable for JPAContainer users.

As where the DefaultFieldFactory in Vaadin can only handle basic data types, this field factory can also automatically create proper fields for various referenced entities. This greatly speeds up construction of CRUD views. Below are field types supported by this FieldFactory:

@ManyToOne
Creates a select backed up by a JPAContainer listing all entities of referenced type. An id->pojo converter is used to automatically convert identifiers to actual referenced entity objects.

Example of a mapped property: @ManyToOne private Address address;
Default type: NativeSelect

Created by createManyToOneField(EntityContainer, Object, Object, Component) method.

The method constructReferenceSelect(EntityContainer, Object, Object, Component, Class) can be used to override the select type. The type can also be set per reference type with setMultiSelectType(Class, Class).

@ManyToMany
Creates a multiselect backed up by a JPAContainer listing all entities of the type in the collection. Selected entities will be reflected to the collection field in the entity using an id->pojo converter.

Example of a mapped property: @ManyToMany private Set<Address> addresses;

Default type: Table (in multiselect mode)

Created by createManyToManyField(EntityContainer, Object, Object, Component) method.

The method constructCollectionSelect(EntityContainer, Object, Object, Component, Class) can be used to override the select type. Type can also be set per reference type with setMultiSelectType(Class, Class).

@OneToMany
Creates a custom field based on Table to edit referenced entities "owned" by the master entity. The table lists only entities which belong to entity being currently edited. Referenced entities are editable straight in the table and instances can be removed and added.

Example of a mapped property: @OneToMany(mappedBy="person", cascade=CascadeType.ALL, orphanRemoval = true) private Set<Address> addresses;

Default type: MasterDetailEditor (in multiselect mode)

Created by createOneToManyField(EntityContainer, Object, Object, Component) method.

Some things to note:

@OneToOne
Creates a sub form for the referenced type. If the value is initially null, the sub form tries to create one with empty parameter constructor.

Example of a mapped property: @OneToOne private Address address;

Default type: OneToOneForm

Created by createOneToOneField(EntityContainer, Object, Object, Component) method.

@Embedded
Creates a sub form for the referenced embedded type. This is closely related to @OneToOne reference. If the value is initially null, the sub form tries to create one with empty parameter constructor.

Example of a mapped property: @Embedded private Address address;

Default type: EmbeddedForm

Created by createEmbeddedField(EntityContainer, Object, Object, Component) method.

@ElementCollection
Creates a custom field based on Table to edit referenced embeddables "owned" by the master entity. Embeddables can be basic datatypes or types annotated with @Embeddable annotation. This is closely related to the master detail editor created for OneToMany relations. The table lists embeddables in element collection. Referenced entities are editable straight in the table and instances can be removed and added.

Example of a mapped property: @ElementCollection private Set<Address> addresses;

Default type: ElementCollectionEditor

Created by createElementCollectionField(EntityContainer, Object, Object, Component) method.

Note that creation of new elements uses empty paramater constructor. Also the ElementCollectionEditor does not currently support Map type element collection.

FieldFactory works recursively. E.g. sub forms or MasterDetailEditors it creates uses the same fieldfactory by default. When using the class in such conditions one often wants to use setVisibleProperties(Class, String...) to configure created fields.

See Also:
Serialized Form

Constructor Summary
FieldFactory()
          Creates a new instance of a FieldFactory.
 
Method Summary
protected  com.vaadin.ui.Field configureBasicFields(com.vaadin.ui.Field field)
          This method can be used to configure field generated by the DefaultFieldFactory.
protected  void configureContainer(EntityContainer<?> referenceContainer, JPAContainer<?> container)
          This method does additional configurations for the container instantiated for a field.
protected  com.vaadin.ui.AbstractSelect constructCollectionSelect(EntityContainer containerForProperty, Object itemId, Object propertyId, com.vaadin.ui.Component uiContext, Class<?> type)
           
protected  com.vaadin.ui.AbstractSelect constructReferenceSelect(EntityContainer containerForProperty, Object itemId, Object propertyId, com.vaadin.ui.Component uiContext, Class<?> type)
           
protected  com.vaadin.ui.Field createElementCollectionField(EntityContainer containerForProperty, Object itemId, Object propertyId, com.vaadin.ui.Component uiContext)
           
protected  com.vaadin.ui.Field createEmbeddedField(EntityContainer jpacontainer, Object itemId, Object propertyId, com.vaadin.ui.Component uiContext)
           
protected  com.vaadin.ui.Field createEnumSelect(Class<?> type, Object propertyId)
           
 com.vaadin.ui.Field createField(com.vaadin.data.Container container, Object itemId, Object propertyId, com.vaadin.ui.Component uiContext)
           
 com.vaadin.ui.Field createField(com.vaadin.data.Item item, Object propertyId, com.vaadin.ui.Component uiContext)
           
protected  com.vaadin.ui.Field createIdentifierField()
          This method creates field for identifier property.
protected  JPAContainer<?> createJPAContainerFor(EntityContainer<?> referenceContainer, Class<?> type, boolean buffered)
          This method creates new JPAContainer instances to be used in fields generated by this FieldFactory.
protected  com.vaadin.ui.Field createManyToManyField(EntityContainer containerForProperty, Object itemId, Object propertyId, com.vaadin.ui.Component uiContext)
           
protected  com.vaadin.ui.Field createManyToOneField(EntityContainer containerForProperty, Object itemId, Object propertyId, com.vaadin.ui.Component uiContext)
          Creates a field for simple reference (ManyToOne)
protected  com.vaadin.ui.Field createOneToManyField(EntityContainer containerForProperty, Object itemId, Object propertyId, com.vaadin.ui.Component uiContext)
           
protected  OneToOneForm createOneToOneField(EntityContainer<?> jpacontainer, Object itemId, Object propertyId, com.vaadin.ui.Component uiContext)
           
protected  Class detectReferencedType(javax.persistence.EntityManagerFactory emf, Object propertyId, Class masterEntityClass)
          Detects the type entities in "collection types" (oneToMany, ManyToMany).
protected  javax.persistence.EntityManagerFactory getEntityManagerFactory(EntityContainer<?> containerForProperty)
           
 String[] getVisibleProperties(Class<?> containerType)
          Returns customized visible properties (and their order) for given entity type.
 void setMultiSelectType(Class<?> referenceType, Class<? extends com.vaadin.ui.AbstractSelect> selectType)
           
 void setSingleSelectType(Class<?> referenceType, Class<? extends com.vaadin.ui.AbstractSelect> selectType)
           
 void setVisibleProperties(Class<?> containerType, String... propertyIdentifiers)
          Configures visible properties and their order for fields created for reference/collection types referencing to given entity type.
 
Methods inherited from class com.vaadin.ui.DefaultFieldFactory
createCaptionByPropertyId, createFieldByPropertyType, get
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FieldFactory

public FieldFactory()
Creates a new instance of a FieldFactory.

Method Detail

createField

public com.vaadin.ui.Field createField(com.vaadin.data.Item item,
                                       Object propertyId,
                                       com.vaadin.ui.Component uiContext)
Specified by:
createField in interface com.vaadin.ui.FormFieldFactory
Overrides:
createField in class com.vaadin.ui.DefaultFieldFactory

createEnumSelect

protected com.vaadin.ui.Field createEnumSelect(Class<?> type,
                                               Object propertyId)
Parameters:
type -
propertyId -
Returns:

createIdentifierField

protected com.vaadin.ui.Field createIdentifierField()
This method creates field for identifier property. The default implementation does nothing. We expect identifiers to be assigned automatically. This method can be overridden to change the behavior.

Returns:
the field for identifier property

configureBasicFields

protected com.vaadin.ui.Field configureBasicFields(com.vaadin.ui.Field field)
This method can be used to configure field generated by the DefaultFieldFactory. By default it sets null representation of textfields to empty string instead of 'null'.

Parameters:
field -
Returns:

createField

public com.vaadin.ui.Field createField(com.vaadin.data.Container container,
                                       Object itemId,
                                       Object propertyId,
                                       com.vaadin.ui.Component uiContext)
Specified by:
createField in interface com.vaadin.ui.TableFieldFactory
Overrides:
createField in class com.vaadin.ui.DefaultFieldFactory

createEmbeddedField

protected com.vaadin.ui.Field createEmbeddedField(EntityContainer jpacontainer,
                                                  Object itemId,
                                                  Object propertyId,
                                                  com.vaadin.ui.Component uiContext)

createOneToOneField

protected OneToOneForm createOneToOneField(EntityContainer<?> jpacontainer,
                                           Object itemId,
                                           Object propertyId,
                                           com.vaadin.ui.Component uiContext)

createManyToManyField

protected com.vaadin.ui.Field createManyToManyField(EntityContainer containerForProperty,
                                                    Object itemId,
                                                    Object propertyId,
                                                    com.vaadin.ui.Component uiContext)

createOneToManyField

protected com.vaadin.ui.Field createOneToManyField(EntityContainer containerForProperty,
                                                   Object itemId,
                                                   Object propertyId,
                                                   com.vaadin.ui.Component uiContext)

createElementCollectionField

protected com.vaadin.ui.Field createElementCollectionField(EntityContainer containerForProperty,
                                                           Object itemId,
                                                           Object propertyId,
                                                           com.vaadin.ui.Component uiContext)

detectReferencedType

protected Class detectReferencedType(javax.persistence.EntityManagerFactory emf,
                                     Object propertyId,
                                     Class masterEntityClass)
Detects the type entities in "collection types" (oneToMany, ManyToMany).

Parameters:
propertyId -
masterEntityClass -
Returns:
the type of entities in collection type

getEntityManagerFactory

protected javax.persistence.EntityManagerFactory getEntityManagerFactory(EntityContainer<?> containerForProperty)

createManyToOneField

protected com.vaadin.ui.Field createManyToOneField(EntityContainer containerForProperty,
                                                   Object itemId,
                                                   Object propertyId,
                                                   com.vaadin.ui.Component uiContext)
Creates a field for simple reference (ManyToOne)

Parameters:
containerForProperty -
propertyId -
Returns:

constructReferenceSelect

protected com.vaadin.ui.AbstractSelect constructReferenceSelect(EntityContainer containerForProperty,
                                                                Object itemId,
                                                                Object propertyId,
                                                                com.vaadin.ui.Component uiContext,
                                                                Class<?> type)

constructCollectionSelect

protected com.vaadin.ui.AbstractSelect constructCollectionSelect(EntityContainer containerForProperty,
                                                                 Object itemId,
                                                                 Object propertyId,
                                                                 com.vaadin.ui.Component uiContext,
                                                                 Class<?> type)

createJPAContainerFor

protected JPAContainer<?> createJPAContainerFor(EntityContainer<?> referenceContainer,
                                                Class<?> type,
                                                boolean buffered)
This method creates new JPAContainer instances to be used in fields generated by this FieldFactory.

After setting up the container (with provider) the method configures it with configureContainer(EntityContainer, JPAContainer) method.

If you need to use JPAContaiener with some special settings (e.g. customized EntityProvider) you should override this method.

Parameters:
referenceContainer - most commonly this is the container for which property the field is being created. The default implementation uses this extensively to decide the new JPAContainer should be configured.
type - the entity type to be listed in the container
buffered - true if the container should be "buffered" (e.g. for a MasterDetailEditor that is used in a buffered Form).
Returns:
new JPAContainer for given type that is to be used in a relation field

configureContainer

protected void configureContainer(EntityContainer<?> referenceContainer,
                                  JPAContainer<?> container)
This method does additional configurations for the container instantiated for a field. By default it copies the registered LazyLoadingDelegate and EntityManagerProvider from the reference container.

Parameters:
referenceContainer -
container -

setVisibleProperties

public void setVisibleProperties(Class<?> containerType,
                                 String... propertyIdentifiers)
Configures visible properties and their order for fields created for reference/collection types referencing to given entity type. This order is for example used by Tables created for OneToMany or ManyToMany reference types.

Parameters:
containerType - the entity type for which the visible properties will be set
propertyIdentifiers - the identifiers in wished order to be displayed

setMultiSelectType

public void setMultiSelectType(Class<?> referenceType,
                               Class<? extends com.vaadin.ui.AbstractSelect> selectType)

setSingleSelectType

public void setSingleSelectType(Class<?> referenceType,
                                Class<? extends com.vaadin.ui.AbstractSelect> selectType)

getVisibleProperties

public String[] getVisibleProperties(Class<?> containerType)
Returns customized visible properties (and their order) for given entity type.

Parameters:
containerType -
Returns:
property identifiers that are configured to be displayed


Copyright © 2013 Vaadin Ltd. All rights reserved.