JIDE 3.5.15

com.jidesoft.tree
Class FilterableTreeModel

java.lang.Object
  extended by com.jidesoft.tree.AbstractTreeModel
      extended by com.jidesoft.tree.DefaultTreeModelWrapper
          extended by com.jidesoft.tree.FilterableTreeModel
All Implemented Interfaces:
TreeModelWrapper, EventListener, TreeModelListener, TreeModel

public class FilterableTreeModel
extends DefaultTreeModelWrapper

FilterableTreeModel is a TreeModelWrapper that can handle filters.

You can add multiple filters to this tree model. By default, the filters are AND relation, meaning only as long as one filter return true for a value, the value will be filtered. You can call setAndMode(boolean) to set to false. If so, as long as one filter return false for a value, the value will be kept.

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.


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.tree.DefaultTreeModelWrapper
EVENT_TYPE_NODES_CHANGED, EVENT_TYPE_NODES_INSERTED, EVENT_TYPE_NODES_REMOVED, EVENT_TYPE_STRUCTURE_CHANGED
 
Fields inherited from class com.jidesoft.tree.AbstractTreeModel
listenerList
 
Constructor Summary
FilterableTreeModel()
           
FilterableTreeModel(TreeModel model)
          Creates a FilterableTreeModel from any tree model.
 
Method Summary
 void addFilter(Filter filter)
          Adds a filter.
 void addFilters(List<Filter> filters)
          Adds a list of filters.
 void clearFilters()
          Removes all filters.
 void clearFiltersOnTreePath(TreePath path)
          Clears the filters related to the tree path.
protected  void configureListModelWrapper(ListModelWrapper wrapper, Object node)
          This is the method that will be called to configure the empty list model wrapper you created in DefaultTreeModelWrapper.createListModelWrapper(Object).
protected  ListModelWrapper createListModelWrapper(Object node)
          Creates an empty list model wrapper.
 Filter[] getFilters()
          Gets the filters.
protected  Object getParent(Object node)
          Get parent object of the specific node.
 boolean isAndMode()
          Sets the logic of filters.
protected  boolean isFiltered(Object node)
          Check if the node should be filtered away.
protected  boolean isFilterEndNode(Object node)
          Checks if the node is filter end node.
 boolean isFilterEntireTreeImmediately()
           
 boolean isFiltersApplied()
          Checks if the filters are in effect.
 boolean isHideEmptyParentNode()
          Returns the value of hideEmptyParentNode property.
 boolean isKeepAllChildren()
          If a parent node matches, all its children will be kept if this flag is true.
 boolean isMatchesLeafNodeOnly()
          If the filters matches the leaf node only.
 void refresh()
          Reapply all filters after they are changed.
 void removeFilter(Filter filter)
          Removes the filters.
 void setAndMode(boolean andMode)
          Sets the logic of the filters.
 void setFilterEntireTreeImmediately(boolean filterEntireTreeImmediately)
           
 void setFiltersApplied(boolean apply)
          Applies or unapplies the filters.
 void setHideEmptyParentNode(boolean hideEmptyParentNode)
          Sets the value of hideEmptyParentNode property.
 void setKeepAllChildren(boolean keepAllChildren)
          Sets the value of keepAllChildren property.
 void setMatchesLeafNodeOnly(boolean matchesLeafNodeOnly)
          Sets the value of matchesLeafNodeOnly property.
 void treeNodesChanged(TreeModelEvent e)
          Method for TreeModelListener.
 void treeNodesInserted(TreeModelEvent e)
          Method for TreeModelListener.
 void treeNodesRemoved(TreeModelEvent e)
          Method for TreeModelListener.
 void treeStructureChanged(TreeModelEvent e)
          Method for TreeModelListener.
protected  void updateTree(TreeModelEvent e)
          Updates the tree based on the TreeModelEvent.
protected  void updateTreeOnNodeChanged(TreeModelEvent e)
          Updates the tree based on the node changed event.
protected  void updateTreeOnNodeInserted(TreeModelEvent e)
          Updates the tree based on the node inserted event.
protected  void updateTreeOnNodeRemoved(TreeModelEvent e)
          Updates the tree based on the node removed event.
