jdk/src/share/classes/javax/swing/plaf/basic/BasicTreeUI.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 20169 d7fa6d7586c9
child 21982 fd6e5fe509df
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
20097
a4a07c4ead5f 8023474: First mousepress doesn't start editing in JTree
dmarkov
parents: 18133
diff changeset
     2
 * Copyright (c) 1997, 2013, 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: 4394
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: 4394
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: 4394
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
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.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.datatransfer.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.swing.plaf.ComponentUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.swing.plaf.UIResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.swing.plaf.TreeUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.swing.tree.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.swing.text.Position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.swing.plaf.basic.DragRecognitionSupport.BeforeDrag;
20097
a4a07c4ead5f 8023474: First mousepress doesn't start editing in JTree
dmarkov
parents: 18133
diff changeset
    45
import sun.awt.AWTAccessor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.swing.SwingUtilities2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.swing.DefaultLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.swing.UIAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/**
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 20097
diff changeset
    52
 * The basic L&F for a hierarchical data structure.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author Shannon Hickey (drag and drop)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class BasicTreeUI extends TreeUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final StringBuilder BASELINE_COMPONENT_KEY =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        new StringBuilder("Tree.baselineComponent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // Old actions forward to an instance of this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static private final Actions SHARED_ACTION = new Actions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    transient protected Icon        collapsedIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    transient protected Icon        expandedIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
      * Color used to draw hash marks.  If <code>null</code> no hash marks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
      * will be drawn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private Color hashColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /** Distance between left margin and where vertical dashes will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
      * drawn. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    protected int               leftChildIndent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /** Distance to add to leftChildIndent to determine where cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
      * contents will be drawn. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    protected int               rightChildIndent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /** Total distance that will be indented.  The sum of leftChildIndent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
      * and rightChildIndent. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    protected int               totalChildIndent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /** Minimum preferred size. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    protected Dimension         preferredMinSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /** Index of the row that was last selected. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    protected int               lastSelectedRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /** Component that we're going to be drawing into. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    protected JTree             tree;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /** Renderer that is being used to do the actual cell drawing. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    transient protected TreeCellRenderer   currentCellRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /** Set to true if the renderer that is currently in the tree was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * created by this instance. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    protected boolean           createdRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /** Editor for the tree. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    transient protected TreeCellEditor     cellEditor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /** Set to true if editor that is currently in the tree was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * created by this instance. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    protected boolean           createdCellEditor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /** Set to false when editing and shouldSelectCell() returns true meaning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
      * the node should be selected before editing, used in completeEditing. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    protected boolean           stopEditingInCompleteEditing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /** Used to paint the TreeCellRenderer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    protected CellRendererPane  rendererPane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /** Size needed to completely display all the nodes. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    protected Dimension         preferredSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /** Is the preferredSize valid? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    protected boolean           validCachedPreferredSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /** Object responsible for handling sizing and expanded issues. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    // WARNING: Be careful with the bounds held by treeState. They are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    // always in terms of left-to-right. They get mapped to right-to-left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    // by the various methods of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    protected AbstractLayoutCache  treeState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /** Used for minimizing the drawing of vertical lines. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    protected Hashtable<TreePath,Boolean> drawingCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /** True if doing optimizations for a largeModel. Subclasses that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * don't support this may wish to override createLayoutCache to not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * return a FixedHeightLayoutCache instance. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    protected boolean           largeModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /** Reponsible for telling the TreeState the size needed for a node. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    protected AbstractLayoutCache.NodeDimensions     nodeDimensions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /** Used to determine what to display. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    protected TreeModel         treeModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
   143
    /** Model maintaining the selection. */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    protected TreeSelectionModel treeSelectionModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /** How much the depth should be offset to properly calculate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * x locations. This is based on whether or not the root is visible,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * and if the root handles are visible. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    protected int               depthOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    // Following 4 ivars are only valid when editing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /** When editing, this will be the Component that is doing the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
      * editing. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    protected Component         editingComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /** Path that is being edited. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    protected TreePath          editingPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /** Row that is being edited. Should only be referenced if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * editingComponent is not null. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    protected int               editingRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /** Set to true if the editor has a different size than the renderer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    protected boolean           editorHasDifferentSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /** Row correspondin to lead path. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private int                 leadRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /** If true, the property change event for LEAD_SELECTION_PATH_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * or ANCHOR_SELECTION_PATH_PROPERTY will not generate a repaint. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private boolean             ignoreLAChange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /** Indicates the orientation. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private boolean             leftToRight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    // Cached listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    private PropertyChangeListener propertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private PropertyChangeListener selectionModelPropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private MouseListener mouseListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private FocusListener focusListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private KeyListener keyListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /** Used for large models, listens for moved/resized events and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * updates the validCachedPreferredSize bit accordingly. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private ComponentListener   componentListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /** Listens for CellEditor events. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private CellEditorListener  cellEditorListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /** Updates the display when the selection changes. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private TreeSelectionListener treeSelectionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /** Is responsible for updating the display based on model events. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private TreeModelListener treeModelListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /** Updates the treestate as the nodes expand. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    private TreeExpansionListener treeExpansionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /** UI property indicating whether to paint lines */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private boolean paintLines = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /** UI property for painting dashed lines */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private boolean lineTypeDashed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * The time factor to treate the series of typed alphanumeric key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * as prefix for first letter navigation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private long timeFactor = 1000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    private Handler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * A temporary variable for communication between startEditingOnRelease
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * and startEditing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private MouseEvent releaseEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public static ComponentUI createUI(JComponent x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return new BasicTreeUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    static void loadActionMap(LazyActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        map.put(new Actions(Actions.SELECT_PREVIOUS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        map.put(new Actions(Actions.SELECT_PREVIOUS_CHANGE_LEAD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        map.put(new Actions(Actions.SELECT_PREVIOUS_EXTEND_SELECTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        map.put(new Actions(Actions.SELECT_NEXT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        map.put(new Actions(Actions.SELECT_NEXT_CHANGE_LEAD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        map.put(new Actions(Actions.SELECT_NEXT_EXTEND_SELECTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        map.put(new Actions(Actions.SELECT_CHILD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        map.put(new Actions(Actions.SELECT_CHILD_CHANGE_LEAD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        map.put(new Actions(Actions.SELECT_PARENT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        map.put(new Actions(Actions.SELECT_PARENT_CHANGE_LEAD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        map.put(new Actions(Actions.SCROLL_UP_CHANGE_SELECTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        map.put(new Actions(Actions.SCROLL_UP_CHANGE_LEAD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        map.put(new Actions(Actions.SCROLL_UP_EXTEND_SELECTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        map.put(new Actions(Actions.SCROLL_DOWN_CHANGE_SELECTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        map.put(new Actions(Actions.SCROLL_DOWN_EXTEND_SELECTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        map.put(new Actions(Actions.SCROLL_DOWN_CHANGE_LEAD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        map.put(new Actions(Actions.SELECT_FIRST));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        map.put(new Actions(Actions.SELECT_FIRST_CHANGE_LEAD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        map.put(new Actions(Actions.SELECT_FIRST_EXTEND_SELECTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        map.put(new Actions(Actions.SELECT_LAST));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        map.put(new Actions(Actions.SELECT_LAST_CHANGE_LEAD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        map.put(new Actions(Actions.SELECT_LAST_EXTEND_SELECTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        map.put(new Actions(Actions.TOGGLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        map.put(new Actions(Actions.CANCEL_EDITING));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        map.put(new Actions(Actions.START_EDITING));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        map.put(new Actions(Actions.SELECT_ALL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        map.put(new Actions(Actions.CLEAR_SELECTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        map.put(new Actions(Actions.SCROLL_LEFT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        map.put(new Actions(Actions.SCROLL_RIGHT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        map.put(new Actions(Actions.SCROLL_LEFT_EXTEND_SELECTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        map.put(new Actions(Actions.SCROLL_RIGHT_EXTEND_SELECTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        map.put(new Actions(Actions.SCROLL_RIGHT_CHANGE_LEAD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        map.put(new Actions(Actions.SCROLL_LEFT_CHANGE_LEAD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        map.put(new Actions(Actions.EXPAND));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        map.put(new Actions(Actions.COLLAPSE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        map.put(new Actions(Actions.MOVE_SELECTION_TO_PARENT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        map.put(new Actions(Actions.ADD_TO_SELECTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        map.put(new Actions(Actions.TOGGLE_AND_ANCHOR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        map.put(new Actions(Actions.EXTEND_TO));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        map.put(new Actions(Actions.MOVE_SELECTION_TO));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        map.put(TransferHandler.getCutAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        map.put(TransferHandler.getCopyAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        map.put(TransferHandler.getPasteAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public BasicTreeUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    protected Color getHashColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return hashColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    protected void setHashColor(Color color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        hashColor = color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public void setLeftChildIndent(int newAmount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        leftChildIndent = newAmount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        totalChildIndent = leftChildIndent + rightChildIndent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if(treeState != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            treeState.invalidateSizes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public int getLeftChildIndent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return leftChildIndent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public void setRightChildIndent(int newAmount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        rightChildIndent = newAmount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        totalChildIndent = leftChildIndent + rightChildIndent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if(treeState != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            treeState.invalidateSizes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public int getRightChildIndent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return rightChildIndent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public void setExpandedIcon(Icon newG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        expandedIcon = newG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public Icon getExpandedIcon() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return expandedIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public void setCollapsedIcon(Icon newG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        collapsedIcon = newG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    public Icon getCollapsedIcon() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return collapsedIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    // Methods for configuring the behavior of the tree. None of them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    // push the value to the JTree instance. You should really only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    // call these methods on the JTree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Updates the componentListener, if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    protected void setLargeModel(boolean largeModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if(getRowHeight() < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            largeModel = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if(this.largeModel != largeModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            completeEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            this.largeModel = largeModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            treeState = createLayoutCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            configureLayoutCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            updateLayoutCacheExpandedNodesIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    protected boolean isLargeModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        return largeModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Sets the row height, this is forwarded to the treeState.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    protected void setRowHeight(int rowHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        completeEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if(treeState != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            setLargeModel(tree.isLargeModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            treeState.setRowHeight(rowHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    protected int getRowHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return (tree == null) ? -1 : tree.getRowHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Sets the TreeCellRenderer to <code>tcr</code>. This invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * <code>updateRenderer</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    protected void setCellRenderer(TreeCellRenderer tcr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        completeEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        updateRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if(treeState != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            treeState.invalidateSizes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Return currentCellRenderer, which will either be the trees
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * renderer, or defaultCellRenderer, which ever wasn't null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    protected TreeCellRenderer getCellRenderer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        return currentCellRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Sets the TreeModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    protected void setModel(TreeModel model) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        completeEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        if(treeModel != null && treeModelListener != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            treeModel.removeTreeModelListener(treeModelListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        treeModel = model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if(treeModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            if(treeModelListener != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                treeModel.addTreeModelListener(treeModelListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if(treeState != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            treeState.setModel(model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            updateLayoutCacheExpandedNodesIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    protected TreeModel getModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        return treeModel;
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
     * Sets the root to being visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    protected void setRootVisible(boolean newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        completeEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        updateDepthOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if(treeState != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            treeState.setRootVisible(newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            treeState.invalidateSizes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    protected boolean isRootVisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return (tree != null) ? tree.isRootVisible() : false;
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
     * Determines whether the node handles are to be displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    protected void setShowsRootHandles(boolean newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        completeEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        updateDepthOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if(treeState != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            treeState.invalidateSizes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    protected boolean getShowsRootHandles() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        return (tree != null) ? tree.getShowsRootHandles() : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * Sets the cell editor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    protected void setCellEditor(TreeCellEditor editor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        updateCellEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    protected TreeCellEditor getCellEditor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        return (tree != null) ? tree.getCellEditor() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * Configures the receiver to allow, or not allow, editing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    protected void setEditable(boolean newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        updateCellEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    protected boolean isEditable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        return (tree != null) ? tree.isEditable() : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * Resets the selection model. The appropriate listener are installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * on the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    protected void setSelectionModel(TreeSelectionModel newLSM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        completeEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        if(selectionModelPropertyChangeListener != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
           treeSelectionModel != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            treeSelectionModel.removePropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                              (selectionModelPropertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        if(treeSelectionListener != null && treeSelectionModel != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            treeSelectionModel.removeTreeSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                               (treeSelectionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        treeSelectionModel = newLSM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        if(treeSelectionModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            if(selectionModelPropertyChangeListener != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                treeSelectionModel.addPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                              (selectionModelPropertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            if(treeSelectionListener != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                treeSelectionModel.addTreeSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                                   (treeSelectionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            if(treeState != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                treeState.setSelectionModel(treeSelectionModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        else if(treeState != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            treeState.setSelectionModel(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        if(tree != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            tree.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    protected TreeSelectionModel getSelectionModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        return treeSelectionModel;
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
    // TreeUI methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
      * Returns the Rectangle enclosing the label portion that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
      * last item in path will be drawn into.  Will return null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
      * any component in path is currently valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    public Rectangle getPathBounds(JTree tree, TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        if(tree != null && treeState != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            return getPathBounds(path, tree.getInsets(), new Rectangle());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    private Rectangle getPathBounds(TreePath path, Insets insets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                                    Rectangle bounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        bounds = treeState.getBounds(path, bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        if (bounds != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            if (leftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                bounds.x += insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                bounds.x = tree.getWidth() - (bounds.x + bounds.width) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                        insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            bounds.y += insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        return bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
      * Returns the path for passed in row.  If row is not visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
      * null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    public TreePath getPathForRow(JTree tree, int row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        return (treeState != null) ? treeState.getPathForRow(row) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
      * Returns the row that the last item identified in path is visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
      * at.  Will return -1 if any of the elements in path are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
      * currently visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    public int getRowForPath(JTree tree, TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        return (treeState != null) ? treeState.getRowForPath(path) : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
      * Returns the number of rows that are being displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    public int getRowCount(JTree tree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        return (treeState != null) ? treeState.getRowCount() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
      * Returns the path to the node that is closest to x,y.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
      * there is nothing currently visible this will return null, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
      * it'll always return a valid path.  If you need to test if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
      * returned object is exactly at x, y you should get the bounds for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
      * the returned path and test x, y against that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    public TreePath getClosestPathForLocation(JTree tree, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        if(tree != null && treeState != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            // TreeState doesn't care about the x location, hence it isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            // adjusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            y -= tree.getInsets().top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            return treeState.getPathClosestTo(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        return null;
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
      * Returns true if the tree is being edited.  The item that is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
      * edited can be returned by getEditingPath().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    public boolean isEditing(JTree tree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        return (editingComponent != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
      * Stops the current editing session.  This has no effect if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
      * tree isn't being edited.  Returns true if the editor allows the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
      * editing session to stop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    public boolean stopEditing(JTree tree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        if(editingComponent != null && cellEditor.stopCellEditing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            completeEditing(false, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        return false;
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
      * Cancels the current editing session.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    public void cancelEditing(JTree tree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        if(editingComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            completeEditing(false, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
      * Selects the last item in path and tries to edit it.  Editing will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
      * fail if the CellEditor won't allow it for the selected item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    public void startEditingAtPath(JTree tree, TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        tree.scrollPathToVisible(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        if(path != null && tree.isVisible(path))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            startEditing(path, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * Returns the path to the element that is being edited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    public TreePath getEditingPath(JTree tree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        return editingPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    // Install methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        if ( c == null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            throw new NullPointerException( "null component passed to BasicTreeUI.installUI()" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        tree = (JTree)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        prepareForUIInstall();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        // Boilerplate install block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        installDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        installComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        completeUIInstall();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * Invoked after the <code>tree</code> instance variable has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * set, but before any defaults/listeners have been installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    protected void prepareForUIInstall() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        drawingCache = new Hashtable<TreePath,Boolean>(7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        // Data member initializations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        leftToRight = BasicGraphicsUtils.isLeftToRight(tree);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        stopEditingInCompleteEditing = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        lastSelectedRow = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        leadRow = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        preferredSize = new Dimension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        largeModel = tree.isLargeModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        if(getRowHeight() <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            largeModel = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        setModel(tree.getModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * Invoked from installUI after all the defaults/listeners have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    protected void completeUIInstall() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        // Custom install code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        this.setShowsRootHandles(tree.getShowsRootHandles());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        updateRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        updateDepthOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        setSelectionModel(tree.getSelectionModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        // Create, if necessary, the TreeState instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        treeState = createLayoutCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        configureLayoutCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    protected void installDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        if(tree.getBackground() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
           tree.getBackground() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            tree.setBackground(UIManager.getColor("Tree.background"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        if(getHashColor() == null || getHashColor() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            setHashColor(UIManager.getColor("Tree.hash"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        if (tree.getFont() == null || tree.getFont() instanceof UIResource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            tree.setFont( UIManager.getFont("Tree.font") );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        // JTree's original row height is 16.  To correctly display the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        // contents on Linux we should have set it to 18, Windows 19 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        // Solaris 20.  As these values vary so much it's too hard to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        // be backward compatable and try to update the row height, we're
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        // therefor NOT going to adjust the row height based on font.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        // developer changes the font, it's there responsibility to update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        // the row height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        setExpandedIcon( (Icon)UIManager.get( "Tree.expandedIcon" ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        setCollapsedIcon( (Icon)UIManager.get( "Tree.collapsedIcon" ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        setLeftChildIndent(((Integer)UIManager.get("Tree.leftChildIndent")).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                           intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        setRightChildIndent(((Integer)UIManager.get("Tree.rightChildIndent")).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                           intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        LookAndFeel.installProperty(tree, "rowHeight",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                                    UIManager.get("Tree.rowHeight"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        largeModel = (tree.isLargeModel() && tree.getRowHeight() > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        Object scrollsOnExpand = UIManager.get("Tree.scrollsOnExpand");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        if (scrollsOnExpand != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            LookAndFeel.installProperty(tree, "scrollsOnExpand", scrollsOnExpand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        paintLines = UIManager.getBoolean("Tree.paintLines");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        lineTypeDashed = UIManager.getBoolean("Tree.lineTypeDashed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        Long l = (Long)UIManager.get("Tree.timeFactor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        timeFactor = (l!=null) ? l.longValue() : 1000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        Object showsRootHandles = UIManager.get("Tree.showsRootHandles");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        if (showsRootHandles != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            LookAndFeel.installProperty(tree,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                    JTree.SHOWS_ROOT_HANDLES_PROPERTY, showsRootHandles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        if ( (propertyChangeListener = createPropertyChangeListener())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
             != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            tree.addPropertyChangeListener(propertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        if ( (mouseListener = createMouseListener()) != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            tree.addMouseListener(mouseListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            if (mouseListener instanceof MouseMotionListener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                tree.addMouseMotionListener((MouseMotionListener)mouseListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        if ((focusListener = createFocusListener()) != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            tree.addFocusListener(focusListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        if ((keyListener = createKeyListener()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            tree.addKeyListener(keyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        if((treeExpansionListener = createTreeExpansionListener()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            tree.addTreeExpansionListener(treeExpansionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        if((treeModelListener = createTreeModelListener()) != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
           treeModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            treeModel.addTreeModelListener(treeModelListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        if((selectionModelPropertyChangeListener =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            createSelectionModelPropertyChangeListener()) != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
           treeSelectionModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            treeSelectionModel.addPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                (selectionModelPropertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        if((treeSelectionListener = createTreeSelectionListener()) != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
           treeSelectionModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            treeSelectionModel.addTreeSelectionListener(treeSelectionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        TransferHandler th = tree.getTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        if (th == null || th instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            tree.setTransferHandler(defaultTransferHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            // default TransferHandler doesn't support drop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            // so we don't want drop handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            if (tree.getDropTarget() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                tree.setDropTarget(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        LookAndFeel.installProperty(tree, "opaque", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    protected void installKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        InputMap km = getInputMap(JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                                  WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        SwingUtilities.replaceUIInputMap(tree, JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                                         WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                                         km);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        km = getInputMap(JComponent.WHEN_FOCUSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        SwingUtilities.replaceUIInputMap(tree, JComponent.WHEN_FOCUSED, km);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        LazyActionMap.installLazyActionMap(tree, BasicTreeUI.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                                           "Tree.actionMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    InputMap getInputMap(int condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            return (InputMap)DefaultLookup.get(tree, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                                               "Tree.ancestorInputMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        else if (condition == JComponent.WHEN_FOCUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            InputMap keyMap = (InputMap)DefaultLookup.get(tree, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                                                      "Tree.focusInputMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            InputMap rtlKeyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            if (tree.getComponentOrientation().isLeftToRight() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                  ((rtlKeyMap = (InputMap)DefaultLookup.get(tree, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                  "Tree.focusInputMap.RightToLeft")) == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                return keyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                rtlKeyMap.setParent(keyMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                return rtlKeyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * Intalls the subcomponents of the tree, which is the renderer pane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    protected void installComponents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        if ((rendererPane = createCellRendererPane()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            tree.add( rendererPane );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    // Create methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * Creates an instance of NodeDimensions that is able to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * the size of a given node in the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    protected AbstractLayoutCache.NodeDimensions createNodeDimensions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        return new NodeDimensionsHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * Creates a listener that is responsible that updates the UI based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * how the tree changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    protected PropertyChangeListener createPropertyChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    private Handler getHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * Creates the listener responsible for updating the selection based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * mouse events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    protected MouseListener createMouseListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * Creates a listener that is responsible for updating the display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * when focus is lost/gained.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    protected FocusListener createFocusListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * Creates the listener reponsible for getting key events from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    protected KeyListener createKeyListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * Creates the listener responsible for getting property change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * events from the selection model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    protected PropertyChangeListener createSelectionModelPropertyChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * Creates the listener that updates the display based on selection change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    protected TreeSelectionListener createTreeSelectionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * Creates a listener to handle events from the current editor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    protected CellEditorListener createCellEditorListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * Creates and returns a new ComponentHandler. This is used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * the large model to mark the validCachedPreferredSize as invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * when the component moves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    protected ComponentListener createComponentListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        return new ComponentHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * Creates and returns the object responsible for updating the treestate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * when nodes expanded state changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    protected TreeExpansionListener createTreeExpansionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * Creates the object responsible for managing what is expanded, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * well as the size of nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    protected AbstractLayoutCache createLayoutCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        if(isLargeModel() && getRowHeight() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            return new FixedHeightLayoutCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        return new VariableHeightLayoutCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * Returns the renderer pane that renderer components are placed in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    protected CellRendererPane createCellRendererPane() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        return new CellRendererPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
      * Creates a default cell editor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    protected TreeCellEditor createDefaultCellEditor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        if(currentCellRenderer != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
           (currentCellRenderer instanceof DefaultTreeCellRenderer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            DefaultTreeCellEditor editor = new DefaultTreeCellEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                        (tree, (DefaultTreeCellRenderer)currentCellRenderer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            return editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        return new DefaultTreeCellEditor(tree, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
      * Returns the default cell renderer that is used to do the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
      * stamping of each node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    protected TreeCellRenderer createDefaultCellRenderer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        return new DefaultTreeCellRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * Returns a listener that can update the tree when the model changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    protected TreeModelListener createTreeModelListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    // Uninstall methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    public void uninstallUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        completeEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        prepareForUIUninstall();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        uninstallDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        uninstallKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        uninstallComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        completeUIUninstall();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    protected void prepareForUIUninstall() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    protected void completeUIUninstall() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        if(createdRenderer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            tree.setCellRenderer(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        if(createdCellEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            tree.setCellEditor(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        cellEditor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        currentCellRenderer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        rendererPane = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        componentListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        propertyChangeListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        mouseListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        focusListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        keyListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        setSelectionModel(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        treeState = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        drawingCache = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        selectionModelPropertyChangeListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        tree = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        treeModel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        treeSelectionModel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        treeSelectionListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        treeExpansionListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    protected void uninstallDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        if (tree.getTransferHandler() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            tree.setTransferHandler(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        if(componentListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            tree.removeComponentListener(componentListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        if (propertyChangeListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            tree.removePropertyChangeListener(propertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        if (mouseListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            tree.removeMouseListener(mouseListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            if (mouseListener instanceof MouseMotionListener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                tree.removeMouseMotionListener((MouseMotionListener)mouseListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        if (focusListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            tree.removeFocusListener(focusListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        if (keyListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            tree.removeKeyListener(keyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        if(treeExpansionListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            tree.removeTreeExpansionListener(treeExpansionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        if(treeModel != null && treeModelListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            treeModel.removeTreeModelListener(treeModelListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        if(selectionModelPropertyChangeListener != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
           treeSelectionModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            treeSelectionModel.removePropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                (selectionModelPropertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        if(treeSelectionListener != null && treeSelectionModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            treeSelectionModel.removeTreeSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                               (treeSelectionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    protected void uninstallKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        SwingUtilities.replaceUIActionMap(tree, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        SwingUtilities.replaceUIInputMap(tree, JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                                         WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                                         null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        SwingUtilities.replaceUIInputMap(tree, JComponent.WHEN_FOCUSED, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * Uninstalls the renderer pane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    protected void uninstallComponents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        if(rendererPane != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            tree.remove(rendererPane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * Recomputes the right margin, and invalidates any tree states
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    private void redoTheLayout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        if (treeState != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            treeState.invalidateSizes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * Returns the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    public int getBaseline(JComponent c, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        super.getBaseline(c, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        UIDefaults lafDefaults = UIManager.getLookAndFeelDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        Component renderer = (Component)lafDefaults.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                BASELINE_COMPONENT_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        if (renderer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            TreeCellRenderer tcr = createDefaultCellRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            renderer = tcr.getTreeCellRendererComponent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                    tree, "a", false, false, false, -1, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            lafDefaults.put(BASELINE_COMPONENT_KEY, renderer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        int rowHeight = tree.getRowHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        int baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        if (rowHeight > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            baseline = renderer.getBaseline(Integer.MAX_VALUE, rowHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            Dimension pref = renderer.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            baseline = renderer.getBaseline(pref.width, pref.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        return baseline + tree.getInsets().top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * Returns an enum indicating how the baseline of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * changes as the size changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    public Component.BaselineResizeBehavior getBaselineResizeBehavior(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        super.getBaselineResizeBehavior(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
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
    // Painting routines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    public void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        if (tree != c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            throw new InternalError("incorrect component");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        // Should never happen if installed for a UI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        if(treeState == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        Rectangle        paintBounds = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        Insets           insets = tree.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        TreePath         initialPath = getClosestPathForLocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                                       (tree, 0, paintBounds.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        Enumeration      paintingEnumerator = treeState.getVisiblePathsFrom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                                              (initialPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        int              row = treeState.getRowForPath(initialPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        int              endY = paintBounds.y + paintBounds.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        drawingCache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        if(initialPath != null && paintingEnumerator != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            TreePath   parentPath = initialPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            // Draw the lines, knobs, and rows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            // Find each parent and have them draw a line to their last child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            parentPath = parentPath.getParentPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            while(parentPath != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                paintVerticalPartOfLeg(g, paintBounds, insets, parentPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                drawingCache.put(parentPath, Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                parentPath = parentPath.getParentPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            boolean         done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            // Information for the node being rendered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            boolean         isExpanded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            boolean         hasBeenExpanded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            boolean         isLeaf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            Rectangle       boundsBuffer = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            Rectangle       bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            TreePath        path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            boolean         rootVisible = isRootVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            while(!done && paintingEnumerator.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                path = (TreePath)paintingEnumerator.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                if(path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                    isLeaf = treeModel.isLeaf(path.getLastPathComponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                    if(isLeaf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                        isExpanded = hasBeenExpanded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                        isExpanded = treeState.getExpandedState(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                        hasBeenExpanded = tree.hasBeenExpanded(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                    bounds = getPathBounds(path, insets, boundsBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                    if(bounds == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                        // This will only happen if the model changes out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                        // from under us (usually in another thread).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                        // Swing isn't multithreaded, but I'll put this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                        // check in anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                    // See if the vertical line to the parent has been drawn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                    parentPath = path.getParentPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                    if(parentPath != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                        if(drawingCache.get(parentPath) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                            paintVerticalPartOfLeg(g, paintBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                                                   insets, parentPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                            drawingCache.put(parentPath, Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                        paintHorizontalPartOfLeg(g, paintBounds, insets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                                                 bounds, path, row,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                                                 isExpanded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                                                 hasBeenExpanded, isLeaf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                    else if(rootVisible && row == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                        paintHorizontalPartOfLeg(g, paintBounds, insets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                                                 bounds, path, row,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                                                 isExpanded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                                                 hasBeenExpanded, isLeaf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                    if(shouldPaintExpandControl(path, row, isExpanded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                                                hasBeenExpanded, isLeaf)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                        paintExpandControl(g, paintBounds, insets, bounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                                           path, row, isExpanded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                                           hasBeenExpanded, isLeaf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                    paintRow(g, paintBounds, insets, bounds, path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                                 row, isExpanded, hasBeenExpanded, isLeaf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                    if((bounds.y + bounds.height) >= endY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                        done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                    done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                row++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        paintDropLine(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        // Empty out the renderer pane, allowing renderers to be gc'ed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        rendererPane.removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        drawingCache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1244
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1245
     * Tells if a {@code DropLocation} should be indicated by a line between
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1246
     * nodes. This is meant for {@code javax.swing.DropMode.INSERT} and
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1247
     * {@code javax.swing.DropMode.ON_OR_INSERT} drop modes.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1248
     *
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1249
     * @param loc a {@code DropLocation}
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1250
     * @return {@code true} if the drop location should be shown as a line
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1251
     * @since 1.7
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1252
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1253
    protected boolean isDropLine(JTree.DropLocation loc) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        return loc != null && loc.getPath() != null && loc.getChildIndex() != -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1257
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1258
     * Paints the drop line.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1259
     *
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1260
     * @param g {@code Graphics} object to draw on
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1261
     * @since 1.7
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1262
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1263
    protected void paintDropLine(Graphics g) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        JTree.DropLocation loc = tree.getDropLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        if (!isDropLine(loc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        Color c = UIManager.getColor("Tree.dropLineColor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            g.setColor(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            Rectangle rect = getDropLineRect(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            g.fillRect(rect.x, rect.y, rect.width, rect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1277
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  1278
     * Returns a unbounding box for the drop line.
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1279
     *
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1280
     * @param loc a {@code DropLocation}
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1281
     * @return bounding box for the drop line
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1282
     * @since 1.7
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1283
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1284
    protected Rectangle getDropLineRect(JTree.DropLocation loc) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
  1285
        Rectangle rect;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        TreePath path = loc.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        int index = loc.getChildIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        boolean ltr = leftToRight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        Insets insets = tree.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        if (tree.getRowCount() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
            rect = new Rectangle(insets.left,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                                 insets.top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                                 tree.getWidth() - insets.left - insets.right,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                                 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            TreeModel model = getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            Object root = model.getRoot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            if (path.getLastPathComponent() == root
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                    && index >= model.getChildCount(root)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                rect = tree.getRowBounds(tree.getRowCount() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                rect.y = rect.y + rect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                Rectangle xRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                if (!tree.isRootVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                    xRect = tree.getRowBounds(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                } else if (model.getChildCount(root) == 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                    xRect = tree.getRowBounds(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                    xRect.x += totalChildIndent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                    xRect.width -= totalChildIndent + totalChildIndent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                    TreePath lastChildPath = path.pathByAddingChild(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                        model.getChild(root, model.getChildCount(root) - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                    xRect = tree.getPathBounds(lastChildPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                rect.x = xRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                rect.width = xRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                rect = tree.getPathBounds(path.pathByAddingChild(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                    model.getChild(path.getLastPathComponent(), index)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        if (rect.y != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            rect.y--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        if (!ltr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            rect.x = rect.x + rect.width - 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        rect.width = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        rect.height = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        return rect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * Paints the horizontal part of the leg. The receiver should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * NOT modify <code>clipBounds</code>, or <code>insets</code>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * NOTE: <code>parentRow</code> can be -1 if the root is not visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
    protected void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                                            Insets insets, Rectangle bounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                                            TreePath path, int row,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                                            boolean isExpanded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                                            boolean hasBeenExpanded, boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                                            isLeaf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        if (!paintLines) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        // Don't paint the legs for the root'ish node if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        int depth = path.getPathCount() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        if((depth == 0 || (depth == 1 && !isRootVisible())) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
           !getShowsRootHandles()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        int clipLeft = clipBounds.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        int clipRight = clipBounds.x + clipBounds.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        int clipTop = clipBounds.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        int clipBottom = clipBounds.y + clipBounds.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        int lineY = bounds.y + bounds.height / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        if (leftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            int leftX = bounds.x - getRightChildIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            int nodeX = bounds.x - getHorizontalLegBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            if(lineY >= clipTop
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                    && lineY < clipBottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                    && nodeX >= clipLeft
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                    && leftX < clipRight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                    && leftX < nodeX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                g.setColor(getHashColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                paintHorizontalLine(g, tree, lineY, leftX, nodeX - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            int nodeX = bounds.x + bounds.width + getHorizontalLegBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            int rightX = bounds.x + bounds.width + getRightChildIndent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            if(lineY >= clipTop
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                    && lineY < clipBottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                    && rightX >= clipLeft
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                    && nodeX < clipRight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                    && nodeX < rightX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                g.setColor(getHashColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                paintHorizontalLine(g, tree, lineY, nodeX, rightX - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * Paints the vertical part of the leg. The receiver should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * NOT modify <code>clipBounds</code>, <code>insets</code>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
    protected void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                                          Insets insets, TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        if (!paintLines) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        int depth = path.getPathCount() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        if (depth == 0 && !getShowsRootHandles() && !isRootVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        int lineX = getRowX(-1, depth + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        if (leftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            lineX = lineX - getRightChildIndent() + insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            lineX = tree.getWidth() - lineX - insets.right +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                    getRightChildIndent() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        int clipLeft = clipBounds.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        int clipRight = clipBounds.x + (clipBounds.width - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        if (lineX >= clipLeft && lineX <= clipRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
            int clipTop = clipBounds.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
            int clipBottom = clipBounds.y + clipBounds.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
            Rectangle parentBounds = getPathBounds(tree, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            Rectangle lastChildBounds = getPathBounds(tree,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
                                                     getLastChildPath(path));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
            if(lastChildBounds == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                // This shouldn't happen, but if the model is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                // in another thread it is possible for this to happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                // Swing isn't multithreaded, but I'll add this check in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                // anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            int       top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            if(parentBounds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                top = Math.max(insets.top + getVerticalLegBuffer(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                               clipTop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                top = Math.max(parentBounds.y + parentBounds.height +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                               getVerticalLegBuffer(), clipTop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            if(depth == 0 && !isRootVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                TreeModel      model = getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                if(model != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                    Object        root = model.getRoot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                    if(model.getChildCount(root) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                        parentBounds = getPathBounds(tree, path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                                  pathByAddingChild(model.getChild(root, 0)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
                        if(parentBounds != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                            top = Math.max(insets.top + getVerticalLegBuffer(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                                           parentBounds.y +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                                           parentBounds.height / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            int bottom = Math.min(lastChildBounds.y +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                                  (lastChildBounds.height / 2), clipBottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
            if (top <= bottom) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                g.setColor(getHashColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                paintVerticalLine(g, tree, lineX, top, bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * Paints the expand (toggle) part of a row. The receiver should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     * NOT modify <code>clipBounds</code>, or <code>insets</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    protected void paintExpandControl(Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                                      Rectangle clipBounds, Insets insets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                                      Rectangle bounds, TreePath path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                                      int row, boolean isExpanded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                                      boolean hasBeenExpanded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                                      boolean isLeaf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        Object       value = path.getLastPathComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        // Draw icons if not a leaf and either hasn't been loaded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        // or the model child count is > 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        if (!isLeaf && (!hasBeenExpanded ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                        treeModel.getChildCount(value) > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
            int middleXOfKnob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            if (leftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                middleXOfKnob = bounds.x - getRightChildIndent() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
                middleXOfKnob = bounds.x + bounds.width + getRightChildIndent() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
            int middleYOfKnob = bounds.y + (bounds.height / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            if (isExpanded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
                Icon expandedIcon = getExpandedIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
                if(expandedIcon != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                  drawCentered(tree, g, expandedIcon, middleXOfKnob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
                               middleYOfKnob );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                Icon collapsedIcon = getCollapsedIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                if(collapsedIcon != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                  drawCentered(tree, g, collapsedIcon, middleXOfKnob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                               middleYOfKnob);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     * Paints the renderer part of a row. The receiver should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * NOT modify <code>clipBounds</code>, or <code>insets</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
    protected void paintRow(Graphics g, Rectangle clipBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                            Insets insets, Rectangle bounds, TreePath path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                            int row, boolean isExpanded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                            boolean hasBeenExpanded, boolean isLeaf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        // Don't paint the renderer if editing this row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        if(editingComponent != null && editingRow == row)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        int leadIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        if(tree.hasFocus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            leadIndex = getLeadSelectionRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
            leadIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        Component component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        component = currentCellRenderer.getTreeCellRendererComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                      (tree, path.getLastPathComponent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                       tree.isRowSelected(row), isExpanded, isLeaf, row,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                       (leadIndex == row));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        rendererPane.paintComponent(g, component, tree, bounds.x, bounds.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                                    bounds.width, bounds.height, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     * Returns true if the expand (toggle) control should be drawn for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     * the specified row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
    protected boolean shouldPaintExpandControl(TreePath path, int row,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                                               boolean isExpanded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                                               boolean hasBeenExpanded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                                               boolean isLeaf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        if(isLeaf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        int              depth = path.getPathCount() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        if((depth == 0 || (depth == 1 && !isRootVisible())) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
           !getShowsRootHandles())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * Paints a vertical line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
    protected void paintVerticalLine(Graphics g, JComponent c, int x, int top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                                    int bottom) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        if (lineTypeDashed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            drawDashedVerticalLine(g, x, top, bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            g.drawLine(x, top, x, bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     * Paints a horizontal line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
    protected void paintHorizontalLine(Graphics g, JComponent c, int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                                      int left, int right) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        if (lineTypeDashed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
            drawDashedHorizontalLine(g, y, left, right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            g.drawLine(left, y, right, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     * The vertical element of legs between nodes starts at the bottom of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * parent node by default.  This method makes the leg start below that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
    protected int getVerticalLegBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
     * The horizontal element of legs between nodes starts at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     * right of the left-hand side of the child node by default.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
     * method makes the leg end before that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
    protected int getHorizontalLegBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
    private int findCenteredX(int x, int iconWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        return leftToRight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
               ? x - (int)Math.ceil(iconWidth / 2.0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
               : x - (int)Math.floor(iconWidth / 2.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
    // Generic painting methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
    // Draws the icon centered at (x,y)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
    protected void drawCentered(Component c, Graphics graphics, Icon icon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                                int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        icon.paintIcon(c, graphics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                      findCenteredX(x, icon.getIconWidth()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                      y - icon.getIconHeight() / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
    // This method is slow -- revisit when Java2D is ready.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    // assumes x1 <= x2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
    protected void drawDashedHorizontalLine(Graphics g, int y, int x1, int x2){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        // Drawing only even coordinates helps join line segments so they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        // appear as one line.  This can be defeated by translating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
        // Graphics by an odd amount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        x1 += (x1 % 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        for (int x = x1; x <= x2; x+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
            g.drawLine(x, y, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
    // This method is slow -- revisit when Java2D is ready.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
    // assumes y1 <= y2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
    protected void drawDashedVerticalLine(Graphics g, int x, int y1, int y2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        // Drawing only even coordinates helps join line segments so they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        // appear as one line.  This can be defeated by translating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
        // Graphics by an odd amount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
        y1 += (y1 % 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        for (int y = y1; y <= y2; y+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            g.drawLine(x, y, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
    // Various local methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     * Returns the location, along the x-axis, to render a particular row
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     * at. The return value does not include any Insets specified on the JTree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     * This does not check for the validity of the row or depth, it is assumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     * to be correct and will not throw an Exception if the row or depth
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     * doesn't match that of the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     * @param row Row to return x location for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     * @param depth Depth of the row
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     * @return amount to indent the given row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
    protected int getRowX(int row, int depth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
        return totalChildIndent * (depth + depthOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     * Makes all the nodes that are expanded in JTree expanded in LayoutCache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     * This invokes updateExpandedDescendants with the root path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
    protected void updateLayoutCacheExpandedNodes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
        if(treeModel != null && treeModel.getRoot() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
            updateExpandedDescendants(new TreePath(treeModel.getRoot()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
    private void updateLayoutCacheExpandedNodesIfNecessary() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        if (treeModel != null && treeModel.getRoot() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            TreePath rootPath = new TreePath(treeModel.getRoot());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            if (tree.isExpanded(rootPath)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                updateLayoutCacheExpandedNodes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                treeState.setExpandedState(rootPath, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     * Updates the expanded state of all the descendants of <code>path</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     * by getting the expanded descendants from the tree and forwarding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     * to the tree state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
    protected void updateExpandedDescendants(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        completeEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        if(treeState != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
            treeState.setExpandedState(path, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
            Enumeration   descendants = tree.getExpandedDescendants(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
            if(descendants != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                while(descendants.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                    path = (TreePath)descendants.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                    treeState.setExpandedState(path, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
            }
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  1706
            updateLeadSelectionRow();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
            updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     * Returns a path to the last child of <code>parent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
    protected TreePath getLastChildPath(TreePath parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        if(treeModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
            int         childCount = treeModel.getChildCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                (parent.getLastPathComponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
            if(childCount > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                return parent.pathByAddingChild(treeModel.getChild
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                           (parent.getLastPathComponent(), childCount - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     * Updates how much each depth should be offset by.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
    protected void updateDepthOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        if(isRootVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
            if(getShowsRootHandles())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                depthOffset = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                depthOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        else if(!getShowsRootHandles())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            depthOffset = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
            depthOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
      * Updates the cellEditor based on the editability of the JTree that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
      * we're contained in.  If the tree is editable but doesn't have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
      * cellEditor, a basic one will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
    protected void updateCellEditor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
        TreeCellEditor        newEditor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        completeEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
        if(tree == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
            newEditor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            if(tree.isEditable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                newEditor = tree.getCellEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                if(newEditor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                    newEditor = createDefaultCellEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                    if(newEditor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                        tree.setCellEditor(newEditor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                        createdCellEditor = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                newEditor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        if(newEditor != cellEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
            if(cellEditor != null && cellEditorListener != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                cellEditor.removeCellEditorListener(cellEditorListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
            cellEditor = newEditor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            if(cellEditorListener == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
                cellEditorListener = createCellEditorListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
            if(newEditor != null && cellEditorListener != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
                newEditor.addCellEditorListener(cellEditorListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
            createdCellEditor = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
      * Messaged from the tree we're in when the renderer has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
    protected void updateRenderer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        if(tree != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            TreeCellRenderer      newCellRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
            newCellRenderer = tree.getCellRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            if(newCellRenderer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                tree.setCellRenderer(createDefaultCellRenderer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                createdRenderer = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                createdRenderer = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                currentCellRenderer = newCellRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
                if(createdCellEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
                    tree.setCellEditor(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
            createdRenderer = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
            currentCellRenderer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        updateCellEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
     * Resets the TreeState instance based on the tree we're providing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
     * look and feel for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    protected void configureLayoutCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        if(treeState != null && tree != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
            if(nodeDimensions == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                nodeDimensions = createNodeDimensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
            treeState.setNodeDimensions(nodeDimensions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
            treeState.setRootVisible(tree.isRootVisible());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
            treeState.setRowHeight(tree.getRowHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
            treeState.setSelectionModel(getSelectionModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
            // Only do this if necessary, may loss state if call with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
            // same model as it currently has.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            if(treeState.getModel() != tree.getModel())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                treeState.setModel(tree.getModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
            updateLayoutCacheExpandedNodesIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
            // Create a listener to update preferred size when bounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
            // changes, if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
            if(isLargeModel()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
                if(componentListener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                    componentListener = createComponentListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                    if(componentListener != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                        tree.addComponentListener(componentListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            else if(componentListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                tree.removeComponentListener(componentListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                componentListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
        else if(componentListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
            tree.removeComponentListener(componentListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            componentListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     * Marks the cached size as being invalid, and messages the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     * tree with <code>treeDidChange</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
    protected void updateSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
        validCachedPreferredSize = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        tree.treeDidChange();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
    private void updateSize0() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        validCachedPreferredSize = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        tree.revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     * Updates the <code>preferredSize</code> instance variable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     * which is returned from <code>getPreferredSize()</code>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     * For left to right orientations, the size is determined from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     * current AbstractLayoutCache. For RTL orientations, the preferred size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     * becomes the width minus the minimum x position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
    protected void updateCachedPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
        if(treeState != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
            Insets               i = tree.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
            if(isLargeModel()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                Rectangle            visRect = tree.getVisibleRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                if (visRect.x == 0 && visRect.y == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                        visRect.width == 0 && visRect.height == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                        tree.getVisibleRowCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                    // The tree doesn't have a valid bounds yet. Calculate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                    // based on visible row count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
                    visRect.width = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
                    visRect.height = tree.getRowHeight() *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                            tree.getVisibleRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                    visRect.x -= i.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                    visRect.y -= i.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                }
15333
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  1883
                // we should consider a non-visible area above
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  1884
                Component component = SwingUtilities.getUnwrappedParent(tree);
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  1885
                if (component instanceof JViewport) {
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  1886
                    component = component.getParent();
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  1887
                    if (component instanceof JScrollPane) {
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  1888
                        JScrollPane pane = (JScrollPane) component;
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  1889
                        JScrollBar bar = pane.getHorizontalScrollBar();
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  1890
                        if ((bar != null) && bar.isVisible()) {
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  1891
                            int height = bar.getHeight();
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  1892
                            visRect.y -= height;
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  1893
                            visRect.height += height;
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  1894
                        }
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  1895
                    }
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  1896
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
                preferredSize.width = treeState.getPreferredWidth(visRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
                preferredSize.width = treeState.getPreferredWidth(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
            preferredSize.height = treeState.getPreferredHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
            preferredSize.width += i.left + i.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
            preferredSize.height += i.top + i.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        validCachedPreferredSize = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
      * Messaged from the VisibleTreeNode after it has been expanded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
    protected void pathWasExpanded(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        if(tree != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
            tree.fireTreeExpanded(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
      * Messaged from the VisibleTreeNode after it has collapsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
    protected void pathWasCollapsed(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
        if(tree != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
            tree.fireTreeCollapsed(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
      * Ensures that the rows identified by beginRow through endRow are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
      * visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
    protected void ensureRowsAreVisible(int beginRow, int endRow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
        if(tree != null && beginRow >= 0 && endRow < getRowCount(tree)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
            boolean scrollVert = DefaultLookup.getBoolean(tree, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
                              "Tree.scrollsHorizontallyAndVertically", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
            if(beginRow == endRow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
                Rectangle     scrollBounds = getPathBounds(tree, getPathForRow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                                                           (tree, beginRow));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
                if(scrollBounds != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
                    if (!scrollVert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
                        scrollBounds.x = tree.getVisibleRect().x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
                        scrollBounds.width = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
                    tree.scrollRectToVisible(scrollBounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
                Rectangle   beginRect = getPathBounds(tree, getPathForRow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                                                      (tree, beginRow));
11499
f486e3ec76e6 6505523: NullPointerException in BasicTreeUI when a node is removed by expansion listener
alexsch
parents: 9035
diff changeset
  1950
                if (beginRect != null) {
f486e3ec76e6 6505523: NullPointerException in BasicTreeUI when a node is removed by expansion listener
alexsch
parents: 9035
diff changeset
  1951
                    Rectangle   visRect = tree.getVisibleRect();
f486e3ec76e6 6505523: NullPointerException in BasicTreeUI when a node is removed by expansion listener
alexsch
parents: 9035
diff changeset
  1952
                    Rectangle   testRect = beginRect;
f486e3ec76e6 6505523: NullPointerException in BasicTreeUI when a node is removed by expansion listener
alexsch
parents: 9035
diff changeset
  1953
                    int         beginY = beginRect.y;
f486e3ec76e6 6505523: NullPointerException in BasicTreeUI when a node is removed by expansion listener
alexsch
parents: 9035
diff changeset
  1954
                    int         maxY = beginY + visRect.height;
f486e3ec76e6 6505523: NullPointerException in BasicTreeUI when a node is removed by expansion listener
alexsch
parents: 9035
diff changeset
  1955
f486e3ec76e6 6505523: NullPointerException in BasicTreeUI when a node is removed by expansion listener
alexsch
parents: 9035
diff changeset
  1956
                    for(int counter = beginRow + 1; counter <= endRow; counter++) {
f486e3ec76e6 6505523: NullPointerException in BasicTreeUI when a node is removed by expansion listener
alexsch
parents: 9035
diff changeset
  1957
                            testRect = getPathBounds(tree,
f486e3ec76e6 6505523: NullPointerException in BasicTreeUI when a node is removed by expansion listener
alexsch
parents: 9035
diff changeset
  1958
                                    getPathForRow(tree, counter));
14655
d524ae0f511e 6671481: NPE at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection
alexsch
parents: 11499
diff changeset
  1959
                        if (testRect == null) {
d524ae0f511e 6671481: NPE at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection
alexsch
parents: 11499
diff changeset
  1960
                            return;
d524ae0f511e 6671481: NPE at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection
alexsch
parents: 11499
diff changeset
  1961
                        }
11499
f486e3ec76e6 6505523: NullPointerException in BasicTreeUI when a node is removed by expansion listener
alexsch
parents: 9035
diff changeset
  1962
                        if((testRect.y + testRect.height) > maxY)
f486e3ec76e6 6505523: NullPointerException in BasicTreeUI when a node is removed by expansion listener
alexsch
parents: 9035
diff changeset
  1963
                                counter = endRow;
f486e3ec76e6 6505523: NullPointerException in BasicTreeUI when a node is removed by expansion listener
alexsch
parents: 9035
diff changeset
  1964
                            }
f486e3ec76e6 6505523: NullPointerException in BasicTreeUI when a node is removed by expansion listener
alexsch
parents: 9035
diff changeset
  1965
                        tree.scrollRectToVisible(new Rectangle(visRect.x, beginY, 1,
f486e3ec76e6 6505523: NullPointerException in BasicTreeUI when a node is removed by expansion listener
alexsch
parents: 9035
diff changeset
  1966
                                                      testRect.y + testRect.height-
f486e3ec76e6 6505523: NullPointerException in BasicTreeUI when a node is removed by expansion listener
alexsch
parents: 9035
diff changeset
  1967
                                                      beginY));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
    /** Sets the preferred minimum size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
    public void setPreferredMinSize(Dimension newSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
        preferredMinSize = newSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
    /** Returns the minimum preferred size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
    public Dimension getPreferredMinSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
        if(preferredMinSize == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
        return new Dimension(preferredMinSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
    /** Returns the preferred size to properly display the tree,
8374
d262ac869cda 6956842: BasicTreeUI.getPreferredSize(JComp c) is delegated to getPreferredSize(c, true) not (c, false)
alexp
parents: 5506
diff changeset
  1988
      * this is a cover method for getPreferredSize(c, true).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
    public Dimension getPreferredSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
        return getPreferredSize(c, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
    /** Returns the preferred size to represent the tree in
8374
d262ac869cda 6956842: BasicTreeUI.getPreferredSize(JComp c) is delegated to getPreferredSize(c, true) not (c, false)
alexp
parents: 5506
diff changeset
  1995
      * <I>c</I>.  If <I>checkConsistency</I> is true
d262ac869cda 6956842: BasicTreeUI.getPreferredSize(JComp c) is delegated to getPreferredSize(c, true) not (c, false)
alexp
parents: 5506
diff changeset
  1996
      * <b>checkConsistency</b> is messaged first.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
    public Dimension getPreferredSize(JComponent c,
8374
d262ac869cda 6956842: BasicTreeUI.getPreferredSize(JComp c) is delegated to getPreferredSize(c, true) not (c, false)
alexp
parents: 5506
diff changeset
  1999
                                      boolean checkConsistency) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        Dimension       pSize = this.getPreferredMinSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        if(!validCachedPreferredSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
            updateCachedPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        if(tree != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
            if(pSize != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
                return new Dimension(Math.max(pSize.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                                              preferredSize.width),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
                              Math.max(pSize.height, preferredSize.height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
            return new Dimension(preferredSize.width, preferredSize.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
        else if(pSize != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
            return pSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
            return new Dimension(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
      * Returns the minimum size for this component.  Which will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
      * the min preferred size or 0, 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
    public Dimension getMinimumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
        if(this.getPreferredMinSize() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
            return this.getPreferredMinSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
        return new Dimension(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
      * Returns the maximum size for this component, which will be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
      * preferred size if the instance is currently in a JTree, or 0, 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
    public Dimension getMaximumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
        if(tree != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
            return getPreferredSize(tree);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
        if(this.getPreferredMinSize() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
            return this.getPreferredMinSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
        return new Dimension(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
     * Messages to stop the editing session. If the UI the receiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
     * is providing the look and feel for returns true from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
     * <code>getInvokesStopCellEditing</code>, stopCellEditing will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
     * invoked on the current editor. Then completeEditing will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
     * be messaged with false, true, false to cancel any lingering
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
     * editing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
    protected void completeEditing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
        /* If should invoke stopCellEditing, try that */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
        if(tree.getInvokesStopCellEditing() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
           stopEditingInCompleteEditing && editingComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
            cellEditor.stopCellEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
        /* Invoke cancelCellEditing, this will do nothing if stopCellEditing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
           was successful. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
        completeEditing(false, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
      * Stops the editing session.  If messageStop is true the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
      * is messaged with stopEditing, if messageCancel is true the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
      * editor is messaged with cancelEditing. If messageTree is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
      * the treeModel is messaged with valueForPathChanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
    protected void completeEditing(boolean messageStop,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
                                   boolean messageCancel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                                   boolean messageTree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
        if(stopEditingInCompleteEditing && editingComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
            Component             oldComponent = editingComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
            TreePath              oldPath = editingPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
            TreeCellEditor        oldEditor = cellEditor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
            Object                newValue = oldEditor.getCellEditorValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
            Rectangle             editingBounds = getPathBounds(tree,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
                                                                editingPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
            boolean               requestFocus = (tree != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                                   (tree.hasFocus() || SwingUtilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
                                    findFocusOwner(editingComponent) != null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
            editingComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
            editingPath = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
            if(messageStop)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                oldEditor.stopCellEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
            else if(messageCancel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                oldEditor.cancelCellEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
            tree.remove(oldComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
            if(editorHasDifferentSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
                treeState.invalidatePathBounds(oldPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
            }
14655
d524ae0f511e 6671481: NPE at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection
alexsch
parents: 11499
diff changeset
  2090
            else if (editingBounds != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                editingBounds.x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
                editingBounds.width = tree.getSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
                tree.repaint(editingBounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
            if(requestFocus)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
                tree.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
            if(messageTree)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
                treeModel.valueForPathChanged(oldPath, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
    // cover method for startEditing that allows us to pass extra
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
    // information into that method via a class variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
    private boolean startEditingOnRelease(TreePath path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                                          MouseEvent event,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                                          MouseEvent releaseEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
        this.releaseEvent = releaseEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
            return startEditing(path, event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
            this.releaseEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
      * Will start editing for node if there is a cellEditor and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
      * shouldSelectCell returns true.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
      * This assumes that path is valid and visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
    protected boolean startEditing(TreePath path, MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
        if (isEditing(tree) && tree.getInvokesStopCellEditing() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                               !stopEditing(tree)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
        completeEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
        if(cellEditor != null && tree.isPathEditable(path)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
            int           row = getRowForPath(tree, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
            if(cellEditor.isCellEditable(event)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                editingComponent = cellEditor.getTreeCellEditorComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                      (tree, path.getLastPathComponent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
                       tree.isPathSelected(path), tree.isExpanded(path),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                       treeModel.isLeaf(path.getLastPathComponent()), row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
                Rectangle           nodeBounds = getPathBounds(tree, path);
14655
d524ae0f511e 6671481: NPE at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection
alexsch
parents: 11499
diff changeset
  2135
                if (nodeBounds == null) {
d524ae0f511e 6671481: NPE at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection
alexsch
parents: 11499
diff changeset
  2136
                    return false;
d524ae0f511e 6671481: NPE at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection
alexsch
parents: 11499
diff changeset
  2137
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                editingRow = row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                Dimension editorSize = editingComponent.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                // Only allow odd heights if explicitly set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
                if(editorSize.height != nodeBounds.height &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                   getRowHeight() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
                    editorSize.height = getRowHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                if(editorSize.width != nodeBounds.width ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                   editorSize.height != nodeBounds.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                    // Editor wants different width or height, invalidate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                    // treeState and relayout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                    editorHasDifferentSize = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                    treeState.invalidatePathBounds(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                    updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                    // To make sure x/y are updated correctly, fetch
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                    // the bounds again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                    nodeBounds = getPathBounds(tree, path);
14655
d524ae0f511e 6671481: NPE at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection
alexsch
parents: 11499
diff changeset
  2158
                    if (nodeBounds == null) {
d524ae0f511e 6671481: NPE at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection
alexsch
parents: 11499
diff changeset
  2159
                        return false;
d524ae0f511e 6671481: NPE at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection
alexsch
parents: 11499
diff changeset
  2160
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                    editorHasDifferentSize = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                tree.add(editingComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                editingComponent.setBounds(nodeBounds.x, nodeBounds.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                                           nodeBounds.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                                           nodeBounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
                editingPath = path;
20097
a4a07c4ead5f 8023474: First mousepress doesn't start editing in JTree
dmarkov
parents: 18133
diff changeset
  2169
                AWTAccessor.getComponentAccessor().revalidateSynchronously(editingComponent);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                editingComponent.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
                if(cellEditor.shouldSelectCell(event)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
                    stopEditingInCompleteEditing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
                    tree.setSelectionRow(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                    stopEditingInCompleteEditing = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
                Component focusedComponent = SwingUtilities2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                                 compositeRequestFocus(editingComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
                boolean selectAll = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
  2181
                if(event != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
                    /* Find the component that will get forwarded all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
                       mouse events until mouseReleased. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                    Point          componentPoint = SwingUtilities.convertPoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                        (tree, new Point(event.getX(), event.getY()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                         editingComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                    /* Create an instance of BasicTreeMouseListener to handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
                       passing the mouse/motion events to the necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                       component. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                    // We really want similar behavior to getMouseEventTarget,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                    // but it is package private.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                    Component activeComponent = SwingUtilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                                    getDeepestComponentAt(editingComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                                       componentPoint.x, componentPoint.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                    if (activeComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                        MouseInputHandler handler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                            new MouseInputHandler(tree, activeComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                                                  event, focusedComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                        if (releaseEvent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                            handler.mouseReleased(releaseEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                        selectAll = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                if (selectAll && focusedComponent instanceof JTextField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                    ((JTextField)focusedComponent).selectAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
                editingComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
    // Following are primarily for handling mouse events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
     * If the <code>mouseX</code> and <code>mouseY</code> are in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
     * expand/collapse region of the <code>row</code>, this will toggle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
     * the row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
    protected void checkForClickInExpandControl(TreePath path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                                                int mouseX, int mouseY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
      if (isLocationInExpandControl(path, mouseX, mouseY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
          handleExpandControlClick(path, mouseX, mouseY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
     * Returns true if <code>mouseX</code> and <code>mouseY</code> fall
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
     * in the area of row that is used to expand/collapse the node and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
     * the node at <code>row</code> does not represent a leaf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
    protected boolean isLocationInExpandControl(TreePath path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
                                                int mouseX, int mouseY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
        if(path != null && !treeModel.isLeaf(path.getLastPathComponent())){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
            int                     boxWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
            Insets                  i = tree.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
            if(getExpandedIcon() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
                boxWidth = getExpandedIcon().getIconWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
                boxWidth = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
            int boxLeftX = getRowX(tree.getRowForPath(path),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
                                   path.getPathCount() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
            if (leftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
                boxLeftX = boxLeftX + i.left - getRightChildIndent() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
                boxLeftX = tree.getWidth() - boxLeftX - i.right + getRightChildIndent() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
            boxLeftX = findCenteredX(boxLeftX, boxWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
            return (mouseX >= boxLeftX && mouseX < (boxLeftX + boxWidth));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
     * Messaged when the user clicks the particular row, this invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
     * toggleExpandState.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
    protected void handleExpandControlClick(TreePath path, int mouseX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
                                            int mouseY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
        toggleExpandState(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
     * Expands path if it is not expanded, or collapses row if it is expanded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
     * If expanding a path and JTree scrolls on expand, ensureRowsAreVisible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
     * is invoked to scroll as many of the children to visible as possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
     * (tries to scroll to last visible descendant of path).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
    protected void toggleExpandState(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
        if(!tree.isExpanded(path)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
            int       row = getRowForPath(tree, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
            tree.expandPath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
            updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
            if(row != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
                if(tree.getScrollsOnExpand())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
                    ensureRowsAreVisible(row, row + treeState.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                                         getVisibleChildCount(path));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
                    ensureRowsAreVisible(row, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
            tree.collapsePath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
            updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
     * Returning true signifies a mouse event on the node should toggle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
     * the selection of only the row under mouse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
    protected boolean isToggleSelectionEvent(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
        return (SwingUtilities.isLeftMouseButton(event) &&
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2658
diff changeset
  2308
                BasicGraphicsUtils.isMenuShortcutKeyDown(event));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
     * Returning true signifies a mouse event on the node should select
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
     * from the anchor point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
    protected boolean isMultiSelectEvent(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
        return (SwingUtilities.isLeftMouseButton(event) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                event.isShiftDown());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
     * Returning true indicates the row under the mouse should be toggled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
     * based on the event. This is invoked after checkForClickInExpandControl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
     * implying the location is not in the expand (toggle) control
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
    protected boolean isToggleEvent(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
        if(!SwingUtilities.isLeftMouseButton(event)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
        int           clickCount = tree.getToggleClickCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
        if(clickCount <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
        return ((event.getClickCount() % clickCount) == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
     * Messaged to update the selection based on a MouseEvent over a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
     * particular row. If the event is a toggle selection event, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
     * row is either selected, or deselected. If the event identifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
     * a multi selection event, the selection is updated from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
     * anchor point. Otherwise the row is selected, and if the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
     * specified a toggle event the row is expanded/collapsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
    protected void selectPathForEvent(TreePath path, MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
        /* Adjust from the anchor point. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
        if(isMultiSelectEvent(event)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
            TreePath    anchor = getAnchorSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
            int         anchorRow = (anchor == null) ? -1 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
                                    getRowForPath(tree, anchor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
            if(anchorRow == -1 || tree.getSelectionModel().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                      getSelectionMode() == TreeSelectionModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                      SINGLE_TREE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
                tree.setSelectionPath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
                int          row = getRowForPath(tree, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
                TreePath     lastAnchorPath = anchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
                if (isToggleSelectionEvent(event)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
                    if (tree.isRowSelected(anchorRow)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
                        tree.addSelectionInterval(anchorRow, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
                        tree.removeSelectionInterval(anchorRow, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
                        tree.addSelectionInterval(row, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
                } else if(row < anchorRow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
                    tree.setSelectionInterval(row, anchorRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
                    tree.setSelectionInterval(anchorRow, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
                lastSelectedRow = row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
                setAnchorSelectionPath(lastAnchorPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
                setLeadSelectionPath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
        // Should this event toggle the selection of this row?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
        /* Control toggles just this node. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
        else if(isToggleSelectionEvent(event)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
            if(tree.isPathSelected(path))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
                tree.removeSelectionPath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
                tree.addSelectionPath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
            lastSelectedRow = getRowForPath(tree, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
            setAnchorSelectionPath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
            setLeadSelectionPath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
        /* Otherwise set the selection to just this interval. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
        else if(SwingUtilities.isLeftMouseButton(event)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
            tree.setSelectionPath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
            if(isToggleEvent(event)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
                toggleExpandState(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
     * @return true if the node at <code>row</code> is a leaf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
    protected boolean isLeaf(int row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
        TreePath          path = getPathForRow(tree, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
        if(path != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
            return treeModel.isLeaf(path.getLastPathComponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
        // Have to return something here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
    // The following selection methods (lead/anchor) are covers for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
    // methods in JTree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
    private void setAnchorSelectionPath(TreePath newPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
        ignoreLAChange = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
            tree.setAnchorSelectionPath(newPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
        } finally{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
            ignoreLAChange = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
    private TreePath getAnchorSelectionPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
        return tree.getAnchorSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
    private void setLeadSelectionPath(TreePath newPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
        setLeadSelectionPath(newPath, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
    private void setLeadSelectionPath(TreePath newPath, boolean repaint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
        Rectangle       bounds = repaint ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
                            getPathBounds(tree, getLeadSelectionPath()) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
        ignoreLAChange = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
            tree.setLeadSelectionPath(newPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
            ignoreLAChange = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
        leadRow = getRowForPath(tree, newPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  2445
        if (repaint) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  2446
            if (bounds != null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  2447
                tree.repaint(getRepaintPathBounds(bounds));
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  2448
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
            bounds = getPathBounds(tree, newPath);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  2450
            if (bounds != null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  2451
                tree.repaint(getRepaintPathBounds(bounds));
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  2452
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  2456
    private Rectangle getRepaintPathBounds(Rectangle bounds) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  2457
        if (UIManager.getBoolean("Tree.repaintWholeRow")) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  2458
           bounds.x = 0;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  2459
           bounds.width = tree.getWidth();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  2460
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  2461
        return bounds;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  2462
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 1639
diff changeset
  2463
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
    private TreePath getLeadSelectionPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
        return tree.getLeadSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  2468
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  2469
     * Updates the lead row of the selection.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  2470
     * @since 1.7
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  2471
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  2472
    protected void updateLeadSelectionRow() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
        leadRow = getRowForPath(tree, getLeadSelectionPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  2476
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  2477
     * Returns the lead row of the selection.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  2478
     *
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  2479
     * @return selection lead row
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  2480
     * @since 1.7
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  2481
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  2482
    protected int getLeadSelectionRow() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
        return leadRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
     * Extends the selection from the anchor to make <code>newLead</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
     * the lead of the selection. This does not scroll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
    private void extendSelection(TreePath newLead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
        TreePath           aPath = getAnchorSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
        int                aRow = (aPath == null) ? -1 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
                                  getRowForPath(tree, aPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
        int                newIndex = getRowForPath(tree, newLead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
        if(aRow == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
            tree.setSelectionRow(newIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
            if(aRow < newIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
                tree.setSelectionInterval(aRow, newIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
                tree.setSelectionInterval(newIndex, aRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
            setAnchorSelectionPath(aPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
            setLeadSelectionPath(newLead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
     * Invokes <code>repaint</code> on the JTree for the passed in TreePath,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
     * <code>path</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
    private void repaintPath(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
        if (path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
            Rectangle bounds = getPathBounds(tree, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
            if (bounds != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
                tree.repaint(bounds.x, bounds.y, bounds.width, bounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
     * Updates the TreeState in response to nodes expanding/collapsing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
    public class TreeExpansionHandler implements TreeExpansionListener {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  2528
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
         * Called whenever an item in the tree has been expanded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
        public void treeExpanded(TreeExpansionEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
            getHandler().treeExpanded(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
         * Called whenever an item in the tree has been collapsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
        public void treeCollapsed(TreeExpansionEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
            getHandler().treeCollapsed(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
    } // BasicTreeUI.TreeExpansionHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
     * Updates the preferred size when scrolling (if necessary).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
    public class ComponentHandler extends ComponentAdapter implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
                 ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
        /** Timer used when inside a scrollpane and the scrollbar is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
         * adjusting. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
        protected Timer                timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
        /** ScrollBar that is being adjusted. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
        protected JScrollBar           scrollBar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
        public void componentMoved(ComponentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
            if(timer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
                JScrollPane   scrollPane = getScrollPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
                if(scrollPane == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
                    updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
                    scrollBar = scrollPane.getVerticalScrollBar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
                    if(scrollBar == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
                        !scrollBar.getValueIsAdjusting()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
                        // Try the horizontal scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
                        if((scrollBar = scrollPane.getHorizontalScrollBar())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
                            != null && scrollBar.getValueIsAdjusting())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
                            startTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
                            updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
                        startTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
         * Creates, if necessary, and starts a Timer to check if need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
         * resize the bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
        protected void startTimer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
            if(timer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
                timer = new Timer(200, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
                timer.setRepeats(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
            timer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
         * Returns the JScrollPane housing the JTree, or null if one isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
         * found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
        protected JScrollPane getScrollPane() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
            Component       c = tree.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
            while(c != null && !(c instanceof JScrollPane))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
                c = c.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
            if(c instanceof JScrollPane)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
                return (JScrollPane)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
         * Public as a result of Timer. If the scrollBar is null, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
         * not adjusting, this stops the timer and updates the sizing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
        public void actionPerformed(ActionEvent ae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
            if(scrollBar == null || !scrollBar.getValueIsAdjusting()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
                if(timer != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
                    timer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
                updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
                timer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
                scrollBar = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
    } // End of BasicTreeUI.ComponentHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
     * Forwards all TreeModel events to the TreeState.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
    public class TreeModelHandler implements TreeModelListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  2630
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
        public void treeNodesChanged(TreeModelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
            getHandler().treeNodesChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
        public void treeNodesInserted(TreeModelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
            getHandler().treeNodesInserted(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
        public void treeNodesRemoved(TreeModelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
            getHandler().treeNodesRemoved(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
        public void treeStructureChanged(TreeModelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
            getHandler().treeStructureChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
    } // End of BasicTreeUI.TreeModelHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
     * Listens for changes in the selection model and updates the display
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
     * accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
    public class TreeSelectionHandler implements TreeSelectionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  2659
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
         * Messaged when the selection changes in the tree we're displaying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
         * for.  Stops editing, messages super and displays the changed paths.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
        public void valueChanged(TreeSelectionEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
            getHandler().valueChanged(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
    }// End of BasicTreeUI.TreeSelectionHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
     * Listener responsible for getting cell editing events and updating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
     * the tree accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
    public class CellEditorHandler implements CellEditorListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  2680
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
        /** Messaged when editing has stopped in the tree. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
        public void editingStopped(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
            getHandler().editingStopped(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
        /** Messaged when editing has been canceled in the tree. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
        public void editingCanceled(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
            getHandler().editingCanceled(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
    } // BasicTreeUI.CellEditorHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  2698
     * This is used to get multiple key down events to appropriately generate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
     * events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
    public class KeyHandler extends KeyAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  2703
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
        // Also note these fields aren't use anymore, nor does Handler have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
        // the old functionality. This behavior worked around an old bug
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
        // in JComponent that has long since been fixed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
        /** Key code that is being generated for. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
        protected Action              repeatKeyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
        /** Set to true while keyPressed is active. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
        protected boolean            isKeyDown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
         * Invoked when a key has been typed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
         * Moves the keyboard focus to the first element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
         * whose first letter matches the alphanumeric key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
         * pressed by the user. Subsequent same key presses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
         * move the keyboard focus to the next object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
         * starts with the same letter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
        public void keyTyped(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
            getHandler().keyTyped(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
        public void keyPressed(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
            getHandler().keyPressed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
        public void keyReleased(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
            getHandler().keyReleased(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
    } // End of BasicTreeUI.KeyHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
     * Repaints the lead selection row when focus is lost/gained.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
    public class FocusHandler implements FocusListener {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  2745
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
         * Invoked when focus is activated on the tree we're in, redraws the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
         * lead row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
        public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
            getHandler().focusGained(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
         * Invoked when focus is activated on the tree we're in, redraws the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
         * lead row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
        public void focusLost(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
            getHandler().focusLost(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
    } // End of class BasicTreeUI.FocusHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
     * Class responsible for getting size of node, method is forwarded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
     * to BasicTreeUI method. X location does not include insets, that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
     * handled in getPathBounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
    // This returns locations that don't include any Insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
    public class NodeDimensionsHandler extends
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
                 AbstractLayoutCache.NodeDimensions {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
         * Responsible for getting the size of a particular node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
        public Rectangle getNodeDimensions(Object value, int row,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
                                           int depth, boolean expanded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
                                           Rectangle size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
            // Return size of editing component, if editing and asking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
            // for editing row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
            if(editingComponent != null && editingRow == row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
                Dimension        prefSize = editingComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
                                              getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
                int              rh = getRowHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
                if(rh > 0 && rh != prefSize.height)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
                    prefSize.height = rh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
                if(size != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
                    size.x = getRowX(row, depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
                    size.width = prefSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
                    size.height = prefSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
                    size = new Rectangle(getRowX(row, depth), 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
                                         prefSize.width, prefSize.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
                return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
            // Not editing, use renderer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
            if(currentCellRenderer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
                Component          aComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
                aComponent = currentCellRenderer.getTreeCellRendererComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
                    (tree, value, tree.isRowSelected(row),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
                     expanded, treeModel.isLeaf(value), row,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
                     false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
                if(tree != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
                    // Only ever removed when UI changes, this is OK!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
                    rendererPane.add(aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
                    aComponent.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
                Dimension        prefSize = aComponent.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
                if(size != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
                    size.x = getRowX(row, depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
                    size.width = prefSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
                    size.height = prefSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
                    size = new Rectangle(getRowX(row, depth), 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
                                         prefSize.width, prefSize.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
                return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
         * @return amount to indent the given row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
        protected int getRowX(int row, int depth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
            return BasicTreeUI.this.getRowX(row, depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
    } // End of class BasicTreeUI.NodeDimensionsHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
     * TreeMouseListener is responsible for updating the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
     * based on mouse events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
    public class MouseHandler extends MouseAdapter implements MouseMotionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
 {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  2847
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
         * Invoked when a mouse button has been pressed on a component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
            getHandler().mousePressed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
        public void mouseDragged(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
            getHandler().mouseDragged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
         * Invoked when the mouse button has been moved on a component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
         * (with no buttons no down).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
        public void mouseMoved(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
            getHandler().mouseMoved(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
        public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
            getHandler().mouseReleased(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
    } // End of BasicTreeUI.MouseHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
     * PropertyChangeListener for the tree. Updates the appropriate
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  2880
     * variable, or TreeState, based on what changes.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
    public class PropertyChangeHandler implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
                       PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  2885
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
        public void propertyChange(PropertyChangeEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
            getHandler().propertyChange(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
    } // End of BasicTreeUI.PropertyChangeHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
     * Listener on the TreeSelectionModel, resets the row selection if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
     * any of the properties of the model change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
    public class SelectionModelPropertyChangeHandler implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
                      PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  2903
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
        public void propertyChange(PropertyChangeEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
            getHandler().propertyChange(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
    } // End of BasicTreeUI.SelectionModelPropertyChangeHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
     * <code>TreeTraverseAction</code> is the action used for left/right keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
     * Will toggle the expandedness of a node, as well as potentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
     * incrementing the selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
    public class TreeTraverseAction extends AbstractAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
        /** Determines direction to traverse, 1 means expand, -1 means
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
          * collapse. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
        protected int direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
        /** True if the selection is reset, false means only the lead path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
         * changes. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
        private boolean changeSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
        public TreeTraverseAction(int direction, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
            this(direction, name, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
        private TreeTraverseAction(int direction, String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
                                   boolean changeSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
            this.direction = direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
            this.changeSelection = changeSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
            if (tree != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
                SHARED_ACTION.traverse(tree, BasicTreeUI.this, direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
                                       changeSelection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
        public boolean isEnabled() { return (tree != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
                                             tree.isEnabled()); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
    } // BasicTreeUI.TreeTraverseAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
    /** TreePageAction handles page up and page down events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
    public class TreePageAction extends AbstractAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
        /** Specifies the direction to adjust the selection by. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
        protected int         direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
        /** True indicates should set selection from anchor path. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
        private boolean       addToSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
        private boolean       changeSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
        public TreePageAction(int direction, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
            this(direction, name, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
        private TreePageAction(int direction, String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
                               boolean addToSelection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
                               boolean changeSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
            this.direction = direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
            this.addToSelection = addToSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
            this.changeSelection = changeSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
            if (tree != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
                SHARED_ACTION.page(tree, BasicTreeUI.this, direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
                                   addToSelection, changeSelection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
        public boolean isEnabled() { return (tree != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
                                             tree.isEnabled()); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
    } // BasicTreeUI.TreePageAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
    /** TreeIncrementAction is used to handle up/down actions.  Selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
      * is moved up or down based on direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
    public class TreeIncrementAction extends AbstractAction  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
        /** Specifies the direction to adjust the selection by. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
        protected int         direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
        /** If true the new item is added to the selection, if false the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
         * selection is reset. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
        private boolean       addToSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
        private boolean       changeSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
        public TreeIncrementAction(int direction, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
            this(direction, name, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
        private TreeIncrementAction(int direction, String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
                                   boolean addToSelection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
                                    boolean changeSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
            this.direction = direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
            this.addToSelection = addToSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
            this.changeSelection = changeSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
            if (tree != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
                SHARED_ACTION.increment(tree, BasicTreeUI.this, direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
                                        addToSelection, changeSelection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
        public boolean isEnabled() { return (tree != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
                                             tree.isEnabled()); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
    } // End of class BasicTreeUI.TreeIncrementAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
      * TreeHomeAction is used to handle end/home actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
      * Scrolls either the first or last cell to be visible based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
      * direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
    public class TreeHomeAction extends AbstractAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
        protected int            direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
        /** Set to true if append to selection. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
        private boolean          addToSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
        private boolean          changeSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
        public TreeHomeAction(int direction, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
            this(direction, name, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
        private TreeHomeAction(int direction, String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
                               boolean addToSelection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
                               boolean changeSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
            this.direction = direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
            this.changeSelection = changeSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
            this.addToSelection = addToSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
            if (tree != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
                SHARED_ACTION.home(tree, BasicTreeUI.this, direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
                                   addToSelection, changeSelection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
        public boolean isEnabled() { return (tree != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
                                             tree.isEnabled()); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
    } // End of class BasicTreeUI.TreeHomeAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
      * For the first selected row expandedness will be toggled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
    public class TreeToggleAction extends AbstractAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
        public TreeToggleAction(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
            if(tree != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
                SHARED_ACTION.toggle(tree, BasicTreeUI.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
        public boolean isEnabled() { return (tree != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
                                             tree.isEnabled()); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
    } // End of class BasicTreeUI.TreeToggleAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
     * ActionListener that invokes cancelEditing when action performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
    public class TreeCancelEditingAction extends AbstractAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
        public TreeCancelEditingAction(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
            if(tree != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
                SHARED_ACTION.cancelEditing(tree, BasicTreeUI.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
        public boolean isEnabled() { return (tree != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
                                             tree.isEnabled() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
                                             isEditing(tree)); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
    } // End of class BasicTreeUI.TreeCancelEditingAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
      * MouseInputHandler handles passing all mouse events,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
      * including mouse motion events, until the mouse is released to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
      * the destination it is constructed with. It is assumed all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
      * events are currently target at source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
    public class MouseInputHandler extends Object implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
                     MouseInputListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
        /** Source that events are coming from. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
        protected Component        source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
        /** Destination that receives all events. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
        protected Component        destination;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
        private Component          focusComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
        private boolean            dispatchedEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
        public MouseInputHandler(Component source, Component destination,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
                                      MouseEvent event){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
            this(source, destination, event, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
        MouseInputHandler(Component source, Component destination,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
                          MouseEvent event, Component focusComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
            this.source = source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
            this.destination = destination;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
            this.source.addMouseListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
            this.source.addMouseMotionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
            SwingUtilities2.setSkipClickCount(destination,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
                                              event.getClickCount() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
            /* Dispatch the editing event! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
            destination.dispatchEvent(SwingUtilities.convertMouseEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
                                          (source, event, destination));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
            this.focusComponent = focusComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
        public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
            if(destination != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
                dispatchedEvent = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
                destination.dispatchEvent(SwingUtilities.convertMouseEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
                                          (source, e, destination));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
        public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
            if(destination != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
                destination.dispatchEvent(SwingUtilities.convertMouseEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
                                          (source, e, destination));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
            removeFromSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
        public void mouseEntered(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
            if (!SwingUtilities.isLeftMouseButton(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
                removeFromSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
        public void mouseExited(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
            if (!SwingUtilities.isLeftMouseButton(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
                removeFromSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
        public void mouseDragged(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
            if(destination != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
                dispatchedEvent = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
                destination.dispatchEvent(SwingUtilities.convertMouseEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
                                          (source, e, destination));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
        public void mouseMoved(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
            removeFromSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
        protected void removeFromSource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
            if(source != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
                source.removeMouseListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
                source.removeMouseMotionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
                if (focusComponent != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
                      focusComponent == destination && !dispatchedEvent &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
                      (focusComponent instanceof JTextField)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
                    ((JTextField)focusComponent).selectAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
            source = destination = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
    } // End of class BasicTreeUI.MouseInputHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
    private static final TransferHandler defaultTransferHandler = new TreeTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
  3188
    static class TreeTransferHandler extends TransferHandler implements UIResource, Comparator<TreePath> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
        private JTree tree;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
         * Create a Transferable to use as the source for a data transfer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
         * @param c  The component holding the data to be transfered.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
         *  argument is provided to enable sharing of TransferHandlers by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
         *  multiple components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
         * @return  The representation of the data to be transfered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
        protected Transferable createTransferable(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
            if (c instanceof JTree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
                tree = (JTree) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
                TreePath[] paths = tree.getSelectionPaths();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
                if (paths == null || paths.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
                StringBuffer plainBuf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
                StringBuffer htmlBuf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
                htmlBuf.append("<html>\n<body>\n<ul>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
                TreeModel model = tree.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
                TreePath lastPath = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
                TreePath[] displayPaths = getDisplayOrderPaths(paths);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
  3219
                for (TreePath path : displayPaths) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
                    Object node = path.getLastPathComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
                    boolean leaf = model.isLeaf(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
                    String label = getDisplayString(path, true, leaf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
                    plainBuf.append(label + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
                    htmlBuf.append("  <li>" + label + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
                // remove the last newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
                plainBuf.deleteCharAt(plainBuf.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
                htmlBuf.append("</ul>\n</body>\n</html>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
                tree = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
                return new BasicTransferable(plainBuf.toString(), htmlBuf.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
  3240
        public int compare(TreePath o1, TreePath o2) {
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
  3241
            int row1 = tree.getRowForPath(o1);
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
  3242
            int row2 = tree.getRowForPath(o2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
            return row1 - row2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
        String getDisplayString(TreePath path, boolean selected, boolean leaf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
            int row = tree.getRowForPath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
            boolean hasFocus = tree.getLeadSelectionRow() == row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
            Object node = path.getLastPathComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
            return tree.convertValueToText(node, selected, tree.isExpanded(row),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
                                           leaf, row, hasFocus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
         * Selection paths are in selection order.  The conversion to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
         * HTML requires display order.  This method resorts the paths
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
         * to be in the display order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
        TreePath[] getDisplayOrderPaths(TreePath[] paths) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
            // sort the paths to display order rather than selection order
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
  3261
            ArrayList<TreePath> selOrder = new ArrayList<TreePath>();
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
  3262
            for (TreePath path : paths) {
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
  3263
                selOrder.add(path);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
            Collections.sort(selOrder, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
            int n = selOrder.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
            TreePath[] displayPaths = new TreePath[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
            for (int i = 0; i < n; i++) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
  3269
                displayPaths[i] = selOrder.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
            return displayPaths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
        public int getSourceActions(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
            return COPY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
    private class Handler implements CellEditorListener, FocusListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
                  KeyListener, MouseListener, MouseMotionListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
                  PropertyChangeListener, TreeExpansionListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
                  TreeModelListener, TreeSelectionListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
                  BeforeDrag {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
        // KeyListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
        private String prefix = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
        private String typedString = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
        private long lastTime = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
         * Invoked when a key has been typed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
         * Moves the keyboard focus to the first element whose prefix matches the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
         * sequence of alphanumeric keys pressed by the user with delay less
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
         * than value of <code>timeFactor</code> property (or 1000 milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
         * if it is not defined). Subsequent same key presses move the keyboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
         * focus to the next object that starts with the same letter until another
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
         * key is pressed, then it is treated as the prefix with appropriate number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
         * of the same letters followed by first typed another letter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
        public void keyTyped(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
            // handle first letter navigation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
            if(tree != null && tree.getRowCount()>0 && tree.hasFocus() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
               tree.isEnabled()) {
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2658
diff changeset
  3308
                if (e.isAltDown() || BasicGraphicsUtils.isMenuShortcutKeyDown(e) ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
                    isNavigationKey(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
                boolean startingFromSelection = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
                char c = e.getKeyChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
                long time = e.getWhen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
                int startingRow = tree.getLeadSelectionRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
                if (time - lastTime < timeFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
                    typedString += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
                    if((prefix.length() == 1) && (c == prefix.charAt(0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
                        // Subsequent same key presses move the keyboard focus to the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
                        // object that starts with the same letter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
                        startingRow++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
                        prefix = typedString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
                    startingRow++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
                    typedString = "" + c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
                    prefix = typedString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
                lastTime = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
                if (startingRow < 0 || startingRow >= tree.getRowCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
                    startingFromSelection = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
                    startingRow = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
                TreePath path = tree.getNextMatch(prefix, startingRow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
                                                  Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
                if (path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
                    tree.setSelectionPath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
                    int row = getRowForPath(tree, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
                    ensureRowsAreVisible(row, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
                } else if (startingFromSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
                    path = tree.getNextMatch(prefix, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
                                             Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
                    if (path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
                        tree.setSelectionPath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
                        int row = getRowForPath(tree, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
                        ensureRowsAreVisible(row, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
         * Invoked when a key has been pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
         * Checks to see if the key event is a navigation key to prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
         * dispatching these keys for the first letter navigation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
        public void keyPressed(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
            if (tree != null && isNavigationKey(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
                prefix = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
                typedString = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
                lastTime = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
        public void keyReleased(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
         * Returns whether or not the supplied key event maps to a key that is used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
         * navigation.  This is used for optimizing key input by only passing non-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
         * navigation keys to the first letter navigation mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
        private boolean isNavigationKey(KeyEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
            InputMap inputMap = tree.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
            KeyStroke key = KeyStroke.getKeyStrokeForEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
  3382
            return inputMap != null && inputMap.get(key) != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
        // PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
        public void propertyChange(PropertyChangeEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
            if (event.getSource() == treeSelectionModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
                treeSelectionModel.resetRowSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
            else if(event.getSource() == tree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
                String              changeName = event.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
                if (changeName == JTree.LEAD_SELECTION_PATH_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
                    if (!ignoreLAChange) {
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  3398
                        updateLeadSelectionRow();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
                        repaintPath((TreePath)event.getOldValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
                        repaintPath((TreePath)event.getNewValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
                else if (changeName == JTree.ANCHOR_SELECTION_PATH_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
                    if (!ignoreLAChange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
                        repaintPath((TreePath)event.getOldValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
                        repaintPath((TreePath)event.getNewValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
                if(changeName == JTree.CELL_RENDERER_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
                    setCellRenderer((TreeCellRenderer)event.getNewValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
                    redoTheLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
                else if(changeName == JTree.TREE_MODEL_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
                    setModel((TreeModel)event.getNewValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
                else if(changeName == JTree.ROOT_VISIBLE_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
                    setRootVisible(((Boolean)event.getNewValue()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
                                   booleanValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
                else if(changeName == JTree.SHOWS_ROOT_HANDLES_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
                    setShowsRootHandles(((Boolean)event.getNewValue()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
                                        booleanValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
                else if(changeName == JTree.ROW_HEIGHT_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
                    setRowHeight(((Integer)event.getNewValue()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
                                 intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
                else if(changeName == JTree.CELL_EDITOR_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
                    setCellEditor((TreeCellEditor)event.getNewValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
                else if(changeName == JTree.EDITABLE_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
                    setEditable(((Boolean)event.getNewValue()).booleanValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
                else if(changeName == JTree.LARGE_MODEL_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
                    setLargeModel(tree.isLargeModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
                else if(changeName == JTree.SELECTION_MODEL_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
                    setSelectionModel(tree.getSelectionModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
                else if(changeName == "font") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
                    completeEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
                    if(treeState != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
                        treeState.invalidateSizes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
                    updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
                else if (changeName == "componentOrientation") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
                    if (tree != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
                        leftToRight = BasicGraphicsUtils.isLeftToRight(tree);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
                        redoTheLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
                        tree.treeDidChange();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
                        InputMap km = getInputMap(JComponent.WHEN_FOCUSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
                        SwingUtilities.replaceUIInputMap(tree,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
                                                JComponent.WHEN_FOCUSED, km);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
                } else if ("dropLocation" == changeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
                    JTree.DropLocation oldValue = (JTree.DropLocation)event.getOldValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
                    repaintDropLocation(oldValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
                    repaintDropLocation(tree.getDropLocation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
        private void repaintDropLocation(JTree.DropLocation loc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
            if (loc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
            Rectangle r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
            if (isDropLine(loc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
                r = getDropLineRect(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
                r = tree.getPathBounds(loc.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
            if (r != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
                tree.repaint(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
        // MouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
        // Whether or not the mouse press (which is being considered as part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
        // of a drag sequence) also caused the selection change to be fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
        // processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
        private boolean dragPressDidSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
        // Set to true when a drag gesture has been fully recognized and DnD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
        // begins. Use this to ignore further mouse events which could be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
        // delivered if DnD is cancelled (via ESCAPE for example)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
        private boolean dragStarted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
        // The path over which the press occurred and the press event itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
        private TreePath pressedPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
        private MouseEvent pressedEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
        // Used to detect whether the press event causes a selection change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
        // If it does, we won't try to start editing on the release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
        private boolean valueChangedOnPress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
        private boolean isActualPath(TreePath path, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
            if (path == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
            Rectangle bounds = getPathBounds(tree, path);
11499
f486e3ec76e6 6505523: NullPointerException in BasicTreeUI when a node is removed by expansion listener
alexsch
parents: 9035
diff changeset
  3510
            if (bounds == null || y > (bounds.y + bounds.height)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
            return (x >= bounds.x) && (x <= (bounds.x + bounds.width));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
        public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
        public void mouseEntered(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
        public void mouseExited(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
         * Invoked when a mouse button has been pressed on a component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
            if (SwingUtilities2.shouldIgnore(e, tree)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
            // if we can't stop any ongoing editing, do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
            if (isEditing(tree) && tree.getInvokesStopCellEditing()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
                                && !stopEditing(tree)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
            completeEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
            pressedPath = getClosestPathForLocation(tree, e.getX(), e.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
            if (tree.getDragEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
                mousePressedDND(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
                SwingUtilities2.adjustFocus(tree);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
                handleSelection(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
        private void mousePressedDND(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
            pressedEvent = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
            boolean grabFocus = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
            dragStarted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
            valueChangedOnPress = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
            // if we have a valid path and this is a drag initiating event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
            if (isActualPath(pressedPath, e.getX(), e.getY()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
                    DragRecognitionSupport.mousePressed(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
                dragPressDidSelection = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2658
diff changeset
  3564
                if (BasicGraphicsUtils.isMenuShortcutKeyDown(e)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
                    // do nothing for control - will be handled on release
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
                    // or when drag starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
                } else if (!e.isShiftDown() && tree.isPathSelected(pressedPath)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
                    // clicking on something that's already selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
                    // and need to make it the lead now
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
                    setAnchorSelectionPath(pressedPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
                    setLeadSelectionPath(pressedPath, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
                dragPressDidSelection = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
                // could be a drag initiating event - don't grab focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
                grabFocus = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
            if (grabFocus) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
                SwingUtilities2.adjustFocus(tree);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
            handleSelection(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
        void handleSelection(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
            if(pressedPath != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
                Rectangle bounds = getPathBounds(tree, pressedPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
14655
d524ae0f511e 6671481: NPE at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection
alexsch
parents: 11499
diff changeset
  3593
                if (bounds == null || e.getY() >= (bounds.y + bounds.height)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
                // Preferably checkForClickInExpandControl could take
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
                // the Event to do this it self!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
                if(SwingUtilities.isLeftMouseButton(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
                    checkForClickInExpandControl(pressedPath, e.getX(), e.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
                int x = e.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
                // Perhaps they clicked the cell itself. If so,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
                // select it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
                if (x >= bounds.x && x < (bounds.x + bounds.width)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
                    if (tree.getDragEnabled() || !startEditing(pressedPath, e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
                        selectPathForEvent(pressedPath, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
        public void dragStarting(MouseEvent me) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
            dragStarted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2658
diff changeset
  3618
            if (BasicGraphicsUtils.isMenuShortcutKeyDown(me)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3619
                tree.addSelectionPath(pressedPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
                setAnchorSelectionPath(pressedPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
                setLeadSelectionPath(pressedPath, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
            pressedEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
            pressedPath = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
        public void mouseDragged(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
            if (SwingUtilities2.shouldIgnore(e, tree)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
            if (tree.getDragEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
                DragRecognitionSupport.mouseDragged(e, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
         * Invoked when the mouse button has been moved on a component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
         * (with no buttons no down).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
        public void mouseMoved(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
        public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
            if (SwingUtilities2.shouldIgnore(e, tree)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
            if (tree.getDragEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
                mouseReleasedDND(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
            pressedEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3655
            pressedPath = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
        private void mouseReleasedDND(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
            MouseEvent me = DragRecognitionSupport.mouseReleased(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
            if (me != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
                SwingUtilities2.adjustFocus(tree);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
                if (!dragPressDidSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
                    handleSelection(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
            if (!dragStarted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
                // Note: We don't give the tree a chance to start editing if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
                // mouse press caused a selection change. Otherwise the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
                // tree cell editor will start editing on EVERY press and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
                // release. If it turns out that this affects some editors, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
                // can always parameterize this with a client property. ex:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
                // if (pressedPath != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
                //         (Boolean.TRUE == tree.getClientProperty("Tree.DnD.canEditOnValueChange") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
                //          !valueChangedOnPress) && ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
                if (pressedPath != null && !valueChangedOnPress &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
                        isActualPath(pressedPath, pressedEvent.getX(), pressedEvent.getY())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
                    startEditingOnRelease(pressedPath, pressedEvent, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3683
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3684
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3687
        // FocusListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
        public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
            if(tree != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
                Rectangle                 pBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
                pBounds = getPathBounds(tree, tree.getLeadSelectionPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
                if(pBounds != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
                    tree.repaint(getRepaintPathBounds(pBounds));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
                pBounds = getPathBounds(tree, getLeadSelectionPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
                if(pBounds != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
                    tree.repaint(getRepaintPathBounds(pBounds));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
        public void focusLost(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
            focusGained(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
        // CellEditorListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
        public void editingStopped(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
            completeEditing(false, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
        /** Messaged when editing has been canceled in the tree. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
        public void editingCanceled(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
            completeEditing(false, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
        // TreeSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
        public void valueChanged(TreeSelectionEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
            valueChangedOnPress = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
            // Stop editing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
            completeEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
            // Make sure all the paths are visible, if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
            // PENDING: This should be tweaked when isAdjusting is added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
            if(tree.getExpandsSelectedPaths() && treeSelectionModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
                TreePath[]           paths = treeSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
                                         .getSelectionPaths();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
                if(paths != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
                    for(int counter = paths.length - 1; counter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
                        counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
                        TreePath path = paths[counter].getParentPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
                        boolean expand = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
                        while (path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
                            // Indicates this path isn't valid anymore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3741
                            // we shouldn't attempt to expand it then.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
                            if (treeModel.isLeaf(path.getLastPathComponent())){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
                                expand = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
                                path = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
                            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
                                path = path.getParentPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
                        if (expand) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
                            tree.makeVisible(paths[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
            TreePath oldLead = getLeadSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
            lastSelectedRow = tree.getMinSelectionRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
            TreePath lead = tree.getSelectionModel().getLeadSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
            setAnchorSelectionPath(lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
            setLeadSelectionPath(lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3763
            TreePath[]       changedPaths = event.getPaths();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3764
            Rectangle        nodeBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3765
            Rectangle        visRect = tree.getVisibleRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
            boolean          paintPaths = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
            int              nWidth = tree.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
            if(changedPaths != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3770
                int              counter, maxCounter = changedPaths.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
                if(maxCounter > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3773
                    tree.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3774
                    paintPaths = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3775
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3776
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
                    for (counter = 0; counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
                        nodeBounds = getPathBounds(tree,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3779
                                                   changedPaths[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3780
                        if(nodeBounds != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3781
                           visRect.intersects(nodeBounds))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
                            tree.repaint(0, nodeBounds.y, nWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
                                         nodeBounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3786
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3787
            if(paintPaths) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3788
                nodeBounds = getPathBounds(tree, oldLead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3789
                if(nodeBounds != null && visRect.intersects(nodeBounds))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3790
                    tree.repaint(0, nodeBounds.y, nWidth, nodeBounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
                nodeBounds = getPathBounds(tree, lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
                if(nodeBounds != null && visRect.intersects(nodeBounds))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3793
                    tree.repaint(0, nodeBounds.y, nWidth, nodeBounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3794
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3798
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3799
        // TreeExpansionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3800
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3801
        public void treeExpanded(TreeExpansionEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3802
            if(event != null && tree != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3803
                TreePath      path = event.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3804
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3805
                updateExpandedDescendants(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
        public void treeCollapsed(TreeExpansionEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3810
            if(event != null && tree != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3811
                TreePath        path = event.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3813
                completeEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3814
                if(path != null && tree.isVisible(path)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3815
                    treeState.setExpandedState(path, false);
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  3816
                    updateLeadSelectionRow();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
                    updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3819
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
        // TreeModelListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
        public void treeNodesChanged(TreeModelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3826
            if(treeState != null && e != null) {
18133
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15333
diff changeset
  3827
                TreePath parentPath = SwingUtilities2.getTreePath(e, getModel());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
                int[] indices = e.getChildIndices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3829
                if (indices == null || indices.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3830
                    // The root has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3831
                    treeState.treeNodesChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3832
                    updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3833
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
                else if (treeState.isExpanded(parentPath)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3835
                    // Changed nodes are visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3836
                    // Find the minimum index, we only need paint from there
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3837
                    // down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3838
                    int minIndex = indices[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3839
                    for (int i = indices.length - 1; i > 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3840
                        minIndex = Math.min(indices[i], minIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
                    Object minChild = treeModel.getChild(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
                            parentPath.getLastPathComponent(), minIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
                    TreePath minPath = parentPath.pathByAddingChild(minChild);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
                    Rectangle minBounds = getPathBounds(tree, minPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
                    // Forward to the treestate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
                    treeState.treeNodesChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3849
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3850
                    // Mark preferred size as bogus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3851
                    updateSize0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3852
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3853
                    // And repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3854
                    Rectangle newMinBounds = getPathBounds(tree, minPath);
14655
d524ae0f511e 6671481: NPE at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection
alexsch
parents: 11499
diff changeset
  3855
                    if (minBounds == null || newMinBounds == null) {
d524ae0f511e 6671481: NPE at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection
alexsch
parents: 11499
diff changeset
  3856
                        return;
d524ae0f511e 6671481: NPE at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection
alexsch
parents: 11499
diff changeset
  3857
                    }
d524ae0f511e 6671481: NPE at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection
alexsch
parents: 11499
diff changeset
  3858
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3859
                    if (indices.length == 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3860
                            newMinBounds.height == minBounds.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3861
                        tree.repaint(0, minBounds.y, tree.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3862
                                     minBounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3863
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3864
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3865
                        tree.repaint(0, minBounds.y, tree.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3866
                                     tree.getHeight() - minBounds.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3867
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3868
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3869
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3870
                    // Nodes that changed aren't visible.  No need to paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3871
                    treeState.treeNodesChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3872
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3873
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3876
        public void treeNodesInserted(TreeModelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3877
            if(treeState != null && e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3878
                treeState.treeNodesInserted(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3879
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  3880
                updateLeadSelectionRow();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3881
18133
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15333
diff changeset
  3882
                TreePath       path = SwingUtilities2.getTreePath(e, getModel());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3883
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3884
                if(treeState.isExpanded(path)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3885
                    updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3886
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3887
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3888
                    // PENDING(sky): Need a method in TreeModelEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3889
                    // that can return the count, getChildIndices allocs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3890
                    // a new array!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3891
                    int[]      indices = e.getChildIndices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3892
                    int        childCount = treeModel.getChildCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3893
                                            (path.getLastPathComponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3894
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3895
                    if(indices != null && (childCount - indices.length) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3896
                        updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3897
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3898
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3899
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3900
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3901
        public void treeNodesRemoved(TreeModelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3902
            if(treeState != null && e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3903
                treeState.treeNodesRemoved(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3904
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  3905
                updateLeadSelectionRow();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3906
18133
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15333
diff changeset
  3907
                TreePath       path = SwingUtilities2.getTreePath(e, getModel());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3909
                if(treeState.isExpanded(path) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3910
                   treeModel.getChildCount(path.getLastPathComponent()) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3911
                    updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3912
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3914
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3915
        public void treeStructureChanged(TreeModelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3916
            if(treeState != null && e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3917
                treeState.treeStructureChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3918
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
  3919
                updateLeadSelectionRow();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3920
18133
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15333
diff changeset
  3921
                TreePath       pPath = SwingUtilities2.getTreePath(e, getModel());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3922
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3923
                if (pPath != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3924
                    pPath = pPath.getParentPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3925
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3926
                if(pPath == null || treeState.isExpanded(pPath))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3927
                    updateSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3928
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3929
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3930
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3931
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3933
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3934
    private static class Actions extends UIAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3935
        private static final String SELECT_PREVIOUS = "selectPrevious";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3936
        private static final String SELECT_PREVIOUS_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3937
                             "selectPreviousChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3938
        private static final String SELECT_PREVIOUS_EXTEND_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3939
                             "selectPreviousExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3940
        private static final String SELECT_NEXT = "selectNext";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3941
        private static final String SELECT_NEXT_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3942
                                    "selectNextChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3943
        private static final String SELECT_NEXT_EXTEND_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3944
                                    "selectNextExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3945
        private static final String SELECT_CHILD = "selectChild";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3946
        private static final String SELECT_CHILD_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3947
                                    "selectChildChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3948
        private static final String SELECT_PARENT = "selectParent";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3949
        private static final String SELECT_PARENT_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3950
                                    "selectParentChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3951
        private static final String SCROLL_UP_CHANGE_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3952
                                    "scrollUpChangeSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3953
        private static final String SCROLL_UP_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3954
                                    "scrollUpChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3955
        private static final String SCROLL_UP_EXTEND_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3956
                                    "scrollUpExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3957
        private static final String SCROLL_DOWN_CHANGE_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3958
                                    "scrollDownChangeSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3959
        private static final String SCROLL_DOWN_EXTEND_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3960
                                    "scrollDownExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3961
        private static final String SCROLL_DOWN_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3962
                                    "scrollDownChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3963
        private static final String SELECT_FIRST = "selectFirst";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3964
        private static final String SELECT_FIRST_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3965
                                    "selectFirstChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3966
        private static final String SELECT_FIRST_EXTEND_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3967
                                    "selectFirstExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3968
        private static final String SELECT_LAST = "selectLast";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3969
        private static final String SELECT_LAST_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3970
                                    "selectLastChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3971
        private static final String SELECT_LAST_EXTEND_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3972
                                    "selectLastExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3973
        private static final String TOGGLE = "toggle";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3974
        private static final String CANCEL_EDITING = "cancel";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3975
        private static final String START_EDITING = "startEditing";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3976
        private static final String SELECT_ALL = "selectAll";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3977
        private static final String CLEAR_SELECTION = "clearSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3978
        private static final String SCROLL_LEFT = "scrollLeft";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3979
        private static final String SCROLL_RIGHT = "scrollRight";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3980
        private static final String SCROLL_LEFT_EXTEND_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3981
                                    "scrollLeftExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3982
        private static final String SCROLL_RIGHT_EXTEND_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3983
                                    "scrollRightExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3984
        private static final String SCROLL_RIGHT_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3985
                                    "scrollRightChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3986
        private static final String SCROLL_LEFT_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3987
                                    "scrollLeftChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3988
        private static final String EXPAND = "expand";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3989
        private static final String COLLAPSE = "collapse";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3990
        private static final String MOVE_SELECTION_TO_PARENT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3991
                                    "moveSelectionToParent";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3993
        // add the lead item to the selection without changing lead or anchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3994
        private static final String ADD_TO_SELECTION = "addToSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3995
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3996
        // toggle the selected state of the lead item and move the anchor to it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3997
        private static final String TOGGLE_AND_ANCHOR = "toggleAndAnchor";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3998
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3999
        // extend the selection to the lead item
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4000
        private static final String EXTEND_TO = "extendTo";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4002
        // move the anchor to the lead and ensure only that item is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4003
        private static final String MOVE_SELECTION_TO = "moveSelectionTo";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4005
        Actions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4006
            super(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4007
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4009
        Actions(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4010
            super(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4011
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4013
        public boolean isEnabled(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4014
            if (o instanceof JTree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4015
                if (getName() == CANCEL_EDITING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4016
                    return ((JTree)o).isEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4017
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4018
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4019
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4020
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4022
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4023
            JTree tree = (JTree)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4024
            BasicTreeUI ui = (BasicTreeUI)BasicLookAndFeel.getUIOfType(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4025
                             tree.getUI(), BasicTreeUI.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4026
            if (ui == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4027
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4028
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4029
            String key = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4030
            if (key == SELECT_PREVIOUS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4031
                increment(tree, ui, -1, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4032
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4033
            else if (key == SELECT_PREVIOUS_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4034
                increment(tree, ui, -1, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4035
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4036
            else if (key == SELECT_PREVIOUS_EXTEND_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4037
                increment(tree, ui, -1, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4038
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4039
            else if (key == SELECT_NEXT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4040
                increment(tree, ui, 1, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4041
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4042
            else if (key == SELECT_NEXT_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4043
                increment(tree, ui, 1, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4044
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4045
            else if (key == SELECT_NEXT_EXTEND_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4046
                increment(tree, ui, 1, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4047
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4048
            else if (key == SELECT_CHILD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4049
                traverse(tree, ui, 1, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4050
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4051
            else if (key == SELECT_CHILD_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4052
                traverse(tree, ui, 1, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4053
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4054
            else if (key == SELECT_PARENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4055
                traverse(tree, ui, -1, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4056
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4057
            else if (key == SELECT_PARENT_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4058
                traverse(tree, ui, -1, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4059
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4060
            else if (key == SCROLL_UP_CHANGE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4061
                page(tree, ui, -1, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4062
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4063
            else if (key == SCROLL_UP_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4064
                page(tree, ui, -1, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4065
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4066
            else if (key == SCROLL_UP_EXTEND_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4067
                page(tree, ui, -1, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4068
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4069
            else if (key == SCROLL_DOWN_CHANGE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4070
                page(tree, ui, 1, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4071
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4072
            else if (key == SCROLL_DOWN_EXTEND_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4073
                page(tree, ui, 1, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4074
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4075
            else if (key == SCROLL_DOWN_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4076
                page(tree, ui, 1, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4077
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4078
            else if (key == SELECT_FIRST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4079
                home(tree, ui, -1, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4080
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4081
            else if (key == SELECT_FIRST_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4082
                home(tree, ui, -1, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4083
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4084
            else if (key == SELECT_FIRST_EXTEND_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4085
                home(tree, ui, -1, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4086
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4087
            else if (key == SELECT_LAST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4088
                home(tree, ui, 1, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4089
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4090
            else if (key == SELECT_LAST_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4091
                home(tree, ui, 1, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4092
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4093
            else if (key == SELECT_LAST_EXTEND_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4094
                home(tree, ui, 1, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4095
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4096
            else if (key == TOGGLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4097
                toggle(tree, ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4098
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4099
            else if (key == CANCEL_EDITING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4100
                cancelEditing(tree, ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4102
            else if (key == START_EDITING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4103
                startEditing(tree, ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4105
            else if (key == SELECT_ALL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4106
                selectAll(tree, ui, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4108
            else if (key == CLEAR_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4109
                selectAll(tree, ui, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4111
            else if (key == ADD_TO_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4112
                if (ui.getRowCount(tree) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4113
                    int lead = ui.getLeadSelectionRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4114
                    if (!tree.isRowSelected(lead)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4115
                        TreePath aPath = ui.getAnchorSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4116
                        tree.addSelectionRow(lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4117
                        ui.setAnchorSelectionPath(aPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4118
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4119
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4121
            else if (key == TOGGLE_AND_ANCHOR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4122
                if (ui.getRowCount(tree) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4123
                    int lead = ui.getLeadSelectionRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4124
                    TreePath lPath = ui.getLeadSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4125
                    if (!tree.isRowSelected(lead)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4126
                        tree.addSelectionRow(lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4127
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4128
                        tree.removeSelectionRow(lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4129
                        ui.setLeadSelectionPath(lPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4130
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4131
                    ui.setAnchorSelectionPath(lPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4132
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4134
            else if (key == EXTEND_TO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4135
                extendSelection(tree, ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4137
            else if (key == MOVE_SELECTION_TO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4138
                if (ui.getRowCount(tree) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4139
                    int lead = ui.getLeadSelectionRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4140
                    tree.setSelectionInterval(lead, lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4141
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4143
            else if (key == SCROLL_LEFT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4144
                scroll(tree, ui, SwingConstants.HORIZONTAL, -10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4146
            else if (key == SCROLL_RIGHT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4147
                scroll(tree, ui, SwingConstants.HORIZONTAL, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4149
            else if (key == SCROLL_LEFT_EXTEND_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4150
                scrollChangeSelection(tree, ui, -1, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4152
            else if (key == SCROLL_RIGHT_EXTEND_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4153
                scrollChangeSelection(tree, ui, 1, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4155
            else if (key == SCROLL_RIGHT_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4156
                scrollChangeSelection(tree, ui, 1, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4158
            else if (key == SCROLL_LEFT_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4159
                scrollChangeSelection(tree, ui, -1, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4161
            else if (key == EXPAND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4162
                expand(tree, ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4164
            else if (key == COLLAPSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4165
                collapse(tree, ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4167
            else if (key == MOVE_SELECTION_TO_PARENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4168
                moveSelectionToParent(tree, ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4172
        private void scrollChangeSelection(JTree tree, BasicTreeUI ui,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4173
                           int direction, boolean addToSelection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4174
                           boolean changeSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4175
            int           rowCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4177
            if((rowCount = ui.getRowCount(tree)) > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4178
                ui.treeSelectionModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4179
                TreePath          newPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4180
                Rectangle         visRect = tree.getVisibleRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4182
                if (direction == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4183
                    newPath = ui.getClosestPathForLocation(tree, visRect.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4184
                                                        visRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4185
                    visRect.x = Math.max(0, visRect.x - visRect.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4186
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4187
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4188
                    visRect.x = Math.min(Math.max(0, tree.getWidth() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4189
                                   visRect.width), visRect.x + visRect.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4190
                    newPath = ui.getClosestPathForLocation(tree, visRect.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4191
                                                 visRect.y + visRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4193
                // Scroll
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4194
                tree.scrollRectToVisible(visRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4195
                // select
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4196
                if (addToSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4197
                    ui.extendSelection(newPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4198
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4199
                else if(changeSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4200
                    tree.setSelectionPath(newPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4202
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4203
                    ui.setLeadSelectionPath(newPath, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4208
        private void scroll(JTree component, BasicTreeUI ui, int direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4209
                            int amount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4210
            Rectangle visRect = component.getVisibleRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4211
            Dimension size = component.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4212
            if (direction == SwingConstants.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4213
                visRect.x += amount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4214
                visRect.x = Math.max(0, visRect.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4215
                visRect.x = Math.min(Math.max(0, size.width - visRect.width),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4216
                                     visRect.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4218
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4219
                visRect.y += amount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4220
                visRect.y = Math.max(0, visRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4221
                visRect.y = Math.min(Math.max(0, size.width - visRect.height),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4222
                                     visRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4224
            component.scrollRectToVisible(visRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4227
        private void extendSelection(JTree tree, BasicTreeUI ui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4228
            if (ui.getRowCount(tree) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4229
                int       lead = ui.getLeadSelectionRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4231
                if (lead != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4232
                    TreePath      leadP = ui.getLeadSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4233
                    TreePath      aPath = ui.getAnchorSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4234
                    int           aRow = ui.getRowForPath(tree, aPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4236
                    if(aRow == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4237
                        aRow = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4238
                    tree.setSelectionInterval(aRow, lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4239
                    ui.setLeadSelectionPath(leadP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4240
                    ui.setAnchorSelectionPath(aPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4241
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4245
        private void selectAll(JTree tree, BasicTreeUI ui, boolean selectAll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4246
            int                   rowCount = ui.getRowCount(tree);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4248
            if(rowCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4249
                if(selectAll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4250
                    if (tree.getSelectionModel().getSelectionMode() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4251
                            TreeSelectionModel.SINGLE_TREE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4253
                        int lead = ui.getLeadSelectionRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4254
                        if (lead != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4255
                            tree.setSelectionRow(lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4256
                        } else if (tree.getMinSelectionRow() == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4257
                            tree.setSelectionRow(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4258
                            ui.ensureRowsAreVisible(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4259
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4260
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4261
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4263
                    TreePath      lastPath = ui.getLeadSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4264
                    TreePath      aPath = ui.getAnchorSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4266
                    if(lastPath != null && !tree.isVisible(lastPath)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4267
                        lastPath = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4268
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4269
                    tree.setSelectionInterval(0, rowCount - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4270
                    if(lastPath != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4271
                        ui.setLeadSelectionPath(lastPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4272
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4273
                    if(aPath != null && tree.isVisible(aPath)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4274
                        ui.setAnchorSelectionPath(aPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4275
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4276
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4277
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4278
                    TreePath      lastPath = ui.getLeadSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4279
                    TreePath      aPath = ui.getAnchorSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4281
                    tree.clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4282
                    ui.setAnchorSelectionPath(aPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4283
                    ui.setLeadSelectionPath(lastPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4284
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4288
        private void startEditing(JTree tree, BasicTreeUI ui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4289
            TreePath   lead = ui.getLeadSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4290
            int        editRow = (lead != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4291
                                     ui.getRowForPath(tree, lead) : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4293
            if(editRow != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4294
                tree.startEditingAtPath(lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4297
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4298
        private void cancelEditing(JTree tree, BasicTreeUI ui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4299
            tree.cancelEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4302
        private void toggle(JTree tree, BasicTreeUI ui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4303
            int            selRow = ui.getLeadSelectionRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4305
            if(selRow != -1 && !ui.isLeaf(selRow)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4306
                TreePath aPath = ui.getAnchorSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4307
                TreePath lPath = ui.getLeadSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4309
                ui.toggleExpandState(ui.getPathForRow(tree, selRow));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4310
                ui.setAnchorSelectionPath(aPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4311
                ui.setLeadSelectionPath(lPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4315
        private void expand(JTree tree, BasicTreeUI ui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4316
            int selRow = ui.getLeadSelectionRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4317
            tree.expandRow(selRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4320
        private void collapse(JTree tree, BasicTreeUI ui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4321
            int selRow = ui.getLeadSelectionRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4322
            tree.collapseRow(selRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4325
        private void increment(JTree tree, BasicTreeUI ui, int direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4326
                               boolean addToSelection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4327
                               boolean changeSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4329
            // disable moving of lead unless in discontiguous mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4330
            if (!addToSelection && !changeSelection &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4331
                    tree.getSelectionModel().getSelectionMode() !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4332
                        TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4333
                changeSelection = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4336
            int              rowCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4338
            if(ui.treeSelectionModel != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4339
                  (rowCount = tree.getRowCount()) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4340
                int                  selIndex = ui.getLeadSelectionRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4341
                int                  newIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4343
                if(selIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4344
                    if(direction == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4345
                        newIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4346
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4347
                        newIndex = rowCount - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4348
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4349
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4350
                    /* Aparently people don't like wrapping;( */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4351
                    newIndex = Math.min(rowCount - 1, Math.max
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4352
                                        (0, (selIndex + direction)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4353
                if(addToSelection && ui.treeSelectionModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4354
                        getSelectionMode() != TreeSelectionModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4355
                        SINGLE_TREE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4356
                    ui.extendSelection(tree.getPathForRow(newIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4358
                else if(changeSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4359
                    tree.setSelectionInterval(newIndex, newIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4360
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4361
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4362
                    ui.setLeadSelectionPath(tree.getPathForRow(newIndex),true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4363
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4364
                ui.ensureRowsAreVisible(newIndex, newIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4365
                ui.lastSelectedRow = newIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4369
        private void traverse(JTree tree, BasicTreeUI ui, int direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4370
                              boolean changeSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4372
            // disable moving of lead unless in discontiguous mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4373
            if (!changeSelection &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4374
                    tree.getSelectionModel().getSelectionMode() !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4375
                        TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4376
                changeSelection = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4379
            int                rowCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4381
            if((rowCount = tree.getRowCount()) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4382
                int               minSelIndex = ui.getLeadSelectionRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4383
                int               newIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4385
                if(minSelIndex == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4386
                    newIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4387
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4388
                    /* Try and expand the node, otherwise go to next
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4389
                       node. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4390
                    if(direction == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4391
                        TreePath minSelPath = ui.getPathForRow(tree, minSelIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4392
                        int childCount = tree.getModel().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4393
                            getChildCount(minSelPath.getLastPathComponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4394
                        newIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4395
                        if (!ui.isLeaf(minSelIndex)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4396
                            if (!tree.isExpanded(minSelIndex)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4397
                                ui.toggleExpandState(minSelPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4398
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4399
                            else if (childCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4400
                                newIndex = Math.min(minSelIndex + 1, rowCount - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4401
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4402
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4403
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4404
                    /* Try to collapse node. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4405
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4406
                        if(!ui.isLeaf(minSelIndex) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4407
                           tree.isExpanded(minSelIndex)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4408
                            ui.toggleExpandState(ui.getPathForRow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4409
                                              (tree, minSelIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4410
                            newIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4411
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4412
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4413
                            TreePath         path = ui.getPathForRow(tree,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4414
                                                                  minSelIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4416
                            if(path != null && path.getPathCount() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4417
                                newIndex = ui.getRowForPath(tree, path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4418
                                                         getParentPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4419
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4420
                            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4421
                                newIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4422
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4423
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4424
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4425
                if(newIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4426
                    if(changeSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4427
                        tree.setSelectionInterval(newIndex, newIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4428
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4429
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4430
                        ui.setLeadSelectionPath(ui.getPathForRow(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4431
                                                    tree, newIndex), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4432
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4433
                    ui.ensureRowsAreVisible(newIndex, newIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4434
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4435
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4438
        private void moveSelectionToParent(JTree tree, BasicTreeUI ui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4439
            int selRow = ui.getLeadSelectionRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4440
            TreePath path = ui.getPathForRow(tree, selRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4441
            if (path != null && path.getPathCount() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4442
                int  newIndex = ui.getRowForPath(tree, path.getParentPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4443
                if (newIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4444
                    tree.setSelectionInterval(newIndex, newIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4445
                    ui.ensureRowsAreVisible(newIndex, newIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4446
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4450
        private void page(JTree tree, BasicTreeUI ui, int direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4451
                          boolean addToSelection, boolean changeSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4453
            // disable moving of lead unless in discontiguous mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4454
            if (!addToSelection && !changeSelection &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4455
                    tree.getSelectionModel().getSelectionMode() !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4456
                        TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4457
                changeSelection = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4460
            int           rowCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4462
            if((rowCount = ui.getRowCount(tree)) > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4463
                           ui.treeSelectionModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4464
                Dimension         maxSize = tree.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4465
                TreePath          lead = ui.getLeadSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4466
                TreePath          newPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4467
                Rectangle         visRect = tree.getVisibleRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4469
                if(direction == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4470
                    // up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4471
                    newPath = ui.getClosestPathForLocation(tree, visRect.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4472
                                                         visRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4473
                    if(newPath.equals(lead)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4474
                        visRect.y = Math.max(0, visRect.y - visRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4475
                        newPath = tree.getClosestPathForLocation(visRect.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4476
                                                                 visRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4477
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4478
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4479
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4480
                    // down
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4481
                    visRect.y = Math.min(maxSize.height, visRect.y +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4482
                                         visRect.height - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4483
                    newPath = tree.getClosestPathForLocation(visRect.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4484
                                                             visRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4485
                    if(newPath.equals(lead)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4486
                        visRect.y = Math.min(maxSize.height, visRect.y +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4487
                                             visRect.height - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4488
                        newPath = tree.getClosestPathForLocation(visRect.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4489
                                                                 visRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4490
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4491
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4492
                Rectangle            newRect = ui.getPathBounds(tree, newPath);
14656
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4493
                if (newRect != null) {
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4494
                    newRect.x = visRect.x;
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4495
                    newRect.width = visRect.width;
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4496
                    if(direction == -1) {
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4497
                        newRect.height = visRect.height;
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4498
                    }
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4499
                    else {
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4500
                        newRect.y -= (visRect.height - newRect.height);
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4501
                        newRect.height = visRect.height;
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4502
                    }
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4503
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4504
                    if(addToSelection) {
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4505
                        ui.extendSelection(newPath);
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4506
                    }
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4507
                    else if(changeSelection) {
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4508
                        tree.setSelectionPath(newPath);
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4509
                    }
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4510
                    else {
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4511
                        ui.setLeadSelectionPath(newPath, true);
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4512
                    }
6d8446e5b460 8003830: NPE at BasicTreeUI$Actions.page:4470
alexsch
parents: 14655
diff changeset
  4513
                    tree.scrollRectToVisible(newRect);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4514
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4515
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4517
15333
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  4518
        private void home(JTree tree, final BasicTreeUI ui, int direction,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4519
                          boolean addToSelection, boolean changeSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4521
            // disable moving of lead unless in discontiguous mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4522
            if (!addToSelection && !changeSelection &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4523
                    tree.getSelectionModel().getSelectionMode() !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4524
                        TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4525
                changeSelection = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4526
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4527
15333
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  4528
            final int rowCount = ui.getRowCount(tree);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4530
            if (rowCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4531
                if(direction == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4532
                    ui.ensureRowsAreVisible(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4533
                    if (addToSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4534
                        TreePath        aPath = ui.getAnchorSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4535
                        int             aRow = (aPath == null) ? -1 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4536
                                        ui.getRowForPath(tree, aPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4537
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4538
                        if (aRow == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4539
                            tree.setSelectionInterval(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4540
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4541
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4542
                            tree.setSelectionInterval(0, aRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4543
                            ui.setAnchorSelectionPath(aPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4544
                            ui.setLeadSelectionPath(ui.getPathForRow(tree, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4545
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4546
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4547
                    else if(changeSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4548
                        tree.setSelectionInterval(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4549
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4550
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4551
                        ui.setLeadSelectionPath(ui.getPathForRow(tree, 0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4552
                                                true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4553
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4554
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4555
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4556
                    ui.ensureRowsAreVisible(rowCount - 1, rowCount - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4557
                    if (addToSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4558
                        TreePath        aPath = ui.getAnchorSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4559
                        int             aRow = (aPath == null) ? -1 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4560
                                        ui.getRowForPath(tree, aPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4562
                        if (aRow == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4563
                            tree.setSelectionInterval(rowCount - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4564
                                                      rowCount -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4565
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4566
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4567
                            tree.setSelectionInterval(aRow, rowCount - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4568
                            ui.setAnchorSelectionPath(aPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4569
                            ui.setLeadSelectionPath(ui.getPathForRow(tree,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4570
                                                               rowCount -1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4571
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4572
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4573
                    else if(changeSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4574
                        tree.setSelectionInterval(rowCount - 1, rowCount - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4575
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4576
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4577
                        ui.setLeadSelectionPath(ui.getPathForRow(tree,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4578
                                                          rowCount - 1), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4579
                    }
15333
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  4580
                    if (ui.isLargeModel()){
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  4581
                        SwingUtilities.invokeLater(new Runnable() {
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  4582
                            public void run() {
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  4583
                                ui.ensureRowsAreVisible(rowCount - 1, rowCount - 1);
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  4584
                            }
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  4585
                        });
56b81ef28fb4 8003400: JTree scrolling problem when using large model in WindowsLookAndFeel
malenkov
parents: 14656
diff changeset
  4586
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4587
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4588
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4591
} // End of class BasicTreeUI