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