dsto.dfc.swing.tree
Class DfcSwingTreeModel

java.lang.Object
  extended by javax.swing.tree.DefaultTreeModel
      extended by dsto.dfc.swing.tree.DfcSwingTreeModel
All Implemented Interfaces:
DfcTreeModel, Copyable, Serializable, Cloneable, TreeModel
Direct Known Subclasses:
CustomizersTreeModel

public class DfcSwingTreeModel
extends DefaultTreeModel
implements DfcTreeModel, Copyable

An implementation of a node-based tree DfcTreeModel using Swing's DefaultTreeModel as the base. Unlike DefaultDfcTreeModel, entries in this tree are TreeNode's (or more usefully DfcTreeNode) - the data entries are stored in the userObject property of each node.

Version:
$Revision$
Author:
Matthew Phillips
See Also:
Serialized Form

Field Summary
 
Fields inherited from class javax.swing.tree.DefaultTreeModel
asksAllowsChildren, listenerList, root
 
Constructor Summary
DfcSwingTreeModel()
           
DfcSwingTreeModel(TreeNode root)
           
 
Method Summary
 DfcTreeNode addChild(MutableTreeNode parent, Object entry)
          Shortcut to add a user object to a node in the tree.
 DfcTreeNode addChild(MutableTreeNode parent, Object entry, int index)
          Shortcut to add a user object to a node in the tree.
 void addEntry(Object parent, Object entry, int index)
          Add a child to parent at a given index.
 boolean canAddEntry(Object parent, Object entry, int index)
          Test if a child can be added at a given index.
 boolean canCopyEntry(Object entry)
          Test if an entry may be copied into a transferable.
 boolean canMoveEntry(Object entry, Object newParent, int newIndex)
           
 boolean canPasteEntry(Object parent, int index, Transferable transferable)
          Test if a transferable may be pasted.
 boolean canRemoveEntry(Object entry)
          Test if an entry may be removed.
 Object clone()
          Create a completely separate copy of this object and any mutable objects owned by the object.
 DfcSwingTreeModel clone(TreeNode node)
          Clone a subtree of this model.
static MutableTreeNode cloneNodes(TreeNode root)
          Clone a tree of nodes.
 Transferable copyEntry(Object entry)
          Copy an entry into a transferable.
 void dispose()
          Calls dispose on all Disposable nodes in the model.
protected  void fireTreeNodeChanged(DfcTreeNode node)
          Convenience method to fire the correct event when a node has changed.
 Object[] getObjectPathToRoot(DfcTreeNode node)
          Like getPathToRoot() except that the returned path is defined in terms of the user objects associated with the nodes.
 Object getParent(Object entry)
          Get the parent for a given entry, or null if the entry is the root.
 Object[] getPathForEntry(Object entry)
          Get the path for a given entry.
 DfcTreeNode getRootNode()
           
 boolean isMutable()
          True if the tree can be changed.
 DfcTreeNode[] makeNodePath(Object[] objectPath)
          Convert a path defined in terms of user objects to one in this tree defined in terms of tree node's.
static Object[] makeObjectPath(Object[] nodePath)
          Create a path defined in terms of user objects from a path defined by nodes.
 void moveEntry(Object entry, Object newParent, int newIndex)
          Move a child from one location to another.
 void pasteEntry(Object parent, int index, Transferable transferable)
          Paste transferable data into the tree.
 void removeAll()
          Remove all children of the root node.
 void removeAllChildren(MutableTreeNode node)
          Remove all children of a node.
 void removeEntry(Object entry)
          Remove an entry and any children.
 void setMutable(boolean newValue)
           
 
Methods inherited from class javax.swing.tree.DefaultTreeModel
addTreeModelListener, asksAllowsChildren, fireTreeNodesChanged, fireTreeNodesInserted, fireTreeNodesRemoved, fireTreeStructureChanged, getChild, getChildCount, getIndexOfChild, getListeners, getPathToRoot, getPathToRoot, getRoot, getTreeModelListeners, insertNodeInto, isLeaf, nodeChanged, nodesChanged, nodeStructureChanged, nodesWereInserted, nodesWereRemoved, reload, reload, removeNodeFromParent, removeTreeModelListener, setAsksAllowsChildren, setRoot, valueForPathChanged
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.swing.tree.TreeModel
addTreeModelListener, getChild, getChildCount, getIndexOfChild, getRoot, isLeaf, removeTreeModelListener, valueForPathChanged
 

