Package adams.gui.core
Class SearchableWrapperListModel
- java.lang.Object
-
- javax.swing.AbstractListModel
-
- adams.gui.core.SearchableWrapperListModel
-
- All Implemented Interfaces:
CustomSearchListModel
,SearchableListModel
,Serializable
,ListModel
public class SearchableWrapperListModel extends AbstractListModel implements SearchableListModel, CustomSearchListModel
Wraps around any list model and makes them automatically searchable.- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected gnu.trove.map.TIntIntMap
m_ActualToDisplayIndex
the relation between actual and display index.protected gnu.trove.list.TIntList
m_DisplayIndices
the indices of the indices to display that match a search string.protected gnu.trove.set.TIntSet
m_DisplayIndicesSet
the indices of the indices to display that match a search string.protected ListModel
m_Model
the actual list model.protected boolean
m_RegExpSearch
whether the search was a regular expression based one.protected String
m_SearchString
the string that was searched for.-
Fields inherited from class javax.swing.AbstractListModel
listenerList
-
-
Constructor Summary
Constructors Constructor Description SearchableWrapperListModel()
initializes with no model.SearchableWrapperListModel(ListModel model)
initializes with the given model.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
getActualIndex(int visibleIndex)
Returns the actual underlying index the given visible one represents.ListModel
getActualModel()
returns the underlying model, can be null.int
getActualSize()
Returns the actual element count in the model.Object
getElementAt(int index)
Returns the value at the specified index.String
getSeachString()
Returns the current search string.int
getSize()
Returns the number of (visible) elements in the model.int
getVisibleIndex(int actualIndex)
Returns the visible index for an actual index in the model.protected void
initialize()
Initializes indices etc.protected boolean
isInitialized()
whether the model is initialized.boolean
isRegExpSearch()
Returns whether the last search was a regular expression based one.boolean
isSearchMatch(SearchParameters params, int index)
Tests whether the search matches the specified element index.boolean
isVisible(int actualIndex)
Checks whether the index from the actual underlying model is visible.void
search(String searchString, boolean regexp)
Performs a search for the given string.void
setActualModel(ListModel value)
Sets the model to use.-
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 javax.swing.ListModel
addListDataListener, removeListDataListener
-
-
-
-
Field Detail
-
m_Model
protected ListModel m_Model
the actual list model.
-
m_SearchString
protected String m_SearchString
the string that was searched for.
-
m_RegExpSearch
protected boolean m_RegExpSearch
whether the search was a regular expression based one.
-
m_DisplayIndices
protected gnu.trove.list.TIntList m_DisplayIndices
the indices of the indices to display that match a search string.
-
m_DisplayIndicesSet
protected gnu.trove.set.TIntSet m_DisplayIndicesSet
the indices of the indices to display that match a search string.
-
m_ActualToDisplayIndex
protected gnu.trove.map.TIntIntMap m_ActualToDisplayIndex
the relation between actual and display index.
-
-
Constructor Detail
-
SearchableWrapperListModel
public SearchableWrapperListModel()
initializes with no model.
-
SearchableWrapperListModel
public SearchableWrapperListModel(ListModel model)
initializes with the given model.- Parameters:
model
- the model to initialize with
-
-
Method Detail
-
setActualModel
public void setActualModel(ListModel value)
Sets the model to use.- Parameters:
value
- the model to use
-
getActualModel
public ListModel getActualModel()
returns the underlying model, can be null.- Returns:
- the current model
-
initialize
protected void initialize()
Initializes indices etc.
-
isInitialized
protected boolean isInitialized()
whether the model is initialized.- Returns:
- true if the model is not null
-
getActualIndex
public int getActualIndex(int visibleIndex)
Returns the actual underlying index the given visible one represents. Useful for retrieving "non-visual" data that is also stored in a ListModel.- Specified by:
getActualIndex
in interfaceSearchableListModel
- Parameters:
visibleIndex
- the displayed index to retrieve the original index for- Returns:
- the original index
-
isVisible
public boolean isVisible(int actualIndex)
Checks whether the index from the actual underlying model is visible.- Parameters:
actualIndex
- the actual index to check for visibility- Returns:
- true if visible
-
getVisibleIndex
public int getVisibleIndex(int actualIndex)
Returns the visible index for an actual index in the model.- Parameters:
actualIndex
- the actual index to get the visible index for- Returns:
- the index, -1 if failed to locate
-
getSize
public int getSize()
Returns the number of (visible) elements in the model.
-
getActualSize
public int getActualSize()
Returns the actual element count in the model.- Specified by:
getActualSize
in interfaceSearchableListModel
- Returns:
- the element count in the underlying data
-
isSearchMatch
public boolean isSearchMatch(SearchParameters params, int index)
Tests whether the search matches the specified element index.
Default implementation just checks against the strings that getElementAt(...) returns (using the toString() method of the returned objects). Derived classes should override this method in order to implement a proper/faster search functionality.- Specified by:
isSearchMatch
in interfaceCustomSearchListModel
- Parameters:
params
- the search parametersindex
- the index of the element of the underlying model- Returns:
- true if the search matches this element
-
search
public void search(String searchString, boolean regexp)
Performs a search for the given string. Limits the display of indices to ones containing the search string.- Specified by:
search
in interfaceSearchableListModel
- Parameters:
searchString
- the string to search forregexp
- whether to perform regular expression matching or just plain string comparison
-
getSeachString
public String getSeachString()
Returns the current search string.- Specified by:
getSeachString
in interfaceSearchableListModel
- Returns:
- the search string, null if not filtered
-
isRegExpSearch
public boolean isRegExpSearch()
Returns whether the last search was a regular expression based one.- Specified by:
isRegExpSearch
in interfaceSearchableListModel
- Returns:
- true if last search was a reg exp one
-
getElementAt
public Object getElementAt(int index)
Returns the value at the specified index.- Specified by:
getElementAt
in interfaceListModel
- Parameters:
index
- the requested index- Returns:
- the value at
index
-
-