Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/swing/DefaultCellEditor.java
38829 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.swing;2627import java.awt.Component;28import java.awt.event.*;29import java.beans.ConstructorProperties;30import java.lang.Boolean;31import javax.swing.table.*;32import javax.swing.event.*;33import java.util.EventObject;34import javax.swing.tree.*;35import java.io.Serializable;3637/**38* The default editor for table and tree cells.39* <p>40* <strong>Warning:</strong>41* Serialized objects of this class will not be compatible with42* future Swing releases. The current serialization support is43* appropriate for short term storage or RMI between applications running44* the same version of Swing. As of 1.4, support for long term storage45* of all JavaBeans™46* has been added to the <code>java.beans</code> package.47* Please see {@link java.beans.XMLEncoder}.48*49* @author Alan Chung50* @author Philip Milne51*/5253public class DefaultCellEditor extends AbstractCellEditor54implements TableCellEditor, TreeCellEditor {5556//57// Instance Variables58//5960/** The Swing component being edited. */61protected JComponent editorComponent;62/**63* The delegate class which handles all methods sent from the64* <code>CellEditor</code>.65*/66protected EditorDelegate delegate;67/**68* An integer specifying the number of clicks needed to start editing.69* Even if <code>clickCountToStart</code> is defined as zero, it70* will not initiate until a click occurs.71*/72protected int clickCountToStart = 1;7374//75// Constructors76//7778/**79* Constructs a <code>DefaultCellEditor</code> that uses a text field.80*81* @param textField a <code>JTextField</code> object82*/83@ConstructorProperties({"component"})84public DefaultCellEditor(final JTextField textField) {85editorComponent = textField;86this.clickCountToStart = 2;87delegate = new EditorDelegate() {88public void setValue(Object value) {89textField.setText((value != null) ? value.toString() : "");90}9192public Object getCellEditorValue() {93return textField.getText();94}95};96textField.addActionListener(delegate);97}9899/**100* Constructs a <code>DefaultCellEditor</code> object that uses a check box.101*102* @param checkBox a <code>JCheckBox</code> object103*/104public DefaultCellEditor(final JCheckBox checkBox) {105editorComponent = checkBox;106delegate = new EditorDelegate() {107public void setValue(Object value) {108boolean selected = false;109if (value instanceof Boolean) {110selected = ((Boolean)value).booleanValue();111}112else if (value instanceof String) {113selected = value.equals("true");114}115checkBox.setSelected(selected);116}117118public Object getCellEditorValue() {119return Boolean.valueOf(checkBox.isSelected());120}121};122checkBox.addActionListener(delegate);123checkBox.setRequestFocusEnabled(false);124}125126/**127* Constructs a <code>DefaultCellEditor</code> object that uses a128* combo box.129*130* @param comboBox a <code>JComboBox</code> object131*/132public DefaultCellEditor(final JComboBox comboBox) {133editorComponent = comboBox;134comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);135delegate = new EditorDelegate() {136public void setValue(Object value) {137comboBox.setSelectedItem(value);138}139140public Object getCellEditorValue() {141return comboBox.getSelectedItem();142}143144public boolean shouldSelectCell(EventObject anEvent) {145if (anEvent instanceof MouseEvent) {146MouseEvent e = (MouseEvent)anEvent;147return e.getID() != MouseEvent.MOUSE_DRAGGED;148}149return true;150}151public boolean stopCellEditing() {152if (comboBox.isEditable()) {153// Commit edited value.154comboBox.actionPerformed(new ActionEvent(155DefaultCellEditor.this, 0, ""));156}157return super.stopCellEditing();158}159};160comboBox.addActionListener(delegate);161}162163/**164* Returns a reference to the editor component.165*166* @return the editor <code>Component</code>167*/168public Component getComponent() {169return editorComponent;170}171172//173// Modifying174//175176/**177* Specifies the number of clicks needed to start editing.178*179* @param count an int specifying the number of clicks needed to start editing180* @see #getClickCountToStart181*/182public void setClickCountToStart(int count) {183clickCountToStart = count;184}185186/**187* Returns the number of clicks needed to start editing.188* @return the number of clicks needed to start editing189*/190public int getClickCountToStart() {191return clickCountToStart;192}193194//195// Override the implementations of the superclass, forwarding all methods196// from the CellEditor interface to our delegate.197//198199/**200* Forwards the message from the <code>CellEditor</code> to201* the <code>delegate</code>.202* @see EditorDelegate#getCellEditorValue203*/204public Object getCellEditorValue() {205return delegate.getCellEditorValue();206}207208/**209* Forwards the message from the <code>CellEditor</code> to210* the <code>delegate</code>.211* @see EditorDelegate#isCellEditable(EventObject)212*/213public boolean isCellEditable(EventObject anEvent) {214return delegate.isCellEditable(anEvent);215}216217/**218* Forwards the message from the <code>CellEditor</code> to219* the <code>delegate</code>.220* @see EditorDelegate#shouldSelectCell(EventObject)221*/222public boolean shouldSelectCell(EventObject anEvent) {223return delegate.shouldSelectCell(anEvent);224}225226/**227* Forwards the message from the <code>CellEditor</code> to228* the <code>delegate</code>.229* @see EditorDelegate#stopCellEditing230*/231public boolean stopCellEditing() {232return delegate.stopCellEditing();233}234235/**236* Forwards the message from the <code>CellEditor</code> to237* the <code>delegate</code>.238* @see EditorDelegate#cancelCellEditing239*/240public void cancelCellEditing() {241delegate.cancelCellEditing();242}243244//245// Implementing the TreeCellEditor Interface246//247248/** Implements the <code>TreeCellEditor</code> interface. */249public Component getTreeCellEditorComponent(JTree tree, Object value,250boolean isSelected,251boolean expanded,252boolean leaf, int row) {253String stringValue = tree.convertValueToText(value, isSelected,254expanded, leaf, row, false);255256delegate.setValue(stringValue);257return editorComponent;258}259260//261// Implementing the CellEditor Interface262//263/** Implements the <code>TableCellEditor</code> interface. */264public Component getTableCellEditorComponent(JTable table, Object value,265boolean isSelected,266int row, int column) {267delegate.setValue(value);268if (editorComponent instanceof JCheckBox) {269//in order to avoid a "flashing" effect when clicking a checkbox270//in a table, it is important for the editor to have as a border271//the same border that the renderer has, and have as the background272//the same color as the renderer has. This is primarily only273//needed for JCheckBox since this editor doesn't fill all the274//visual space of the table cell, unlike a text field.275TableCellRenderer renderer = table.getCellRenderer(row, column);276Component c = renderer.getTableCellRendererComponent(table, value,277isSelected, true, row, column);278if (c != null) {279editorComponent.setOpaque(true);280editorComponent.setBackground(c.getBackground());281if (c instanceof JComponent) {282editorComponent.setBorder(((JComponent)c).getBorder());283}284} else {285editorComponent.setOpaque(false);286}287}288return editorComponent;289}290291292//293// Protected EditorDelegate class294//295296/**297* The protected <code>EditorDelegate</code> class.298*/299protected class EditorDelegate implements ActionListener, ItemListener, Serializable {300301/** The value of this cell. */302protected Object value;303304/**305* Returns the value of this cell.306* @return the value of this cell307*/308public Object getCellEditorValue() {309return value;310}311312/**313* Sets the value of this cell.314* @param value the new value of this cell315*/316public void setValue(Object value) {317this.value = value;318}319320/**321* Returns true if <code>anEvent</code> is <b>not</b> a322* <code>MouseEvent</code>. Otherwise, it returns true323* if the necessary number of clicks have occurred, and324* returns false otherwise.325*326* @param anEvent the event327* @return true if cell is ready for editing, false otherwise328* @see #setClickCountToStart329* @see #shouldSelectCell330*/331public boolean isCellEditable(EventObject anEvent) {332if (anEvent instanceof MouseEvent) {333return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart;334}335return true;336}337338/**339* Returns true to indicate that the editing cell may340* be selected.341*342* @param anEvent the event343* @return true344* @see #isCellEditable345*/346public boolean shouldSelectCell(EventObject anEvent) {347return true;348}349350/**351* Returns true to indicate that editing has begun.352*353* @param anEvent the event354*/355public boolean startCellEditing(EventObject anEvent) {356return true;357}358359/**360* Stops editing and361* returns true to indicate that editing has stopped.362* This method calls <code>fireEditingStopped</code>.363*364* @return true365*/366public boolean stopCellEditing() {367fireEditingStopped();368return true;369}370371/**372* Cancels editing. This method calls <code>fireEditingCanceled</code>.373*/374public void cancelCellEditing() {375fireEditingCanceled();376}377378/**379* When an action is performed, editing is ended.380* @param e the action event381* @see #stopCellEditing382*/383public void actionPerformed(ActionEvent e) {384DefaultCellEditor.this.stopCellEditing();385}386387/**388* When an item's state changes, editing is ended.389* @param e the action event390* @see #stopCellEditing391*/392public void itemStateChanged(ItemEvent e) {393DefaultCellEditor.this.stopCellEditing();394}395}396397} // End of class JCellEditor398399400