jdk/src/share/classes/javax/swing/DefaultCellEditor.java
author ant
Thu, 17 May 2012 21:48:57 +0400
changeset 12661 6cf8b7116579
parent 5506 202f599c92aa
child 20458 f2423fb3fd19
permissions -rw-r--r--
7125044: [macosx] Test failure because Component.transferFocus() works differently in applet and application. Summary: forward port from 7u4 Reviewed-by: art
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: 2658
diff changeset
     2
 * Copyright (c) 1997, 2007, 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: 2658
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: 2658
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: 2658
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.beans.ConstructorProperties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.Boolean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.table.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.EventObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.tree.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The default editor for table and tree cells.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Alan Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class DefaultCellEditor extends AbstractCellEditor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    implements TableCellEditor, TreeCellEditor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
//  Instance Variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /** The Swing component being edited. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    protected JComponent editorComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * The delegate class which handles all methods sent from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * <code>CellEditor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    protected EditorDelegate delegate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * An integer specifying the number of clicks needed to start editing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Even if <code>clickCountToStart</code> is defined as zero, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * will not initiate until a click occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    protected int clickCountToStart = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
//  Constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Constructs a <code>DefaultCellEditor</code> that uses a text field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param textField  a <code>JTextField</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    @ConstructorProperties({"component"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public DefaultCellEditor(final JTextField textField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        editorComponent = textField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        this.clickCountToStart = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        delegate = new EditorDelegate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            public void setValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                textField.setText((value != null) ? value.toString() : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            public Object getCellEditorValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                return textField.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        textField.addActionListener(delegate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Constructs a <code>DefaultCellEditor</code> object that uses a check box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param checkBox  a <code>JCheckBox</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public DefaultCellEditor(final JCheckBox checkBox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        editorComponent = checkBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        delegate = new EditorDelegate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            public void setValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                boolean selected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                if (value instanceof Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    selected = ((Boolean)value).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                else if (value instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    selected = value.equals("true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                checkBox.setSelected(selected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            public Object getCellEditorValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                return Boolean.valueOf(checkBox.isSelected());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        checkBox.addActionListener(delegate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        checkBox.setRequestFocusEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Constructs a <code>DefaultCellEditor</code> object that uses a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * combo box.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param comboBox  a <code>JComboBox</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public DefaultCellEditor(final JComboBox comboBox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        editorComponent = comboBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        delegate = new EditorDelegate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            public void setValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                comboBox.setSelectedItem(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            public Object getCellEditorValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                return comboBox.getSelectedItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            public boolean shouldSelectCell(EventObject anEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                if (anEvent instanceof MouseEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    MouseEvent e = (MouseEvent)anEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    return e.getID() != MouseEvent.MOUSE_DRAGGED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            public boolean stopCellEditing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                if (comboBox.isEditable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    // Commit edited value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    comboBox.actionPerformed(new ActionEvent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                     DefaultCellEditor.this, 0, ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                return super.stopCellEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        comboBox.addActionListener(delegate);
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 a reference to the editor component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return the editor <code>Component</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public Component getComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return editorComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
//  Modifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Specifies the number of clicks needed to start editing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param count  an int specifying the number of clicks needed to start editing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @see #getClickCountToStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public void setClickCountToStart(int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        clickCountToStart = count;
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
     * Returns the number of clicks needed to start editing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @return the number of clicks needed to start editing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public int getClickCountToStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return clickCountToStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
//  Override the implementations of the superclass, forwarding all methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
//  from the CellEditor interface to our delegate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Forwards the message from the <code>CellEditor</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * the <code>delegate</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @see EditorDelegate#getCellEditorValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public Object getCellEditorValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return delegate.getCellEditorValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Forwards the message from the <code>CellEditor</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * the <code>delegate</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @see EditorDelegate#isCellEditable(EventObject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public boolean isCellEditable(EventObject anEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return delegate.isCellEditable(anEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Forwards the message from the <code>CellEditor</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * the <code>delegate</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @see EditorDelegate#shouldSelectCell(EventObject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public boolean shouldSelectCell(EventObject anEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return delegate.shouldSelectCell(anEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Forwards the message from the <code>CellEditor</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * the <code>delegate</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @see EditorDelegate#stopCellEditing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public boolean stopCellEditing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return delegate.stopCellEditing();
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
     * Forwards the message from the <code>CellEditor</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * the <code>delegate</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @see EditorDelegate#cancelCellEditing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public void cancelCellEditing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        delegate.cancelCellEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
