javax.swing.tree

Class DefaultTreeSelectionModel

Implemented Interfaces:
Cloneable, Serializable, TreeSelectionModel
Known Direct Subclasses:
JTree.EmptySelectionModel

public class DefaultTreeSelectionModel
extends Object
implements Cloneable, Serializable, TreeSelectionModel

DefaultTreeSelectionModel

See Also:
Serialized Form

Field Summary

static String
SELECTION_MODE_PROPERTY
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 or we have no RowMapper assigned.
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
EventListener[]
getListeners(Class 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()
Deprecated and not used.
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 value0, TreePath value1)
notifyPathChange
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 rowMapper)
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 index instance field.

Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

SELECTION_MODE_PROPERTY

public static final String SELECTION_MODE_PROPERTY
SELECTION_MODE_PROPERTY

Field Value:
""


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

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 or 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:
  • paths is null or empty
  • we have no RowMapper assigned
  • nothing is currently selected
  • selectionMode is DefaultTreeSelectionModel
  • adding the paths to the selection still results in a contiguous set of paths

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.
Specified by:
clearSelection in interface TreeSelectionModel


clone

public Object clone()
            throws CloneNotSupportedException
Creates a clone of this DefaultTreeSelectionModel with the same selection.
Overrides:
clone in interface Object

Returns:
a clone of this DefaultTreeSelectionModel

Throws:
CloneNotSupportedException - should not be thrown here


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


getListeners

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

Parameters:
listenerType - the listener type

Returns:
an array of listeners

Since:
1.3


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

See Also:
setSelectionMode(int), DefaultTreeSelectionModel, DefaultTreeSelectionModel, DefaultTreeSelectionModel


getSelectionPath

public TreePath getSelectionPath()
Returns the first path in the selection. This is especially useful when the selectionMode is DefaultTreeSelectionModel.
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 DefaultTreeSelectionModel and the selection isn't contiguous then the selection is reset to the first set of contguous paths. If the selectionMode is set to DefaultTreeSelectionModel and the selection has more than one path, the selection is reset to the contain only the first path.


insureUniqueness

protected void insureUniqueness()
Deprecated and not used.


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


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 value0,
                                TreePath value1)
notifyPathChange

Parameters:
value0 - TODO
value1 - TODO


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.

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 rowMapper)
Sets the RowMapper that should be used to map between paths and their rows.
Specified by:
setRowMapper in interface TreeSelectionModel

Parameters:
rowMapper - the RowMapper to set

See Also:
RowMapper


setSelectionMode

public void setSelectionMode(int mode)
Sets the current selection mode. Possible values are DefaultTreeSelectionModel, DefaultTreeSelectionModel and DefaultTreeSelectionModel.
Specified by:
setSelectionMode in interface TreeSelectionModel

Parameters:
mode - the selection mode to be set

See Also:
getSelectionMode(), DefaultTreeSelectionModel, DefaultTreeSelectionModel, DefaultTreeSelectionModel


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.

Parameters:
paths - the paths to set as selection


toString

public String toString()
Returns a string that shows this object's properties.
Overrides:
toString in interface Object

Returns:
a string that shows this object's properties


updateLeadIndex

protected void updateLeadIndex()
Updates the lead index instance field.


DefaultTreeSelectionModel.java Copyright (C) 2002, 2004, 2005 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.