jdk/src/java.desktop/share/classes/javax/swing/tree/DefaultTreeCellEditor.java
author darcy
Fri, 03 Apr 2015 10:41:34 -0700
changeset 29894 3e16b51732f5
parent 26037 508779ce6619
child 32865 f9cb6e427f9e
permissions -rw-r--r--
8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}} Reviewed-by: alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29894
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 26037
diff changeset
     2
 * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
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: 1639
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: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.tree;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.plaf.FontUIResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.EventObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * A <code>TreeCellEditor</code>. You need to supply an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * instance of <code>DefaultTreeCellRenderer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * so that the icons can be obtained. You can optionally supply
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * a <code>TreeCellEditor</code> that will be layed out according
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * to the icon in the <code>DefaultTreeCellRenderer</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * If you do not supply a <code>TreeCellEditor</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * a <code>TextField</code> will be used. Editing is started
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * on a triple mouse click, or after a click, pause, click and
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
    45
 * a delay of 1200 milliseconds.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20169
diff changeset
    52
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @see javax.swing.JTree
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public class DefaultTreeCellEditor implements ActionListener, TreeCellEditor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            TreeSelectionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /** Editor handling the editing. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    protected TreeCellEditor               realEditor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /** Renderer, used to get border and offsets from. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    protected DefaultTreeCellRenderer      renderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /** Editing container, will contain the <code>editorComponent</code>. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    protected Container                    editingContainer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Component used in editing, obtained from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * <code>editingContainer</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    transient protected Component          editingComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * As of Java 2 platform v1.4 this field should no longer be used. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * you wish to provide similar behavior you should directly override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * <code>isCellEditable</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    protected boolean                      canEdit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Used in editing. Indicates x position to place
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * <code>editingComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    protected transient int                offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /** <code>JTree</code> instance listening too. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    protected transient JTree              tree;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /** Last path that was selected. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    protected transient TreePath           lastPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /** Used before starting the editing session. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    protected transient Timer              timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Row that was last passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * <code>getTreeCellEditorComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    protected transient int                lastRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /** True if the border selection color should be drawn. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    protected Color                        borderSelectionColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /** Icon to use when editing. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    protected transient Icon               editingIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Font to paint with, <code>null</code> indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * font of renderer is to be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    protected Font                         font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Constructs a <code>DefaultTreeCellEditor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * object for a JTree using the specified renderer and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * a default editor. (Use this constructor for normal editing.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param tree      a <code>JTree</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param renderer  a <code>DefaultTreeCellRenderer</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public DefaultTreeCellEditor(JTree tree,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                 DefaultTreeCellRenderer renderer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        this(tree, renderer, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Constructs a <code>DefaultTreeCellEditor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * object for a <code>JTree</code> using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * specified renderer and the specified editor. (Use this constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * for specialized editing.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param tree      a <code>JTree</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param renderer  a <code>DefaultTreeCellRenderer</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param editor    a <code>TreeCellEditor</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public DefaultTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                 TreeCellEditor editor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        this.renderer = renderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        realEditor = editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if(realEditor == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            realEditor = createTreeCellEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        editingContainer = createContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        setTree(tree);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        setBorderSelectionColor(UIManager.getColor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                ("Tree.editorBorderSelectionColor"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
      * Sets the color to use for the border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
      * @param newColor the new border color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public void setBorderSelectionColor(Color newColor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        borderSelectionColor = newColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
      * Returns the color the border is drawn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
      * @return the border selection color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public Color getBorderSelectionColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return borderSelectionColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Sets the font to edit with. <code>null</code> indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * the renderers font should be used. This will NOT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * override any font you have set in the editor
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
   173
     * the receiver was instantiated with. If <code>null</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * for an editor was passed in a default editor will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * created that will pick up this font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param font  the editing <code>Font</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @see #getFont
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public void setFont(Font font) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        this.font = font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Gets the font used for editing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return the editing <code>Font</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @see #setFont
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public Font getFont() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    // TreeCellEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Configures the editor.  Passed onto the <code>realEditor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public Component getTreeCellEditorComponent(JTree tree, Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                                boolean isSelected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                                boolean expanded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                                boolean leaf, int row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        setTree(tree);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        lastRow = row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        determineOffset(tree, value, isSelected, expanded, leaf, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (editingComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            editingContainer.remove(editingComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        editingComponent = realEditor.getTreeCellEditorComponent(tree, value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                        isSelected, expanded,leaf, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
   216
        // this is kept for backwards compatibility but isn't really needed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        // with the current BasicTreeUI implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        TreePath        newPath = tree.getPathForRow(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        canEdit = (lastPath != null && newPath != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                   lastPath.equals(newPath));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        Font            font = getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if(font == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if(renderer != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                font = renderer.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            if(font == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                font = tree.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        editingContainer.setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        prepareForEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return editingContainer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Returns the value currently being edited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @return the value currently being edited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public Object getCellEditorValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return realEditor.getCellEditorValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * If the <code>realEditor</code> returns true to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * message, <code>prepareForEditing</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * is messaged and true is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public boolean isCellEditable(EventObject event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        boolean            retValue = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        boolean            editable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (event != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            if (event.getSource() instanceof JTree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                setTree((JTree)event.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                if (event instanceof MouseEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    TreePath path = tree.getPathForLocation(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                         ((MouseEvent)event).getX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                         ((MouseEvent)event).getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    editable = (lastPath != null && path != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                               lastPath.equals(path));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    if (path!=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        lastRow = tree.getRowForPath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        Object value = path.getLastPathComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        boolean isSelected = tree.isRowSelected(lastRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        boolean expanded = tree.isExpanded(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        TreeModel treeModel = tree.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        boolean leaf = treeModel.isLeaf(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        determineOffset(tree, value, isSelected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                                        expanded, leaf, lastRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if(!realEditor.isCellEditable(event))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if(canEditImmediately(event))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            retValue = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        else if(editable && shouldStartEditingTimer(event)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            startEditingTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        else if(timer != null && timer.isRunning())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            timer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if(retValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            prepareForEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Messages the <code>realEditor</code> for the return value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public boolean shouldSelectCell(EventObject event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        return realEditor.shouldSelectCell(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * If the <code>realEditor</code> will allow editing to stop,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * the <code>realEditor</code> is removed and true is returned,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * otherwise false is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public boolean stopCellEditing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if(realEditor.stopCellEditing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            cleanupAfterEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Messages <code>cancelCellEditing</code> to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <code>realEditor</code> and removes it from this instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public void cancelCellEditing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        realEditor.cancelCellEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        cleanupAfterEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Adds the <code>CellEditorListener</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @param l the listener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public void addCellEditorListener(CellEditorListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        realEditor.addCellEditorListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
      * Removes the previously added <code>CellEditorListener</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
      * @param l the listener to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public void removeCellEditorListener(CellEditorListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        realEditor.removeCellEditorListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Returns an array of all the <code>CellEditorListener</code>s added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * to this DefaultTreeCellEditor with addCellEditorListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @return all of the <code>CellEditorListener</code>s added or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *         array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public CellEditorListener[] getCellEditorListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return ((DefaultCellEditor)realEditor).getCellEditorListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    // TreeSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Resets <code>lastPath</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public void valueChanged(TreeSelectionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if(tree != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            if(tree.getSelectionCount() == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                lastPath = tree.getSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                lastPath = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if(timer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            timer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    // ActionListener (for Timer).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Messaged when the timer fires, this will start the editing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * session.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if(tree != null && lastPath != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            tree.startEditingAtPath(lastPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    // Local methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Sets the tree currently editing for. This is needed to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * a selection listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @param newTree the new tree to be edited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    protected void setTree(JTree newTree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if(tree != newTree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            if(tree != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                tree.removeTreeSelectionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            tree = newTree;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            if(tree != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                tree.addTreeSelectionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            if(timer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                timer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Returns true if <code>event</code> is a <code>MouseEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * and the click count is 1.
24495
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 23010
diff changeset
   404
     *
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 23010
diff changeset
   405
     * @param event the event being studied
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 23010
diff changeset
   406
     * @return whether {@code event} should starts the editing timer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    protected boolean shouldStartEditingTimer(EventObject event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if((event instanceof MouseEvent) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            SwingUtilities.isLeftMouseButton((MouseEvent)event)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            MouseEvent        me = (MouseEvent)event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            return (me.getClickCount() == 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    inHitRegion(me.getX(), me.getY()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Starts the editing timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    protected void startEditingTimer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if(timer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            timer = new Timer(1200, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            timer.setRepeats(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        timer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Returns true if <code>event</code> is <code>null</code>,
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 5506
diff changeset
   432
     * or it is a <code>MouseEvent</code> with a click count &gt; 2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * and <code>inHitRegion</code> returns true.
24495
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 23010
diff changeset
   434
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @param event the event being studied
24495
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 23010
diff changeset
   436
     * @return whether editing can be started for the given {@code event}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    protected boolean canEditImmediately(EventObject event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        if((event instanceof MouseEvent) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
           SwingUtilities.isLeftMouseButton((MouseEvent)event)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            MouseEvent       me = (MouseEvent)event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            return ((me.getClickCount() > 2) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    inHitRegion(me.getX(), me.getY()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        return (event == null);
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
     * Returns true if the passed in location is a valid mouse location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * to start editing from. This is implemented to return false if
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 5506
diff changeset
   452
     * <code>x</code> is &lt;= the width of the icon and icon gap displayed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * by the renderer. In other words this returns true if the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * clicks over the text part displayed by the renderer, and false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @param x the x-coordinate of the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @param y the y-coordinate of the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @return true if the passed in location is a valid mouse location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    protected boolean inHitRegion(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        if(lastRow != -1 && tree != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            Rectangle bounds = tree.getRowBounds(lastRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            ComponentOrientation treeOrientation = tree.getComponentOrientation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            if ( treeOrientation.isLeftToRight() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                if (bounds != null && x <= (bounds.x + offset) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    offset < (bounds.width - 5)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            } else if ( bounds != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                        ( x >= (bounds.x+bounds.width-offset+5) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                          x <= (bounds.x + 5) ) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                        offset < (bounds.width - 5) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
29894
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 26037
diff changeset
   480
    /**
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 26037
diff changeset
   481
     * Determine the offset.
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 26037
diff changeset
   482
     * @param tree      a <code>JTree</code> object
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 26037
diff changeset
   483
     * @param value a value
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 26037
diff changeset
   484
     * @param isSelected selection status
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 26037
diff changeset
   485
     * @param expanded expansion status
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 26037
diff changeset
   486
     * @param leaf leaf status
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 26037
diff changeset
   487
     * @param row current row
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 26037
diff changeset
   488
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    protected void determineOffset(JTree tree, Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                                   boolean isSelected, boolean expanded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                                   boolean leaf, int row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if(renderer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            if(leaf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                editingIcon = renderer.getLeafIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            else if(expanded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                editingIcon = renderer.getOpenIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                editingIcon = renderer.getClosedIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            if(editingIcon != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                offset = renderer.getIconTextGap() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                         editingIcon.getIconWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                offset = renderer.getIconTextGap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            editingIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * Invoked just before editing is to start. Will add the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * <code>editingComponent</code> to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * <code>editingContainer</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    protected void prepareForEditing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        if (editingComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            editingContainer.add(editingComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Creates the container to manage placement of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * <code>editingComponent</code>.
24495
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 23010
diff changeset
   525
     *
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 23010
diff changeset
   526
     * @return new Container object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    protected Container createContainer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        return new EditorContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * This is invoked if a <code>TreeCellEditor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * is not supplied in the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * It returns a <code>TextField</code> editor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @return a new <code>TextField</code> editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    protected TreeCellEditor createTreeCellEditor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        Border              aBorder = UIManager.getBorder("Tree.editorBorder");
26024
854e6eab0174 6521727: DefaultTreeCellEditor doesn't implement Serializable
serb
parents: 26001
diff changeset
   540
        @SuppressWarnings("serial") // Safe: outer class is non-serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        DefaultCellEditor   editor = new DefaultCellEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            (new DefaultTextField(aBorder)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            public boolean shouldSelectCell(EventObject event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                boolean retValue = super.shouldSelectCell(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        // One click to edit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        editor.setClickCountToStart(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        return editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * Cleans up any state after editing has completed. Removes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * <code>editingComponent</code> the <code>editingContainer</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    private void cleanupAfterEditing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        if (editingComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            editingContainer.remove(editingComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        editingComponent = null;
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
     * <code>TextField</code> used when no editor is supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * This textfield locks into the border it is constructed with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * It also prefers its parents font over its font. And if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * renderer is not <code>null</code> and no font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * has been specified the preferred height is that of the renderer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     */
