JIDE 3.5.15

com.jidesoft.tree
Class SortableTreeModel

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

public class SortableTreeModel
extends DefaultTreeModelWrapper

SortableTreeModel is a tree model wrapper which can do the sorting. Given any existing tree model, you can wrap it into SortableTreeModel and use it later on any JTree.


 SortableTreeModel sortableTreeModel = new SortableTreeModel(treeModel);
 JTree sortableTree = new JTree(sortableTreeModel);
 
To sort the tree, you just call

 sortableTreeModel.sort(); // or sort(SortableTreeModel.SORT_DESCENDING) to sort descending.
 


Field Summary
static int SORT_ASCENDING
           
static int SORT_DESCENDING
           
static int UNSORTED
           
 
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
SortableTreeModel()
           
SortableTreeModel(TreeModel model)
          Creates a DefaultTreeModelWrapper from any table model.
 
Method Summary
protected  void configureListModelWrapper(ListModelWrapper wrapper, Object parent)
          Configures the ListModelWrapper that is used to sort the tree nodes at the same level.
protected  TreeModelEvent convertTreeModelEvent(TreeModelEvent e, int eventType)
          Converts the tree model event for the actual model to the event for this model.
protected  ListModelWrapper createListModelWrapper(Object parent)
          Creates an empty list model wrapper.
 int getSortOrder()
           
 void setSortOrder(int sortOrder)
          Set the sort order.
protected  boolean shouldOptimize(TreeModelEvent e)
          Check if the sorting regarding data insertion or removal should be optimized.
 void sort()
          Sort or unsort the tree model.
 void sort(int order)
          Sort the tree model.
 void treeNodesChanged(TreeModelEvent e)
          Method for TreeModelListener.
 void treeNodesInserted(TreeModelEvent e)
          Method for TreeModelListener.
 void treeNodesRemoved(TreeModelEvent e)
          Method for TreeModelListener.
 void unsort()
          Unsort the tree model.
 
Methods inherited from class com.jidesoft.tree.DefaultTreeModelWrapper
clearAllListModelWrappers, clearListModelWrapper, convertTreeModelEvent, getActualModel, getChild, getChildCount, getIndexOfChild, getListModelWrapper, getRoot, initWrapper, isLeaf, setActualModel, setListModelWrapper, treeStructureChanged, 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

SORT_ASCENDING

public static final int SORT_ASCENDING
See Also:
Constant Field Values

SORT_DESCENDING

public static final int SORT_DESCENDING
See Also:
Constant Field Values

UNSORTED

public static final int UNSORTED
See Also:
Constant Field Values
Constructor Detail

SortableTreeModel

public SortableTreeModel()

SortableTreeModel

public SortableTreeModel(TreeModel model)
Creates a DefaultTreeModelWrapper from any table model.

Parameters:
model - the tree model
Method Detail

createListModelWrapper

protected ListModelWrapper createListModelWrapper(Object parent)
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:
parent - the tree node
Returns:
an empty list model wrapper.

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

convertTreeModelEvent

protected TreeModelEvent convertTreeModelEvent(TreeModelEvent e,
                                               int eventType)
Description copied from class: DefaultTreeModelWrapper
Converts the tree model event for the actual model to the event for this model.

Overrides:
convertTreeModelEvent in class DefaultTreeModelWrapper
Parameters:
e - the TreeModelEvent
eventType - the event type, DefaultTreeModelWrapper.EVENT_TYPE_NODES_INSERTED, DefaultTreeModelWrapper.EVENT_TYPE_NODES_REMOVED or DefaultTreeModelWrapper.EVENT_TYPE_NODES_CHANGED
Returns:
the tree model event for this model.

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

shouldOptimize

protected boolean shouldOptimize(TreeModelEvent e)
Check if the sorting regarding data insertion or removal should be optimized. In optimized scenario, the list model will not be recreated and a insertion/removal will be conducted in the existing list model. Otherwise, the list model will be recreated and the entire new list model will be sorted from scratch.

By default, it will only optimize while the impacted child indices contain only one entry. You could always override this method to change the threshold.

Parameters:
e - the tree model event
Returns:
true if the event should be optimized. Otherwise false.

configureListModelWrapper

protected void configureListModelWrapper(ListModelWrapper wrapper,
                                         Object parent)
Configures the ListModelWrapper that is used to sort the tree nodes at the same level. Please note, if you want to override this method to set a new comparator to the wrapper (you can cast it to SortableListModel and call setComparator to set it), please make sure you call setComparator first then call super.configureListModelWrapper.

Overrides:
configureListModelWrapper in class DefaultTreeModelWrapper
Parameters:
wrapper - the list model wrapper
parent - the parent row

getSortOrder

public int getSortOrder()

setSortOrder

public void setSortOrder(int sortOrder)
Set the sort order.

Parameters:
sortOrder - the new sort order. Valid values for sortOrder are SORT_ASCENDING, SORT_DESCENDING and UNSORTED.

sort

public void sort(int order)
Sort the tree model. The order could be either SORT_ASCENDING or SORT_DESCENDING. You can also pass in UNSORTED, which will be the same as unsort().

Parameters:
order - the new sort order.

sort

public void sort()
Sort or unsort the tree model.


unsort

public void unsort()
Unsort the tree model.


JIDE 3.5.15