Module is.codion.swing.common.model
Class FilteredComboBoxModel<T>
java.lang.Object
is.codion.swing.common.model.component.combobox.FilteredComboBoxModel<T>
- Type Parameters:
T
- the type of values in this combo box model
- All Implemented Interfaces:
FilteredModel<T>
,ComboBoxModel<T>
,ListModel<T>
- Direct Known Subclasses:
EntityComboBoxModel
,ItemComboBoxModel
A default combo box model implementation based on
FilteredModel
.- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Responsible for finding an item of typeFilteredComboBoxModel.ItemFinder
by a single value of typeFilteredComboBoxModel.ItemFinder
.Nested classes/interfaces inherited from interface is.codion.common.model.FilteredModel
FilteredModel.AbstractRefresher<T>, FilteredModel.Refresher<T>
-
Field Summary
Modifier and TypeFieldDescriptionstatic final PropertyValue<String>
Specifies the caption used by default to represent null in combo box models.Fields inherited from interface is.codion.common.model.FilteredModel
ASYNC_REFRESH
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfinal void
Adds the given item to this model, respecting the sorting order if specified.final void
addListDataListener
(ListDataListener listener) final void
clear()
Clears all items from this combo box model, including the null item and sets the selected item to nullfinal boolean
cleared()
final Value<Comparator<T>>
Controls the Comparator used when sorting the visible items in this model and sorts the model accordingly.final boolean
containsItem
(T item) Returns true if this model contains the given item, visible or filtered.final <V> Value<V>
createSelectorValue
(FilteredComboBoxModel.ItemFinder<T, V> itemFinder) final boolean
Returns true if this model contains the given item, and it is filtered, that is, is not visiblefinal int
final Collection<T>
final void
Filters this model according to the condition specified byFilteredModel.includeCondition()
.final State
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.final T
getElementAt
(int index) final T
final int
getSize()
final State
final Collection<T>
items()
Provides a way for the model to prevent the addition of certain items.nullItem()
Controls the item that should represent the null value in this model.final boolean
Returns true if this model contains null and it is selected.final void
refresh()
Refreshes the items in this filtered model using itsFilteredModel.Refresher
.final FilteredModel.Refresher<T>
final void
refreshThen
(Consumer<Collection<T>> afterRefresh) Refreshes the data in this filtered model using itsFilteredModel.Refresher
.final void
Removes the given item from this modelfinal void
removeListDataListener
(ListDataListener listener) final void
Replaces the given item in this combo box modelProvides 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.final T
final StateObserver
final EventObserver<T>
final void
setItems
(Collection<T> items) Resets the items of this model using the values found initems
.final void
setSelectedItem
(Object item) Provides a way for the combo box model to prevent the selection of certain items.final boolean
Returns true if this model contains the given item, and it is visible, that is, not filteredfinal int
-
Field Details
-
COMBO_BOX_NULL_CAPTION
Specifies the caption used by default to represent null in combo box models. Value type: String
Default value: -
-
-
Constructor Details
-
FilteredComboBoxModel
public FilteredComboBoxModel()Instantiates a new FilteredComboBoxModel. The model items are sorted automatically with a default collation based comparator. To prevent sorting set the comparator to null viacomparator()
before adding items.
-
-
Method Details
-
refresher
- Specified by:
refresher
in interfaceFilteredModel<T>
- Returns:
- this models Refresher instance
-
refresh
public final void refresh()Description copied from interface:FilteredModel
Refreshes the items in this filtered model using itsFilteredModel.Refresher
.- Specified by:
refresh
in interfaceFilteredModel<T>
- See Also:
-
refreshThen
Description copied from interface:FilteredModel
Refreshes the data in this filtered model using itsFilteredModel.Refresher
. Note that this method only throws exceptions when run synchronously off the user interface thread. UseFilteredModel.Refresher.refreshFailedEvent()
to listen for exceptions that happen during asynchronous refresh.- Specified by:
refreshThen
in interfaceFilteredModel<T>
- Parameters:
afterRefresh
- called after a successful refresh, may be null- See Also:
-
clear
public final void clear()Clears all items from this combo box model, including the null item and sets the selected item to null -
cleared
public final boolean cleared()- Returns:
- true if the model data has been cleared and needs to be refreshed
-
setItems
Resets the items of this model using the values found initems
.- Parameters:
items
- the items to display in this combo box model- Throws:
IllegalArgumentException
- in case an item fails validation- See Also:
-
filterItems
public final void filterItems()Description copied from interface:FilteredModel
Filters this model according to the condition specified byFilteredModel.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 interfaceFilteredModel<T>
- See Also:
-
visibleItems
- Specified by:
visibleItems
in interfaceFilteredModel<T>
- Returns:
- an unmodifiable view of the visible items, in the order they appear in the model
-
filteredItems
- Specified by:
filteredItems
in interfaceFilteredModel<T>
- Returns:
- an unmodifiable view of the filtered items
-
items
- Specified by:
items
in interfaceFilteredModel<T>
- Returns:
- an unmodifiable view of all visible and filtered items in this model
- See Also:
-
includeCondition
- Specified by:
includeCondition
in interfaceFilteredModel<T>
- Returns:
- the include condition value
-
filteredCount
public final int filteredCount()- Specified by:
filteredCount
in interfaceFilteredModel<T>
- Returns:
- the number of currently filtered items
-
visibleCount
public final int visibleCount()- Specified by:
visibleCount
in interfaceFilteredModel<T>
- Returns:
- the number of currently visible items
-
visible
Description copied from interface:FilteredModel
Returns true if this model contains the given item, and it is visible, that is, not filtered- Specified by:
visible
in interfaceFilteredModel<T>
- Parameters:
item
- the item- Returns:
- true if the given item is visible
-
filtered
Description copied from interface:FilteredModel
Returns true if this model contains the given item, and it is filtered, that is, is not visible- Specified by:
filtered
in interfaceFilteredModel<T>
- Parameters:
item
- the item- Returns:
- true if the given item is filtered
-
add
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.- Parameters:
item
- the item to add- Throws:
IllegalArgumentException
- in case the item fails validation- See Also:
-
remove
Removes the given item from this model- Parameters:
item
- the item to remove
-
replace
Replaces the given item in this combo box model- Parameters:
item
- the item to replacereplacement
- the replacement item- Throws:
IllegalArgumentException
- in case the replacement item fails validation
-
containsItem
Description copied from interface:FilteredModel
Returns true if this model contains the given item, visible or filtered.- Specified by:
containsItem
in interfaceFilteredModel<T>
- Parameters:
item
- the item- Returns:
- true if this model contains the item
-
comparator
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 vianullItem()
. If a nullcomparator
is provided no sorting will be performed.- Returns:
- the Value controlling the comparator used when sorting, value may be null if the items of this model should not be sorted
-
itemValidator
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.- Returns:
- the Value controlling the item validator
-
selectedItemTranslator
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.- Returns:
- the Value controlling the selected item translator
-
validSelectionPredicate
Provides a way for the combo box model to prevent the selection of certain items.- Returns:
- the Value controlling the valid selection predicate
-
includeNull
- Returns:
- the State controlling whether a null value is included as the first item
- See Also:
-
nullItem
Controls the item that should represent the null value in this model. Note thatincludeNull()
must be used as well to enable the null value.- Returns:
- the Value controlling the item representing null
- See Also:
-
nullSelected
public final boolean nullSelected()Returns true if this model contains null and it is selected.- Returns:
- true if this model contains null and it is selected, false otherwise
- See Also:
-
selectionEmpty
- Returns:
- a StateObserver indicating whether the selection is empty or the value representing null is selected
-
selectedValue
- Returns:
- the selected value, null in case the value representing null is selected
- See Also:
-
getSelectedItem
- Specified by:
getSelectedItem
in interfaceComboBoxModel<T>
- Returns:
- the selected item, N.B. this can include the
nullItem
in case it has been set vianullItem()
,selectedValue()
is usually what you want
-
setSelectedItem
- Specified by:
setSelectedItem
in interfaceComboBoxModel<T>
- Parameters:
item
- the item to select
-
filterSelectedItem
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.- Returns:
- the State controlling whether the selected item is changed when it is filtered
-
addListDataListener
- Specified by:
addListDataListener
in interfaceListModel<T>
-
removeListDataListener
- Specified by:
removeListDataListener
in interfaceListModel<T>
-
getElementAt
- Specified by:
getElementAt
in interfaceListModel<T>
- Parameters:
index
- the index- Returns:
- the item at the given index
-
getSize
public final int getSize() -
createSelectorValue
- 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
- Returns:
- an observer notified each time the selection changes
-