javax.swing.tree
Class DefaultTreeSelectionModel

java.lang.Object
  extended by javax.swing.tree.DefaultTreeSelectionModel
All Implemented Interfaces:
Serializable, Cloneable, TreeSelectionModel
Direct Known Subclasses:
JTree.EmptySelectionModel

public class DefaultTreeSelectionModel
extends Object
implements Cloneable, Serializable, TreeSelectionModel

The implementation of the default tree selection model. The installed listeners are notified about the path and not the row changes. If you specifically need to track the row changes, register the listener for the expansion events.

See Also:
Serialized Form

Field Summary
protected  SwingPropertyChangeSupport changeSupport
          Our Swing property change support.
protected  int leadIndex
          The index of the last added path.
protected  TreePath leadPath
          The path that has been added last.
protected  int leadRow
          The row of the last added path according to the RowMapper.
protected  EventListenerList listenerList
          Our TreeSelectionListeners.
protected  DefaultListSelectionModel listSelectionModel
          The current listSelectionModel.
protected  RowMapper rowMapper
          The current RowMapper.
protected  TreePath[] selection
          The current selection.
static String SELECTION_MODE_PROPERTY
          The name of the selection mode property.
protected  int selectionMode
          The current selection mode.
 
Fields inherited from interface javax.swing.tree.TreeSelectionModel
CONTIGUOUS_TREE_SELECTION, DISCONTIGUOUS_TREE_SELECTION, SINGLE_TREE_SELECTION
 
Constructor Summary
DefaultTreeSelectionModel()
          Constructs a new DefaultTreeSelectionModel.
 
Method Summary
 void addPropertyChangeListener(PropertyChangeListener listener)
          Adds a PropertyChangeListener object to this model.
 void addSelectionPath(TreePath path)
          Adds a path to the list of selected paths.
 void addSelectionPaths(TreePath[] paths)
          Adds the paths to the list of selected paths.
 void addTreeSelectionListener(TreeSelectionListener listener)
          Adds a TreeSelectionListener object to this model.
