0N/A/*
2362N/A * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing.tree;
0N/A
0N/Aimport java.beans.PropertyChangeListener;
0N/Aimport java.io.*;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.BitSet;
0N/Aimport java.util.Enumeration;
0N/Aimport java.util.EventListener;
0N/Aimport java.util.Hashtable;
0N/Aimport java.util.List;
0N/Aimport java.util.Vector;
0N/Aimport javax.swing.event.*;
0N/Aimport javax.swing.DefaultListSelectionModel;
0N/A
0N/A/**
0N/A * Default implementation of TreeSelectionModel. Listeners are notified
0N/A * whenever
0N/A * the paths in the selection change, not the rows. In order
0N/A * to be able to track row changes you may wish to become a listener
0N/A * for expansion events on the tree and test for changes from there.
0N/A * <p>resetRowSelection is called from any of the methods that update
0N/A * the selected paths. If you subclass any of these methods to
0N/A * filter what is allowed to be selected, be sure and message
0N/A * <code>resetRowSelection</code> if you do not message super.
0N/A *
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A *
0N/A * @see javax.swing.JTree
0N/A *
0N/A * @author Scott Violet
0N/A */
625N/Apublic class DefaultTreeSelectionModel implements Cloneable, Serializable, TreeSelectionModel
0N/A{
0N/A /** Property name for selectionMode. */
0N/A public static final String SELECTION_MODE_PROPERTY = "selectionMode";
0N/A
0N/A /** Used to messaged registered listeners. */
0N/A protected SwingPropertyChangeSupport changeSupport;
0N/A
0N/A /** Paths that are currently selected. Will be null if nothing is
0N/A * currently selected. */
0N/A protected TreePath[] selection;
0N/A
0N/A /** Event listener list. */
0N/A protected EventListenerList listenerList = new EventListenerList();
0N/A
0N/A /** Provides a row for a given path. */
0N/A transient protected RowMapper rowMapper;
0N/A
0N/A /** Handles maintaining the list selection model. The RowMapper is used
0N/A * to map from a TreePath to a row, and the value is then placed here. */
0N/A protected DefaultListSelectionModel listSelectionModel;
0N/A
0N/A /** Mode for the selection, will be either SINGLE_TREE_SELECTION,
0N/A * CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION.
0N/A */
0N/A protected int selectionMode;
0N/A
0N/A /** Last path that was added. */
0N/A protected TreePath leadPath;
0N/A /** Index of the lead path in selection. */
0N/A protected int leadIndex;
0N/A /** Lead row. */
0N/A protected int leadRow;
0N/A
0N/A /** Used to make sure the paths are unique, will contain all the paths
0N/A * in <code>selection</code>.
0N/A */
625N/A private Hashtable<TreePath, Boolean> uniquePaths;
625N/A private Hashtable<TreePath, Boolean> lastPaths;
0N/A private TreePath[] tempPaths;
0N/A
0N/A
0N/A /**
0N/A * Creates a new instance of DefaultTreeSelectionModel that is
0N/A * empty, with a selection mode of DISCONTIGUOUS_TREE_SELECTION.
0N/A */
0N/A public DefaultTreeSelectionModel() {
0N/A listSelectionModel = new DefaultListSelectionModel();
0N/A selectionMode = DISCONTIGUOUS_TREE_SELECTION;
0N/A leadIndex = leadRow = -1;
625N/A uniquePaths = new Hashtable<TreePath, Boolean>();
625N/A lastPaths = new Hashtable<TreePath, Boolean>();
0N/A tempPaths = new TreePath[1];
0N/A }
0N/A
0N/A /**
0N/A * Sets the RowMapper instance. This instance is used to determine
0N/A * the row for a particular TreePath.
0N/A */
0N/A public void setRowMapper(RowMapper newMapper) {
0N/A rowMapper = newMapper;
0N/A resetRowSelection();
0N/A }
0N/A
0N/A /**
0N/A * Returns the RowMapper instance that is able to map a TreePath to a
0N/A * row.
0N/A */
0N/A public RowMapper getRowMapper() {
0N/A return rowMapper;
0N/A }
0N/A
0N/A /**
0N/A * Sets the selection model, which must be one of SINGLE_TREE_SELECTION,
0N/A * CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION. If mode
0N/A * is not one of the defined value,
0N/A * <code>DISCONTIGUOUS_TREE_SELECTION</code> is assumed.
0N/A * <p>This may change the selection if the current selection is not valid
0N/A * for the new mode. For example, if three TreePaths are
0N/A * selected when the mode is changed to <code>SINGLE_TREE_SELECTION</code>,
0N/A * only one TreePath will remain selected. It is up to the particular
0N/A * implementation to decide what TreePath remains selected.
0N/A * <p>
0N/A * Setting the mode to something other than the defined types will
0N/A * result in the mode becoming <code>DISCONTIGUOUS_TREE_SELECTION</code>.
0N/A */
0N/A public void setSelectionMode(int mode) {
0N/A int oldMode = selectionMode;
0N/A
0N/A selectionMode = mode;
0N/A if(selectionMode != TreeSelectionModel.SINGLE_TREE_SELECTION &&
0N/A selectionMode != TreeSelectionModel.CONTIGUOUS_TREE_SELECTION &&
0N/A selectionMode != TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION)
0N/A selectionMode = TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION;
0N/A if(oldMode != selectionMode && changeSupport != null)
0N/A changeSupport.firePropertyChange(SELECTION_MODE_PROPERTY,
215N/A Integer.valueOf(oldMode),
215N/A Integer.valueOf(selectionMode));
0N/A }
0N/A
0N/A /**
0N/A * Returns the selection mode, one of <code>SINGLE_TREE_SELECTION</code>,
0N/A * <code>DISCONTIGUOUS_TREE_SELECTION</code> or
0N/A * <code>CONTIGUOUS_TREE_SELECTION</code>.
0N/A */
0N/A public int getSelectionMode() {
0N/A return selectionMode;
0N/A }
0N/A
0N/A /**
0N/A * Sets the selection to path. If this represents a change, then
0N/A * the TreeSelectionListeners are notified. If <code>path</code> is
0N/A * null, this has the same effect as invoking <code>clearSelection</code>.
0N/A *
0N/A * @param path new path to select
0N/A */
0N/A public void setSelectionPath(TreePath path) {
0N/A if(path == null)
0N/A setSelectionPaths(null);
0N/A else {
0N/A TreePath[] newPaths = new TreePath[1];
0N/A
0N/A newPaths[0] = path;
0N/A setSelectionPaths(newPaths);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Sets the selection. Whether the supplied paths are taken as the
0N/A * new selection depends upon the selection mode. If the supplied
0N/A * array is {@code null}, or empty, the selection is cleared. If
0N/A * the selection mode is {@code SINGLE_TREE_SELECTION}, only the
0N/A * first path in {@code pPaths} is used. If the selection
0N/A * mode is {@code CONTIGUOUS_TREE_SELECTION} and the supplied paths
0N/A * are not contiguous, then only the first path in {@code pPaths} is
0N/A * used. If the selection mode is
0N/A * {@code DISCONTIGUOUS_TREE_SELECTION}, then all paths are used.
0N/A * <p>
0N/A * All {@code null} paths in {@code pPaths} are ignored.
0N/A * <p>
0N/A * If this represents a change, all registered {@code
0N/A * TreeSelectionListener}s are notified.
0N/A * <p>
0N/A * The lead path is set to the last unique path.
0N/A * <p>
0N/A * The paths returned from {@code getSelectionPaths} are in the same
0N/A * order as those supplied to this method.
0N/A *
0N/A * @param pPaths the new selection
0N/A */
0N/A public void setSelectionPaths(TreePath[] pPaths) {
0N/A int newCount, newCounter, oldCount, oldCounter;
0N/A TreePath[] paths = pPaths;
0N/A
0N/A if(paths == null)
0N/A newCount = 0;
0N/A else
0N/A newCount = paths.length;
0N/A if(selection == null)
0N/A oldCount = 0;
0N/A else
0N/A oldCount = selection.length;
0N/A if((newCount + oldCount) != 0) {
0N/A if(selectionMode == TreeSelectionModel.SINGLE_TREE_SELECTION) {
0N/A /* If single selection and more than one path, only allow
0N/A first. */
0N/A if(newCount > 1) {
0N/A paths = new TreePath[1];
0N/A paths[0] = pPaths[0];
0N/A newCount = 1;
0N/A }
0N/A }
0N/A else if(selectionMode ==
0N/A TreeSelectionModel.CONTIGUOUS_TREE_SELECTION) {
0N/A /* If contiguous selection and paths aren't contiguous,
0N/A only select the first path item. */
0N/A if(newCount > 0 && !arePathsContiguous(paths)) {
0N/A paths = new TreePath[1];
0N/A paths[0] = pPaths[0];
0N/A newCount = 1;
0N/A }
0N/A }
0N/A
0N/A TreePath beginLeadPath = leadPath;
625N/A Vector<PathPlaceHolder> cPaths = new Vector<PathPlaceHolder>(newCount + oldCount);
0N/A List<TreePath> newSelectionAsList =
0N/A new ArrayList<TreePath>(newCount);
0N/A
0N/A lastPaths.clear();
0N/A leadPath = null;
0N/A /* Find the paths that are new. */
0N/A for(newCounter = 0; newCounter < newCount; newCounter++) {
0N/A TreePath path = paths[newCounter];
0N/A if (path != null && lastPaths.get(path) == null) {
0N/A lastPaths.put(path, Boolean.TRUE);
0N/A if (uniquePaths.get(path) == null) {
0N/A cPaths.addElement(new PathPlaceHolder(path, true));
0N/A }
0N/A leadPath = path;
0N/A newSelectionAsList.add(path);
0N/A }
0N/A }
0N/A
0N/A TreePath[] newSelection = newSelectionAsList.toArray(
0N/A new TreePath[newSelectionAsList.size()]);
0N/A
0N/A /* Get the paths that were selected but no longer selected. */
0N/A for(oldCounter = 0; oldCounter < oldCount; oldCounter++)
0N/A if(selection[oldCounter] != null &&
0N/A lastPaths.get(selection[oldCounter]) == null)
0N/A cPaths.addElement(new PathPlaceHolder
0N/A (selection[oldCounter], false));
0N/A
0N/A selection = newSelection;
0N/A
625N/A Hashtable<TreePath, Boolean> tempHT = uniquePaths;
0N/A
0N/A uniquePaths = lastPaths;
0N/A lastPaths = tempHT;
0N/A lastPaths.clear();
0N/A
0N/A // No reason to do this now, but will still call it.
0N/A insureUniqueness();
0N/A
0N/A updateLeadIndex();
0N/A
0N/A resetRowSelection();
0N/A /* Notify of the change. */
0N/A if(cPaths.size() > 0)
0N/A notifyPathChange(cPaths, beginLeadPath);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Adds path to the current selection. If path is not currently
0N/A * in the selection the TreeSelectionListeners are notified. This has
0N/A * no effect if <code>path</code> is null.
0N/A *
0N/A * @param path the new path to add to the current selection
0N/A */
0N/A public void addSelectionPath(TreePath path) {
0N/A if(path != null) {
0N/A TreePath[] toAdd = new TreePath[1];
0N/A
0N/A toAdd[0] = path;
0N/A addSelectionPaths(toAdd);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Adds paths to the current selection. If any of the paths in
0N/A * paths are not currently in the selection the TreeSelectionListeners
0N/A * are notified. This has
0N/A * no effect if <code>paths</code> is null.
0N/A * <p>The lead path is set to the last element in <code>paths</code>.
0N/A * <p>If the selection mode is <code>CONTIGUOUS_TREE_SELECTION</code>,
0N/A * and adding the new paths would make the selection discontiguous.
0N/A * Then two things can result: if the TreePaths in <code>paths</code>
0N/A * are contiguous, then the selection becomes these TreePaths,
0N/A * otherwise the TreePaths aren't contiguous and the selection becomes
0N/A * the first TreePath in <code>paths</code>.
0N/A *
0N/A * @param paths the new path to add to the current selection
0N/A */
0N/A public void addSelectionPaths(TreePath[] paths) {
0N/A int newPathLength = ((paths == null) ? 0 : paths.length);
0N/A
0N/A if(newPathLength > 0) {
0N/A if(selectionMode == TreeSelectionModel.SINGLE_TREE_SELECTION) {
0N/A setSelectionPaths(paths);
0N/A }
0N/A else if(selectionMode == TreeSelectionModel.
0N/A CONTIGUOUS_TREE_SELECTION && !canPathsBeAdded(paths)) {
0N/A if(arePathsContiguous(paths)) {
0N/A setSelectionPaths(paths);
0N/A }
0N/A else {
0N/A TreePath[] newPaths = new TreePath[1];
0N/A
0N/A newPaths[0] = paths[0];
0N/A setSelectionPaths(newPaths);
0N/A }
0N/A }
0N/A else {
0N/A int counter, validCount;
0N/A int oldCount;
0N/A TreePath beginLeadPath = leadPath;
625N/A Vector<PathPlaceHolder> cPaths = null;
0N/A
0N/A if(selection == null)
0N/A oldCount = 0;
0N/A else
0N/A oldCount = selection.length;
0N/A /* Determine the paths that aren't currently in the
0N/A selection. */
0N/A lastPaths.clear();
0N/A for(counter = 0, validCount = 0; counter < newPathLength;
0N/A counter++) {
0N/A if(paths[counter] != null) {
0N/A if (uniquePaths.get(paths[counter]) == null) {
0N/A validCount++;
0N/A if(cPaths == null)
625N/A cPaths = new Vector<PathPlaceHolder>();
0N/A cPaths.addElement(new PathPlaceHolder
0N/A (paths[counter], true));
0N/A uniquePaths.put(paths[counter], Boolean.TRUE);
0N/A lastPaths.put(paths[counter], Boolean.TRUE);
0N/A }
0N/A leadPath = paths[counter];
0N/A }
0N/A }
0N/A
0N/A if(leadPath == null) {
0N/A leadPath = beginLeadPath;
0N/A }
0N/A
0N/A if(validCount > 0) {
0N/A TreePath newSelection[] = new TreePath[oldCount +
0N/A validCount];
0N/A
0N/A /* And build the new selection. */
0N/A if(oldCount > 0)
0N/A System.arraycopy(selection, 0, newSelection, 0,
0N/A oldCount);
0N/A if(validCount != paths.length) {
0N/A /* Some of the paths in paths are already in
0N/A the selection. */
625N/A Enumeration<TreePath> newPaths = lastPaths.keys();
0N/A
0N/A counter = oldCount;
0N/A while (newPaths.hasMoreElements()) {
625N/A newSelection[counter++] = newPaths.nextElement();
0N/A }
0N/A }
0N/A else {
0N/A System.arraycopy(paths, 0, newSelection, oldCount,
0N/A validCount);
0N/A }
0N/A
0N/A selection = newSelection;
0N/A
0N/A insureUniqueness();
0N/A
0N/A updateLeadIndex();
0N/A
0N/A resetRowSelection();
0N/A
0N/A notifyPathChange(cPaths, beginLeadPath);
0N/A }
0N/A else
0N/A leadPath = beginLeadPath;
0N/A lastPaths.clear();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Removes path from the selection. If path is in the selection
0N/A * The TreeSelectionListeners are notified. This has no effect if
0N/A * <code>path</code> is null.
0N/A *
0N/A * @param path the path to remove from the selection
0N/A */
0N/A public void removeSelectionPath(TreePath path) {
0N/A if(path != null) {
0N/A TreePath[] rPath = new TreePath[1];
0N/A
0N/A rPath[0] = path;
0N/A removeSelectionPaths(rPath);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Removes paths from the selection. If any of the paths in paths
0N/A * are in the selection the TreeSelectionListeners are notified.
0N/A * This has no effect if <code>paths</code> is null.
0N/A *
0N/A * @param paths the paths to remove from the selection
0N/A */
0N/A public void removeSelectionPaths(TreePath[] paths) {
0N/A if (paths != null && selection != null && paths.length > 0) {
0N/A if(!canPathsBeRemoved(paths)) {
0N/A /* Could probably do something more interesting here! */
0N/A clearSelection();
0N/A }
0N/A else {
625N/A Vector<PathPlaceHolder> pathsToRemove = null;
0N/A
0N/A /* Find the paths that can be removed. */
0N/A for (int removeCounter = paths.length - 1; removeCounter >= 0;
0N/A removeCounter--) {
0N/A if(paths[removeCounter] != null) {
0N/A if (uniquePaths.get(paths[removeCounter]) != null) {
0N/A if(pathsToRemove == null)
625N/A pathsToRemove = new Vector<PathPlaceHolder>(paths.length);
0N/A uniquePaths.remove(paths[removeCounter]);
0N/A pathsToRemove.addElement(new PathPlaceHolder
0N/A (paths[removeCounter], false));
0N/A }
0N/A }
0N/A }
0N/A if(pathsToRemove != null) {
0N/A int removeCount = pathsToRemove.size();
0N/A TreePath beginLeadPath = leadPath;
0N/A
0N/A if(removeCount == selection.length) {
0N/A selection = null;
0N/A }
0N/A else {
625N/A Enumeration<TreePath> pEnum = uniquePaths.keys();
0N/A int validCount = 0;
0N/A
0N/A selection = new TreePath[selection.length -
0N/A removeCount];
0N/A while (pEnum.hasMoreElements()) {
625N/A selection[validCount++] = pEnum.nextElement();
0N/A }
0N/A }
0N/A if (leadPath != null &&
0N/A uniquePaths.get(leadPath) == null) {
0N/A if (selection != null) {
0N/A leadPath = selection[selection.length - 1];
0N/A }
0N/A else {
0N/A leadPath = null;
0N/A }
0N/A }
0N/A else if (selection != null) {
0N/A leadPath = selection[selection.length - 1];
0N/A }
0N/A else {
0N/A leadPath = null;
0N/A }
0N/A updateLeadIndex();
0N/A
0N/A resetRowSelection();
0N/A
0N/A notifyPathChange(pathsToRemove, beginLeadPath);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the first path in the selection. This is useful if there
0N/A * if only one item currently selected.
0N/A */
0N/A public TreePath getSelectionPath() {
0N/A if (selection != null && selection.length > 0) {
0N/A return selection[0];
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the selection.
0N/A *
0N/A * @return the selection
0N/A */
0N/A public TreePath[] getSelectionPaths() {
0N/A if(selection != null) {
0N/A int pathSize = selection.length;
0N/A TreePath[] result = new TreePath[pathSize];
0N/A
0N/A System.arraycopy(selection, 0, result, 0, pathSize);
0N/A return result;
0N/A }
0N/A return new TreePath[0];
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of paths that are selected.
0N/A */
0N/A public int getSelectionCount() {
0N/A return (selection == null) ? 0 : selection.length;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the path, <code>path</code>,
0N/A * is in the current selection.
0N/A */
0N/A public boolean isPathSelected(TreePath path) {
0N/A return (path != null) ? (uniquePaths.get(path) != null) : false;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the selection is currently empty.
0N/A */
0N/A public boolean isSelectionEmpty() {
0N/A return (selection == null || selection.length == 0);
0N/A }
0N/A
0N/A /**
0N/A * Empties the current selection. If this represents a change in the
0N/A * current selection, the selection listeners are notified.
0N/A */
0N/A public void clearSelection() {
0N/A if (selection != null && selection.length > 0) {
0N/A int selSize = selection.length;
0N/A boolean[] newness = new boolean[selSize];
0N/A
0N/A for(int counter = 0; counter < selSize; counter++)
0N/A newness[counter] = false;
0N/A
0N/A TreeSelectionEvent event = new TreeSelectionEvent
0N/A (this, selection, newness, leadPath, null);
0N/A
0N/A leadPath = null;
0N/A leadIndex = leadRow = -1;
0N/A uniquePaths.clear();
0N/A selection = null;
0N/A resetRowSelection();
0N/A fireValueChanged(event);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Adds x to the list of listeners that are notified each time the
0N/A * set of selected TreePaths changes.
0N/A *
0N/A * @param x the new listener to be added
0N/A */
0N/A public void addTreeSelectionListener(TreeSelectionListener x) {
0N/A listenerList.add(TreeSelectionListener.class, x);
0N/A }
0N/A
0N/A /**
0N/A * Removes x from the list of listeners that are notified each time
0N/A * the set of selected TreePaths changes.
0N/A *
0N/A * @param x the listener to remove
0N/A */
0N/A public void removeTreeSelectionListener(TreeSelectionListener x) {
0N/A listenerList.remove(TreeSelectionListener.class, x);
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of all the tree selection listeners
0N/A * registered on this model.
0N/A *
0N/A * @return all of this model's <code>TreeSelectionListener</code>s
0N/A * or an empty
0N/A * array if no tree selection listeners are currently registered
0N/A *
0N/A * @see #addTreeSelectionListener
0N/A * @see #removeTreeSelectionListener
0N/A *
0N/A * @since 1.4
0N/A */
0N/A public TreeSelectionListener[] getTreeSelectionListeners() {
625N/A return listenerList.getListeners(TreeSelectionListener.class);
0N/A }
0N/A
0N/A /**
0N/A * Notifies all listeners that are registered for
0N/A * tree selection events on this object.
0N/A * @see #addTreeSelectionListener
0N/A * @see EventListenerList
0N/A */
0N/A protected void fireValueChanged(TreeSelectionEvent e) {
0N/A // Guaranteed to return a non-null array
0N/A Object[] listeners = listenerList.getListenerList();
0N/A // TreeSelectionEvent e = null;
0N/A // Process the listeners last to first, notifying
0N/A // those that are interested in this event
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==TreeSelectionListener.class) {
0N/A // Lazily create the event:
0N/A // if (e == null)
0N/A // e = new ListSelectionEvent(this, firstIndex, lastIndex);
0N/A ((TreeSelectionListener)listeners[i+1]).valueChanged(e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of all the objects currently registered
0N/A * as <code><em>Foo</em>Listener</code>s
0N/A * upon this model.
0N/A * <code><em>Foo</em>Listener</code>s are registered using the
0N/A * <code>add<em>Foo</em>Listener</code> method.
0N/A *
0N/A * <p>
0N/A *
0N/A * You can specify the <code>listenerType</code> argument
0N/A * with a class literal,
0N/A * such as
0N/A * <code><em>Foo</em>Listener.class</code>.
0N/A * For example, you can query a
0N/A * <code>DefaultTreeSelectionModel</code> <code>m</code>
0N/A * for its tree selection listeners with the following code:
0N/A *
0N/A * <pre>TreeSelectionListener[] tsls = (TreeSelectionListener[])(m.getListeners(TreeSelectionListener.class));</pre>
0N/A *
0N/A * If no such listeners exist, this method returns an empty array.
0N/A *
0N/A * @param listenerType the type of listeners requested; this parameter
0N/A * should specify an interface that descends from
0N/A * <code>java.util.EventListener</code>
0N/A * @return an array of all objects registered as
0N/A * <code><em>Foo</em>Listener</code>s on this component,
0N/A * or an empty array if no such
0N/A * listeners have been added
0N/A * @exception ClassCastException if <code>listenerType</code>
0N/A * doesn't specify a class or interface that implements
0N/A * <code>java.util.EventListener</code>
0N/A *
0N/A * @see #getTreeSelectionListeners
0N/A * @see #getPropertyChangeListeners
0N/A *
0N/A * @since 1.3
0N/A */
0N/A public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
0N/A return listenerList.getListeners(listenerType);
0N/A }
0N/A
0N/A /**
0N/A * Returns the selection in terms of rows. There is not
0N/A * necessarily a one-to-one mapping between the {@code TreePath}s
0N/A * returned from {@code getSelectionPaths} and this method. In
0N/A * particular, if a {@code TreePath} is not viewable (the {@code
0N/A * RowMapper} returns {@code -1} for the row corresponding to the
0N/A * {@code TreePath}), then the corresponding row is not included
0N/A * in the returned array. For example, if the selection consists
0N/A * of two paths, {@code A} and {@code B}, with {@code A} at row
0N/A * {@code 10}, and {@code B} not currently viewable, then this method
0N/A * returns an array with the single entry {@code 10}.
0N/A *
0N/A * @return the selection in terms of rows
0N/A */
0N/A public int[] getSelectionRows() {
0N/A // This is currently rather expensive. Needs
0N/A // to be better support from ListSelectionModel to speed this up.
0N/A if (rowMapper != null && selection != null && selection.length > 0) {
0N/A int[] rows = rowMapper.getRowsForPaths(selection);
0N/A
0N/A if (rows != null) {
0N/A int invisCount = 0;
0N/A
0N/A for (int counter = rows.length - 1; counter >= 0; counter--) {
0N/A if (rows[counter] == -1) {
0N/A invisCount++;
0N/A }
0N/A }
0N/A if (invisCount > 0) {
0N/A if (invisCount == rows.length) {
0N/A rows = null;
0N/A }
0N/A else {
0N/A int[] tempRows = new int[rows.length - invisCount];
0N/A
0N/A for (int counter = rows.length - 1, visCounter = 0;
0N/A counter >= 0; counter--) {
0N/A if (rows[counter] != -1) {
0N/A tempRows[visCounter++] = rows[counter];
0N/A }
0N/A }
0N/A rows = tempRows;
0N/A }
0N/A }
0N/A }
0N/A return rows;
0N/A }
0N/A return new int[0];
0N/A }
0N/A
0N/A /**
0N/A * Returns the smallest value obtained from the RowMapper for the
0N/A * current set of selected TreePaths. If nothing is selected,
0N/A * or there is no RowMapper, this will return -1.
0N/A */
0N/A public int getMinSelectionRow() {
0N/A return listSelectionModel.getMinSelectionIndex();
0N/A }
0N/A
0N/A /**
0N/A * Returns the largest value obtained from the RowMapper for the
0N/A * current set of selected TreePaths. If nothing is selected,
0N/A * or there is no RowMapper, this will return -1.
0N/A */
0N/A public int getMaxSelectionRow() {
0N/A return listSelectionModel.getMaxSelectionIndex();
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the row identified by <code>row</code> is selected.
0N/A */
0N/A public boolean isRowSelected(int row) {
0N/A return listSelectionModel.isSelectedIndex(row);
0N/A }
0N/A
0N/A /**
0N/A * Updates this object's mapping from TreePath to rows. This should
0N/A * be invoked when the mapping from TreePaths to integers has changed
0N/A * (for example, a node has been expanded).
0N/A * <p>You do not normally have to call this, JTree and its associated
0N/A * Listeners will invoke this for you. If you are implementing your own
0N/A * View class, then you will have to invoke this.
0N/A * <p>This will invoke <code>insureRowContinuity</code> to make sure
0N/A * the currently selected TreePaths are still valid based on the
0N/A * selection mode.
0N/A */
0N/A public void resetRowSelection() {
0N/A listSelectionModel.clearSelection();
0N/A if(selection != null && rowMapper != null) {
0N/A int aRow;
0N/A int validCount = 0;
0N/A int[] rows = rowMapper.getRowsForPaths(selection);
0N/A
0N/A for(int counter = 0, maxCounter = selection.length;
0N/A counter < maxCounter; counter++) {
0N/A aRow = rows[counter];
0N/A if(aRow != -1) {
0N/A listSelectionModel.addSelectionInterval(aRow, aRow);
0N/A }
0N/A }
0N/A if(leadIndex != -1 && rows != null) {
0N/A leadRow = rows[leadIndex];
0N/A }
0N/A else if (leadPath != null) {
0N/A // Lead selection path doesn't have to be in the selection.
0N/A tempPaths[0] = leadPath;
0N/A rows = rowMapper.getRowsForPaths(tempPaths);
0N/A leadRow = (rows != null) ? rows[0] : -1;
0N/A }
0N/A else {
0N/A leadRow = -1;
0N/A }
0N/A insureRowContinuity();
0N/A
0N/A }
0N/A else
0N/A leadRow = -1;
0N/A }
0N/A
0N/A /**
0N/A * Returns the lead selection index. That is the last index that was
0N/A * added.
0N/A */
0N/A public int getLeadSelectionRow() {
0N/A return leadRow;
0N/A }
0N/A
0N/A /**
0N/A * Returns the last path that was added. This may differ from the
0N/A * leadSelectionPath property maintained by the JTree.
0N/A */
0N/A public TreePath getLeadSelectionPath() {
0N/A return leadPath;
0N/A }
0N/A
0N/A /**
0N/A * Adds a PropertyChangeListener to the listener list.
0N/A * The listener is registered for all properties.
0N/A * <p>
0N/A * A PropertyChangeEvent will get fired when the selection mode
0N/A * changes.
0N/A *
0N/A * @param listener the PropertyChangeListener to be added
0N/A */
0N/A public synchronized void addPropertyChangeListener(
0N/A PropertyChangeListener listener) {
0N/A if (changeSupport == null) {
0N/A changeSupport = new SwingPropertyChangeSupport(this);
0N/A }
0N/A changeSupport.addPropertyChangeListener(listener);
0N/A }
0N/A
0N/A /**
0N/A * Removes a PropertyChangeListener from the listener list.
0N/A * This removes a PropertyChangeListener that was registered
0N/A * for all properties.
0N/A *
0N/A * @param listener the PropertyChangeListener to be removed
0N/A */
0N/A
0N/A public synchronized void removePropertyChangeListener(
0N/A PropertyChangeListener listener) {
0N/A if (changeSupport == null) {
0N/A return;
0N/A }
0N/A changeSupport.removePropertyChangeListener(listener);
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of all the property change listeners
0N/A * registered on this <code>DefaultTreeSelectionModel</code>.
0N/A *
0N/A * @return all of this model's <code>PropertyChangeListener</code>s
0N/A * or an empty
0N/A * array if no property change listeners are currently registered
0N/A *
0N/A * @see #addPropertyChangeListener
0N/A * @see #removePropertyChangeListener
0N/A *
0N/A * @since 1.4
0N/A */
0N/A public PropertyChangeListener[] getPropertyChangeListeners() {
0N/A if (changeSupport == null) {
0N/A return new PropertyChangeListener[0];
0N/A }
0N/A return changeSupport.getPropertyChangeListeners();
0N/A }
0N/A
0N/A /**
0N/A * Makes sure the currently selected <code>TreePath</code>s are valid
0N/A * for the current selection mode.
0N/A * If the selection mode is <code>CONTIGUOUS_TREE_SELECTION</code>
0N/A * and a <code>RowMapper</code> exists, this will make sure all
0N/A * the rows are contiguous, that is, when sorted all the rows are
0N/A * in order with no gaps.
0N/A * If the selection isn't contiguous, the selection is
0N/A * reset to contain the first set, when sorted, of contiguous rows.
0N/A * <p>
0N/A * If the selection mode is <code>SINGLE_TREE_SELECTION</code> and
0N/A * more than one TreePath is selected, the selection is reset to
0N/A * contain the first path currently selected.
0N/A */
0N/A protected void insureRowContinuity() {
0N/A if(selectionMode == TreeSelectionModel.CONTIGUOUS_TREE_SELECTION &&
0N/A selection != null && rowMapper != null) {
0N/A DefaultListSelectionModel lModel = listSelectionModel;
0N/A int min = lModel.getMinSelectionIndex();
0N/A
0N/A if(min != -1) {
0N/A for(int counter = min,
0N/A maxCounter = lModel.getMaxSelectionIndex();
0N/A counter <= maxCounter; counter++) {
0N/A if(!lModel.isSelectedIndex(counter)) {
0N/A if(counter == min) {
0N/A clearSelection();
0N/A }
0N/A else {
0N/A TreePath[] newSel = new TreePath[counter - min];
0N/A int selectionIndex[] = rowMapper.getRowsForPaths(selection);
0N/A // find the actual selection pathes corresponded to the
0N/A // rows of the new selection
0N/A for (int i = 0; i < selectionIndex.length; i++) {
0N/A if (selectionIndex[i]<counter) {
0N/A newSel[selectionIndex[i]-min] = selection[i];
0N/A }
0N/A }
0N/A setSelectionPaths(newSel);
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A else if(selectionMode == TreeSelectionModel.SINGLE_TREE_SELECTION &&
0N/A selection != null && selection.length > 1) {
0N/A setSelectionPath(selection[0]);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the paths are contiguous,
0N/A * or this object has no RowMapper.
0N/A */
0N/A protected boolean arePathsContiguous(TreePath[] paths) {
0N/A if(rowMapper == null || paths.length < 2)
0N/A return true;
0N/A else {
0N/A BitSet bitSet = new BitSet(32);
0N/A int anIndex, counter, min;
0N/A int pathCount = paths.length;
0N/A int validCount = 0;
0N/A TreePath[] tempPath = new TreePath[1];
0N/A
0N/A tempPath[0] = paths[0];
0N/A min = rowMapper.getRowsForPaths(tempPath)[0];
0N/A for(counter = 0; counter < pathCount; counter++) {
0N/A if(paths[counter] != null) {
0N/A tempPath[0] = paths[counter];
0N/A int[] rows = rowMapper.getRowsForPaths(tempPath);
0N/A if (rows == null) {
0N/A return false;
0N/A }
0N/A anIndex = rows[0];
0N/A if(anIndex == -1 || anIndex < (min - pathCount) ||
0N/A anIndex > (min + pathCount))
0N/A return false;
0N/A if(anIndex < min)
0N/A min = anIndex;
0N/A if(!bitSet.get(anIndex)) {
0N/A bitSet.set(anIndex);
0N/A validCount++;
0N/A }
0N/A }
0N/A }
0N/A int maxCounter = validCount + min;
0N/A
0N/A for(counter = min; counter < maxCounter; counter++)
0N/A if(!bitSet.get(counter))
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Used to test if a particular set of <code>TreePath</code>s can
0N/A * be added. This will return true if <code>paths</code> is null (or
0N/A * empty), or this object has no RowMapper, or nothing is currently selected,
0N/A * or the selection mode is <code>DISCONTIGUOUS_TREE_SELECTION</code>, or
0N/A * adding the paths to the current selection still results in a
0N/A * contiguous set of <code>TreePath</code>s.
0N/A */
0N/A protected boolean canPathsBeAdded(TreePath[] paths) {
0N/A if(paths == null || paths.length == 0 || rowMapper == null ||
0N/A selection == null || selectionMode ==
0N/A TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION)
0N/A return true;
0N/A else {
0N/A BitSet bitSet = new BitSet();
0N/A DefaultListSelectionModel lModel = listSelectionModel;
0N/A int anIndex;
0N/A int counter;
0N/A int min = lModel.getMinSelectionIndex();
0N/A int max = lModel.getMaxSelectionIndex();
0N/A TreePath[] tempPath = new TreePath[1];
0N/A
0N/A if(min != -1) {
0N/A for(counter = min; counter <= max; counter++) {
0N/A if(lModel.isSelectedIndex(counter))
0N/A bitSet.set(counter);
0N/A }
0N/A }
0N/A else {
0N/A tempPath[0] = paths[0];
0N/A min = max = rowMapper.getRowsForPaths(tempPath)[0];
0N/A }
0N/A for(counter = paths.length - 1; counter >= 0; counter--) {
0N/A if(paths[counter] != null) {
0N/A tempPath[0] = paths[counter];
0N/A int[] rows = rowMapper.getRowsForPaths(tempPath);
0N/A if (rows == null) {
0N/A return false;
0N/A }
0N/A anIndex = rows[0];
0N/A min = Math.min(anIndex, min);
0N/A max = Math.max(anIndex, max);
0N/A if(anIndex == -1)
0N/A return false;
0N/A bitSet.set(anIndex);
0N/A }
0N/A }
0N/A for(counter = min; counter <= max; counter++)
0N/A if(!bitSet.get(counter))
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the paths can be removed without breaking the
0N/A * continuity of the model.
0N/A * This is rather expensive.
0N/A */
0N/A protected boolean canPathsBeRemoved(TreePath[] paths) {
0N/A if(rowMapper == null || selection == null ||
0N/A selectionMode == TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION)
0N/A return true;
0N/A else {
0N/A BitSet bitSet = new BitSet();
0N/A int counter;
0N/A int pathCount = paths.length;
0N/A int anIndex;
0N/A int min = -1;
0N/A int validCount = 0;
0N/A TreePath[] tempPath = new TreePath[1];
0N/A int[] rows;
0N/A
0N/A /* Determine the rows for the removed entries. */
0N/A lastPaths.clear();
0N/A for (counter = 0; counter < pathCount; counter++) {
0N/A if (paths[counter] != null) {
0N/A lastPaths.put(paths[counter], Boolean.TRUE);
0N/A }
0N/A }
0N/A for(counter = selection.length - 1; counter >= 0; counter--) {
0N/A if(lastPaths.get(selection[counter]) == null) {
0N/A tempPath[0] = selection[counter];
0N/A rows = rowMapper.getRowsForPaths(tempPath);
0N/A if(rows != null && rows[0] != -1 && !bitSet.get(rows[0])) {
0N/A validCount++;
0N/A if(min == -1)
0N/A min = rows[0];
0N/A else
0N/A min = Math.min(min, rows[0]);
0N/A bitSet.set(rows[0]);
0N/A }
0N/A }
0N/A }
0N/A lastPaths.clear();
0N/A /* Make sure they are contiguous. */
0N/A if(validCount > 1) {
0N/A for(counter = min + validCount - 1; counter >= min;
0N/A counter--)
0N/A if(!bitSet.get(counter))
0N/A return false;
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A /**
1635N/A * Notifies listeners of a change in path. changePaths should contain
1635N/A * instances of PathPlaceHolder.
1635N/A *
1635N/A * @deprecated As of JDK version 1.7
1635N/A */
1635N/A @Deprecated
1635N/A protected void notifyPathChange(Vector changedPaths,
0N/A TreePath oldLeadSelection) {
0N/A int cPathCount = changedPaths.size();
0N/A boolean[] newness = new boolean[cPathCount];
0N/A TreePath[] paths = new TreePath[cPathCount];
0N/A PathPlaceHolder placeholder;
0N/A
0N/A for(int counter = 0; counter < cPathCount; counter++) {
1635N/A placeholder = (PathPlaceHolder) changedPaths.elementAt(counter);
0N/A newness[counter] = placeholder.isNew;
0N/A paths[counter] = placeholder.path;
0N/A }
0N/A
0N/A TreeSelectionEvent event = new TreeSelectionEvent
0N/A (this, paths, newness, oldLeadSelection, leadPath);
0N/A
0N/A fireValueChanged(event);
0N/A }
0N/A
0N/A /**
0N/A * Updates the leadIndex instance variable.
0N/A */
0N/A protected void updateLeadIndex() {
0N/A if(leadPath != null) {
0N/A if(selection == null) {
0N/A leadPath = null;
0N/A leadIndex = leadRow = -1;
0N/A }
0N/A else {
0N/A leadRow = leadIndex = -1;
0N/A for(int counter = selection.length - 1; counter >= 0;
0N/A counter--) {
0N/A // Can use == here since we know leadPath came from
0N/A // selection
0N/A if(selection[counter] == leadPath) {
0N/A leadIndex = counter;
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A else {
0N/A leadIndex = -1;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This method is obsolete and its implementation is now a noop. It's
0N/A * still called by setSelectionPaths and addSelectionPaths, but only
0N/A * for backwards compatability.
0N/A */
0N/A protected void insureUniqueness() {
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a string that displays and identifies this
0N/A * object's properties.
0N/A *
0N/A * @return a String representation of this object
0N/A */
0N/A public String toString() {
0N/A int selCount = getSelectionCount();
0N/A StringBuffer retBuffer = new StringBuffer();
0N/A int[] rows;
0N/A
0N/A if(rowMapper != null)
0N/A rows = rowMapper.getRowsForPaths(selection);
0N/A else
0N/A rows = null;
0N/A retBuffer.append(getClass().getName() + " " + hashCode() + " [ ");
0N/A for(int counter = 0; counter < selCount; counter++) {
0N/A if(rows != null)
0N/A retBuffer.append(selection[counter].toString() + "@" +
0N/A Integer.toString(rows[counter])+ " ");
0N/A else
0N/A retBuffer.append(selection[counter].toString() + " ");
0N/A }
0N/A retBuffer.append("]");
0N/A return retBuffer.toString();
0N/A }
0N/A
0N/A /**
0N/A * Returns a clone of this object with the same selection.
0N/A * This method does not duplicate
0N/A * selection listeners and property listeners.
0N/A *
0N/A * @exception CloneNotSupportedException never thrown by instances of
0N/A * this class
0N/A */
0N/A public Object clone() throws CloneNotSupportedException {
0N/A DefaultTreeSelectionModel clone = (DefaultTreeSelectionModel)
0N/A super.clone();
0N/A
0N/A clone.changeSupport = null;
0N/A if(selection != null) {
0N/A int selLength = selection.length;
0N/A
0N/A clone.selection = new TreePath[selLength];
0N/A System.arraycopy(selection, 0, clone.selection, 0, selLength);
0N/A }
0N/A clone.listenerList = new EventListenerList();
0N/A clone.listSelectionModel = (DefaultListSelectionModel)
0N/A listSelectionModel.clone();
625N/A clone.uniquePaths = new Hashtable<TreePath, Boolean>();
625N/A clone.lastPaths = new Hashtable<TreePath, Boolean>();
0N/A clone.tempPaths = new TreePath[1];
0N/A return clone;
0N/A }
0N/A
0N/A // Serialization support.
0N/A private void writeObject(ObjectOutputStream s) throws IOException {
0N/A Object[] tValues;
0N/A
0N/A s.defaultWriteObject();
0N/A // Save the rowMapper, if it implements Serializable
0N/A if(rowMapper != null && rowMapper instanceof Serializable) {
0N/A tValues = new Object[2];
0N/A tValues[0] = "rowMapper";
0N/A tValues[1] = rowMapper;
0N/A }
0N/A else
0N/A tValues = new Object[0];
0N/A s.writeObject(tValues);
0N/A }
0N/A
0N/A
0N/A private void readObject(ObjectInputStream s)
0N/A throws IOException, ClassNotFoundException {
0N/A Object[] tValues;
0N/A
0N/A s.defaultReadObject();
0N/A
0N/A tValues = (Object[])s.readObject();
0N/A
0N/A if(tValues.length > 0 && tValues[0].equals("rowMapper"))
0N/A rowMapper = (RowMapper)tValues[1];
0N/A }
0N/A}
0N/A
0N/A/**
0N/A * Holds a path and whether or not it is new.
0N/A */
0N/Aclass PathPlaceHolder {
0N/A protected boolean isNew;
0N/A protected TreePath path;
0N/A
0N/A PathPlaceHolder(TreePath path, boolean isNew) {
0N/A this.path = path;
0N/A this.isNew = isNew;
0N/A }
0N/A}