Copyright 2012-02-25 Werner Randelshofer

ch.randelshofer.gui.tree
Class DefaultMutableTreeModel

java.lang.Object
  extended by javax.swing.tree.DefaultTreeModel
      extended by ch.randelshofer.gui.tree.DefaultMutableTreeModel
All Implemented Interfaces:
MutableTreeModel, java.io.Serializable, javax.swing.tree.TreeModel
Direct Known Subclasses:
ZipTreeModel

public class DefaultMutableTreeModel
extends javax.swing.tree.DefaultTreeModel
implements MutableTreeModel

A simple mutable tree model. FIXME: Should override more methods in the superclass.

Author:
Werner Randelshofer, 3.0 2008-03-21 Rewrote default implementation of importTransferable.
2.3 2002-04-08 Support for transferables added.
2.0 2001-07-18
See Also:
Serialized Form

Field Summary
protected  java.lang.Object[] childTypes
           
static java.awt.datatransfer.DataFlavor listFlavor
           
static java.awt.datatransfer.DataFlavor objectFlavor
           
 
Fields inherited from class javax.swing.tree.DefaultTreeModel
asksAllowsChildren, listenerList, root
 
Constructor Summary
DefaultMutableTreeModel()
          Constructs a new DefaultMutableTreeModel using a DefaultMutableTreeNode as root of the tree.
DefaultMutableTreeModel(javax.swing.tree.TreeNode root)
          Constructs a new DefaultMutableTreeModel using the given tree node as the root of the tree.
 
Method Summary
 javax.swing.tree.TreePath createNodeAt(java.lang.Object type, javax.swing.tree.MutableTreeNode parent, int index)
          Invoke this to insert a new child at location index in parents children.
 java.awt.datatransfer.Transferable exportTransferable(javax.swing.tree.MutableTreeNode[] nodes)
          Creates a Transferable to use as the source for a data transfer of the specified elements.
 java.lang.Object getCreatableNodeType(java.lang.Object node)
          Returns the type of children that may be created at this node.
 java.lang.Object[] getCreatableNodeTypes(java.lang.Object node)
          Returns the type of children that may be created at this node.
 javax.swing.Action[] getNodeActions(javax.swing.tree.MutableTreeNode[] nodes)
          Gets actions for the indicated nodes.
 int importTransferable(java.awt.datatransfer.Transferable t, int action, javax.swing.tree.MutableTreeNode parent, int index)
          Causes a transfer to the model from a clipboard or a DND drop operation.
 int insertAllInto(java.util.List l, javax.swing.tree.MutableTreeNode parent, int index)
           
 void insertNodeInto(javax.swing.tree.MutableTreeNode newChild, javax.swing.tree.MutableTreeNode parent, int index)
           
 boolean isEditable()
          Gets the editable state of the model.
 boolean isEnabled()
          Gets the enabled state of the model.
 boolean isImportable(java.awt.datatransfer.DataFlavor[] transferFlavors, int action, javax.swing.tree.MutableTreeNode parent, int index)
          Indicates whether the model would accept an import of the given set of data flavors prior to actually attempting to import it.
 boolean isNodeAddable(javax.swing.tree.MutableTreeNode parent, int index)
          Returns whether a node may be added.
 boolean isNodeEditable(javax.swing.tree.MutableTreeNode node)
          Returns wether the specified node may be renamed.
 boolean isNodeRemovable(javax.swing.tree.MutableTreeNode node)
          Returns wether the specified node may be removed.
 void removeNode(javax.swing.tree.MutableTreeNode node)
          Removes a child from its parent.
 void setEditable(boolean b)
          Sets the editable state of the model.
 void setEnabled(boolean b)
          Sets the enabled state of the model.
protected  void setInsertableNodeTypes(java.lang.Object[] childTypes)
          Sets the node types to be returned by getInsertableNodeTypes.
 void setRoot(javax.swing.tree.TreeNode aRoot)
          Sets the root to root.
 
Methods inherited from class javax.swing.tree.DefaultTreeModel
addTreeModelListener, asksAllowsChildren, fireTreeNodesChanged, fireTreeNodesInserted, fireTreeNodesRemoved, fireTreeStructureChanged, getChild, getChildCount, getIndexOfChild, getListeners, getPathToRoot, getPathToRoot, getRoot, getTreeModelListeners, isLeaf, nodeChanged, nodesChanged, nodeStructureChanged, nodesWereInserted, nodesWereRemoved, reload, reload, removeNodeFromParent, removeTreeModelListener, setAsksAllowsChildren, valueForPathChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface ch.randelshofer.gui.tree.MutableTreeModel
removeNodeFromParent
 
Methods inherited from interface javax.swing.tree.TreeModel
addTreeModelListener, getChild, getChildCount, getIndexOfChild, getRoot, isLeaf, removeTreeModelListener, valueForPathChanged
 

Field Detail

childTypes

protected java.lang.Object[] childTypes

listFlavor

public static final java.awt.datatransfer.DataFlavor listFlavor

objectFlavor

public static final java.awt.datatransfer.DataFlavor objectFlavor
Constructor Detail

DefaultMutableTreeModel

public DefaultMutableTreeModel()
Constructs a new DefaultMutableTreeModel using a DefaultMutableTreeNode as root of the tree.


DefaultMutableTreeModel

public DefaultMutableTreeModel(javax.swing.tree.TreeNode root)
Constructs a new DefaultMutableTreeModel using the given tree node as the root of the tree.

Method Detail

createNodeAt

public javax.swing.tree.TreePath createNodeAt(java.lang.Object type,
                                              javax.swing.tree.MutableTreeNode parent,
                                              int index)
                                       throws java.lang.IllegalStateException