Constructor Detail

DfcSwingTreeModel

public DfcSwingTreeModel()

DfcSwingTreeModel

public DfcSwingTreeModel(TreeNode root)
Method Detail

dispose

public void dispose()
Calls dispose on all Disposable nodes in the model.

See Also:
Trees.disposeNodes(TreeNode)

getRootNode

public DfcTreeNode getRootNode()

addChild

public DfcTreeNode addChild(MutableTreeNode parent,
                            Object entry)
Shortcut to add a user object to a node in the tree.

Parameters:
parent - The node to add the new child node to.
entry - The entry (user object) for the new child.
Returns:
The new node (node.getUserObject () == entry).

addChild

public DfcTreeNode addChild(MutableTreeNode parent,
                            Object entry,
                            int index)
Shortcut to add a user object to a node in the tree.

Parameters:
parent - The node to add the new child node to.
entry - The entry (user object) for the new child.
index - The index to insert the child at.
Returns:
The new node (node.getUserObject () == entry).

removeAll

public void removeAll()
Remove all children of the root node.

See Also:
removeAllChildren(javax.swing.tree.MutableTreeNode)

removeAllChildren

public void removeAllChildren(MutableTreeNode node)
Remove all children of a node.

See Also:
removeAll()

getObjectPathToRoot

public Object[] getObjectPathToRoot(DfcTreeNode node)
Like getPathToRoot() except that the returned path is defined in terms of the user objects associated with the nodes. This can be used, for example, to create a 'logical' tree path that works across two tree models with similar content but different nodes.

See Also:
makeNodePath(java.lang.Object[])

makeNodePath

public DfcTreeNode[] makeNodePath(Object[] objectPath)
                           throws IllegalArgumentException
Convert a path defined in terms of user objects to one in this tree defined in terms of tree node's.

Throws:
IllegalArgumentException - if objectPath did not match a path in the tree.
See Also:
getObjectPathToRoot(dsto.dfc.swing.tree.DfcTreeNode), makeObjectPath(java.lang.Object[])

makeObjectPath

public static Object[] makeObjectPath(Object[] nodePath)
                               throws ClassCastException
Create a path defined in terms of user objects from a path defined by nodes. This can be used, for example, to create a 'logical' tree path that works across two tree models with similar content but different node instances.

Throws:
ClassCastException - if an entry in nodePath is not a DfcTreeNode.
See Also:
makeNodePath(java.lang.Object[])

fireTreeNodeChanged

protected void fireTreeNodeChanged(DfcTreeNode node)
Convenience method to fire the correct event when a node has changed.


isMutable

public boolean isMutable()
Description copied from interface: DfcTreeModel
True if the tree can be changed. The can* methods indicate what operations are permitted.

Specified by:
isMutable in interface DfcTreeModel

setMutable

public void setMutable(boolean newValue)

getParent

public Object getParent(Object entry)
Description copied from interface: DfcTreeModel
Get the parent for a given entry, or null if the entry is the root.

Specified by:
getParent in interface DfcTreeModel

getPathForEntry

public Object[] getPathForEntry(Object entry)
Description copied from interface: DfcTreeModel
Get the path for a given entry.

Specified by:
getPathForEntry in interface DfcTreeModel
Returns:
the path for the entry or null if entry does not exist in the tree.

canAddEntry

public boolean canAddEntry(Object parent,
                           Object entry,
                           int index)
Description copied from interface: DfcTreeModel
Test if a child can be added at a given index.

Specified by:
canAddEntry in interface DfcTreeModel
Parameters:
parent - The parent entry.
entry - The child entry.
index - The index to add the child at. Use -1 to add child at end.

addEntry

public void addEntry(Object parent,
                     Object entry,
                     int index)
              throws UnsupportedOperationException,
                     IndexOutOfBoundsException
Description copied from interface: DfcTreeModel
Add a child to parent at a given index.

