fri.gui.swing.polytreetable
Class DefaultCachedPolyTreeNode

java.lang.Object
  |
  +--fri.gui.swing.polytreetable.AbstractPolyTreeNode
        |
        +--fri.gui.swing.polytreetable.DefaultPolyTreeNode
              |
              +--fri.gui.swing.polytreetable.DefaultCachedPolyTreeNode
All Implemented Interfaces:
MutablePolyTreeNode, PolyTreeNode

public class DefaultCachedPolyTreeNode
extends DefaultPolyTreeNode

Implements a cached PolyTreeNode that avoids redundancy of nodes in child and parent lists. This makes it possible that every node holds its parent and child references to other nodes without pointing to objects created by unlimited "new" calls (the target is to substitute the "new" operator, so that there exists exactly one node for one userObject).

If you use a PolyTreeTableUserObject subclass, you do not need to extend this class. Use it like this:

                PolyTreeNode startNode = DefaultCachedPolyTreeNode.create(userObject);
        
Else override getChildCount() and getParentCount() to read data for the rendered structure, as they get messaged when a tree expansion takes place.
Override createPolyTreeNode() to allocate a new node of your type and use createCachedPolyTreeNode() like the following:
        class FilePolyTreeNode extends DefaultPolyTreeNode	{
                public int getChildCount() {
                        // messaged when enumerating children
                        if (children == null)	{
                                children = new Vector();
                                
                                File file = (File)getUserObject();
                                File [] files = file.listFiles();
                                        
                                for (int i = 0; files != null && i < files.length; i++)	{
                                        children.add(createCachedPolyTreeNode(files[i]));
                                        // createCachedPolyTreeNode() calls createPolyTreeNode() when
                                        // node not cached, and caches it.
                                }
                        }
                        return super.getChildCount();
                }
                
                protected abstract PolyTreeNode createPolyTreeNode(Object userObject)	{
                        // substitutes the "new" operator and creates a specific class of nodes.
                        return new FilePolyTreeNode(userObject);
                }
                
                ...
        }
        

Author:
Ritzberger Fritz
See Also:
DefaultPolyTreeNode

Field Summary
 
Fields inherited from class fri.gui.swing.polytreetable.AbstractPolyTreeNode
allowsChildren, allowsParents, children, EMPTY_ENUMERATION, parents, userObject
 
Constructor Summary
  DefaultCachedPolyTreeNode()
          No-arg do-nothing constructor just for calling PolyTreeNode startNode = new DefaultCachedPolyTreeNode().createCachedPolyTreeNode(userObject); to get a start node.
protected DefaultCachedPolyTreeNode(java.lang.Object userObject)
          Constructor used by factory method createPolyTreeNode(userObject).
 
Method Summary
protected  PolyTreeNode cacheInsertNode(PolyTreeNode newNode)
          Caches a new node or replaces the new node by a chached one.
static void clearCache(java.lang.Class userObjectClass)
          Clears the cache of userObjects -> PolyTreeNode
static PolyTreeNode create(java.lang.Object userObject)
          Convenience method that calls new DefaultCachedPolyTreeNode().createCachedPolyTreeNode(userObject).
protected  PolyTreeNode createCachedPolyTreeNode(java.lang.Object userObject)
          Factory method to create and find buffered PolyTreeNodes, identified by their userObject.
protected  PolyTreeNode createPolyTreeNode(java.lang.Object userObject)
          Factory method to create a typed PolyTreeNodes.
 int getChildCount()
          Returns the number of children the receiver contains.
 int getParentCount()
          Returns the number of parents the receiver contains.
 void insertChild(PolyTreeNode newNode, int index)
          Insert a new child at passed index.
 void insertParent(PolyTreeNode newNode, int index)
          Insert a new parent at passed index.
 boolean setColumnObject(int column, java.lang.Object aValue)
          Convenience implementation for column objects in conjunction with DefaultPolyTreeTableModel.setValueAt().
 