//  Implementing the TreeCellEditor Interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /** Implements the <code>TreeCellEditor</code> interface. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public Component getTreeCellEditorComponent(JTree tree, Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                                boolean isSelected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                                boolean expanded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                                boolean leaf, int row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        String         stringValue = tree.convertValueToText(value, isSelected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                                            expanded, leaf, row, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        delegate.setValue(stringValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return editorComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
//  Implementing the CellEditor Interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /** Implements the <code>TableCellEditor</code> interface. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public Component getTableCellEditorComponent(JTable table, Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                                 boolean isSelected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                                                 int row, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        delegate.setValue(value);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   269
        if (editorComponent instanceof JCheckBox) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   270
            //in order to avoid a "flashing" effect when clicking a checkbox
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   271
            //in a table, it is important for the editor to have as a border
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   272
            //the same border that the renderer has, and have as the background
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   273
            //the same color as the renderer has. This is primarily only
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   274
            //needed for JCheckBox since this editor doesn't fill all the
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   275
            //visual space of the table cell, unlike a text field.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   276
            TableCellRenderer renderer = table.getCellRenderer(row, column);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   277
            Component c = renderer.getTableCellRendererComponent(table, value,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   278
                    isSelected, true, row, column);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   279
            if (c != null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   280
                editorComponent.setOpaque(true);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   281
                editorComponent.setBackground(c.getBackground());
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   282
                if (c instanceof JComponent) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   283
                    editorComponent.setBorder(((JComponent)c).getBorder());
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   284
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   285
            } else {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   286
                editorComponent.setOpaque(false);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   287
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   288
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return editorComponent;
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
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
//  Protected EditorDelegate class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * The protected <code>EditorDelegate</code> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    protected class EditorDelegate implements ActionListener, ItemListener, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        /**  The value of this cell. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        protected Object value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        * Returns the value of this cell.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        * @return the value of this cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        public Object getCellEditorValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        * Sets the value of this cell.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        * @param value the new value of this cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        public void setValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            this.value = value;
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
        * Returns true if <code>anEvent</code> is <b>not</b> a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        * <code>MouseEvent</code>.  Otherwise, it returns true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        * if the necessary number of clicks have occurred, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        * returns false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        * @param   anEvent         the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        * @return  true  if cell is ready for editing, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        * @see #setClickCountToStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        * @see #shouldSelectCell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        public boolean isCellEditable(EventObject anEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (anEvent instanceof MouseEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        * Returns true to indicate that the editing cell may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        * be selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        * @param   anEvent         the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        * @return  true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        * @see #isCellEditable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        public boolean shouldSelectCell(EventObject anEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        * Returns true to indicate that editing has begun.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        * @param anEvent          the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        public boolean startCellEditing(EventObject anEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        * Stops editing and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        * returns true to indicate that editing has stopped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        * This method calls <code>fireEditingStopped</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        * @return  true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        public boolean stopCellEditing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            fireEditingStopped();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            return true;
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
        * Cancels editing.  This method calls <code>fireEditingCanceled</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
       public void cancelCellEditing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
           fireEditingCanceled();
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
        * When an action is performed, editing is ended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        * @param e the action event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        * @see #stopCellEditing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            DefaultCellEditor.this.stopCellEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        * When an item's state changes, editing is ended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        * @param e the action event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        * @see #stopCellEditing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        public void itemStateChanged(ItemEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            DefaultCellEditor.this.stopCellEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
} // End of class JCellEditor