Package ca.cgjennings.ui
Class FilteredListModel<E>
- java.lang.Object
-
- javax.swing.AbstractListModel<E>
-
- ca.cgjennings.ui.FilteredListModel<E>
-
- All Implemented Interfaces:
java.io.Serializable
,javax.swing.ListModel<E>
public class FilteredListModel<E> extends javax.swing.AbstractListModel<E>
A model forJList
s that supports filtering. TheListModel
methodsgetElementAt
andgetSize
return values appropriate for the applied filter. It is recommended that you get and set the selection using values rather than indices.- Since:
- 3.0
- Author:
- Chris Jennings
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FilteredListModel.ChoosableListFilter
A list filter that can be selected from a list of filters.static interface
FilteredListModel.FilterChangeListener
static interface
FilteredListModel.ListFilter<E>
-
Field Summary
Fields Modifier and Type Field Description static FilteredListModel.ListFilter
ACCEPT_ALL_FILTER
Deprecated.UseacceptAllFilter()
to get a typed filter that accepts all items.
-
Constructor Summary
Constructors Constructor Description FilteredListModel()
FilteredListModel(E[] items)
FilteredListModel(java.util.Collection<? extends E> items)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> FilteredListModel.ListFilter<T>
acceptAllFilter()
Returns a filter that accepts all items, of a type matching that of the list model.void
add(int index, E item)
void
add(E item)
void
clear()
Removes all elements from the list.static <E> FilteredListModel.ListFilter<E>
createRegexFilter(java.lang.String regex)
static <E> FilteredListModel.ListFilter<E>
createStringFilter(java.lang.String substring)
E
getElementAt(int index)
E
getItem(int index)
Returns the item at the unfiltered index.int
getItemCount()
Returns the size of the unfiltered list.int
getSize()
int
getUnfilteredIndex(java.lang.Object o)
Returns the index of the first object equal too
, ignoring the current filter.void
linkTo(JFilterField field, javax.swing.JList<E> list, boolean restoreSelection)
This is a convenience method that attaches this model to the current document of the specified search field.static <E> void
restoreSelection(javax.swing.JList<E> list, java.util.List<E> selection)
Resores a previously stashed selection.void
setFilter(FilteredListModel.ListFilter<E> f)
static <E> java.util.List<E>
stashSelection(javax.swing.JList<E> list)
Captures the current selection for use withrestoreSelection(JList, List)
.
-
-
-
Field Detail
-
ACCEPT_ALL_FILTER
@Deprecated public static final FilteredListModel.ListFilter ACCEPT_ALL_FILTER
Deprecated.UseacceptAllFilter()
to get a typed filter that accepts all items.A filter that accepts all items.
-
-
Method Detail
-
add
public void add(E item)
-
add
public void add(int index, E item)
-
getItem
public E getItem(int index)
Returns the item at the unfiltered index.- Parameters:
index
- the model index- Returns:
- the item at the specified index
-
getItemCount
public int getItemCount()
Returns the size of the unfiltered list.- Returns:
- the unfiltered model size
-
clear
public void clear()
Removes all elements from the list.
-
getUnfilteredIndex
public int getUnfilteredIndex(java.lang.Object o)
Returns the index of the first object equal too
, ignoring the current filter. This is useful when the list is an index into a list of objects and the indexed object must be retrieved.- Parameters:
o
- the object to find the index of- Returns:
- the unfiltered index of the first such object, or -1
-
setFilter
public void setFilter(FilteredListModel.ListFilter<E> f)
-
createRegexFilter
public static <E> FilteredListModel.ListFilter<E> createRegexFilter(java.lang.String regex)
-
createStringFilter
public static <E> FilteredListModel.ListFilter<E> createStringFilter(java.lang.String substring)
-
linkTo
public void linkTo(JFilterField field, javax.swing.JList<E> list, boolean restoreSelection)
This is a convenience method that attaches this model to the current document of the specified search field. When the text in the document changes, the text from the field will be used to create a string filter, which will then be applied to the list.- Parameters:
field
- the field to link this model tolist
- the list whose items are filteredrestoreSelection
- if true, any selection is restore after updating the listT
- the item type of the target list
-
stashSelection
public static <E> java.util.List<E> stashSelection(javax.swing.JList<E> list)
Captures the current selection for use withrestoreSelection(JList, List)
.
-
restoreSelection
public static <E> void restoreSelection(javax.swing.JList<E> list, java.util.List<E> selection)
Resores a previously stashed selection. This can be used to restore the selection after the filter has been updated.- Parameters:
list
- the list to restore the selection toselection
- the selection to restore
-
getElementAt
public E getElementAt(int index)
-
getSize
public int getSize()
-
acceptAllFilter
public static <T> FilteredListModel.ListFilter<T> acceptAllFilter()
Returns a filter that accepts all items, of a type matching that of the list model.- Type Parameters:
T
- the type of the list model- Returns:
- a filter that accepts all items
-
-