Methods inherited from class fri.gui.swing.polytreetable.DefaultPolyTreeNode
getAllowsChildren, getAllowsParents, getColumnObject, isCellEditable, removeChild, removeParent, setUserObject
 
Methods inherited from class fri.gui.swing.polytreetable.AbstractPolyTreeNode
children, getChildAt, getChildIndex, getElementCount, getParentAt, getParentIndex, getUserObject, isChildLeaf, isNodeChild, isNodeParent, isParentLeaf, parents, setAllowsChildren, setAllowsParents, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface fri.gui.swing.polytreetable.PolyTreeNode
children, getChildAt, getChildIndex, getParentAt, getParentIndex, getUserObject, isChildLeaf, isParentLeaf, parents
 

Constructor Detail

DefaultCachedPolyTreeNode

public DefaultCachedPolyTreeNode()
No-arg do-nothing constructor just for calling
                        PolyTreeNode startNode = new DefaultCachedPolyTreeNode().createCachedPolyTreeNode(userObject);
                
to get a start node.


DefaultCachedPolyTreeNode

protected DefaultCachedPolyTreeNode(java.lang.Object userObject)
Constructor used by factory method createPolyTreeNode(userObject).

Method Detail

getChildCount

public int getChildCount()
Returns the number of children the receiver contains. Overridden to check for PolyTreeTableUserObject.

Specified by:
getChildCount in interface PolyTreeNode
Overrides:
getChildCount in class DefaultPolyTreeNode

getParentCount

public int getParentCount()
Returns the number of parents the receiver contains. Overridden to check for PolyTreeTableUserObject.

Specified by:
getParentCount in interface PolyTreeNode
Overrides:
getParentCount in class DefaultPolyTreeNode

setColumnObject

public boolean setColumnObject(int column,
                               java.lang.Object aValue)
Convenience implementation for column objects in conjunction with DefaultPolyTreeTableModel.setValueAt(). Default this does nothing. If userObject is instanceof PolyTreeTableUserObject, this method delegates to userObject and caches a newly created node. Override this to change rendered data of different columns in userObject if you do not use PolyTreeTableUserObject.

Overrides:
setColumnObject in class DefaultPolyTreeNode
Returns:
true if a real change was done

clearCache

public static void clearCache(java.lang.Class userObjectClass)
Clears the cache of userObjects -> PolyTreeNode


create

public static PolyTreeNode create(java.lang.Object userObject)
Convenience method that calls new DefaultCachedPolyTreeNode().createCachedPolyTreeNode(userObject).

ATTENTION: This method depends on userObject.hashCode() to find existing nodes, this means that one has to implement this if not done by some superclass.


createCachedPolyTreeNode

protected PolyTreeNode createCachedPolyTreeNode(java.lang.Object userObject)
Factory method to create and find buffered PolyTreeNodes, identified by their userObject. This method calls createPolyTreeNode(). If userObject is not found in cache, a new node is created and put into cache. The properties allowsChildren and allowsParents are both set to true.

ATTENTION: This method depends on userObject.hashCode() to find existing nodes, this means that one has to implement this if not done by some superclass.


createPolyTreeNode

protected PolyTreeNode createPolyTreeNode(java.lang.Object userObject)
Factory method to create a typed PolyTreeNodes. Override this to create a PolyTreeNode of your class with code like this:
                        return new FilePolyTreeNode(userObject);
                


insertParent

public void insertParent(PolyTreeNode newNode,
                         int index)
Insert a new parent at passed index. Replace passed node by a cached one if found, put the node into cache if not.

Specified by:
insertParent in interface MutablePolyTreeNode
Overrides:
insertParent in class DefaultPolyTreeNode

insertChild

public void insertChild(PolyTreeNode newNode,
                        int index)
Insert a new child at passed index. Replace passed node by a cached one if found, put the node into cache if not.

Specified by:
insertChild in interface MutablePolyTreeNode
Overrides:
insertChild in class DefaultPolyTreeNode

cacheInsertNode

protected PolyTreeNode cacheInsertNode(PolyTreeNode newNode)
Caches a new node or replaces the new node by a chached one. Put a newly created node into cache.