JIDE 3.5.15

com.jidesoft.list
Class FilterableListModel

java.lang.Object
  extended by javax.swing.AbstractListModel
      extended by com.jidesoft.list.DefaultListModelWrapper
          extended by com.jidesoft.list.FilterableListModel
All Implemented Interfaces:
IndexChangeEventGenerator, EventFireListModel, ListModelWrapper, Serializable, EventListener, ListDataListener, ListModel
Direct Known Subclasses:
FilterableGroupableListModel, LuceneFilterableListModel

public class FilterableListModel
extends DefaultListModelWrapper

FilterableListModel is a list model which wraps another list model so that user can apply filters on it.

You can use addFilter(com.jidesoft.filter.Filter) to a filter to the ListModel. By default, filters won't take effect immediately. You need to call setFiltersApplied(true) to apply those filters. If filtersApplied flag is true already, you just need to call refresh(). We don't refresh automatically because you might have several filters to add. You can add all of them, then only call refresh once. setFiltersApplied(boolean) controls all the filters. Each filter has its own enabled flag which will control each individual filter.

By default, if you have more than one filters, all filters will be used in AND mode. You can see javadoc of setAndMode(boolean) to find more information.

See Also:
Serialized Form

Field Summary
protected  List<Filter> _filters
          The data structure contains all the filters.
protected  boolean _filtersApplied
          A flag to turn on/off filters.
 
Fields inherited from class com.jidesoft.list.DefaultListModelWrapper
_adjustingIndexes, _indexes, _model
 
Fields inherited from class javax.swing.AbstractListModel
listenerList
 
Constructor Summary
FilterableListModel()
           
FilterableListModel(ListModel model)
          Creates a FilterableListModel from any table model.
 
Method Summary
 void addFilter(Filter filter)
          Adds a filter to the list model.
 void addFilterableListModelListener(FilterableListModelListener l)
          Adds a listener to the list that's notified each time a change to the filter occurs.
 void addFilters(List<Filter> filters)
          Adds a list of filters to the list model.
 void clearFilters()
          Removes all filters of this list model.
 void contentsChanged(ListDataEvent e)
           
protected  void filter()
          Applies filters and generates a new array of indices.
 void fireFilterChanged(FilterableListModelEvent e)
          Forwards the given notification event to all FilterableTableModelListeners that registered themselves as listeners for this list model.
 FilterableListModelListener[] getFilterableTableModelListeners()
          Returns an array of all the FilterableListModel listeners registered on this filter.
 Filter[] getFilters()
          Gets all filters.
 boolean hasFilter()
          Checks if the FilterableTableModel has any filters.
 void intervalAdded(ListDataEvent e)
           
 void intervalRemoved(ListDataEvent e)
           
 boolean isAdjusting()
          Checks if the FilterableListModel is adjusting.
 boolean isAndMode()
          Sets the logic of filters on the list model.
 boolean isFiltersApplied()
          Checks if the filters are in effect.
 void refresh()
          Reapply all filters after they are changed.
 void removeFilter(Filter filter)
          Removes the filter from the list model.
 void removeFilterableListModelListener(FilterableListModelListener l)
          Removes a listener from the list that's notified each time a change to the filter occurs.
 void setAdjusting(boolean adjusting)
          Sets the FilterableListModel to adjusting mode.
 void setAndMode(boolean andMode)
          Sets the logic of the filters on the same column.
 void setFiltersApplied(boolean apply)
          Applies or unapplies the filters.
protected  boolean shouldBeFiltered(int row, List<Filter> filters)
          The method contains the algorithm to filter the specified row.
 boolean shouldBeFiltered(Object value, List<Filter> filters)
          The method contains the algorithm to filter the specified value.
 
Methods inherited from class com.jidesoft.list.DefaultListModelWrapper
addIndexChangeListener, createCompoundListDataEvent, fireEvents, fireIndexChanged, fireListDataEvent, getActualIndexAt, getActualModel, getElementAt, getIndexAt, getIndexChangeListeners, getIndexes, getSize, reallocateIndexes, removeIndexChangeListener, setActualModel, setIndexes
 
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
 

Field Detail

_filters

protected List<Filter> _filters
The data structure contains all the filters.


_filtersApplied

protected boolean _filtersApplied
A flag to turn on/off filters. setFiltersApplied(boolean) with true will turn it on and setFiltersApplied(boolean) with false will turn it off.

Constructor Detail

FilterableListModel

public FilterableListModel()

FilterableListModel

public FilterableListModel(ListModel model)
Creates a FilterableListModel from any table model.

Parameters:
model - list model
Method Detail

intervalAdded

public void intervalAdded(ListDataEvent e)
Specified by:
intervalAdded in interface ListDataListener
Overrides:
intervalAdded in class DefaultListModelWrapper

intervalRemoved