protected  void updateTreeOnStructureChanged(TreeModelEvent e)
          Updates the tree based on the tree structure changed event.
 
Methods inherited from class com.jidesoft.tree.DefaultTreeModelWrapper
clearAllListModelWrappers, clearListModelWrapper, convertTreeModelEvent, convertTreeModelEvent, getActualModel, getChild, getChildCount, getIndexOfChild, getListModelWrapper, getRoot, initWrapper, isLeaf, setActualModel, setListModelWrapper, valueForPathChanged
 
Methods inherited from class com.jidesoft.tree.AbstractTreeModel
addTreeModelListener, fireTreeNodesChanged, fireTreeNodesInserted, fireTreeNodesRemoved, fireTreeStructureChanged, fireTreeStructureChanged, getPathToRoot, getPathToRoot, getTreeModelListeners, nodeChanged, nodesChanged, removeTreeModelListener
 
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

FilterableTreeModel

public FilterableTreeModel()

FilterableTreeModel

public FilterableTreeModel(TreeModel model)
Creates a FilterableTreeModel from any tree model.

Parameters:
model - the tree model
Method Detail

createListModelWrapper

protected ListModelWrapper createListModelWrapper(Object node)
Description copied from class: DefaultTreeModelWrapper
Creates an empty list model wrapper. Subclass can override this method to create their special list model wrapper. For example, FilterableTreeModel overrides it to create an instance of FilterableListModel. SortableTreeModel overrides it to create an instance of SortableListModel.

Default implementation of this method in this class simply returns a new instance of DefaultListModelWrapper.

Overrides:
createListModelWrapper in class DefaultTreeModelWrapper
Parameters:
node - the tree node
Returns:
an empty list model wrapper.

configureListModelWrapper

protected void configureListModelWrapper(ListModelWrapper wrapper,
                                         Object node)
Description copied from class: DefaultTreeModelWrapper
This is the method that will be called to configure the empty list model wrapper you created in DefaultTreeModelWrapper.createListModelWrapper(Object). Subclass can override it to configure the list model. For example, FilterableTreeModel uses this method to add filters that defined on FilterableTreeModel to FilterableListModel and turn those filters on.

In default implementation on DefaultTreeModelWrapper, this method is empty.

Overrides:
configureListModelWrapper in class DefaultTreeModelWrapper
Parameters:
wrapper - the wrapper to be configured.
node - the tree node

isFilterEndNode

protected boolean isFilterEndNode(Object node)
Checks if the node is filter end node.

If the node is a filter end node, the filter will not apply to its descendants. It will help you avoid potential recursive request if you return true for those nodes.

By default, it will return false directly.

Parameters:
node - the tree node to check
Returns:
true if the node is a filter end node. Otherwise false.

addFilters

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

Parameters:
filters - the filters to be added

addFilter

public void addFilter(Filter filter)
Adds a filter.

Parameters:
filter - the filter to be added

removeFilter

public void removeFilter(Filter filter)
Removes the filters. The filter must be added using addFilter(Filter).

Parameters:
filter - the filter to be removed

clearFilters

public void clearFilters()
Removes all filters.


getFilters

public Filter[] getFilters()
Gets the filters.

Returns:
the filters.
See Also:
addFilters(java.util.List)

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.

refresh

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


isFiltersApplied

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

Returns:
true if filters are in effect.

isAndMode

public boolean isAndMode()
Sets the logic of filters. 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.

Parameters:
andMode - the filter mode

treeNodesInserted

public void treeNodesInserted(TreeModelEvent e)
Description copied from class: DefaultTreeModelWrapper
Method for TreeModelListener. It will listen to the event from actual tree model and fire converted tree model for this tree model.

Specified by:
treeNodesInserted in interface TreeModelListener
Overrides:
treeNodesInserted in class DefaultTreeModelWrapper
Parameters:
e - the TreeModelEvent

treeNodesRemoved

public void treeNodesRemoved(TreeModelEvent e)
Description copied from class: DefaultTreeModelWrapper
Method for TreeModelListener. It will listen to the event from actual tree model and fire converted tree model for this tree model.

Specified by:
treeNodesRemoved in interface TreeModelListener
Overrides:
treeNodesRemoved in class DefaultTreeModelWrapper
Parameters:
e - the TreeModelEvent

