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