Class EntityComboBoxModel
java.lang.Object
is.codion.swing.framework.model.component.EntityComboBoxModel
- All Implemented Interfaces:
FilterModel<Entity>
,FilterComboBoxModel<Entity>
,ComboBoxModel<Entity>
,ListModel<Entity>
A ComboBoxModel based on an Entity, showing by default all the entities in the underlying table.
-
Nested Class Summary
Nested classes/interfaces inherited from interface is.codion.swing.common.model.component.combobox.FilterComboBoxModel
FilterComboBoxModel.ItemFinder<T,
V> Nested classes/interfaces inherited from interface is.codion.common.model.FilterModel
FilterModel.AbstractRefresher<T>, FilterModel.Refresher<T>
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final PropertyValue<Boolean>
Specifies whether entity combo box models handle entity edit events, by replacing updated entities and removing deleted ones
Value type: Boolean
Default value: trueFields inherited from interface is.codion.swing.common.model.component.combobox.FilterComboBoxModel
COMBO_BOX_NULL_CAPTION
Fields inherited from interface is.codion.common.model.FilterModel
ASYNC_REFRESH
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds the given item to this model, respecting the sorting order if specified.void
addListDataListener
(ListDataListener listener) Controls the attributes to include when selecting the entities to populate this model with.void
clear()
Clears all items from this combo box model, including the null item and sets the selected item to nullboolean
cleared()
Controls the Comparator used when sorting the visible items in this model and sorts the model accordingly.Controls the condition supplier to use when querying data, set to null to fetch all underlying entities.boolean
containsItem
(Entity item) Returns true if this model contains the given item, visible or filtered.createForeignKeyConditionComboBoxModel
(ForeignKey foreignKey) Returns a combo box model for selecting a foreign key value for using as a condition this model.createForeignKeyFilterComboBoxModel
(ForeignKey foreignKey) Returns a combo box model for selecting a foreign key value for filtering this model.<T> Value<T>
createSelectorValue
(Attribute<T> attribute) Creates aValue
linked to the selected entity via the value of the given attribute.<V> Value<V>
createSelectorValue
(FilterComboBoxModel.ItemFinder<Entity, V> itemFinder) static EntityComboBoxModel
entityComboBoxModel
(EntityType entityType, EntityConnectionProvider connectionProvider) boolean
Returns true if this model contains the given item, and it is filtered, that is, is not visibleint
void
Filters this model according to the condition specified byFilterModel.includeCondition()
.Specifies whether filtering can change the selected item, if true then the selected item is set to null when the currently selected item is filtered from the model, otherwise the selected item can potentially represent a value which is not currently visible in the model.find
(Entity.Key primaryKey) Use this method to retrieve the default foreign key filter include condition if you want to add a customPredicate
to this model viaincludeCondition()
.getElementAt
(int index) getForeignKeyFilterKeys
(ForeignKey foreignKey) int
getSize()
items()
void
linkForeignKeyConditionComboBoxModel
(ForeignKey foreignKey, EntityComboBoxModel foreignKeyModel) Links the given combo box model representing master entities to this combo box model so that selection in the master model refreshes this model with the selected master entity as conditionvoid
linkForeignKeyFilterComboBoxModel
(ForeignKey foreignKey, EntityComboBoxModel foreignKeyModel) Links the given combo box model representing master entities to this combo box model so that selection in the master model filters this model according to the selected master entitynullItem()
Controls the item that should represent the null value in this model.boolean
Returns true if this model contains null and it is selected.orderBy()
Controls the order by to use when selecting entities for this model.void
refresh()
Refreshes the items in this filtered model using itsFilterModel.Refresher
.void
refreshThen
(Consumer<Collection<Entity>> afterRefresh) Refreshes the data in this filter model using itsFilterModel.Refresher
.void
Removes the given item from this modelvoid
removeListDataListener
(ListDataListener listener) void
Replaces the given item in this combo box modelvoid
select
(Entity.Key primaryKey) Selects the entity with the given primary key, whether filtered or visible.Provides a way for the combo box model to translate an item when it is selected, such as selecting the String "1" in a String based model when selected item is set to the number 1.void
setForeignKeyFilterKeys
(ForeignKey foreignKey, Collection<Entity.Key> keys) Filters this combo box model so that only items referencing the given keys via the given foreign key are shown.void
setItems
(Collection<Entity> items) Resets the items of this model using the values found initems
.void
setNullCaption
(String nullCaption) Enables the null item and sets the null item caption.void
setSelectedItem
(Object selectedItem) void
Sorts the items in this modelControls whether foreign key filtering should be strict or not.toString()
Provides a way for the model to prevent the addition of certain items.Provides a way for the combo box model to prevent the selection of certain items.boolean
Returns true if this model contains the given item, and it is visible, that is, not filteredint
-
Field Details
-
HANDLE_EDIT_EVENTS
Specifies whether entity combo box models handle entity edit events, by replacing updated entities and removing deleted ones
Value type: Boolean
Default value: true
-
-
Method Details
-
toString
-
connectionProvider
- Returns:
- the connection provider used by this combo box model
-
entityType
- Returns:
- the type of the entity this combo box model is based on
-
setNullCaption
Enables the null item and sets the null item caption.- Parameters:
nullCaption
- the null item caption- Throws:
NullPointerException
- in casenullCaption
is null- See Also:
-
attributes
Controls the attributes to include when selecting the entities to populate this model with. Note that the primary key attribute values are always included. An empty Collection indicates that all attributes should be selected.- Returns:
- the ValueSet controlling the attributes to select, an empty ValueSet indicating all available attributes
-
handleEditEvents
- Returns:
- the state controlling whether this combo box model should handle entity edit events, by adding inserted items, updating any updated items and removing deleted ones
- See Also:
-
find
- Parameters:
primaryKey
- the primary key of the entity to fetch from this model- Returns:
- the entity with the given key if found in the model, an empty Optional otherwise
-
select
Selects the entity with the given primary key, whether filtered or visible. If the entity is not available in the model this method returns silently without changing the selection.- Parameters:
primaryKey
- the primary key of the entity to select
-
condition
Controls the condition supplier to use when querying data, set to null to fetch all underlying entities.- Returns:
- a value controlling the condition supplier
-
orderBy
Controls the order by to use when selecting entities for this model. Note that in order for this to have an effect, you must disable sorting by setting the sort comparator to null (comparator()
- Returns:
- the Value controlling the orderBy
- See Also:
-
foreignKeyIncludeCondition
Use this method to retrieve the default foreign key filter include condition if you want to add a customPredicate
to this model viaincludeCondition()
.Predicate fkCondition = model.foreignKeyIncludeCondition(); model.includeCondition().set(item -> fkCondition.test(item) && ...);
- Returns:
- the
Predicate
based on the foreign key filter entities - See Also:
-
setForeignKeyFilterKeys
Filters this combo box model so that only items referencing the given keys via the given foreign key are shown.- Parameters:
foreignKey
- the foreign keykeys
- the keys, an empty Collection for none
-
getForeignKeyFilterKeys
- Parameters:
foreignKey
- the foreign key- Returns:
- the keys currently used to filter the items of this model by foreign key, an empty collection for none
-
strictForeignKeyFiltering
Controls whether foreign key filtering should be strict or not. When the filtering is strict only entities with the correct reference are included, that is, entities with null values for the given foreign key are filtered. Non-strict simply means that entities with null references are not filtered.- Returns:
- the State controlling whether foreign key filtering should be strict
- See Also:
-
createForeignKeyFilterComboBoxModel
Returns a combo box model for selecting a foreign key value for filtering this model.- Parameters:
foreignKey
- the foreign key- Returns:
- a combo box model for selecting a filtering value for this combo box model
- See Also:
-
createForeignKeyConditionComboBoxModel
Returns a combo box model for selecting a foreign key value for using as a condition this model. Note that each time the selection changes in the created model this model is refreshed.- Parameters:
foreignKey
- the foreign key- Returns:
- a combo box model for selecting a filtering value for this combo box model
- See Also:
-
linkForeignKeyFilterComboBoxModel
public void linkForeignKeyFilterComboBoxModel(ForeignKey foreignKey, EntityComboBoxModel foreignKeyModel) Links the given combo box model representing master entities to this combo box model so that selection in the master model filters this model according to the selected master entity- Parameters:
foreignKey
- the foreign key attributeforeignKeyModel
- the combo box model to link
-
linkForeignKeyConditionComboBoxModel
public void linkForeignKeyConditionComboBoxModel(ForeignKey foreignKey, EntityComboBoxModel foreignKeyModel) Links the given combo box model representing master entities to this combo box model so that selection in the master model refreshes this model with the selected master entity as condition- Parameters:
foreignKey
- the foreign key attributeforeignKeyModel
- the combo box model to link
-
createSelectorValue
Creates aValue
linked to the selected entity via the value of the given attribute.- Type Parameters:
T
- the attribute type- Parameters:
attribute
- the attribute- Returns:
- a
Value
for selecting items by attribute value
-
clear
public void clear()Description copied from interface:FilterComboBoxModel
Clears all items from this combo box model, including the null item and sets the selected item to null- Specified by:
clear
in interfaceFilterComboBoxModel<Entity>
-
cleared
public boolean cleared()- Specified by:
cleared
in interfaceFilterComboBoxModel<Entity>
- Returns:
- true if the model data has been cleared and needs to be refreshed
-
setItems
Description copied from interface:FilterComboBoxModel
Resets the items of this model using the values found initems
.- Specified by:
setItems
in interfaceFilterComboBoxModel<Entity>
- Parameters:
items
- the items to display in this combo box model- See Also:
-
add
Description copied from interface:FilterComboBoxModel
Adds the given item to this model, respecting the sorting order if specified. If this model already contains the item, calling this method has no effect. Note that if the item does not satisfy the include condition, it will be filtered right away.- Specified by:
add
in interfaceFilterComboBoxModel<Entity>
- Parameters:
item
- the item to add- See Also:
-
remove
Description copied from interface:FilterComboBoxModel
Removes the given item from this model- Specified by:
remove
in interfaceFilterComboBoxModel<Entity>
- Parameters:
item
- the item to remove
-
replace
Description copied from interface:FilterComboBoxModel
Replaces the given item in this combo box model- Specified by:
replace
in interfaceFilterComboBoxModel<Entity>
- Parameters:
item
- the item to replacereplacement
- the replacement item
-
sortItems
public void sortItems()Description copied from interface:FilterComboBoxModel
Sorts the items in this model- Specified by:
sortItems
in interfaceFilterComboBoxModel<Entity>
- See Also:
-
comparator
Description copied from interface:FilterComboBoxModel
Controls the Comparator used when sorting the visible items in this model and sorts the model accordingly. This Comparator must take into account the null value if a null item has been set viaFilterComboBoxModel.nullItem()
. If a nullcomparator
is provided no sorting will be performed.- Specified by:
comparator
in interfaceFilterComboBoxModel<Entity>
- Returns:
- the Value controlling the comparator used when sorting, value may be null if the items of this model should not be sorted
-
validator
Description copied from interface:FilterComboBoxModel
Provides a way for the model to prevent the addition of certain items. Trying to add items that fail validation will result in an exception. Note that any translation of the selected item is done before validation.- Specified by:
validator
in interfaceFilterComboBoxModel<Entity>
- Returns:
- the Value controlling the item validator
-
selectedItemTranslator
Description copied from interface:FilterComboBoxModel
Provides a way for the combo box model to translate an item when it is selected, such as selecting the String "1" in a String based model when selected item is set to the number 1.- Specified by:
selectedItemTranslator
in interfaceFilterComboBoxModel<Entity>
- Returns:
- the Value controlling the selected item translator
-
validSelectionPredicate
Description copied from interface:FilterComboBoxModel
Provides a way for the combo box model to prevent the selection of certain items.- Specified by:
validSelectionPredicate
in interfaceFilterComboBoxModel<Entity>
- Returns:
- the Value controlling the valid selection predicate
-
includeNull
- Specified by:
includeNull
in interfaceFilterComboBoxModel<Entity>
- Returns:
- the State controlling whether a null value is included as the first item
- See Also:
-
nullItem
Description copied from interface:FilterComboBoxModel
Controls the item that should represent the null value in this model. Note thatFilterComboBoxModel.includeNull()
must be used as well to enable the null value.- Specified by:
nullItem
in interfaceFilterComboBoxModel<Entity>
- Returns:
- the Value controlling the item representing null
- See Also:
-
nullSelected
public boolean nullSelected()Description copied from interface:FilterComboBoxModel
Returns true if this model contains null and it is selected.- Specified by:
nullSelected
in interfaceFilterComboBoxModel<Entity>
- Returns:
- true if this model contains null and it is selected, false otherwise
- See Also:
-
selectionEmpty
- Specified by:
selectionEmpty
in interfaceFilterComboBoxModel<Entity>
- Returns:
- a StateObserver indicating whether the selection is empty or the value representing null is selected
-
selectedValue
- Specified by:
selectedValue
in interfaceFilterComboBoxModel<Entity>
- Returns:
- the selected value, null in case the value representing null is selected
- See Also:
-
getSelectedItem
- Specified by:
getSelectedItem
in interfaceComboBoxModel<Entity>
- Specified by:
getSelectedItem
in interfaceFilterComboBoxModel<Entity>
- Returns:
- the selected item, N.B. this can include the
nullItem
in case it has been set viaFilterComboBoxModel.nullItem()
,FilterComboBoxModel.selectedValue()
is usually what you want
-
filterSelectedItem
Description copied from interface:FilterComboBoxModel
Specifies whether filtering can change the selected item, if true then the selected item is set to null when the currently selected item is filtered from the model, otherwise the selected item can potentially represent a value which is not currently visible in the model. This is true by default.- Specified by:
filterSelectedItem
in interfaceFilterComboBoxModel<Entity>
- Returns:
- the State controlling whether the selected item is changed when it is filtered
-
createSelectorValue
- Specified by:
createSelectorValue
in interfaceFilterComboBoxModel<Entity>
- Type Parameters:
V
- the value type- Parameters:
itemFinder
- responsible for finding the item to select- Returns:
- a value linked to the selected item via the given finder instance
-
selectionEvent
- Specified by:
selectionEvent
in interfaceFilterComboBoxModel<Entity>
- Returns:
- an observer notified each time the selection changes
-
filterItems
public void filterItems()Description copied from interface:FilterModel
Filters this model according to the condition specified byFilterModel.includeCondition()
. If no include condition is specified this method does nothing. This method does not interfere with the internal ordering of the visible items.- Specified by:
filterItems
in interfaceFilterModel<Entity>
- See Also:
-
includeCondition
- Specified by:
includeCondition
in interfaceFilterModel<Entity>
- Returns:
- the include condition value
-
items
- Specified by:
items
in interfaceFilterModel<Entity>
- Returns:
- an unmodifiable view of all visible and filtered items in this model
- See Also:
-
visibleItems
- Specified by:
visibleItems
in interfaceFilterModel<Entity>
- Returns:
- an unmodifiable view of the visible items, in the order they appear in the model
-
filteredItems
- Specified by:
filteredItems
in interfaceFilterModel<Entity>
- Returns:
- an unmodifiable view of the filtered items
-
visibleCount
public int visibleCount()- Specified by:
visibleCount
in interfaceFilterModel<Entity>
- Returns:
- the number of currently visible items
-
filteredCount
public int filteredCount()- Specified by:
filteredCount
in interfaceFilterModel<Entity>
- Returns:
- the number of currently filtered items
-
containsItem
Description copied from interface:FilterModel
Returns true if this model contains the given item, visible or filtered.- Specified by:
containsItem
in interfaceFilterModel<Entity>
- Parameters:
item
- the item- Returns:
- true if this model contains the item
-
visible
Description copied from interface:FilterModel
Returns true if this model contains the given item, and it is visible, that is, not filtered- Specified by:
visible
in interfaceFilterModel<Entity>
- Parameters:
item
- the item- Returns:
- true if the given item is visible
-
filtered
Description copied from interface:FilterModel
Returns true if this model contains the given item, and it is filtered, that is, is not visible- Specified by:
filtered
in interfaceFilterModel<Entity>
- Parameters:
item
- the item- Returns:
- true if the given item is filtered
-
refresher
- Specified by:
refresher
in interfaceFilterModel<Entity>
- Returns:
- this models Refresher instance
-
refresh
public void refresh()Description copied from interface:FilterModel
Refreshes the items in this filtered model using itsFilterModel.Refresher
.- Specified by:
refresh
in interfaceFilterModel<Entity>
- See Also:
-
refreshThen
Description copied from interface:FilterModel
Refreshes the data in this filter model using itsFilterModel.Refresher
. Note that this method only throws exceptions when run synchronously off the user interface thread. UseFilterModel.Refresher.refreshFailedEvent()
to listen for exceptions that happen during asynchronous refresh.- Specified by:
refreshThen
in interfaceFilterModel<Entity>
- Parameters:
afterRefresh
- called after a successful refresh, may be null- See Also:
-
setSelectedItem
- Specified by:
setSelectedItem
in interfaceComboBoxModel<Entity>
-
getSize
public int getSize() -
getElementAt
- Specified by:
getElementAt
in interfaceListModel<Entity>
-
addListDataListener
- Specified by:
addListDataListener
in interfaceListModel<Entity>
-
removeListDataListener
- Specified by:
removeListDataListener
in interfaceListModel<Entity>
-
entityComboBoxModel
public static EntityComboBoxModel entityComboBoxModel(EntityType entityType, EntityConnectionProvider connectionProvider) - Parameters:
entityType
- the type of the entity this combo box model should representconnectionProvider
- a EntityConnectionProvider instance- Returns:
- a new
EntityComboBoxModel
instance
-