treeStructureChanged

public void treeStructureChanged(TreeModelEvent e)
Description copied from class: DefaultTreeModelWrapper
Method for TreeModelListener. It will listen to the event from actual tree model and fire converted tree model for this tree model.

Specified by:
treeStructureChanged in interface TreeModelListener
Overrides:
treeStructureChanged in class DefaultTreeModelWrapper
Parameters:
e - the TreeModelEvent

treeNodesChanged

public void treeNodesChanged(TreeModelEvent e)
Description copied from class: DefaultTreeModelWrapper
Method for TreeModelListener. It will listen to the event from actual tree model and fire converted tree model for this tree model.

Specified by:
treeNodesChanged in interface TreeModelListener
Overrides:
treeNodesChanged in class DefaultTreeModelWrapper
Parameters:
e - the TreeModelEvent

clearFiltersOnTreePath

public void clearFiltersOnTreePath(TreePath path)
Clears the filters related to the tree path.

Parameters:
path - the tree path.

updateTreeOnNodeInserted

protected void updateTreeOnNodeInserted(TreeModelEvent e)
Updates the tree based on the node inserted event.

Parameters:
e - TreeModelEvent

updateTreeOnNodeRemoved

protected void updateTreeOnNodeRemoved(TreeModelEvent e)
Updates the tree based on the node removed event.

Parameters:
e - TreeModelEvent

updateTreeOnNodeChanged

protected void updateTreeOnNodeChanged(TreeModelEvent e)
Updates the tree based on the node changed event.

Parameters:
e - TreeModelEvent

updateTreeOnStructureChanged

protected void updateTreeOnStructureChanged(TreeModelEvent e)
Updates the tree based on the tree structure changed event.

Parameters:
e - TreeModelEvent

isFiltered

protected boolean isFiltered(Object node)
Check if the node should be filtered away.

Parameters:
node - the node
Returns:
true if the node should be filtered away. Otherwise false.
Since:
3.5.12

updateTree

protected void updateTree(TreeModelEvent e)
Updates the tree based on the TreeModelEvent. By default, we will fire structure changed on the parent node which is not a good idea.

Parameters:
e - TreeModelEvent

isFilterEntireTreeImmediately

public boolean isFilterEntireTreeImmediately()

setFilterEntireTreeImmediately

public void setFilterEntireTreeImmediately(boolean filterEntireTreeImmediately)

isHideEmptyParentNode

public boolean isHideEmptyParentNode()
Returns the value of hideEmptyParentNode property. By default it will return true, meaning if none of the parent node's children match with the searching text, the parent will be hidden too. This flag will take effect only when isMatchesLeafNodeOnly() returns true.

Returns:
true or false.

setHideEmptyParentNode

public void setHideEmptyParentNode(boolean hideEmptyParentNode)
Sets the value of hideEmptyParentNode property. By default this value is true. Changing this value will cause the filters to be applied again.

Parameters:
hideEmptyParentNode - true or false

isMatchesLeafNodeOnly

public boolean isMatchesLeafNodeOnly()
If the filters matches the leaf node only. By default it is true, meaning only leaf node will be compared to the searching text.

Returns:
true if only leaf node will be compared to the searching text. False if all nodes will be compared to the searching text.

setMatchesLeafNodeOnly

public void setMatchesLeafNodeOnly(boolean matchesLeafNodeOnly)
Sets the value of matchesLeafNodeOnly property. By default this value is true. Changing this value will cause the filters to be applied again.

Parameters:
matchesLeafNodeOnly - true or false

isKeepAllChildren

public boolean isKeepAllChildren()
If a parent node matches, all its children will be kept if this flag is true. Otherwise, we will further filter all the children and only leave those matched children visible.

Returns:
true if all children nodes will be kept if the parent node matches the filter condition.

setKeepAllChildren

public void setKeepAllChildren(boolean keepAllChildren)
Sets the value of keepAllChildren property. By default this value is true.

Parameters:
keepAllChildren - the flag

getParent

protected Object getParent(Object node)
Get parent object of the specific node.

Parameters:
node - the node
Returns:
the parent of the node.
Since:
3.4.5

JIDE 3.5.15