26024
854e6eab0174 6521727: DefaultTreeCellEditor doesn't implement Serializable
serb
parents: 26001
diff changeset
   572
    @SuppressWarnings("serial") // Safe: outer class is non-serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    public class DefaultTextField extends JTextField {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        /** Border to use. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        protected Border         border;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
         * Constructs a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
         * <code>DefaultTreeCellEditor.DefaultTextField</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
         * @param border  a <code>Border</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        public DefaultTextField(Border border) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            setBorder(border);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
         * Sets the border of this component.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
         * This is a bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
         * @param border the border to be rendered for this component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
         * @see Border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
         * @see CompoundBorder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
         * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
         *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
         *    preferred: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
         *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
         *  description: The component's border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        public void setBorder(Border border) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            super.setBorder(border);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            this.border = border;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
         * Overrides <code>JComponent.getBorder</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         * returns the current border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        public Border getBorder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            return border;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        // implements java.awt.MenuContainer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        public Font getFont() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            Font     font = super.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            // Prefer the parent containers font if our font is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            // FontUIResource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            if(font instanceof FontUIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                Container     parent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                if(parent != null && parent.getFont() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    font = parent.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            return font;
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
         * Overrides <code>JTextField.getPreferredSize</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
         * return the preferred size based on current font, if set,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         * or else use renderer's font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         * @return a <code>Dimension</code> object containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
         *   the preferred size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            Dimension      size = super.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            // If not font has been set, prefer the renderers height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            if(renderer != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
               DefaultTreeCellEditor.this.getFont() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                Dimension     rSize = renderer.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                size.height = rSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
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
     * Container responsible for placing the <code>editingComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     */