public void intervalRemoved(ListDataEvent e)
Specified by:
intervalRemoved in interface ListDataListener
Overrides:
intervalRemoved in class DefaultListModelWrapper

contentsChanged

public void contentsChanged(ListDataEvent e)
Specified by:
contentsChanged in interface ListDataListener
Overrides:
contentsChanged in class DefaultListModelWrapper

refresh

public void refresh()
Reapply all filters after they are changed.


filter

protected void filter()
Applies filters and generates a new array of indices.


shouldBeFiltered

protected boolean shouldBeFiltered(int row,
                                   List<Filter> filters)
The method contains the algorithm to filter the specified row. If you want to filter the row, return true. Otherwise, return false. The default algorithm will filter the row whose if any of columns filters return true. Subclass can override this method to do a different way. Use getFilters(int col) to get filters. You should always check for isFiltersApplied(). If it's false, always return false. You should also check for each filter to see if it's enabled (filter.isEnabled()). If enabled return false, you should skip the filter.

Parameters:
row - the row index
filters - The list of filters
Returns:
true if the row should be filtered. Otherwise, false.

shouldBeFiltered

public boolean shouldBeFiltered(Object value,
                                List<Filter> filters)
The method contains the algorithm to filter the specified value. Assume the filters have already been configured at shouldBeFiltered(int, java.util.List) if some of the filters are row index sensitive.

Parameters:
value - the value
filters - The list of filters
Returns:
true if the value should be filtered. Otherwise, false.

addFilterableListModelListener

public void addFilterableListModelListener(FilterableListModelListener l)
Adds a listener to the list that's notified each time a change to the filter occurs.

Parameters:
l - the FilterableListModelListener

removeFilterableListModelListener

public void removeFilterableListModelListener(FilterableListModelListener l)
Removes a listener from the list that's notified each time a change to the filter occurs.

Parameters:
l - the FilterableListModelListener

getFilterableTableModelListeners

public FilterableListModelListener[] getFilterableTableModelListeners()
Returns an array of all the FilterableListModel listeners registered on this filter.

Returns:
all of this filter's FilterableListModelListeners or an empty array if no filter listeners are currently registered
See Also:
addFilterableListModelListener(com.jidesoft.list.FilterableListModelListener), removeFilterableListModelListener(com.jidesoft.list.FilterableListModelListener)

fireFilterChanged

public void fireFilterChanged(FilterableListModelEvent e)
Forwards the given notification event to all FilterableTableModelListeners that registered themselves as listeners for this list model.

Parameters:
e - the event to be forwarded
See Also:
addFilterableListModelListener(com.jidesoft.list.FilterableListModelListener), FilterableListModelEvent, EventListenerList

addFilters

public void addFilters(List<Filter> filters)
Adds a list of filters to the list model.

Parameters:
filters - the filters to be added

addFilter

public void addFilter(Filter filter)
Adds a filter to the list model.

You can use AbstractFilter to create new filter. If you need the row index in order to decide if the value should be filtered, you can use AbstractListFilter and use getRowIndex() to find out current row index.

Parameters:
filter - the filter to be added

removeFilter

public void removeFilter(Filter filter)
Removes the filter from the list model.

Parameters:
filter - the filter to be removed

clearFilters

public void clearFilters()
Removes all filters of this list model.


getFilters

public Filter[] getFilters()
Gets all filters.

Returns:
the filters.

setFiltersApplied

public void setFiltersApplied(boolean apply)
Applies or unapplies the filters. By default, the filters are not applied. So after user adds several filters, this method should be called to make filters taking effect. When new filter is added or existing is removed, this method should be called as well.

Parameters:
apply - true to apply the filters.

isFiltersApplied

public boolean isFiltersApplied()
Checks if the filters are in effect.

Returns:
true if filters are in effect.

hasFilter

public boolean hasFilter()
Checks if the FilterableTableModel has any filters.

Returns:
true if there are any filters. Otherwise false.

isAndMode

public boolean isAndMode()
Sets the logic of filters on the list model. If true, filters are in AND mode, meaning if one of the filters return true in isValueFiltered method, the value will be filtered. If false, filters are in OR mode, meaning if one of the filters return false, the value will NOT be filtered.

Returns:
the AND/OR mode. Default is true which means AND mode.

setAndMode

public void setAndMode(boolean andMode)
Sets the logic of the filters on the same column.

Parameters:
andMode - the and mode flag

isAdjusting

public boolean isAdjusting()
Checks if the FilterableListModel is adjusting. If it is adjusting, FilterableListModel will not apply filter when the actual list model changes.

Returns:
true if adjusting. Otherwise false.

setAdjusting

public void setAdjusting(boolean adjusting)
Sets the FilterableListModel to adjusting mode. If it is adjusting, FilterableListModel will not apply filter when the actual list model changes. After you set adjusting to false, FilterableListModel will re-apply the filter.

Parameters:
adjusting - the adjusting flag

JIDE 3.5.15