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:
is.codion.common.model.FilteredModel<T>, ComboBoxModel<T>, ListModel<T>
Direct Known Subclasses:
ItemComboBoxModel

public class FilteredComboBoxModel<T> extends Object implements is.codion.common.model.FilteredModel<T>, ComboBoxModel<T>
A default combo box model implementation based on FilteredModel.
See Also:
  • Field Details

    • COMBO_BOX_NULL_CAPTION

      public static final is.codion.common.property.PropertyValue<String> 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 via comparator() before adding items.
  • Method Details

    • refresher

      public final is.codion.common.model.FilteredModel.Refresher<T> refresher()
      Specified by:
      refresher in interface is.codion.common.model.FilteredModel<T>
    • refresh

      public final void refresh()
      Specified by:
      refresh in interface is.codion.common.model.FilteredModel<T>
    • refreshThen

      public final void refreshThen(Consumer<Collection<T>> afterRefresh)
      Specified by:
      refreshThen in interface is.codion.common.model.FilteredModel<T>
    • 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

      public final void setItems(Collection<T> items)
      Resets the items of this model using the values found in items, if items is null then the model is cleared.
      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()
      Specified by:
      filterItems in interface is.codion.common.model.FilteredModel<T>
    • visibleItems

      public final List<T> visibleItems()
      Specified by:
      visibleItems in interface is.codion.common.model.FilteredModel<T>
    • filteredItems

      public final Collection<T> filteredItems()
      Specified by:
      filteredItems in interface is.codion.common.model.FilteredModel<T>
    • items

      public final Collection<T> items()
      Specified by:
      items in interface is.codion.common.model.FilteredModel<T>
    • includeCondition

      public final is.codion.common.value.Value<Predicate<T>> includeCondition()
      Specified by:
      includeCondition in interface is.codion.common.model.FilteredModel<T>
    • filteredCount

      public final int filteredCount()
      Specified by:
      filteredCount in interface is.codion.common.model.FilteredModel<T>
    • visibleCount

      public final int visibleCount()
      Specified by:
      visibleCount in interface is.codion.common.model.FilteredModel<T>
    • visible

      public final boolean visible(T item)
      Specified by:
      visible in interface is.codion.common.model.FilteredModel<T>
    • filtered

      public final boolean filtered(T item)
      Specified by:
      filtered in interface is.codion.common.model.FilteredModel<T>
    • add

      public final void add(T item)
      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

      public final void remove(T item)
      Removes the given item from this model
      Parameters:
      item - the item to remove
    • replace

      public final void replace(T item, T replacement)
      Replaces the given item in this combo box model
      Parameters:
      item - the item to replace
      replacement - the replacement item
      Throws:
      IllegalArgumentException - in case the replacement item fails validation
    • containsItem

      public final boolean containsItem(T item)
      Specified by:
      containsItem in interface is.codion.common.model.FilteredModel<T>
    • comparator

      public final is.codion.common.value.Value<Comparator<T>> 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 via nullItem(). If a null comparator 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

      public final is.codion.common.value.Value<Predicate<T>> 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

      public final is.codion.common.value.Value<Function<Object,T>> 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
    • allowSelectionPredicate

      public final is.codion.common.value.Value<Predicate<T>> allowSelectionPredicate()
      Provides a way for the combo box model to prevent the selection of certain items.
      Returns:
      the Value controlling the allow selection predicate
      Throws:
      IllegalArgumentException - in case the current selected item does not satisfy the allow selection predicate
    • includeNull

      public final is.codion.common.state.State includeNull()
      Returns:
      the State controlling whether a null value is included as the first item
      See Also:
    • nullItem

      public final is.codion.common.value.Value<T> nullItem()
      Controls the item that should represent the null value in this model. Note that includeNull() 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

      public final is.codion.common.state.StateObserver selectionEmpty()
      Returns:
      a StateObserver indicating whether the selection is empty or the value representing null is selected
    • selectedValue

      public final T selectedValue()
      Returns:
      the selected value, null in case the value representing null is selected
      See Also:
    • getSelectedItem

      public final T getSelectedItem()
      Specified by:
      getSelectedItem in interface ComboBoxModel<T>
      Returns:
      the selected item, N.B. this can include the nullItem in case it has been set via nullItem(), selectedValue() is usually what you want
    • setSelectedItem

      public final void setSelectedItem(Object item)
      Specified by:
      setSelectedItem in interface ComboBoxModel<T>
      Parameters:
      item - the item to select
    • filterSelectedItem

      public final is.codion.common.state.State 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

      public final void addListDataListener(ListDataListener listener)
      Specified by:
      addListDataListener in interface ListModel<T>
    • removeListDataListener

      public final void removeListDataListener(ListDataListener listener)
      Specified by:
      removeListDataListener in interface ListModel<T>
    • getElementAt

      public final T getElementAt(int index)
      Specified by:
      getElementAt in interface ListModel<T>
      Parameters:
      index - the index
      Returns:
      the item at the given index
    • getSize

      public final int getSize()
      Specified by:
      getSize in interface ListModel<T>
      Returns:
      the number of visible items in this model
    • createSelectorValue

      public final <V> is.codion.common.value.Value<V> createSelectorValue(FilteredComboBoxModel.ItemFinder<T,V> itemFinder)
      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
    • addSelectionListener

      public final void addSelectionListener(Consumer<T> listener)
      Parameters:
      listener - a listener notified each time the selection changes
    • removeSelectionListener

      public final void removeSelectionListener(Consumer<T> listener)
      Parameters:
      listener - a selection listener to remove