Class PredicateSuggestModel<T>
- java.lang.Object
-
- javax.swing.AbstractListModel<T>
-
- io.github.jonestimd.swing.component.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 ofSuggestModel
that suggests the items from a list that match a predicate.- See Also:
- Serialized Form
-
-
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
-
-
-
-
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 stringscomparator
- 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 itemscomparator
- 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 itemsitems
- the unfiltered list of itemscomparator
- 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 stringscomparator
- 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 interfaceSuggestModel<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 interfaceLazyLoadComboBoxModel<T>
- Parameters:
items
- the unfiltered list of choicesallowSelectionNotInList
- if true then the selected item is not changed, if false then the selected item is cleared if it is not in list of filtereditems
-
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 interfaceLazyLoadComboBoxModel<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 interfaceLazyLoadComboBoxModel<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 interfaceLazyLoadComboBoxModel<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 interfacejava.lang.Iterable<T>
- Returns:
- an iterator for the filtered items
-
setSelectedItem
public void setSelectedItem(java.lang.Object anItem)
- Specified by:
setSelectedItem
in interfacejavax.swing.ComboBoxModel<T>
-
getSelectedItem
public java.lang.Object getSelectedItem()
- Specified by:
getSelectedItem
in interfacejavax.swing.ComboBoxModel<T>
-
getSize
public int getSize()
Get the number of items matching the filter.- Specified by:
getSize
in interfacejavax.swing.ListModel<T>
- Returns:
- the number of items matching the filter
-
-