protected  boolean arePathsContiguous(TreePath[] paths)
          Returns true if the paths are contiguous (take subsequent rows in the diplayed tree view.
protected  boolean canPathsBeAdded(TreePath[] paths)
          Checks if the paths can be added.
protected  boolean canPathsBeRemoved(TreePath[] paths)
          Checks if the paths can be removed without breaking the continuity of the selection according to selectionMode.
 void clearSelection()
          Removes all paths from the selection.
 Object clone()
          Creates a clone of this DefaultTreeSelectionModel with the same selection.
protected  void fireValueChanged(TreeSelectionEvent event)
          fireValueChanged
 TreePath getLeadSelectionPath()
          getLeadSelectionPath
 int getLeadSelectionRow()
          getLeadSelectionRow
<T extends EventListener>
T[]
getListeners(Class<T> listenerType)
          Returns all added listeners of a special type.
 int getMaxSelectionRow()
          Returns the largest row index from the selection.
 int getMinSelectionRow()
          Returns the smallest row index from the selection.
 PropertyChangeListener[] getPropertyChangeListeners()
          Returns all added PropertyChangeListener objects.
 RowMapper getRowMapper()
          Returns the RowMapper that is currently used to map between paths and their rows.
 int getSelectionCount()
          Returns the number of paths in the selection.
 int getSelectionMode()
          Returns the current selection mode.
 TreePath getSelectionPath()
          Returns the first path in the selection.
 TreePath[] getSelectionPaths()
          Returns the complete selection.
 int[] getSelectionRows()
          Returns the currently selected rows.
 TreeSelectionListener[] getTreeSelectionListeners()
          Returns all TreeSelectionListener added to this model.
protected  void insureRowContinuity()
          Makes sure the currently selected paths are valid according to the current selectionMode.
protected  void insureUniqueness()
          This method exists due historical reasons and returns without action (unless overridden).
 boolean isPathSelected(TreePath path)
          Checks if a given path is in the selection.
 boolean isRowSelected(int row)
          Checks if a particular row is selected.
 boolean isSelectionEmpty()
          Checks if the selection is empty.
protected  void notifyPathChange(Vector vPaths, TreePath oldLeadSelection)
          Notify the installed listeners that the given patches have changed.
 void removePropertyChangeListener(PropertyChangeListener listener)
          Removes a PropertyChangeListener object from this model.
 void removeSelectionPath(TreePath path)
          Removes the path from the selection.
 void removeSelectionPaths(TreePath[] paths)
          Removes the paths from the selection.
 void removeTreeSelectionListener(TreeSelectionListener listener)
          Removes a TreeSelectionListener object from this model.
 void resetRowSelection()
          Updates the mappings from TreePaths to row indices.
 void setRowMapper(RowMapper mapper)
          Sets the RowMapper that should be used to map between paths and their rows.
 void setSelectionMode(int mode)
          Sets the current selection mode.
 void setSelectionPath(TreePath path)
          Sets this path as the only selection.
 void setSelectionPaths(TreePath[] paths)
          Sets the paths as selection.
 String toString()
          Returns a string that shows this object's properties.
protected  void updateLeadIndex()
          Updates the lead selection row number after changing the lead selection path.
 
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
The name of the selection mode property.

See Also:
Constant Field Values

changeSupport

protected SwingPropertyChangeSupport changeSupport
Our Swing property change support.


selection

protected TreePath[] selection
The current selection.


listenerList

protected EventListenerList listenerList
Our TreeSelectionListeners.


rowMapper

protected transient RowMapper rowMapper
The current RowMapper.


listSelectionModel

protected DefaultListSelectionModel listSelectionModel
The current listSelectionModel.


selectionMode

protected int selectionMode
The current selection mode.


leadPath

protected TreePath leadPath
The path that has been added last.


leadIndex

protected int leadIndex
The index of the last added path.


leadRow

protected int leadRow
The row of the last added path according to the RowMapper.

Constructor Detail

DefaultTreeSelectionModel

public DefaultTreeSelectionModel()
Constructs a new DefaultTreeSelectionModel.

Method Detail

clone

public Object clone()
             throws CloneNotSupportedException
Creates a clone of this DefaultTreeSelectionModel with the same selection. The cloned instance will have the same registered listeners, the listeners themselves will not be cloned. The selection will be cloned.

Overrides:
clone in class Object
Returns:
a copy of this DefaultTreeSelectionModel
Throws:
CloneNotSupportedException - should not be thrown here
See Also:
Cloneable

toString

public String toString()
Returns a string that shows this object's properties. The returned string lists the selected tree rows, if any.

Overrides:
toString in class Object
Returns:
a string that shows this object's properties
See Also:
Object.getClass(), Object.hashCode(), Class.getName(), Integer.toHexString(int)

setRowMapper

public void setRowMapper(RowMapper mapper)
Sets the RowMapper that should be used to map between paths and their rows.

Specified by:
setRowMapper in interface TreeSelectionModel
Parameters:
mapper - the RowMapper to set
See Also:
RowMapper

getRowMapper

public RowMapper getRowMapper()
Returns the RowMapper that is currently used to map between paths and their rows.

Specified by:
getRowMapper in interface TreeSelectionModel
Returns:
the current RowMapper
See Also:
RowMapper

setSelectionMode

public void setSelectionMode(int mode)
Sets the current selection mode. Possible values are TreeSelectionModel.SINGLE_TREE_SELECTION, TreeSelectionModel.CONTIGUOUS_TREE_SELECTION and TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION.

Specified by:
setSelectionMode in interface TreeSelectionModel
Parameters:
mode - the selection mode to be set
See Also:
getSelectionMode(), TreeSelectionModel.SINGLE_TREE_SELECTION, TreeSelectionModel.CONTIGUOUS_TREE_SELECTION, TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION

getSelectionMode

public int getSelectionMode()
Returns the current selection mode.

Specified by:
getSelectionMode in interface TreeSelectionModel
Returns:
the current selection mode
See Also:
setSelectionMode(int), TreeSelectionModel.SINGLE_TREE_SELECTION, TreeSelectionModel.CONTIGUOUS_TREE_SELECTION, TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION

setSelectionPath

public void setSelectionPath(TreePath path)
Sets this path as the only selection. If this changes the selection the registered TreeSelectionListeners are notified.

Specified by:
setSelectionPath in interface TreeSelectionModel
Parameters:
path - the path to set as selection

setSelectionPaths

public void setSelectionPaths(TreePath[] paths)
Sets the paths as selection. This method checks for duplicates and removes them. If this changes the selection the registered TreeSelectionListeners are notified.

Specified by:
setSelectionPaths in interface TreeSelectionModel
Parameters:
paths - the paths to set as selection

addSelectionPath

public void addSelectionPath(TreePath path)
Adds a path to the list of selected paths. This method checks if the path is already selected and doesn't add the same path twice. If this changes the selection the registered TreeSelectionListeners are notified. The lead path is changed to the added path. This also happen if the passed path was already selected before.

Specified by:
addSelectionPath in interface TreeSelectionModel
Parameters:
path - the path to add to the selection

addSelectionPaths

public void addSelectionPaths(TreePath[] paths)
Adds the paths to the list of selected paths. This method checks if the paths are already selected and doesn't add the same path twice. If this changes the selection the registered TreeSelectionListeners are notified.

Specified by:
addSelectionPaths in interface TreeSelectionModel
Parameters:
paths - the paths to add to the selection

removeSelectionPath

public void removeSelectionPath(TreePath path)
Removes the path from the selection. If this changes the selection the registered TreeSelectionListeners are notified.

Specified by:
removeSelectionPath in interface TreeSelectionModel
Parameters:
path - the path to remove

removeSelectionPaths

public void removeSelectionPaths(TreePath[] paths)
Removes the paths from the selection. If this changes the selection the registered TreeSelectionListeners are notified.

Specified by:
removeSelectionPaths in interface TreeSelectionModel
Parameters:
paths - the paths to remove

getSelectionPath

public TreePath getSelectionPath()
Returns the first path in the selection. This is especially useful when the selectionMode is TreeSelectionModel.SINGLE_TREE_SELECTION.

Specified by:
getSelectionPath in interface TreeSelectionModel
Returns:
the first path in the selection

getSelectionPaths

public TreePath[] getSelectionPaths()
Returns the complete selection.

Specified by:
getSelectionPaths in interface TreeSelectionModel
Returns:
the complete selection

getSelectionCount

public int getSelectionCount()
Returns the number of paths in the selection.

Specified by:
getSelectionCount in interface TreeSelectionModel
Returns:
the number of paths in the selection

isPathSelected

public boolean isPathSelected(TreePath path)
Checks if a given path is in the selection.

Specified by:
isPathSelected in interface TreeSelectionModel
Parameters:
path - the path to check
Returns:
true if the path is in the selection, false otherwise

isSelectionEmpty

public boolean isSelectionEmpty()
Checks if the selection is empty.

Specified by:
isSelectionEmpty in interface TreeSelectionModel
Returns:
true if the selection is empty, false otherwise

clearSelection

public void clearSelection()
Removes all paths from the selection. Fire the unselection event.

Specified by:
clearSelection in interface TreeSelectionModel

addTreeSelectionListener

public void addTreeSelectionListener(TreeSelectionListener listener)
Adds a TreeSelectionListener object to this model.

Specified by:
addTreeSelectionListener in interface TreeSelectionModel
Parameters:
listener - the listener to add

removeTreeSelectionListener

public void removeTreeSelectionListener(TreeSelectionListener listener)
Removes a TreeSelectionListener object from this model.

Specified by:
removeTreeSelectionListener in interface TreeSelectionModel
Parameters:
listener - the listener to remove

getTreeSelectionListeners

public TreeSelectionListener[] getTreeSelectionListeners()
Returns all TreeSelectionListener added to this model.

Returns:
an array of listeners
Since:
1.4

fireValueChanged

protected void fireValueChanged(TreeSelectionEvent event)
fireValueChanged

Parameters:
event - the event to fire.

getListeners

public <T extends EventListener> T[] getListeners(Class<T> listenerType)
Returns all added listeners of a special type.

Parameters:
listenerType - the listener type
Returns:
an array of listeners
Since:
1.3

getSelectionRows

public int[] getSelectionRows()
Returns the currently selected rows.

Specified by:
getSelectionRows in interface TreeSelectionModel
Returns:
the currently selected rows

getMinSelectionRow

public int getMinSelectionRow()
Returns the smallest row index from the selection.

Specified by:
getMinSelectionRow in interface TreeSelectionModel
Returns:
the smallest row index from the selection

getMaxSelectionRow

public int getMaxSelectionRow()
Returns the largest row index from the selection.

Specified by:
getMaxSelectionRow in interface TreeSelectionModel
Returns:
the largest row index from the selection

isRowSelected

public boolean isRowSelected(int row)
Checks if a particular row is selected.

Specified by:
isRowSelected in interface TreeSelectionModel
Parameters:
row - the index of the row to check
Returns:
true if the row is in this selection, false otherwise
Throws:
NullPointerException - if the row mapper is not set (can only happen if the user has plugged in the custom incorrect TreeUI implementation.

resetRowSelection

public void resetRowSelection()
Updates the mappings from TreePaths to row indices.

Specified by:
resetRowSelection in interface TreeSelectionModel

getLeadSelectionRow

public int getLeadSelectionRow()
getLeadSelectionRow

Specified by:
getLeadSelectionRow in interface TreeSelectionModel
Returns:
int

getLeadSelectionPath

public TreePath getLeadSelectionPath()
getLeadSelectionPath

Specified by:
getLeadSelectionPath in interface TreeSelectionModel
Returns:
TreePath

addPropertyChangeListener

public void addPropertyChangeListener(PropertyChangeListener listener)
Adds a PropertyChangeListener object to this model.

Specified by:
addPropertyChangeListener in interface TreeSelectionModel
Parameters:
listener - the listener to add.

removePropertyChangeListener

public void removePropertyChangeListener(PropertyChangeListener listener)
Removes a PropertyChangeListener object from this model.

Specified by:
removePropertyChangeListener in interface TreeSelectionModel
Parameters:
listener - the listener to remove.

getPropertyChangeListeners

public PropertyChangeListener[] getPropertyChangeListeners()
Returns all added PropertyChangeListener objects.

Returns:
an array of listeners.
Since:
1.4

insureRowContinuity

protected void insureRowContinuity()
Makes sure the currently selected paths are valid according to the current selectionMode. If the selectionMode is set to TreeSelectionModel.CONTIGUOUS_TREE_SELECTION and the selection isn't contiguous then the selection is reset to the first set of contguous paths. If the selectionMode is set to TreeSelectionModel.SINGLE_TREE_SELECTION and the selection has more than one path, the selection is reset to the contain only the first path.


arePathsContiguous

protected boolean arePathsContiguous(TreePath[] paths)
Returns true if the paths are contiguous (take subsequent rows in the diplayed tree view. The method returns true if we have no RowMapper assigned.

Parameters:
paths - the paths to check for continuity
Returns:
true if the paths are contiguous or we have no RowMapper assigned

canPathsBeAdded

protected boolean canPathsBeAdded(TreePath[] paths)
Checks if the paths can be added. This returns true if:

canPathsBeRemoved

protected boolean canPathsBeRemoved(TreePath[] paths)
Checks if the paths can be removed without breaking the continuity of the selection according to selectionMode.

Parameters:
paths - the paths to check
Returns:
true if the paths can be removed with respect to the selectionMode

notifyPathChange

protected void notifyPathChange(Vector vPaths,
                                TreePath oldLeadSelection)
Notify the installed listeners that the given patches have changed. This method will call listeners if invoked, but it is not called from the implementation of this class.

Parameters:
vPaths - the vector of the changed patches
oldLeadSelection - the old selection index

updateLeadIndex

protected void updateLeadIndex()
Updates the lead selection row number after changing the lead selection path.


insureUniqueness

protected void insureUniqueness()
This method exists due historical reasons and returns without action (unless overridden). For compatibility with the applications that override it, it is still called from the setSelectionPaths(TreePath[]) and addSelectionPaths(TreePath[]).