public class CustomCollectionEditor extends PropertyEditorSupport
By default registered for Set, SortedSet and List, to automatically convert any given Collection to one of those target types if the type does not match the target property.
Collection
,
Set
,
SortedSet
,
List
Constructor and Description |
---|
CustomCollectionEditor(Class<? extends Collection> collectionType)
Create a new CustomCollectionEditor for the given target type,
keeping an incoming
null as-is. |
CustomCollectionEditor(Class<? extends Collection> collectionType,
boolean nullAsEmptyCollection)
Create a new CustomCollectionEditor for the given target type.
|
Modifier and Type | Method and Description |
---|---|
protected boolean |
alwaysCreateNewCollection()
Return whether to always create a new Collection,
even if the type of the passed-in Collection already matches.
|
protected Object |
convertElement(Object element)
Hook to convert each encountered Collection/array element.
|
protected Collection<Object> |
createCollection(Class<? extends Collection> collectionType,
int initialCapacity)
Create a Collection of the given type, with the given
initial capacity (if supported by the Collection type).
|
String |
getAsText()
This implementation returns
null to indicate that
there is no appropriate text representation. |
void |
setAsText(String text)
Convert the given text value to a Collection with a single element.
|
void |
setValue(Object value)
Convert the given value to a Collection of the target type.
|
addPropertyChangeListener, firePropertyChange, getCustomEditor, getJavaInitializationString, getSource, getTags, getValue, isPaintable, paintValue, removePropertyChangeListener, setSource, supportsCustomEditor
public CustomCollectionEditor(Class<? extends Collection> collectionType)
null
as-is.collectionType
- the target type, which needs to be a
sub-interface of Collection or a concrete Collection classCollection
,
ArrayList
,
TreeSet
,
LinkedHashSet
public CustomCollectionEditor(Class<? extends Collection> collectionType, boolean nullAsEmptyCollection)
If the incoming value is of the given type, it will be used as-is. If it is a different Collection type or an array, it will be converted to a default implementation of the given Collection type. If the value is anything else, a target Collection with that single value will be created.
The default Collection implementations are: ArrayList for List, TreeSet for SortedSet, and LinkedHashSet for Set.
collectionType
- the target type, which needs to be a
sub-interface of Collection or a concrete Collection classnullAsEmptyCollection
- whether to convert an incoming null
value to an empty Collection (of the appropriate type)Collection
,
ArrayList
,
TreeSet
,
LinkedHashSet
public void setAsText(String text) throws IllegalArgumentException
setAsText
in interface PropertyEditor
setAsText
in class PropertyEditorSupport
IllegalArgumentException
public void setValue(Object value)
setValue
in interface PropertyEditor
setValue
in class PropertyEditorSupport
protected Collection<Object> createCollection(Class<? extends Collection> collectionType, int initialCapacity)
collectionType
- a sub-interface of CollectioninitialCapacity
- the initial capacityprotected boolean alwaysCreateNewCollection()
Default is "false"; can be overridden to enforce creation of a new Collection, for example to convert elements in any case.
convertElement(java.lang.Object)
protected Object convertElement(Object element)
Can be overridden to perform conversion of certain elements, for example String to Integer if a String array comes in and should be converted to a Set of Integer objects.
Only called if actually creating a new Collection!
This is by default not the case if the type of the passed-in Collection
already matches. Override alwaysCreateNewCollection()
to
enforce creating a new Collection in every case.
element
- the source elementalwaysCreateNewCollection()
public String getAsText()
null
to indicate that
there is no appropriate text representation.getAsText
in interface PropertyEditor
getAsText
in class PropertyEditorSupport