JavaTM 2 Platform
Standard Edition

javax.swing.tree
Class DefaultTreeSelectionModel

java.lang.Object
  |
  +--javax.swing.tree.DefaultTreeSelectionModel
Direct Known Subclasses:
JTree.EmptySelectionModel

public class DefaultTreeSelectionModel
extends Object
implements Cloneable, Serializable, TreeSelectionModel

Implementation of TreeSelectionModel. Listeners are notified whenever the paths in the selection change, not the rows. In order to be able to track row changes you may wish to become a listener for expansion events on the tree and test for changes from there.

resetRowSelection is called from any of the methods that update the selected paths. If you subclass and of these methods to filter what is allowed to be selected, be sure and message resetRowSelection if you do not message super.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. A future release of Swing will provide support for long term persistence.

See Also:
Serialized Form

Field Summary
protected  SwingPropertyChangeSupport changeSupport
          Used to messaged registered listeners.
protected  int leadIndex
          Index of the lead path in selection.
protected  TreePath leadPath
          Last path that was added.
protected  int leadRow
          Lead row.
protected  EventListenerList listenerList
          Event listener list.
protected  DefaultListSelectionModel listSelectionModel
          Handles maintaining the list selection model.
protected  RowMapper rowMapper
          Provides a row for a given path.
protected  TreePath[] selection
          Paths that are currently selected.
static String SELECTION_MODE_PROPERTY
          Property name for selectionMode.
protected  int selectionMode
          Mode for the selection, will be either SINGLE_TREE_SELECTION, CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION.
 
Fields inherited from interface javax.swing.tree.TreeSelectionModel
CONTIGUOUS_TREE_SELECTION, DISCONTIGUOUS_TREE_SELECTION, SINGLE_TREE_SELECTION
 
Constructor Summary
DefaultTreeSelectionModel()
          Creates a new instance of DefaultTreeSelectionModel that is empty, and having a selection mode of DISCONTIGUOUS_TREE_SELECTION.
 
Method Summary
 void addPropertyChangeListener(PropertyChangeListener listener)
          Add a PropertyChangeListener to the listener list.
 void addSelectionPath(TreePath path)
          Adds path to the current selection.
 void addSelectionPaths(TreePath[] paths)
          Adds paths to the current selection.
 void addTreeSelectionListener(TreeSelectionListener x)
          Adds x to the list of listeners that are notified each time the selection changes.
protected  boolean arePathsContiguous(TreePath[] paths)
          Returns true if the paths are contiguous.
protected  boolean canPathsBeAdded(TreePath[] paths)
          Returns true if the paths can be added without breaking the continuity of the model.
protected  boolean canPathsBeRemoved(TreePath[] paths)
          Returns true if the paths can be removed without breaking the continuity of the model.
 void clearSelection()
          Empties the current selection.
 Object clone()
          Returns a clone of the reciever with the same selection.
protected  void fireValueChanged(TreeSelectionEvent e)
           
 TreePath getLeadSelectionPath()
          Returns the last path that was added.
 int getLeadSelectionRow()
          Returns the lead selection index.
 int getMaxSelectionRow()
          Gets the last selected row.
 int getMinSelectionRow()
          Gets the first selected row.
 RowMapper getRowMapper()
          Returns the RowMapper instance that is able to map a path to a row.
 int getSelectionCount()
          Returns the number of paths that are selected.
 int getSelectionMode()
          Returns the selection mode.
 TreePath getSelectionPath()
          Returns the first path in the selection.
 TreePath[] getSelectionPaths()
          Returns the paths in the selection.
 int[] getSelectionRows()
          Returns all of the currently selected rows.
protected  void insureRowContinuity()
          Useful for CONTIGUOUS_TREE_SELECTION.
protected  void insureUniqueness()
          Insures that all the elements in path are unique.
 boolean isPathSelected(TreePath path)
          Returns true if the path, path, is in the current selection.
 boolean isRowSelected(int row)
          Returns true if the row identitifed by row is selected.
 boolean isSelectionEmpty()
          Returns true if the selection is currently empty.
protected  void notifyPathChange(Vector changedPaths, TreePath oldLeadSelection)
          Notifies listeners of a change in path.
 void removePropertyChangeListener(PropertyChangeListener listener)
          Remove a PropertyChangeListener from the listener list.
 void removeSelectionPath(TreePath path)
          Removes path from the selection.
 void removeSelectionPaths(TreePath[] paths)
          Removes paths from the selection.
 void removeTreeSelectionListener(TreeSelectionListener x)
          Removes x from the list of listeners that are notified each time the selection changes.
 void resetRowSelection()
          Recalculates what rows are selected by asking the RowMapper for the row for each path.
 void setRowMapper(RowMapper newMapper)
          Sets the RowMapper instance.
 void setSelectionMode(int mode)
          Sets the selection model, which must be one of SINGLE_TREE_SELECTION, CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION.
 void setSelectionPath(TreePath path)
          Sets the selection to path.
 void setSelectionPaths(TreePath[] pPaths)
          Sets the selection to the paths in paths.
 String toString()
          Returns a string that displays and identifies this object's properties.
