fri.gui.swing.polytreetable
Class PolyTreeView

java.lang.Object
  |
  +--java.util.AbstractCollection
        |
        +--java.util.AbstractList
              |
              +--java.util.Vector
                    |
                    +--fri.gui.swing.polytreetable.PolyTreeView
All Implemented Interfaces:
java.lang.Cloneable, java.util.Collection, java.util.EventListener, java.util.List, PolyTreeTableModelListener, java.util.RandomAccess, java.io.Serializable

public class PolyTreeView
extends java.util.Vector
implements PolyTreeTableModelListener

Instead of a JTree this tree view proxy holds information about expandable nodes and count of visible rows.
This is the most complex class of polytreetable package. It is a Vector that represents the tree column of all rows in JTable. It implements all the algorithms to insert/delete rows and set a new "lead term". It gets messaged by the BasicPolyTreeTableUI when nodes expand or collapse and then adds or removes rows and sends expansion events. It listens for model changes and inserts or removes rows when an event happens.

Use treeTable.getTree().getNodeForViewRow(row) in conjunction with treeTable.getTree().getViewRowCount() to retrieve all currently visible nodes. The method getLevelForRow() gives the hierarchy of the nodes, negative if parent, zero if root, positive if child.

Author:
Ritzberger Fritz
See Also:
PolyTreeTableModelAdapter, PolyTreeTableModelListener, PolyTreeExpansionListener, Serialized Form

Field Summary
static int CHILDREN_BELOW
          Flag to set the fact that children are shown below root and parents above root.
static int PARENTS_BELOW
          Flag to set the fact that parents are shown below root and children above root.
 
Fields inherited from class java.util.Vector
capacityIncrement, elementCount, elementData
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
PolyTreeView(PolyTreeTableModel model)
          Create a tree view proxy for a PolyTreeTable.
PolyTreeView(PolyTreeTableModel model, int parentChildOrientation)
          Create a tree view proxy for a PolyTreeTable, set optional horizontal orientation of children and parents.
 
Method Summary
 void addPolyTreeExpansionListener(PolyTreeExpansionListener lsnr)
           
 void addPolyTreeWillExpandListener(PolyTreeWillExpandListener lsnr)
           
 void clearRowCache()
          Utility method to cler the row cache, that holds nodes that are currently not visible, as their container has been collapsed.
protected  boolean equalNodes(PolyTreeNode n1, PolyTreeNode n2)
          Returns true if userObject of first node equals userObject of second node.
 int expandChildPath(java.util.Vector path)
          Expands child pathes packed by getPathForRow() into a list containing Strings that represent all nodes to expand.
 int expandChildPathes(java.util.Vector pathes)
          Expands child pathes packed by getOpenPathes() into a list of lists containing Strings that represent all pathes to expand.
 int expandParentPath(java.util.Vector path)
          Expands parent pathes packed by getPathForRow() into a list containing Strings that represent all nodes to expand.
 int expandParentPathes(java.util.Vector pathes)
          Expands parent pathes packed by getOpenPathes() into a list of lists containing Strings that represent all pathes to expand.
protected  void fireTreeCollapsed(PolyTreeNode node, int firstRow, int lastRow)
           
protected  void fireTreeExpanded(PolyTreeNode node, int firstRow, int lastRow)
           
protected  void fireTreeStructureChanged(PolyTreeNode newRoot)
           
protected  void fireTreeWillCollapse(PolyTreeNode node)
           
protected  void fireTreeWillExpand(PolyTreeNode node)
           
