CubeTwister 2.0alpha142 2012-02-11

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

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

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

Version:
3.1 2011-01-22 Methods insertAllInto and importTransferable return the tree paths of the inserted nodes. Adds undo support. Adds method insertNodesInto.
3.0.1 2011-01-20 Method removeNodeFromParent was not properly overriden.
3.0 2008-03-21 Rewrote default implementation of importTransferable.
2.3 2002-04-08 Support for transferables added.
2.0 2001-07-18
Author:
Werner Randelshofer
See Also:
Serialized Form

Field Summary
protected  java.lang.Object[] childTypes
           
static java.awt.datatransfer.DataFlavor listFlavor
           
static java.awt.datatransfer.DataFlavor objectFlavor
           
protected  java.beans.PropertyChangeSupport propertySupport
           
 
Fields inherited from class javax.swing.tree.DefaultTreeModel
asksAllowsChildren, listenerList, root
 
Fields inherited from interface ch.randelshofer.gui.tree.MutableTreeModel
ENABLED_PROPERTY
 
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
 void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
          Adds a PropertyChangeListener which can optionally be wrapped into a WeakPropertyChangeListener.
 void addUndoableEditListener(javax.swing.event.UndoableEditListener l)
          Adds an UndoableEditListener.
 javax.swing.tree.TreePath createNodeAt(java.lang.Object type, javax.swing.tree.DefaultMutableTreeNode parent, int index)
          Invoke this to insert a new child at location index in parents children.
 java.awt.datatransfer.Transferable exportTransferable(javax.swing.tree.DefaultMutableTreeNode[] nodes)
          Creates a Transferable to use as the source for a data transfer of the specified elements.
protected  void firePropertyChange(java.lang.String propertyName, boolean oldValue, boolean newValue)
           
protected  void firePropertyChange(java.lang.String propertyName, int oldValue, int newValue)
           
protected  void firePropertyChange(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue)
           
 void fireUndoableEdit(javax.swing.undo.UndoableEdit edit)
          Notify all listeners that have registered interest for notification on this event type.
 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.DefaultMutableTreeNode[] nodes)
          Gets actions for the indicated nodes.
 java.util.List<javax.swing.tree.TreePath> importTransferable(java.awt.datatransfer.Transferable t, int action, javax.swing.tree.DefaultMutableTreeNode parent, int index)
          Causes a transfer to the model from a clipboard or a DND drop operation.
 java.util.List<javax.swing.tree.TreePath> insertAllInto(java.util.List l, javax.swing.tree.DefaultMutableTreeNode parent, int index)
           
 void insertNodeInto(javax.swing.tree.MutableTreeNode newChild, javax.swing.tree.MutableTreeNode parent, int index)
           
 java.util.List<javax.swing.tree.TreePath> insertNodesInto(java.util.List<? extends javax.swing.tree.DefaultMutableTreeNode> newChilds, javax.swing.tree.DefaultMutableTreeNode 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.DefaultMutableTreeNode 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.DefaultMutableTreeNode parent, int index)
          Returns whether a node may be added.
 boolean isNodeEditable(javax.swing.tree.DefaultMutableTreeNode node)
          Returns wether the specified node may be renamed.
 boolean isNodeRemovable(javax.swing.tree.DefaultMutableTreeNode node)
          Returns wether the specified node may be removed.
 void removeNodeFromParent(javax.swing.tree.DefaultMutableTreeNode node)
          Removes a child from its parent.
 void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
          Removes a PropertyChangeListener.
 void removeUndoableEditListener(javax.swing.event.UndoableEditListener l)
          Removes an UndoableEditListener.
 void setEditable(boolean b)
          Sets the editable state of the model.
 void setEnabled(boolean newValue)
          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 javax.swing.tree.TreeModel
addTreeModelListener, getChild, getChildCount, getIndexOfChild, getRoot, isLeaf, removeTreeModelListener, valueForPathChanged
 

Field Detail

propertySupport

protected java.beans.PropertyChangeSupport propertySupport

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.DefaultMutableTreeNode 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.DefaultMutableTreeNode 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.DefaultMutableTreeNode 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.DefaultMutableTreeNode 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.

removeNodeFromParent

public void removeNodeFromParent(javax.swing.tree.DefaultMutableTreeNode node)
Removes a child from its parent.

Specified by:
removeNodeFromParent in interface MutableTreeModel
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 newValue)
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.DefaultMutableTreeNode 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.DefaultMutableTreeNode[] 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 java.util.List<javax.swing.tree.TreePath> importTransferable(java.awt.datatransfer.Transferable t,
                                                                    int action,
                                                                    javax.swing.tree.DefaultMutableTreeNode 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 paths of the imported nodes.
Throws:
java.awt.datatransfer.UnsupportedFlavorException
java.io.IOException

insertAllInto

public java.util.List<javax.swing.tree.TreePath> insertAllInto(java.util.List l,
                                                               javax.swing.tree.DefaultMutableTreeNode parent,
                                                               int index)

insertNodesInto

public java.util.List<javax.swing.tree.TreePath> insertNodesInto(java.util.List<? extends javax.swing.tree.DefaultMutableTreeNode> newChilds,
                                                                 javax.swing.tree.DefaultMutableTreeNode parent,
                                                                 int index)

getNodeActions

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

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

removeUndoableEditListener

public void removeUndoableEditListener(javax.swing.event.UndoableEditListener l)
Removes an UndoableEditListener.

Specified by:
removeUndoableEditListener in interface MutableTreeModel

addUndoableEditListener

public void addUndoableEditListener(javax.swing.event.UndoableEditListener l)
Adds an UndoableEditListener.

Specified by:
addUndoableEditListener in interface MutableTreeModel

addPropertyChangeListener

public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Adds a PropertyChangeListener which can optionally be wrapped into a WeakPropertyChangeListener.

Specified by:
addPropertyChangeListener in interface MutableTreeModel
Parameters:
listener -

removePropertyChangeListener

public void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
Removes a PropertyChangeListener. If the listener was added wrapped into a WeakPropertyChangeListener, the WeakPropertyChangeListener is removed.

Specified by:
removePropertyChangeListener in interface MutableTreeModel
Parameters:
listener -

fireUndoableEdit

public void fireUndoableEdit(javax.swing.undo.UndoableEdit edit)
Notify all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.

Specified by:
fireUndoableEdit in interface MutableTreeModel

firePropertyChange

protected void firePropertyChange(java.lang.String propertyName,
                                  boolean oldValue,
                                  boolean newValue)

firePropertyChange

protected void firePropertyChange(java.lang.String propertyName,
                                  int oldValue,
                                  int newValue)

firePropertyChange

protected void firePropertyChange(java.lang.String propertyName,
                                  @Nullable
                                  java.lang.Object oldValue,
                                  @Nullable
                                  java.lang.Object newValue)

(c) Werner Randelshofer.
All rights reserved.