protected  void updateLeadIndex()
          Updates the leadIndex instance variable.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SELECTION_MODE_PROPERTY

public static final String SELECTION_MODE_PROPERTY
Property name for selectionMode.

changeSupport

protected SwingPropertyChangeSupport changeSupport
Used to messaged registered listeners.

selection

protected TreePath[] selection
Paths that are currently selected. Will be null if nothing is currently selected.

listenerList

protected EventListenerList listenerList
Event listener list.

rowMapper

protected transient RowMapper rowMapper
Provides a row for a given path.

listSelectionModel

protected DefaultListSelectionModel listSelectionModel
Handles maintaining the list selection model.

selectionMode

protected int selectionMode
Mode for the selection, will be either SINGLE_TREE_SELECTION, CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION.

leadPath

protected TreePath leadPath
Last path that was added.

leadIndex

protected int leadIndex
Index of the lead path in selection.

leadRow

protected int leadRow
Lead row.
Constructor Detail

DefaultTreeSelectionModel

public DefaultTreeSelectionModel()
Creates a new instance of DefaultTreeSelectionModel that is empty, and having a selection mode of DISCONTIGUOUS_TREE_SELECTION.
Method Detail

setRowMapper

public void setRowMapper(RowMapper newMapper)
Sets the RowMapper instance. This instance is used to determine what row corresponds to what path.
Specified by:
setRowMapper in interface TreeSelectionModel

getRowMapper

public RowMapper getRowMapper()
Returns the RowMapper instance that is able to map a path to a row.
Specified by:
getRowMapper in interface TreeSelectionModel

setSelectionMode

public void setSelectionMode(int mode)
Sets the selection model, which must be one of SINGLE_TREE_SELECTION, CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION.
Specified by:
setSelectionMode in interface TreeSelectionModel

getSelectionMode

public int getSelectionMode()
Returns the selection mode.
Specified by:
getSelectionMode in interface TreeSelectionModel

setSelectionPath

public void setSelectionPath(TreePath path)
Sets the selection to path. If this represents a change, then the TreeSelectionListeners are notified.
Specified by:
setSelectionPath in interface TreeSelectionModel
Parameters:
path - new path to select

setSelectionPaths

public void setSelectionPaths(TreePath[] pPaths)
Sets the selection to the paths in paths. If this represents a change the TreeSelectionListeners are notified. Potentially paths will be held by the reciever, in other words don't change any of the objects in the array once passed in.
Specified by:
setSelectionPaths in interface TreeSelectionModel
Parameters:
paths - new selection.

addSelectionPath

public void addSelectionPath(TreePath path)
Adds path to the current selection. If path is not currently in the selection the TreeSelectionListeners are notified.
Specified by:
addSelectionPath in interface TreeSelectionModel
Parameters:
path - the new path to add to the current selection.

addSelectionPaths

public void addSelectionPaths(TreePath[] paths)
Adds paths to the current selection. If any of the paths in paths are not currently in the selection the TreeSelectionListeners are notified.
Specified by:
addSelectionPaths in interface TreeSelectionModel
Parameters:
path - the new path to add to the current selection.

removeSelectionPath

public void removeSelectionPath(TreePath path)
Removes path from the selection. If path is in the selection The TreeSelectionListeners are notified.
Specified by:
removeSelectionPath in interface TreeSelectionModel
Parameters:
path - the path to remove from the selection.

removeSelectionPaths

public void removeSelectionPaths(TreePath[] paths)
Removes paths from the selection. If any of the paths in paths are in the selection the TreeSelectionListeners are notified.
Specified by:
removeSelectionPaths in interface TreeSelectionModel
Parameters:
path - the path to remove from the selection.

getSelectionPath

public TreePath getSelectionPath()
Returns the first path in the selection.
Specified by:
getSelectionPath in interface TreeSelectionModel

getSelectionPaths

public TreePath[] getSelectionPaths()
Returns the paths in the selection.
Specified by:
getSelectionPaths in interface TreeSelectionModel

getSelectionCount

public int getSelectionCount()
Returns the number of paths that are selected.
Specified by:
getSelectionCount in interface TreeSelectionModel

isPathSelected

public boolean isPathSelected(TreePath path)
Returns true if the path, path, is in the current selection.
Specified by:
isPathSelected in interface TreeSelectionModel