protected  void fireTreeWillStructureChange(PolyTreeNode node)
           
 int getLevelForRow(int row)
          Returns a tree level for a row.
 PolyTreeTableModel getModel()
          Returns the PolyTreeTableModel of this view.
 PolyTreeNode getNodeForViewRow(int row)
          Returns a PolyTreeNode for a view row.
 java.util.Vector getOpenChildPathes()
          Returns a list of lists containing Strings representing open child pathes from current root (== start node), inclusive.
 java.util.Vector getOpenParentPathes()
          Returns a list of lists containing Strings representing open parent pathes from current root (== start node), inclusive.
 java.util.Vector getPathForRow(int row)
          Returns a list containing Strings representing the open path for given row, root label inclusive.
 java.lang.String getRootLabelFromPath(java.util.Vector pathes)
          A way to get the root label string from a set of packed pathes, made by getExpandedPathes().
 int getRootRow()
          Returns the row in which the current root resides.
 int getViewRowCount()
          Returns the row count of all visible rows.
 int[] getViewRowsForNode(PolyTreeNode node)
          Sequential search that uses "==" to find the view rows of the passed node.
 boolean isChildLeaf(int row)
          Delegates to model.isChildLeaf().
 boolean isChildLevel(int level)
          PolyTreeView convenience method that returns true if level > 0.
 boolean isChildRow(int row)
          Returns true if passed row is in child part of view.
 boolean isEmpty(int row)
          Calls isChildLeaf() or isParentLeaf() to decide if node is empty.
 boolean isExpanded(int row)
          Returns expanded state for a row.
 boolean[] isExpanded(PolyTreeNode node)
          Return expansion states of all visible occurences of the passed node.
 boolean isLeaf(int row)
          Returns true if node in row is a parent leaf (if the node has a parent level) or is a child leaf (if node has a child level).
 boolean isParentLeaf(int row)
          Delegates to model.isParentLeaf().
 boolean isParentLevel(int level)
          PolyTreeView convenience method that returns true if level < 0.
 boolean isParentRow(int row)
          Returns true if passed row is in parent part of view.
static boolean isRootLevel(int level)
          PolyTreeView convenience method that returns true if level == 0.
 boolean isRootRow(int row)
          Returns true if passed row root row of view.
 void removePolyTreeExpansionListener(PolyTreeExpansionListener lsnr)
           
 void removePolyTreeWillExpandListener(PolyTreeWillExpandListener lsnr)
           
 void setModel(PolyTreeTableModel model)
          Set a (new) model into this tree.
 void setRoot(PolyTreeNode node)
          Set a new root node.
 void setRootRow(int row)
          Change to new root node.
 int toggleExpandedState(int row)
          Toggles the expansion state for a row.
 int[] toggleExpandedState(PolyTreeNode node)
          Toggles the expansion state for all rows containing passed PolyTreeNode.
 java.lang.String toString()
          Write all NodeWrappers of this Vector from 0-n and all cached nodes.
 void treeNodeChanged(PolyTreeTableModelEvent e)
          The name of a node was changed by the CellEditor.
 void treeNodesInserted(PolyTreeTableModelEvent e)
          One or mode nodes were inserted into model.
 void treeNodesRemoved(PolyTreeTableModelEvent e)
          One or mode nodes were removed from model.
 
Methods inherited from class java.util.Vector
add, add, addAll, addAll, addElement, capacity, clear, clone, contains, containsAll, copyInto, elementAt, elements, ensureCapacity, equals, firstElement, get, hashCode, indexOf, indexOf, insertElementAt, isEmpty, lastElement, lastIndexOf, lastIndexOf, remove, remove, removeAll, removeAllElements, removeElement, removeElementAt, removeRange, retainAll, set, setElementAt, setSize, size, subList, toArray, toArray, trimToSize
 
Methods inherited from class java.util.AbstractList
iterator, listIterator, listIterator
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
iterator, listIterator, listIterator
 

Field Detail

CHILDREN_BELOW

public static final int CHILDREN_BELOW
Flag to set the fact that children are shown below root and parents above root.

See Also:
Constant Field Values

PARENTS_BELOW

public static final int PARENTS_BELOW
Flag to set the fact that parents are shown below root and children above root.

See Also:
Constant Field Values
Constructor Detail

PolyTreeView

public PolyTreeView(PolyTreeTableModel model)
Create a tree view proxy for a PolyTreeTable.

Parameters:
model - the data model of the polyhierarchy.

PolyTreeView

public PolyTreeView(PolyTreeTableModel model,
                    int parentChildOrientation)
Create a tree view proxy for a PolyTreeTable, set optional horizontal orientation of children and parents.

Parameters:
model - the data model of the polyhierarchy.
parentChildOrientation - CHILDREN_BELOW (default) or PARENTS_BELOW, default children are shown below root.
Method Detail

isParentLevel

public boolean isParentLevel(int level)
PolyTreeView convenience method that returns true if level < 0.


