Class PredicateSuggestModel<T>

  • Type Parameters:
    T - the class of the list items
    All Implemented Interfaces:
    LazyLoadComboBoxModel<T>, SuggestModel<T>, java.io.Serializable, java.lang.Iterable<T>, javax.swing.ComboBoxModel<T>, javax.swing.ListModel<T>

    public class PredicateSuggestModel<T>
    extends javax.swing.AbstractListModel<T>
    implements SuggestModel<T>, LazyLoadComboBoxModel<T>
    An implementation of SuggestModel that suggests the items from a list that match a predicate.
    See Also:
    Serialized Form
    • Field Summary

      • Fields inherited from class javax.swing.AbstractListModel

        listenerList
    • Constructor Summary

      Constructors 
      Constructor Description
      PredicateSuggestModel​(java.util.function.BiPredicate<T,​java.lang.String> filter, java.util.Collection<T> items, java.util.Comparator<? super T> comparator)
      Create a model that suggests the items that match a predicate.
      PredicateSuggestModel​(java.util.function.BiPredicate<T,​java.lang.String> filter, java.util.Comparator<? super T> comparator)
      Create a model that suggests the items that match a predicate.
      PredicateSuggestModel​(java.util.function.Function<T,​java.lang.String> converter, java.util.Comparator<? super T> comparator)
      Create a model that suggests the items that contain the input text.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addElement​(T item)
      Add an item to the list of available choices.
      void addMissingElements​(java.util.Collection<? extends T> items)
      Add items to the list of available choices, skipping items that are already in the list.
      T getElementAt​(int index)
      Get an item that matches the filter.
      java.lang.Object getSelectedItem()  
      int getSize()
      Get the number of items matching the filter.
      static <T> PredicateSuggestModel<T> ignoreCase​(java.util.function.Function<T,​java.lang.String> converter, java.util.Comparator<? super T> comparator)
      Create a model that suggests the items that contain the input text, ignoring case.
      int indexOf​(java.lang.Object item)
      Get the index of an item in the list of available choices.
      java.util.Iterator<T> iterator()
      Create an iterator for the filtered items.
      void setElements​(java.util.Collection<? extends T> items, boolean allowSelectionNotInList)
      Set the list of available choices.
      void setSelectedItem​(java.lang.Object anItem)  
      T updateSuggestions​(java.lang.String editorText)
      Update the elements in this model and return the selected item.
      • Methods inherited from class javax.swing.AbstractListModel

        addListDataListener, fireContentsChanged, fireIntervalAdded, fireIntervalRemoved, getListDataListeners, getListeners, removeListDataListener
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
      • Methods inherited from interface javax.swing.ListModel

        addListDataListener, removeListDataListener
    • Constructor Detail

      • PredicateSuggestModel

        public PredicateSuggestModel​(java.util.function.Function<T,​java.lang.String> converter,
                                     java.util.Comparator<? super T> comparator)
        Create a model that suggests the items that contain the input text.
        Parameters:
        converter - a function for converting the list items to strings
        comparator - a comparator for ordering the list items
      • PredicateSuggestModel

        public PredicateSuggestModel​(java.util.function.BiPredicate<T,​java.lang.String> filter,
                                     java.util.Comparator<? super T> comparator)
        Create a model that suggests the items that match a predicate.
        Parameters:
        filter - a predicate for suggesting items
        comparator - a comparator for ordering the list items
      • PredicateSuggestModel

        public PredicateSuggestModel​(java.util.function.BiPredicate<T,​java.lang.String> filter,
                                     java.util.Collection<T> items,
                                     java.util.Comparator<? super T> comparator)
        Create a model that suggests the items that match a predicate.
        Parameters:
        filter - a predicate for suggesting items
        items - the unfiltered list of items
        comparator - a comparator for ordering the list items
    • Method Detail

      • ignoreCase

        public static <T> PredicateSuggestModel<T> ignoreCase​(java.util.function.Function<T,​java.lang.String> converter,
                                                              java.util.Comparator<? super T> comparator)
        Create a model that suggests the items that contain the input text, ignoring case.
        Parameters:
        converter - a function for converting the list items to strings
        comparator - a comparator for ordering the list items
      • updateSuggestions

        public T updateSuggestions​(java.lang.String editorText)
        Description copied from interface: SuggestModel
        Update the elements in this model and return the selected item.
        Specified by:
        updateSuggestions in interface SuggestModel<T>
        Parameters:
        editorText - the current editor text
        Returns:
        the selected item based on the editor text
      • setElements

        public void setElements​(java.util.Collection<? extends T> items,
                                boolean allowSelectionNotInList)
        Set the list of available choices.
        Specified by:
        setElements in interface LazyLoadComboBoxModel<T>
        Parameters:
        items - the unfiltered list of choices
        allowSelectionNotInList - if true then the selected item is not changed, if false then the selected item is cleared if it is not in list of filtered items
      • addMissingElements

        public void addMissingElements​(java.util.Collection<? extends T> items)
        Add items to the list of available choices, skipping items that are already in the list.
        Specified by:
        addMissingElements in interface LazyLoadComboBoxModel<T>
        Parameters:
        items - the items to add
      • addElement

        public void addElement​(T item)
        Add an item to the list of available choices.
        Specified by:
        addElement in interface LazyLoadComboBoxModel<T>
        Parameters:
        item - the item to append to the list
      • indexOf

        public int indexOf​(java.lang.Object item)
        Get the index of an item in the list of available choices.
        Specified by:
        indexOf in interface LazyLoadComboBoxModel<T>
        Parameters:
        item - the item to find
        Returns:
        the index of the item or -1 if it is not in the list of available choices
      • iterator

        public java.util.Iterator<T> iterator()
        Create an iterator for the filtered items.
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Returns:
        an iterator for the filtered items
      • setSelectedItem

        public void setSelectedItem​(java.lang.Object anItem)
        Specified by:
        setSelectedItem in interface javax.swing.ComboBoxModel<T>
      • getSelectedItem

        public java.lang.Object getSelectedItem()
        Specified by:
        getSelectedItem in interface javax.swing.ComboBoxModel<T>
      • getSize

        public int getSize()
        Get the number of items matching the filter.
        Specified by:
        getSize in interface javax.swing.ListModel<T>
        Returns:
        the number of items matching the filter
      • getElementAt

        public T getElementAt​(int index)
        Get an item that matches the filter.
        Specified by:
        getElementAt in interface javax.swing.ListModel<T>
        Parameters:
        index - the index of the item in the filtered list
        Returns:
        the item at the specified index