isSelectionEmpty

public boolean isSelectionEmpty()
Returns true if the selection is currently empty.
Specified by:
isSelectionEmpty in interface TreeSelectionModel

clearSelection

public void clearSelection()
Empties the current selection. If this represents a change in the current selection, the selection listeners are notified.
Specified by:
clearSelection in interface TreeSelectionModel

addTreeSelectionListener

public void addTreeSelectionListener(TreeSelectionListener x)
Adds x to the list of listeners that are notified each time the selection changes.
Specified by:
addTreeSelectionListener in interface TreeSelectionModel
Parameters:
x - the new listener to be added.

removeTreeSelectionListener

public void removeTreeSelectionListener(TreeSelectionListener x)
Removes x from the list of listeners that are notified each time the selection changes.
Specified by:
removeTreeSelectionListener in interface TreeSelectionModel
Parameters:
x - the listener to remove.

fireValueChanged

protected void fireValueChanged(TreeSelectionEvent e)

getSelectionRows

public int[] getSelectionRows()
Returns all of the currently selected rows.
Specified by:
getSelectionRows in interface TreeSelectionModel

getMinSelectionRow

public int getMinSelectionRow()
Gets the first selected row.
Specified by:
getMinSelectionRow in interface TreeSelectionModel

getMaxSelectionRow

public int getMaxSelectionRow()
Gets the last selected row.
Specified by:
getMaxSelectionRow in interface TreeSelectionModel

isRowSelected

public boolean isRowSelected(int row)
Returns true if the row identitifed by row is selected.
Specified by:
isRowSelected in interface TreeSelectionModel

resetRowSelection

public void resetRowSelection()
Recalculates what rows are selected by asking the RowMapper for the row for each path.
Specified by:
resetRowSelection in interface TreeSelectionModel

getLeadSelectionRow

public int getLeadSelectionRow()
Returns the lead selection index. That is the last index that was added.
Specified by:
getLeadSelectionRow in interface TreeSelectionModel

getLeadSelectionPath

public TreePath getLeadSelectionPath()
Returns the last path that was added.
Specified by:
getLeadSelectionPath in interface TreeSelectionModel

addPropertyChangeListener

public void addPropertyChangeListener(PropertyChangeListener listener)
Add a PropertyChangeListener to the listener list. The listener is registered for all properties.

A PropertyChangeEvent will get fired in response to an explicit setFont, setBackground, or SetForeground on the current component. Note that if the current component is inheriting its foreground, background, or font from its container, then no event will be fired in response to a change in the inherited property.

Specified by:
addPropertyChangeListener in interface TreeSelectionModel
Parameters:
listener - The PropertyChangeListener to be added

removePropertyChangeListener

public void removePropertyChangeListener(PropertyChangeListener listener)
Remove a PropertyChangeListener from the listener list. This removes a PropertyChangeListener that was registered for all properties.
Specified by:
removePropertyChangeListener in interface TreeSelectionModel
Parameters:
listener - The PropertyChangeListener to be removed

insureRowContinuity

protected void insureRowContinuity()
Useful for CONTIGUOUS_TREE_SELECTION. If the rows that are selected are not contiguous then the selection is reset to be contiguous. Or if the selection mode is single selection and more than one this is selected the selection is reset.

arePathsContiguous

protected boolean arePathsContiguous(TreePath[] paths)
Returns true if the paths are contiguous.

canPathsBeAdded

protected boolean canPathsBeAdded(TreePath[] paths)
Returns true if the paths can be added without breaking the continuity of the model.

canPathsBeRemoved

protected boolean canPathsBeRemoved(TreePath[] paths)
Returns true if the paths can be removed without breaking the continuity of the model. This is rather expensive.

notifyPathChange

protected void notifyPathChange(Vector changedPaths,
                                TreePath oldLeadSelection)
Notifies listeners of a change in path. changePaths should contain instances of PathPlaceHolder.

updateLeadIndex

protected void updateLeadIndex()
Updates the leadIndex instance variable.

insureUniqueness

protected void insureUniqueness()
Insures that all the elements in path are unique. This does not check for a null selection!

toString

public String toString()
Returns a string that displays and identifies this object's properties.
Overrides:
toString in class Object
Returns:
a String representation of this object

clone

public Object clone()
             throws CloneNotSupportedException
Returns a clone of the reciever with the same selection. selectionListeners, and PropertyListeners are not duplicated.
Overrides:
clone in class Object
Throws:
CloneNotSupportedException - if the receiver does not both (a) implement the Cloneable interface and (b) define a clone method.

JavaTM 2 Platform
Standard Edition

Submit a bug or feature
Java, Java 2D, and JDBC are a trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1999 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.