isChildLevel

public boolean isChildLevel(int level)
PolyTreeView convenience method that returns true if level > 0.


isRootLevel

public static boolean isRootLevel(int level)
PolyTreeView convenience method that returns true if level == 0.


getModel

public PolyTreeTableModel getModel()
Returns the PolyTreeTableModel of this view.


setModel

public void setModel(PolyTreeTableModel model)
Set a (new) model into this tree. The first level of parents and children will be expanded.


setRoot

public void setRoot(PolyTreeNode node)
Set a new root node. All rows are cleared and the first level of parent and child nodes get expanded. This method does not touch the startNode of the model!


getRootRow

public int getRootRow()
Returns the row in which the current root resides. This is a sequential search for level 0.


getViewRowCount

public int getViewRowCount()
Returns the row count of all visible rows. This is smaller or equal to size of all existing PolyTreeNodes.


getNodeForViewRow

public PolyTreeNode getNodeForViewRow(int row)
Returns a PolyTreeNode for a view row. This just maps into Vector.


getLevelForRow

public int getLevelForRow(int row)
Returns a tree level for a row.


isChildRow

public boolean isChildRow(int row)
Returns true if passed row is in child part of view.


isParentRow

public boolean isParentRow(int row)
Returns true if passed row is in parent part of view.


isRootRow

public boolean isRootRow(int row)
Returns true if passed row root row of view.


isExpanded

public boolean isExpanded(int row)
Returns expanded state for a row.


isLeaf

public boolean isLeaf(int row)
Returns true if node in row is a parent leaf (if the node has a parent level) or is a child leaf (if node has a child level). The current root node is never a leaf.


isParentLeaf

public boolean isParentLeaf(int row)
Delegates to model.isParentLeaf().


isChildLeaf

public boolean isChildLeaf(int row)
Delegates to model.isChildLeaf().


isEmpty

public boolean isEmpty(int row)
Calls isChildLeaf() or isParentLeaf() to decide if node is empty. Both properties are requested for root. Returns true if count is bigger than zero.


setRootRow

public void setRootRow(int row)
Change to new root node. Keep as much expansions as possible.


equalNodes

protected boolean equalNodes(PolyTreeNode n1,
                             PolyTreeNode n2)
Returns true if userObject of first node equals userObject of second node.


toggleExpandedState

public int toggleExpandedState(int row)
Toggles the expansion state for a row. If the node is collapsed it will be expanded, if it is expanded and not empty it will be collapsed. The JTable is notfied via the TableAdapter. This is called by BasicPolyTreeTableUI when the according input event arrives.

Returns:
number of inserted rows, negative if collapsed

treeNodesInserted

public void treeNodesInserted(PolyTreeTableModelEvent e)
Description copied from interface: PolyTreeTableModelListener
One or mode nodes were inserted into model.

Specified by:
treeNodesInserted in interface PolyTreeTableModelListener

treeNodesRemoved

public void treeNodesRemoved(PolyTreeTableModelEvent e)
Description copied from interface: PolyTreeTableModelListener
One or mode nodes were removed from model.

Specified by:
treeNodesRemoved in interface PolyTreeTableModelListener

treeNodeChanged

public void treeNodeChanged(PolyTreeTableModelEvent e)
Description copied from interface: PolyTreeTableModelListener
The name of a node was changed by the CellEditor.

Specified by:
treeNodeChanged in interface PolyTreeTableModelListener

addPolyTreeExpansionListener

public void addPolyTreeExpansionListener(PolyTreeExpansionListener lsnr)

removePolyTreeExpansionListener

public void removePolyTreeExpansionListener(PolyTreeExpansionListener lsnr)

fireTreeStructureChanged

protected void fireTreeStructureChanged(PolyTreeNode newRoot)

fireTreeExpanded

protected void fireTreeExpanded(PolyTreeNode node,
                                int firstRow,
                                int lastRow)

fireTreeCollapsed

protected void fireTreeCollapsed(PolyTreeNode node,
                                 int firstRow,
                                 int lastRow)

addPolyTreeWillExpandListener

public void addPolyTreeWillExpandListener(PolyTreeWillExpandListener lsnr)

removePolyTreeWillExpandListener