Specified by:
addEntry in interface DfcTreeModel
Parameters:
parent - The parent entry.
entry - The child entry.
index - The index to add the child at. Use -1 to add child at end.
Throws:
UnsupportedOperationException
IndexOutOfBoundsException

canRemoveEntry

public boolean canRemoveEntry(Object entry)
Description copied from interface: DfcTreeModel
Test if an entry may be removed.

Specified by:
canRemoveEntry in interface DfcTreeModel

removeEntry

public void removeEntry(Object entry)
                 throws UnsupportedOperationException
Description copied from interface: DfcTreeModel
Remove an entry and any children.

Specified by:
removeEntry in interface DfcTreeModel
Throws:
UnsupportedOperationException

canMoveEntry

public boolean canMoveEntry(Object entry,
                            Object newParent,
                            int newIndex)
Specified by:
canMoveEntry in interface DfcTreeModel

moveEntry

public void moveEntry(Object entry,
                      Object newParent,
                      int newIndex)
               throws UnsupportedOperationException,
                      IndexOutOfBoundsException
Description copied from interface: DfcTreeModel
Move a child from one location to another. In the case where the old and new parents are the same, newIndex refers to the index space before any changes has been made.

Specified by:
moveEntry in interface DfcTreeModel
Throws:
UnsupportedOperationException
IndexOutOfBoundsException

canPasteEntry

public boolean canPasteEntry(Object parent,
                             int index,
                             Transferable transferable)
Description copied from interface: DfcTreeModel
Test if a transferable may be pasted.

Specified by:
canPasteEntry in interface DfcTreeModel
Parameters:
parent - The parent entry to paste at.
index - The index of the new entry in parent (may be -1 to add last).
transferable - The data to paste.

pasteEntry

public void pasteEntry(Object parent,
                       int index,
                       Transferable transferable)
                throws UnsupportedOperationException,
                       UnsupportedFlavorException,
                       CloneNotSupportedException,
                       IOException
Description copied from interface: DfcTreeModel
Paste transferable data into the tree.

Specified by:
pasteEntry in interface DfcTreeModel
Parameters:
parent - The parent entry to paste at.
index - The index of the new entry in parent (may be -1 to add last).
transferable - The data to paste.
Throws:
UnsupportedOperationException
UnsupportedFlavorException
CloneNotSupportedException
IOException

canCopyEntry

public boolean canCopyEntry(Object entry)
Description copied from interface: DfcTreeModel
Test if an entry may be copied into a transferable.

Specified by:
canCopyEntry in interface DfcTreeModel
Parameters:
entry - The entry to copy.

copyEntry

public Transferable copyEntry(Object entry)
                       throws UnsupportedOperationException,
                              CloneNotSupportedException
Description copied from interface: DfcTreeModel
Copy an entry into a transferable.

Specified by:
copyEntry in interface DfcTreeModel
Parameters:
entry - The entry to copy.
Throws:
UnsupportedOperationException
CloneNotSupportedException

clone

public Object clone()
             throws CloneNotSupportedException
Description copied from interface: Copyable
Create a completely separate copy of this object and any mutable objects owned by the object.

NOTE: subclasses should not remove the CloneNotSupportedException throws clause if they themselves support subclassing: removing the CloneNotSupportedException declaration stops subclasses from indicating that a clone was not possible.

Specified by:
clone in interface Copyable
Overrides:
clone in class Object
Throws:
CloneNotSupportedException - if the clone failed or is not possible.

clone

public DfcSwingTreeModel clone(TreeNode node)
                        throws CloneNotSupportedException
Clone a subtree of this model.

Parameters:
node - The root node in this tree to copy from.
Returns:
A new copy of the subtree. Nodes within the subtree should be cloned also.
Throws:
CloneNotSupportedException - if the tree or any of its entries are not cloneable.

cloneNodes

public static MutableTreeNode cloneNodes(TreeNode root)
                                  throws CloneNotSupportedException
Clone a tree of nodes.

Throws:
CloneNotSupportedException
See Also:
Trees.cloneNodes(javax.swing.tree.TreeNode)


Copyright © 2008 Commonwealth of Australia