jdk/src/share/classes/javax/swing/tree/DefaultTreeSelectionModel.java
author peterz
Tue, 01 Sep 2009 15:34:21 +0400
changeset 3747 760e072cc617
parent 1301 15e81207e1f2
child 5506 202f599c92aa
permissions -rw-r--r--
6387579: Usage of package-private class as parameter of a method (javax.swing.tree.DefaultTreeSelectionModel) Reviewed-by: rupashka
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 438
diff changeset
     2
 * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.tree;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.BitSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.EventListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.DefaultListSelectionModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Default implementation of TreeSelectionModel.  Listeners are notified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * whenever
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * the paths in the selection change, not the rows. In order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * to be able to track row changes you may wish to become a listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * for expansion events on the tree and test for changes from there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>resetRowSelection is called from any of the methods that update
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * the selected paths. If you subclass any of these methods to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * filter what is allowed to be selected, be sure and message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <code>resetRowSelection</code> if you do not message super.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see javax.swing.JTree
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
    64
public class DefaultTreeSelectionModel implements Cloneable, Serializable, TreeSelectionModel
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /** Property name for selectionMode. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static final String          SELECTION_MODE_PROPERTY = "selectionMode";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /** Used to messaged registered listeners. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    protected SwingPropertyChangeSupport     changeSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /** Paths that are currently selected.  Will be null if nothing is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
      * currently selected. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    protected TreePath[]                selection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /** Event listener list. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    protected EventListenerList   listenerList = new EventListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /** Provides a row for a given path. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    transient protected RowMapper               rowMapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /** Handles maintaining the list selection model. The RowMapper is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * to map from a TreePath to a row, and the value is then placed here. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    protected DefaultListSelectionModel     listSelectionModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /** Mode for the selection, will be either SINGLE_TREE_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    protected int                           selectionMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /** Last path that was added. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    protected TreePath                      leadPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /** Index of the lead path in selection. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    protected int                           leadIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /** Lead row. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    protected int                           leadRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /** Used to make sure the paths are unique, will contain all the paths
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * in <code>selection</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   101
    private Hashtable<TreePath, Boolean>    uniquePaths;
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   102
    private Hashtable<TreePath, Boolean>    lastPaths;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private TreePath[]                      tempPaths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Creates a new instance of DefaultTreeSelectionModel that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * empty, with a selection mode of DISCONTIGUOUS_TREE_SELECTION.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public DefaultTreeSelectionModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        listSelectionModel = new DefaultListSelectionModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        selectionMode = DISCONTIGUOUS_TREE_SELECTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        leadIndex = leadRow = -1;
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   114
        uniquePaths = new Hashtable<TreePath, Boolean>();
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   115
        lastPaths = new Hashtable<TreePath, Boolean>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        tempPaths = new TreePath[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Sets the RowMapper instance. This instance is used to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * the row for a particular TreePath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public void setRowMapper(RowMapper newMapper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        rowMapper = newMapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        resetRowSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Returns the RowMapper instance that is able to map a TreePath to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public RowMapper getRowMapper() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return rowMapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Sets the selection model, which must be one of SINGLE_TREE_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION. If mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * is not one of the defined value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * <code>DISCONTIGUOUS_TREE_SELECTION</code> is assumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * <p>This may change the selection if the current selection is not valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * for the new mode. For example, if three TreePaths are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * selected when the mode is changed to <code>SINGLE_TREE_SELECTION</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * only one TreePath will remain selected. It is up to the particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * implementation to decide what TreePath remains selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Setting the mode to something other than the defined types will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * result in the mode becoming <code>DISCONTIGUOUS_TREE_SELECTION</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public void setSelectionMode(int mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int            oldMode = selectionMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        selectionMode = mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if(selectionMode != TreeSelectionModel.SINGLE_TREE_SELECTION &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
           selectionMode != TreeSelectionModel.CONTIGUOUS_TREE_SELECTION &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
           selectionMode != TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            selectionMode = TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if(oldMode != selectionMode && changeSupport != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            changeSupport.firePropertyChange(SELECTION_MODE_PROPERTY,
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   160
                                             Integer.valueOf(oldMode),
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   161
                                             Integer.valueOf(selectionMode));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Returns the selection mode, one of <code>SINGLE_TREE_SELECTION</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <code>DISCONTIGUOUS_TREE_SELECTION</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <code>CONTIGUOUS_TREE_SELECTION</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public int getSelectionMode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return selectionMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
      * Sets the selection to path. If this represents a change, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
      * the TreeSelectionListeners are notified. If <code>path</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
      * null, this has the same effect as invoking <code>clearSelection</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
      * @param path new path to select
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public void setSelectionPath(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if(path == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            setSelectionPaths(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            TreePath[]          newPaths = new TreePath[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            newPaths[0] = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            setSelectionPaths(newPaths);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Sets the selection. Whether the supplied paths are taken as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * new selection depends upon the selection mode. If the supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * array is {@code null}, or empty, the selection is cleared. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * the selection mode is {@code SINGLE_TREE_SELECTION}, only the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * first path in {@code pPaths} is used. If the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * mode is {@code CONTIGUOUS_TREE_SELECTION} and the supplied paths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * are not contiguous, then only the first path in {@code pPaths} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * used. If the selection mode is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * {@code DISCONTIGUOUS_TREE_SELECTION}, then all paths are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * All {@code null} paths in {@code pPaths} are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * If this represents a change, all registered {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * TreeSelectionListener}s are notified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * The lead path is set to the last unique path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * The paths returned from {@code getSelectionPaths} are in the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * order as those supplied to this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param pPaths the new selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public void setSelectionPaths(TreePath[] pPaths) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        int            newCount, newCounter, oldCount, oldCounter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        TreePath[]     paths = pPaths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if(paths == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            newCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            newCount = paths.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if(selection == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            oldCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            oldCount = selection.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if((newCount + oldCount) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if(selectionMode == TreeSelectionModel.SINGLE_TREE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                /* If single selection and more than one path, only allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                   first. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                if(newCount > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    paths = new TreePath[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    paths[0] = pPaths[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    newCount = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            else if(selectionMode ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    TreeSelectionModel.CONTIGUOUS_TREE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                /* If contiguous selection and paths aren't contiguous,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                   only select the first path item. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                if(newCount > 0 && !arePathsContiguous(paths)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    paths = new TreePath[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    paths[0] = pPaths[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    newCount = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            TreePath         beginLeadPath = leadPath;
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   248
            Vector<PathPlaceHolder> cPaths = new Vector<PathPlaceHolder>(newCount + oldCount);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            List<TreePath> newSelectionAsList =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    new ArrayList<TreePath>(newCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            lastPaths.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            leadPath = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            /* Find the paths that are new. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            for(newCounter = 0; newCounter < newCount; newCounter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                TreePath path = paths[newCounter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                if (path != null && lastPaths.get(path) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    lastPaths.put(path, Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    if (uniquePaths.get(path) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                        cPaths.addElement(new PathPlaceHolder(path, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    leadPath = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    newSelectionAsList.add(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            TreePath[] newSelection = newSelectionAsList.toArray(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    new TreePath[newSelectionAsList.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            /* Get the paths that were selected but no longer selected. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            for(oldCounter = 0; oldCounter < oldCount; oldCounter++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                if(selection[oldCounter] != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    lastPaths.get(selection[oldCounter]) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    cPaths.addElement(new PathPlaceHolder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                                      (selection[oldCounter], false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            selection = newSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   279
            Hashtable<TreePath, Boolean>  tempHT = uniquePaths;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            uniquePaths = lastPaths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            lastPaths = tempHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            lastPaths.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            // No reason to do this now, but will still call it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            insureUniqueness();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            updateLeadIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            resetRowSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            /* Notify of the change. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if(cPaths.size() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                notifyPathChange(cPaths, beginLeadPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
      * Adds path to the current selection. If path is not currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
      * in the selection the TreeSelectionListeners are notified. This has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
      * no effect if <code>path</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
      * @param path the new path to add to the current selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public void addSelectionPath(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if(path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            TreePath[]            toAdd = new TreePath[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            toAdd[0] = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            addSelectionPaths(toAdd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
      * Adds paths to the current selection. If any of the paths in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
      * paths are not currently in the selection the TreeSelectionListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
      * are notified. This has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
      * no effect if <code>paths</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
      * <p>The lead path is set to the last element in <code>paths</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
      * <p>If the selection mode is <code>CONTIGUOUS_TREE_SELECTION</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
      * and adding the new paths would make the selection discontiguous.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
      * Then two things can result: if the TreePaths in <code>paths</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
      * are contiguous, then the selection becomes these TreePaths,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
      * otherwise the TreePaths aren't contiguous and the selection becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
      * the first TreePath in <code>paths</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
      * @param paths the new path to add to the current selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public void addSelectionPaths(TreePath[] paths) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        int       newPathLength = ((paths == null) ? 0 : paths.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if(newPathLength > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            if(selectionMode == TreeSelectionModel.SINGLE_TREE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                setSelectionPaths(paths);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            else if(selectionMode == TreeSelectionModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    CONTIGUOUS_TREE_SELECTION && !canPathsBeAdded(paths)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                if(arePathsContiguous(paths)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    setSelectionPaths(paths);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    TreePath[]          newPaths = new TreePath[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    newPaths[0] = paths[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    setSelectionPaths(newPaths);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                int               counter, validCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                int               oldCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                TreePath          beginLeadPath = leadPath;
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   351
                Vector<PathPlaceHolder>  cPaths = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                if(selection == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    oldCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    oldCount = selection.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                /* Determine the paths that aren't currently in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                   selection. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                lastPaths.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                for(counter = 0, validCount = 0; counter < newPathLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    if(paths[counter] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        if (uniquePaths.get(paths[counter]) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                            validCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                            if(cPaths == null)
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   366
                                cPaths = new Vector<PathPlaceHolder>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                            cPaths.addElement(new PathPlaceHolder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                                              (paths[counter], true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                            uniquePaths.put(paths[counter], Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                            lastPaths.put(paths[counter], Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                        leadPath = paths[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                if(leadPath == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    leadPath = beginLeadPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                if(validCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    TreePath         newSelection[] = new TreePath[oldCount +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                                                                  validCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    /* And build the new selection. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    if(oldCount > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                        System.arraycopy(selection, 0, newSelection, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                                         oldCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    if(validCount != paths.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                        /* Some of the paths in paths are already in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                           the selection. */
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   391
                        Enumeration<TreePath> newPaths = lastPaths.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                        counter = oldCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                        while (newPaths.hasMoreElements()) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   395
                            newSelection[counter++] = newPaths.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                        System.arraycopy(paths, 0, newSelection, oldCount,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                                         validCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    selection = newSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    insureUniqueness();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    updateLeadIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    resetRowSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    notifyPathChange(cPaths, beginLeadPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    leadPath = beginLeadPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                lastPaths.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
      * Removes path from the selection. If path is in the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
      * The TreeSelectionListeners are notified. This has no effect if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
      * <code>path</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
      * @param path the path to remove from the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    public void removeSelectionPath(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if(path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            TreePath[]             rPath = new TreePath[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            rPath[0] = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            removeSelectionPaths(rPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
      * Removes paths from the selection.  If any of the paths in paths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
      * are in the selection the TreeSelectionListeners are notified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
      * This has no effect if <code>paths</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
      * @param paths the paths to remove from the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public void removeSelectionPaths(TreePath[] paths) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (paths != null && selection != null && paths.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            if(!canPathsBeRemoved(paths)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                /* Could probably do something more interesting here! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            else {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   450
                Vector<PathPlaceHolder> pathsToRemove = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                /* Find the paths that can be removed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                for (int removeCounter = paths.length - 1; removeCounter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                     removeCounter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    if(paths[removeCounter] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        if (uniquePaths.get(paths[removeCounter]) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                            if(pathsToRemove == null)
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   458
                                pathsToRemove = new Vector<PathPlaceHolder>(paths.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                            uniquePaths.remove(paths[removeCounter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                            pathsToRemove.addElement(new PathPlaceHolder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                                         (paths[removeCounter], false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                if(pathsToRemove != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    int         removeCount = pathsToRemove.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    TreePath    beginLeadPath = leadPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    if(removeCount == selection.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                        selection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    else {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   473
                        Enumeration<TreePath> pEnum = uniquePaths.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                        int                  validCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                        selection = new TreePath[selection.length -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                                                removeCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                        while (pEnum.hasMoreElements()) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   479
                            selection[validCount++] = pEnum.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    if (leadPath != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                        uniquePaths.get(leadPath) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                        if (selection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                            leadPath = selection[selection.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                            leadPath = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    else if (selection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                        leadPath = selection[selection.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                        leadPath = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    updateLeadIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    resetRowSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    notifyPathChange(pathsToRemove, beginLeadPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
      * Returns the first path in the selection. This is useful if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
      * if only one item currently selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    public TreePath getSelectionPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        if (selection != null && selection.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            return selection[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
      * Returns the selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
      * @return the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    public TreePath[] getSelectionPaths() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        if(selection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            int                 pathSize = selection.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            TreePath[]          result = new TreePath[pathSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            System.arraycopy(selection, 0, result, 0, pathSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        return new TreePath[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * Returns the number of paths that are selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    public int getSelectionCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        return (selection == null) ? 0 : selection.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
      * Returns true if the path, <code>path</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
      * is in the current selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    public boolean isPathSelected(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        return (path != null) ? (uniquePaths.get(path) != null) : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
      * Returns true if the selection is currently empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    public boolean isSelectionEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        return (selection == null || selection.length == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
      * Empties the current selection.  If this represents a change in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
      * current selection, the selection listeners are notified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    public void clearSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        if (selection != null && selection.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            int                    selSize = selection.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            boolean[]              newness = new boolean[selSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            for(int counter = 0; counter < selSize; counter++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                newness[counter] = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            TreeSelectionEvent     event = new TreeSelectionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                (this, selection, newness, leadPath, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            leadPath = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            leadIndex = leadRow = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            uniquePaths.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            selection = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            resetRowSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            fireValueChanged(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
      * Adds x to the list of listeners that are notified each time the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
      * set of selected TreePaths changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
      * @param x the new listener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    public void addTreeSelectionListener(TreeSelectionListener x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        listenerList.add(TreeSelectionListener.class, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
      * Removes x from the list of listeners that are notified each time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
      * the set of selected TreePaths changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
      * @param x the listener to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    public void removeTreeSelectionListener(TreeSelectionListener x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        listenerList.remove(TreeSelectionListener.class, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * Returns an array of all the tree selection listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * registered on this model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @return all of this model's <code>TreeSelectionListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *         or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *         array if no tree selection listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @see #addTreeSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @see #removeTreeSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    public TreeSelectionListener[] getTreeSelectionListeners() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   614
        return listenerList.getListeners(TreeSelectionListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * Notifies all listeners that are registered for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * tree selection events on this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @see #addTreeSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    protected void fireValueChanged(TreeSelectionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        // TreeSelectionEvent e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            if (listeners[i]==TreeSelectionListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                // if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                // e = new ListSelectionEvent(this, firstIndex, lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                ((TreeSelectionListener)listeners[i+1]).valueChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * Returns an array of all the objects currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * as <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * upon this model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * <code><em>Foo</em>Listener</code>s are registered using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * <code>add<em>Foo</em>Listener</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * You can specify the <code>listenerType</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * with a class literal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * <code><em>Foo</em>Listener.class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * For example, you can query a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * <code>DefaultTreeSelectionModel</code> <code>m</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * for its tree selection listeners with the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * <pre>TreeSelectionListener[] tsls = (TreeSelectionListener[])(m.getListeners(TreeSelectionListener.class));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * If no such listeners exist, this method returns an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @param listenerType the type of listeners requested; this parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     *          should specify an interface that descends from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @return an array of all objects registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *          <code><em>Foo</em>Listener</code>s on this component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *          or an empty array if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     *          listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @exception ClassCastException if <code>listenerType</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *          doesn't specify a class or interface that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @see #getTreeSelectionListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @see #getPropertyChangeListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        return listenerList.getListeners(listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * Returns the selection in terms of rows. There is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * necessarily a one-to-one mapping between the {@code TreePath}s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * returned from {@code getSelectionPaths} and this method. In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * particular, if a {@code TreePath} is not viewable (the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * RowMapper} returns {@code -1} for the row corresponding to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * {@code TreePath}), then the corresponding row is not included
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * in the returned array. For example, if the selection consists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * of two paths, {@code A} and {@code B}, with {@code A} at row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * {@code 10}, and {@code B} not currently viewable, then this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * returns an array with the single entry {@code 10}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @return the selection in terms of rows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    public int[] getSelectionRows() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        // This is currently rather expensive.  Needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        // to be better support from ListSelectionModel to speed this up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        if (rowMapper != null && selection != null && selection.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            int[]      rows = rowMapper.getRowsForPaths(selection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            if (rows != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                int       invisCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                for (int counter = rows.length - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                    if (rows[counter] == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                        invisCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                if (invisCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                    if (invisCount == rows.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                        rows = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                        int[]    tempRows = new int[rows.length - invisCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                        for (int counter = rows.length - 1, visCounter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                             counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                            if (rows[counter] != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                                tempRows[visCounter++] = rows[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                        rows = tempRows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            return rows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        return new int[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * Returns the smallest value obtained from the RowMapper for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * current set of selected TreePaths. If nothing is selected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * or there is no RowMapper, this will return -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    public int getMinSelectionRow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        return listSelectionModel.getMinSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * Returns the largest value obtained from the RowMapper for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * current set of selected TreePaths. If nothing is selected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * or there is no RowMapper, this will return -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    public int getMaxSelectionRow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        return listSelectionModel.getMaxSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
      * Returns true if the row identified by <code>row</code> is selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    public boolean isRowSelected(int row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        return listSelectionModel.isSelectedIndex(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * Updates this object's mapping from TreePath to rows. This should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * be invoked when the mapping from TreePaths to integers has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * (for example, a node has been expanded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * <p>You do not normally have to call this, JTree and its associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * Listeners will invoke this for you. If you are implementing your own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * View class, then you will have to invoke this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * <p>This will invoke <code>insureRowContinuity</code> to make sure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * the currently selected TreePaths are still valid based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * selection mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    public void resetRowSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        listSelectionModel.clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        if(selection != null && rowMapper != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            int               aRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            int               validCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            int[]             rows = rowMapper.getRowsForPaths(selection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            for(int counter = 0, maxCounter = selection.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                aRow = rows[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                if(aRow != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                    listSelectionModel.addSelectionInterval(aRow, aRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            if(leadIndex != -1 && rows != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                leadRow = rows[leadIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            else if (leadPath != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                // Lead selection path doesn't have to be in the selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                tempPaths[0] = leadPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                rows = rowMapper.getRowsForPaths(tempPaths);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                leadRow = (rows != null) ? rows[0] : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                leadRow = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            insureRowContinuity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            leadRow = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * Returns the lead selection index. That is the last index that was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    public int getLeadSelectionRow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        return leadRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * Returns the last path that was added. This may differ from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * leadSelectionPath property maintained by the JTree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    public TreePath getLeadSelectionPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        return leadPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * Adds a PropertyChangeListener to the listener list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * The listener is registered for all properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * A PropertyChangeEvent will get fired when the selection mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * @param listener  the PropertyChangeListener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    public synchronized void addPropertyChangeListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                                PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        if (changeSupport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            changeSupport = new SwingPropertyChangeSupport(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        changeSupport.addPropertyChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * Removes a PropertyChangeListener from the listener list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * This removes a PropertyChangeListener that was registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * for all properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * @param listener  the PropertyChangeListener to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    public synchronized void removePropertyChangeListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                                PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        if (changeSupport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        changeSupport.removePropertyChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * Returns an array of all the property change listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * registered on this <code>DefaultTreeSelectionModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * @return all of this model's <code>PropertyChangeListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *         or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *         array if no property change listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * @see #addPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * @see #removePropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    public PropertyChangeListener[] getPropertyChangeListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        if (changeSupport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            return new PropertyChangeListener[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        return changeSupport.getPropertyChangeListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * Makes sure the currently selected <code>TreePath</code>s are valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * for the current selection mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * If the selection mode is <code>CONTIGUOUS_TREE_SELECTION</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * and a <code>RowMapper</code> exists, this will make sure all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * the rows are contiguous, that is, when sorted all the rows are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * in order with no gaps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * If the selection isn't contiguous, the selection is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * reset to contain the first set, when sorted, of contiguous rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * If the selection mode is <code>SINGLE_TREE_SELECTION</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * more than one TreePath is selected, the selection is reset to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * contain the first path currently selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    protected void insureRowContinuity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        if(selectionMode == TreeSelectionModel.CONTIGUOUS_TREE_SELECTION &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
           selection != null && rowMapper != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            DefaultListSelectionModel lModel = listSelectionModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            int                       min = lModel.getMinSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            if(min != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                for(int counter = min,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                        maxCounter = lModel.getMaxSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                        counter <= maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                    if(!lModel.isSelectedIndex(counter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                        if(counter == min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                            clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                            TreePath[] newSel = new TreePath[counter - min];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                            int selectionIndex[] = rowMapper.getRowsForPaths(selection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                            // find the actual selection pathes corresponded to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                            // rows of the new selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                            for (int i = 0; i < selectionIndex.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                                if (selectionIndex[i]<counter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                                    newSel[selectionIndex[i]-min] = selection[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                            setSelectionPaths(newSel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        else if(selectionMode == TreeSelectionModel.SINGLE_TREE_SELECTION &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                selection != null && selection.length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            setSelectionPath(selection[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * Returns true if the paths are contiguous,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * or this object has no RowMapper.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    protected boolean arePathsContiguous(TreePath[] paths) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        if(rowMapper == null || paths.length < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            BitSet                             bitSet = new BitSet(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            int                                anIndex, counter, min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            int                                pathCount = paths.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            int                                validCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            TreePath[]                         tempPath = new TreePath[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            tempPath[0] = paths[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            min = rowMapper.getRowsForPaths(tempPath)[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            for(counter = 0; counter < pathCount; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                if(paths[counter] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                    tempPath[0] = paths[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                    int[] rows = rowMapper.getRowsForPaths(tempPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                    if (rows == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                    anIndex = rows[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                    if(anIndex == -1 || anIndex < (min - pathCount) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                       anIndex > (min + pathCount))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                    if(anIndex < min)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                        min = anIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                    if(!bitSet.get(anIndex)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                        bitSet.set(anIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                        validCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            int          maxCounter = validCount + min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            for(counter = min; counter < maxCounter; counter++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                if(!bitSet.get(counter))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * Used to test if a particular set of <code>TreePath</code>s can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * be added. This will return true if <code>paths</code> is null (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * empty), or this object has no RowMapper, or nothing is currently selected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * or the selection mode is <code>DISCONTIGUOUS_TREE_SELECTION</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * adding the paths to the current selection still results in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * contiguous set of <code>TreePath</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    protected boolean canPathsBeAdded(TreePath[] paths) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        if(paths == null || paths.length == 0 || rowMapper == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
           selection == null || selectionMode ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
           TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            BitSet                       bitSet = new BitSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            DefaultListSelectionModel    lModel = listSelectionModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            int                          anIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            int                          counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            int                          min = lModel.getMinSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            int                          max = lModel.getMaxSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            TreePath[]                   tempPath = new TreePath[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            if(min != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                for(counter = min; counter <= max; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                    if(lModel.isSelectedIndex(counter))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                        bitSet.set(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                tempPath[0] = paths[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                min = max = rowMapper.getRowsForPaths(tempPath)[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            for(counter = paths.length - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                if(paths[counter] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                    tempPath[0] = paths[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                    int[]   rows = rowMapper.getRowsForPaths(tempPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    if (rows == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                    anIndex = rows[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                    min = Math.min(anIndex, min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                    max = Math.max(anIndex, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                    if(anIndex == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                    bitSet.set(anIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            for(counter = min; counter <= max; counter++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                if(!bitSet.get(counter))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * Returns true if the paths can be removed without breaking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * continuity of the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * This is rather expensive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    protected boolean canPathsBeRemoved(TreePath[] paths) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        if(rowMapper == null || selection == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
           selectionMode == TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            BitSet               bitSet = new BitSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            int                  counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            int                  pathCount = paths.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            int                  anIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            int                  min = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            int                  validCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            TreePath[]           tempPath = new TreePath[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            int[]                rows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            /* Determine the rows for the removed entries. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            lastPaths.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            for (counter = 0; counter < pathCount; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                if (paths[counter] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                    lastPaths.put(paths[counter], Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            for(counter = selection.length - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                if(lastPaths.get(selection[counter]) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                    tempPath[0] = selection[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                    rows = rowMapper.getRowsForPaths(tempPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                    if(rows != null && rows[0] != -1 && !bitSet.get(rows[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                        validCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                        if(min == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                            min = rows[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                            min = Math.min(min, rows[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                        bitSet.set(rows[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            lastPaths.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            /* Make sure they are contiguous. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            if(validCount > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                for(counter = min + validCount - 1; counter >= min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                    counter--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                    if(!bitSet.get(counter))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    /**
3747
760e072cc617 6387579: Usage of package-private class as parameter of a method (javax.swing.tree.DefaultTreeSelectionModel)
peterz
parents: 1301
diff changeset
  1070
     * Notifies listeners of a change in path. changePaths should contain
760e072cc617 6387579: Usage of package-private class as parameter of a method (javax.swing.tree.DefaultTreeSelectionModel)
peterz
parents: 1301
diff changeset
  1071
     * instances of PathPlaceHolder.
760e072cc617 6387579: Usage of package-private class as parameter of a method (javax.swing.tree.DefaultTreeSelectionModel)
peterz
parents: 1301
diff changeset
  1072
     *
760e072cc617 6387579: Usage of package-private class as parameter of a method (javax.swing.tree.DefaultTreeSelectionModel)
peterz
parents: 1301
diff changeset
  1073
     * @deprecated As of JDK version 1.7
760e072cc617 6387579: Usage of package-private class as parameter of a method (javax.swing.tree.DefaultTreeSelectionModel)
peterz
parents: 1301
diff changeset
  1074
     */
760e072cc617 6387579: Usage of package-private class as parameter of a method (javax.swing.tree.DefaultTreeSelectionModel)
peterz
parents: 1301
diff changeset
  1075
    @Deprecated
760e072cc617 6387579: Usage of package-private class as parameter of a method (javax.swing.tree.DefaultTreeSelectionModel)
peterz
parents: 1301
diff changeset
  1076
    protected void notifyPathChange(Vector changedPaths,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                                    TreePath oldLeadSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        int                    cPathCount = changedPaths.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        boolean[]              newness = new boolean[cPathCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        TreePath[]            paths = new TreePath[cPathCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        PathPlaceHolder        placeholder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        for(int counter = 0; counter < cPathCount; counter++) {
3747
760e072cc617 6387579: Usage of package-private class as parameter of a method (javax.swing.tree.DefaultTreeSelectionModel)
peterz
parents: 1301
diff changeset
  1084
            placeholder = (PathPlaceHolder) changedPaths.elementAt(counter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            newness[counter] = placeholder.isNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            paths[counter] = placeholder.path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        TreeSelectionEvent     event = new TreeSelectionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                          (this, paths, newness, oldLeadSelection, leadPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        fireValueChanged(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * Updates the leadIndex instance variable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    protected void updateLeadIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        if(leadPath != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            if(selection == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                leadPath = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                leadIndex = leadRow = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                leadRow = leadIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                for(int counter = selection.length - 1; counter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                    counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                    // Can use == here since we know leadPath came from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                    // selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                    if(selection[counter] == leadPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                        leadIndex = counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            leadIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * This method is obsolete and its implementation is now a noop.  It's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * still called by setSelectionPaths and addSelectionPaths, but only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * for backwards compatability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    protected void insureUniqueness() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * Returns a string that displays and identifies this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * object's properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * @return a String representation of this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        int                selCount = getSelectionCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        StringBuffer       retBuffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        int[]              rows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        if(rowMapper != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            rows = rowMapper.getRowsForPaths(selection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            rows = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        retBuffer.append(getClass().getName() + " " + hashCode() + " [ ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        for(int counter = 0; counter < selCount; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            if(rows != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                retBuffer.append(selection[counter].toString() + "@" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                                 Integer.toString(rows[counter])+ " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                retBuffer.append(selection[counter].toString() + " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        retBuffer.append("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        return retBuffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * Returns a clone of this object with the same selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * This method does not duplicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * selection listeners and property listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * @exception CloneNotSupportedException never thrown by instances of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     *                                       this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    public Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        DefaultTreeSelectionModel        clone = (DefaultTreeSelectionModel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                            super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        clone.changeSupport = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        if(selection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            int              selLength = selection.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            clone.selection = new TreePath[selLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            System.arraycopy(selection, 0, clone.selection, 0, selLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        clone.listenerList = new EventListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        clone.listSelectionModel = (DefaultListSelectionModel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            listSelectionModel.clone();
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1180
        clone.uniquePaths = new Hashtable<TreePath, Boolean>();
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1181
        clone.lastPaths = new Hashtable<TreePath, Boolean>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        clone.tempPaths = new TreePath[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        return clone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    // Serialization support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        Object[]             tValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        // Save the rowMapper, if it implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        if(rowMapper != null && rowMapper instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            tValues = new Object[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            tValues[0] = "rowMapper";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            tValues[1] = rowMapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            tValues = new Object[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        s.writeObject(tValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        Object[]      tValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        tValues = (Object[])s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        if(tValues.length > 0 && tValues[0].equals("rowMapper"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            rowMapper = (RowMapper)tValues[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
 * Holds a path and whether or not it is new.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
class PathPlaceHolder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
    protected boolean             isNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
    protected TreePath           path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    PathPlaceHolder(TreePath path, boolean isNew) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        this.path = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        this.isNew = isNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
}