Invoke this to insert a new child at location index in parents children. This will then message nodesWereInserted to create the appropriate event. This is the preferred way to add children as it will create the appropriate event.

Specified by:
createNodeAt in interface MutableTreeModel
Parameters:
type - the type of the new child to be created.
parent - a node from the tree, obtained from this data source.
index - index of the child.
Returns:
The path pointing to the newly created node.
Throws:
java.lang.IllegalStateException - if the parent node does not allow children.

insertNodeInto

public void insertNodeInto(javax.swing.tree.MutableTreeNode newChild,
                           javax.swing.tree.MutableTreeNode parent,
                           int index)
Overrides:
insertNodeInto in class javax.swing.tree.DefaultTreeModel

getCreatableNodeTypes

public java.lang.Object[] getCreatableNodeTypes(java.lang.Object node)
Returns the type of children that may be created at this node.

Specified by:
getCreatableNodeTypes in interface MutableTreeModel
Parameters:
node - a node from the tree, obtained from this data source.
Returns:
an array of objects that specify a child type that may be added to the node. Returns an empty array for nodes that cannot have additional children.

getCreatableNodeType

public java.lang.Object getCreatableNodeType(java.lang.Object node)
Returns the type of children that may be created at this node.

Specified by:
getCreatableNodeType in interface MutableTreeModel
Parameters:
node - a node from the tree, obtained from this data source.
Returns:
an Object that specifies the default child type that can be inserted at the insertion point. Returns null if no children can be inserted here. The value must be one of the types returned by operation getCreatableNodeTypes.

isEditable

public boolean isEditable()
Gets the editable state of the model.


isEnabled

public boolean isEnabled()
Gets the enabled state of the model.


isNodeAddable

public boolean isNodeAddable(javax.swing.tree.MutableTreeNode parent,
                             int index)
Description copied from interface: MutableTreeModel
Returns whether a node may be added.

Specified by:
isNodeAddable in interface MutableTreeModel
Parameters:
parent - a node from the tree, obtained from this data source.
index - the insertion index.

isNodeRemovable

public boolean isNodeRemovable(javax.swing.tree.MutableTreeNode node)
Returns wether the specified node may be removed.

Specified by:
isNodeRemovable in interface MutableTreeModel
Parameters:
node - a node from the tree, obtained from this data source.
Returns:
Returns true for all nodes except for the root. Returns false if the model is disabled.

isNodeEditable

public boolean isNodeEditable(javax.swing.tree.MutableTreeNode node)
Returns wether the specified node may be renamed.

Specified by:
isNodeEditable in interface MutableTreeModel
Parameters:
node - a node from the tree, obtained from this data source.
Returns:
Returns true for all nodes except. Returns false if the model is disabled.

removeNode

public void removeNode(javax.swing.tree.MutableTreeNode node)
Removes a child from its parent.

Parameters:
node - a node from the tree, obtained from this data source.

setEditable

public void setEditable(boolean b)
Sets the editable state of the model. A disabled tree model returns Object[]{} for getAllowedChildTypes(Object), and false for isRemoveAllowed()


setEnabled

public void setEnabled(boolean b)
Sets the enabled state of the model. A disabled tree model returns Object[]{} for getAllowedChildTypes(Object), and false for isRemoveAllowed()


setInsertableNodeTypes

protected void setInsertableNodeTypes(java.lang.Object[] childTypes)
Sets the node types to be returned by getInsertableNodeTypes.


setRoot

public void setRoot(javax.swing.tree.TreeNode aRoot)
Sets the root to root. This will throw an IllegalArgumentException if root is null.

Overrides:
setRoot in class javax.swing.tree.DefaultTreeModel

isImportable

public boolean isImportable(java.awt.datatransfer.DataFlavor[] transferFlavors,
                            int action,
                            javax.swing.tree.MutableTreeNode parent,
                            int index)
Indicates whether the model would accept an import of the given set of data flavors prior to actually attempting to import it.

Specified by:
isImportable in interface MutableTreeModel
Parameters:
transferFlavors - the data formats available
action - the action, this is either COPY, MOVE or LINK.
parent - a node from the tree, obtained from this data source.
index - The insertion point.
Returns:
true if the data can be inserted into the component, false otherwise

exportTransferable

public java.awt.datatransfer.Transferable exportTransferable(javax.swing.tree.MutableTreeNode[] nodes)
Description copied from interface: MutableTreeModel
Creates a Transferable to use as the source for a data transfer of the specified elements. Returns the representation of the rows to be transferred, or null if transfer is not possible.

Specified by:
exportTransferable in interface MutableTreeModel
Parameters:
nodes - The nodes.

importTransferable

public int importTransferable(java.awt.datatransfer.Transferable t,
                              int action,
                              javax.swing.tree.MutableTreeNode parent,
                              int index)
                       throws java.awt.datatransfer.UnsupportedFlavorException,
                              java.io.IOException
Description copied from interface: MutableTreeModel
Causes a transfer to the model from a clipboard or a DND drop operation.

Specified by:
importTransferable in interface MutableTreeModel
Parameters:
t - The transfer data.
parent - a node from the tree, obtained from this data source.
index - The insertion point.
Returns:
The number of imported elements or 0 if nothing was imported.
Throws:
java.awt.datatransfer.UnsupportedFlavorException
java.io.IOException

insertAllInto

public int insertAllInto(java.util.List l,
                         javax.swing.tree.MutableTreeNode parent,
                         int index)

getNodeActions

public javax.swing.Action[] getNodeActions(javax.swing.tree.MutableTreeNode[] nodes)
Gets actions for the indicated nodes.

Specified by:
getNodeActions in interface MutableTreeModel
Parameters:
nodes - The nodes.

Copyright 2012-02-25 Werner Randelshofer