dsto.dfc.swing.tree
Class DefaultDfcTreeModel

java.lang.Object
  extended by dsto.dfc.swing.tree.BasicTreeModelEventSource
      extended by dsto.dfc.swing.tree.DefaultDfcTreeModel
All Implemented Interfaces:
DfcTreeModel, Copyable, Disposable, Serializable, Cloneable, TreeModel

public class DefaultDfcTreeModel
extends BasicTreeModelEventSource
implements DfcTreeModel, Copyable, Serializable

Default implementation of DfcTreeModel using HashMap and ArrayList.

Note that the entries in this tree model are the data items themselves, which are internally mapped to tree node structure (see getNodeForEntry(java.lang.Object)). This is convenient in many cases, but means that objects cannot appear more than once in the model. If this is a problem, see DfcSwingTreeModel for a fully node-based model that avoids this problem.

Version:
$Revision$
See Also:
Serialized Form

Nested Class Summary
static class DefaultDfcTreeModel.Node
          A node in the tree.
 
Constructor Summary
DefaultDfcTreeModel()
           
DefaultDfcTreeModel(Object rootEntry)
           
DefaultDfcTreeModel(TreeModel srcModel, Object srcRoot)
           
 
Method Summary
 DefaultDfcTreeModel.Node addChild(DefaultDfcTreeModel.Node parent, Object entry)
           
 DefaultDfcTreeModel.Node addChild(DefaultDfcTreeModel.Node parent, Object entry, int index)
           
 void addEntry(Object parent, Object child, int index)
          Add a child to parent at a given index.
 void addSubtree(Object trgRoot, int trgIndex, TreeModel srcModel, Object srcRoot)
          Copy a subtree into this model.
 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.
 DfcTreeModel clone(Object srcRoot)
          Clone a subtree of this model.
protected  DefaultDfcTreeModel.Node cloneNodes(DefaultDfcTreeModel.Node node)
          Clone a tree of nodes and add them to this models entry to node map.
 Transferable copyEntry(Object entry)
          Copy an entry into a transferable.
protected  void copyFromSubtree(DefaultDfcTreeModel.Node trgRoot, int trgIndex, TreeModel srcModel, Object srcRoot)
          Copy a subtree from a TreeModel into this model.
 Object getChild(Object parent, int index)
           
 int getChildCount(Object parent)
           
 int getIndexOfChild(Object parent, Object child)
           
 DefaultDfcTreeModel.Node getNodeForEntry(Object entry)
           
 DefaultDfcTreeModel.Node getNodeForPath(Object[] path)
           
 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.
 Object[] getPathForNode(DefaultDfcTreeModel.Node node)
           
 Object getRoot()
           
 DefaultDfcTreeModel.Node getRootNode()
          Get the root node of the tree.
 TreePath getTreePathForEntry(Object entry)
           
 boolean isLeaf(Object entry)
           
 boolean isMutable()
          True if the tree can be changed.
 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 entries from tree.
 void removeAllChildren(DefaultDfcTreeModel.Node node)
          Remove all children of a node.
 void removeEntry(Object entry)
          Remove an entry and any children.
protected  void removeEntryMappings(DefaultDfcTreeModel.Node node)
          Remove all entry to node mappings for a node tree.
 void removeNode(DefaultDfcTreeModel.Node node)
           
 void setMutable(boolean newValue)
           
 void valueForPathChanged(TreePath path, Object newValue)
           
 
Methods inherited from class dsto.dfc.swing.tree.BasicTreeModelEventSource
addTreeModelListener, dispose, fireTreeNodeChanged, fireTreeNodeInserted, fireTreeNodeRemoved, fireTreeNodesChanged, fireTreeNodesInserted, fireTreeNodesRemoved, fireTreeStructureChanged, fireTreeStructureChanged, getTreeModelListenerCount, removeTreeModelListener
 
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, removeTreeModelListener
 