26024
854e6eab0174 6521727: DefaultTreeCellEditor doesn't implement Serializable
serb
parents: 26001
diff changeset
   654
    @SuppressWarnings("serial") // Safe: outer class is non-serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    public class EditorContainer extends Container {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
         * Constructs an <code>EditorContainer</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        public EditorContainer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            setLayout(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        // This should not be used. It will be removed when new API is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        // allowed.
29894
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 26037
diff changeset
   665
        /**
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 26037
diff changeset
   666
         * Do not use.
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 26037
diff changeset
   667
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        public void EditorContainer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            setLayout(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
         * Overrides <code>Container.paint</code> to paint the node's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
         * icon and use the selection color for the background.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            int width = getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            int height = getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            // Then the icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            if(editingIcon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                int yLoc = calculateIconY(editingIcon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                if (getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    editingIcon.paintIcon(this, g, 0, yLoc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    editingIcon.paintIcon(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                            this, g, width - editingIcon.getIconWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                            yLoc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            // Border selection color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            Color       background = getBorderSelectionColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            if(background != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                g.setColor(background);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                g.drawRect(0, 0, width - 1, height - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            super.paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
         * Lays out this <code>Container</code>.  If editing,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
         * the editor will be placed at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
         * <code>offset</code> in the x direction and 0 for y.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        public void doLayout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            if(editingComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                int width = getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                int height = getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                if (getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    editingComponent.setBounds(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                            offset, 0, width - offset, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                    editingComponent.setBounds(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                        0, 0, width - offset, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
         * Calculate the y location for the icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        private int calculateIconY(Icon icon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            // To make sure the icon position matches that of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            // renderer, use the same algorithm as JLabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            // (SwingUtilities.layoutCompoundLabel).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            int iconHeight = icon.getIconHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            int textHeight = editingComponent.getFontMetrics(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                editingComponent.getFont()).getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            int textY = iconHeight / 2 - textHeight / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            int totalY = Math.min(0, textY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            int totalHeight = Math.max(iconHeight, textY + textHeight) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                totalY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            return getHeight() / 2 - (totalY + (totalHeight / 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
         * Returns the preferred size for the <code>Container</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
         * This will be at least preferred size of the editor plus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
         * <code>offset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
         * @return a <code>Dimension</code> containing the preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
         *   size for the <code>Container</code>; if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
         *   <code>editingComponent</code> is <code>null</code> the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
         *   <code>Dimension</code> returned is 0, 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            if(editingComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                Dimension         pSize = editingComponent.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                pSize.width += offset + 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                Dimension         rSize = (renderer != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                                          renderer.getPreferredSize() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                if(rSize != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                    pSize.height = Math.max(pSize.height, rSize.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                if(editingIcon != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                    pSize.height = Math.max(pSize.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                                            editingIcon.getIconHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                // Make sure width is at least 100.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                pSize.width = Math.max(pSize.width, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                return pSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            return new Dimension(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
}