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