javax.swing.tree

Class DefaultTreeSelectionModel

Implemented Interfaces:
Cloneable, Serializable, TreeSelectionModel
Known Direct 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

static String
SELECTION_MODE_PROPERTY
The name of the selection mode property.
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 DefaultListSelectionModel
listSelectionModel
The current listSelectionModel.
protected EventListenerList
listenerList
Our TreeSelectionListeners.
protected RowMapper
rowMapper
The current RowMapper.
protected TreePath[]
selection
The current selection.
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.
extends EventListener> T[] getListeners(Class listenerType)
Returns all added listeners of a special type.
protected void
fireValueChanged(TreeSelectionEvent event)
fireValueChanged
TreePath
getLeadSelectionPath()
getLeadSelectionPath
int
getLeadSelectionRow()
getLeadSelectionRow
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

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

SELECTION_MODE_PROPERTY

public static final String SELECTION_MODE_PROPERTY
The name of the selection mode property.
Field Value:
"selectionMode"

changeSupport

protected SwingPropertyChangeSupport changeSupport
Our Swing property change support.

leadIndex

protected int leadIndex
The index of the last added path.

leadPath

protected TreePath leadPath
The path that has been added last.

leadRow

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

listSelectionModel

protected DefaultListSelectionModel listSelectionModel
The current listSelectionModel.

listenerList

protected EventListenerList listenerList
Our TreeSelectionListeners.

rowMapper

protected RowMapper rowMapper
The current RowMapper.

selection

protected TreePath[] selection
The current selection.

selectionMode

protected int selectionMode
The current selection mode.

Constructor Details

DefaultTreeSelectionModel

public DefaultTreeSelectionModel()
Constructs a new DefaultTreeSelectionModel.

Method Details

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.

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

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

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:
Parameters:
paths - the paths to check
Returns:
true if the paths can be added with respect to the selectionMode

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

clearSelection

public void clearSelection()
Removes all paths from the selection. Fire the unselection event.
Specified by:
clearSelection in interface TreeSelectionModel

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 interface Object
Returns:
a copy of this DefaultTreeSelectionModel
Throws:
CloneNotSupportedException - should not be thrown here

extends EventListener> T[] getListeners

public extends EventListener> T[] getListeners(Class listenerType)
Returns all added listeners of a special type.
Parameters:
listenerType - the listener type
Returns:
an array of listeners
Since:
1.3

fireValueChanged

protected void fireValueChanged(TreeSelectionEvent event)
fireValueChanged
Parameters:
event - the event to fire.

getLeadSelectionPath

public TreePath getLeadSelectionPath()
getLeadSelectionPath
Specified by:
getLeadSelectionPath in interface TreeSelectionModel
Returns:
TreePath

getLeadSelectionRow

public int getLeadSelectionRow()
getLeadSelectionRow
Specified by:
getLeadSelectionRow in interface TreeSelectionModel
Returns:
int

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

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

getPropertyChangeListeners

public PropertyChangeListener[] getPropertyChangeListeners()
Returns all added PropertyChangeListener objects.
Returns:
an array of listeners.
Since:
1.4

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

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

getSelectionMode

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

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

getSelectionRows

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

getTreeSelectionListeners

public TreeSelectionListener[] getTreeSelectionListeners()
Returns all TreeSelectionListener added to this model.
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.

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[]).

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

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.

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

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

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.

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

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

resetRowSelection

public void resetRowSelection()
Updates the mappings from TreePaths to row indices.
Specified by:
resetRowSelection in interface TreeSelectionModel

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

setSelectionMode

public void setSelectionMode(int mode)
Specified by:
setSelectionMode in interface TreeSelectionModel
Parameters:
mode - the selection mode to be set

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

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 interface Object
Returns:
a string that shows this object's properties

updateLeadIndex

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

DefaultTreeSelectionModel.java Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.