public void removePolyTreeWillExpandListener(PolyTreeWillExpandListener lsnr)

fireTreeWillExpand

protected void fireTreeWillExpand(PolyTreeNode node)
                           throws javax.swing.tree.ExpandVetoException
javax.swing.tree.ExpandVetoException

fireTreeWillCollapse

protected void fireTreeWillCollapse(PolyTreeNode node)
                             throws javax.swing.tree.ExpandVetoException
javax.swing.tree.ExpandVetoException

fireTreeWillStructureChange

protected void fireTreeWillStructureChange(PolyTreeNode node)
                                    throws javax.swing.tree.ExpandVetoException
javax.swing.tree.ExpandVetoException

toString

public java.lang.String toString()
Write all NodeWrappers of this Vector from 0-n and all cached nodes.

Overrides:
toString in class java.util.Vector

clearRowCache

public void clearRowCache()
Utility method to cler the row cache, that holds nodes that are currently not visible, as their container has been collapsed. Normally it will not be necessary for clients to clear this cache. It DOES NOT collapse all nodes!


getViewRowsForNode

public int[] getViewRowsForNode(PolyTreeNode node)
Sequential search that uses "==" to find the view rows of the passed node.

Returns:
array that contains all view rows of passed node, array of length 0 if the node is not visible.

toggleExpandedState

public int[] toggleExpandedState(PolyTreeNode node)
Toggles the expansion state for all rows containing passed PolyTreeNode. If the nodes are collapsed they will be expanded, if expanded and not empty they will be collapsed. The JTable is notfied via the TableAdapter. This can be used for programmatic expanding or collapsing.

Returns:
row indizes of the passed node after expand/collapse.

isExpanded

public boolean[] isExpanded(PolyTreeNode node)
Return expansion states of all visible occurences of the passed node.


getOpenParentPathes

public java.util.Vector getOpenParentPathes()
Returns a list of lists containing Strings representing open parent pathes from current root (== start node), inclusive. The getNodeForViewRow().toString() method is used to generate the path Strings.

Returns:
Vector of Vectors that hold open visible parent pathes.

getOpenChildPathes

public java.util.Vector getOpenChildPathes()
Returns a list of lists containing Strings representing open child pathes from current root (== start node), inclusive. The getNodeForViewRow().toString() method is used to generate the path Strings.

Returns:
Vector of Vectors that hold open visible child pathes.

expandParentPathes

public int expandParentPathes(java.util.Vector pathes)
Expands parent pathes packed by getOpenPathes() into a list of lists containing Strings that represent all pathes to expand. The root (== start node) is contained in every path.

Parameters:
pathes - Vector of Vectors that hold pathes to open visible.
Returns:
the row of the last path component

expandChildPathes

public int expandChildPathes(java.util.Vector pathes)
Expands child pathes packed by getOpenPathes() into a list of lists containing Strings that represent all pathes to expand. The root (== start node) is contained in every path.

Parameters:
pathes - Vector of Vectors that hold pathes to open visible.
Returns:
the row of the last path component

getPathForRow

public java.util.Vector getPathForRow(int row)
Returns a list containing Strings representing the open path for given row, root label inclusive. The getNodeForViewRow().toString() method is used to generate the path Strings. Use this in conjunction with isChildRow(row) and isParentRow(row) to decide if it is a child or parent path.

Parameters:
row - view row to retrieve path for
Returns:
Vector that hold open visible String path for given row.

expandParentPath

public int expandParentPath(java.util.Vector path)
Expands parent pathes packed by getPathForRow() into a list containing Strings that represent all nodes to expand. The root (== start node) is contained in the path.

Parameters:
path - Vector of Strings that hold nodes to open visible.
Returns:
the row of the last path component

expandChildPath

public int expandChildPath(java.util.Vector path)
Expands child pathes packed by getPathForRow() into a list containing Strings that represent all nodes to expand. The root (== start node) is contained in the path.

Parameters:
path - Vector of Strings that hold nodes to open visible.
Returns:
the row of the last path component

getRootLabelFromPath

public java.lang.String getRootLabelFromPath(java.util.Vector pathes)
A way to get the root label string from a set of packed pathes, made by getExpandedPathes().

Returns:
the label of the root node in the passed bundle of pathes.