Constructor Detail

DefaultDfcTreeModel

public DefaultDfcTreeModel()

DefaultDfcTreeModel

public DefaultDfcTreeModel(Object rootEntry)

DefaultDfcTreeModel

public DefaultDfcTreeModel(TreeModel srcModel,
                           Object srcRoot)
                    throws CloneNotSupportedException
Throws:
CloneNotSupportedException
Method Detail

getRootNode

public DefaultDfcTreeModel.Node getRootNode()
Get the root node of the tree.


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

getPathForNode

public final Object[] getPathForNode(DefaultDfcTreeModel.Node node)

getNodeForPath

public final DefaultDfcTreeModel.Node getNodeForPath(Object[] path)

getNodeForEntry

public DefaultDfcTreeModel.Node getNodeForEntry(Object entry)

getTreePathForEntry

public TreePath getTreePathForEntry(Object entry)

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.

addChild

public DefaultDfcTreeModel.Node addChild(DefaultDfcTreeModel.Node parent,
                                         Object entry)

addChild

public DefaultDfcTreeModel.Node addChild(DefaultDfcTreeModel.Node parent,
                                         Object entry,
                                         int index)

removeAll

public void removeAll()
Remove all entries from tree.


removeAllChildren

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


getRoot

public Object getRoot()
Specified by:
getRoot in interface TreeModel

getChild

public Object getChild(Object parent,
                       int index)
Specified by:
getChild in interface TreeModel

getChildCount

public int getChildCount(Object parent)
Specified by:
getChildCount in interface TreeModel

isLeaf

public boolean isLeaf(Object entry)
Specified by:
isLeaf in interface TreeModel

valueForPathChanged

public void valueForPathChanged(TreePath path,
                                Object newValue)
Specified by:
valueForPathChanged in interface TreeModel

getIndexOfChild

public int getIndexOfChild(Object parent,
                           Object child)
Specified by:
getIndexOfChild in interface TreeModel

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 child,
                     int index)
              throws UnsupportedOperationException
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.
child - The child entry.
index - The index to add the child at. Use -1 to add child at end.
Throws:
UnsupportedOperationException

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

removeNode

public void removeNode(DefaultDfcTreeModel.Node node)

removeEntryMappings

protected final void removeEntryMappings(DefaultDfcTreeModel.Node node)
Remove all entry to node mappings for a node tree.


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 DfcTreeModel clone(Object srcRoot)
                   throws CloneNotSupportedException
Clone a subtree of this model.

Parameters:
srcRoot - 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

protected final DefaultDfcTreeModel.Node cloneNodes(DefaultDfcTreeModel.Node node)
                                             throws CloneNotSupportedException
Clone a tree of nodes and add them to this models entry to node map.

Returns:
The root of the cloned node tree.
Throws:
CloneNotSupportedException

addSubtree

public void addSubtree(Object trgRoot,
                       int trgIndex,
                       TreeModel srcModel,
                       Object srcRoot)
                throws CloneNotSupportedException
Copy a subtree into this model. Only one treeNodesInserted event is fired for the subtree root node.

Parameters:
trgRoot - The node in this tree to add children to.
trgIndex - The index in trgRoot to add the new subtree at.
srcModel - The source model to copy from.
srcRoot - The root node in the source model to copy children from.
Throws:
CloneNotSupportedException

copyFromSubtree

protected final void copyFromSubtree(DefaultDfcTreeModel.Node trgRoot,
                                     int trgIndex,
                                     TreeModel srcModel,
                                     Object srcRoot)
                              throws CloneNotSupportedException
Copy a subtree from a TreeModel into this model.

Parameters:
trgRoot - The root node to add new subtree to.
trgIndex - The Index to add the subtree at.
srcModel - The model to copy from.
srcRoot - The root entry in srcModel to start copying at.
Throws:
CloneNotSupportedException


Copyright © 2008 Commonwealth of Australia