Path: blob/master/src/java.desktop/share/classes/javax/swing/AbstractCellEditor.java
41153 views
/*1* Copyright (c) 1999, 2015, 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 javax.swing.event.*;28import java.util.EventObject;29import java.io.Serializable;3031/**32*33* A base class for <code>CellEditors</code>, providing default34* implementations for the methods in the <code>CellEditor</code>35* interface except <code>getCellEditorValue()</code>.36* Like the other abstract implementations in Swing, also manages a list37* of listeners.38*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 JavaBeans46* has been added to the <code>java.beans</code> package.47* Please see {@link java.beans.XMLEncoder}.48*49* @author Philip Milne50* @since 1.351*/52@SuppressWarnings("serial") // Same-version serialization only53public abstract class AbstractCellEditor implements CellEditor, Serializable {5455/**56* The list of listeners.57*/58protected EventListenerList listenerList = new EventListenerList();59/**60* The change event.61*/62protected transient ChangeEvent changeEvent = null;6364/**65* Constructor for subclasses to call.66*/67protected AbstractCellEditor() {}6869// Force this to be implemented.70// public Object getCellEditorValue()7172/**73* Returns true.74* @param e an event object75* @return true76*/77public boolean isCellEditable(EventObject e) {78return true;79}8081/**82* Returns true.83* @param anEvent an event object84* @return true85*/86public boolean shouldSelectCell(EventObject anEvent) {87return true;88}8990/**91* Calls <code>fireEditingStopped</code> and returns true.92* @return true93*/94public boolean stopCellEditing() {95fireEditingStopped();96return true;97}9899/**100* Calls <code>fireEditingCanceled</code>.101*/102public void cancelCellEditing() {103fireEditingCanceled();104}105106/**107* Adds a <code>CellEditorListener</code> to the listener list.108* @param l the new listener to be added109*/110public void addCellEditorListener(CellEditorListener l) {111listenerList.add(CellEditorListener.class, l);112}113114/**115* Removes a <code>CellEditorListener</code> from the listener list.116* @param l the listener to be removed117*/118public void removeCellEditorListener(CellEditorListener l) {119listenerList.remove(CellEditorListener.class, l);120}121122/**123* Returns an array of all the <code>CellEditorListener</code>s added124* to this AbstractCellEditor with addCellEditorListener().125*126* @return all of the <code>CellEditorListener</code>s added or an empty127* array if no listeners have been added128* @since 1.4129*/130public CellEditorListener[] getCellEditorListeners() {131return listenerList.getListeners(CellEditorListener.class);132}133134/**135* Notifies all listeners that have registered interest for136* notification on this event type. The event instance137* is created lazily.138*139* @see EventListenerList140*/141protected void fireEditingStopped() {142// Guaranteed to return a non-null array143Object[] listeners = listenerList.getListenerList();144// Process the listeners last to first, notifying145// those that are interested in this event146for (int i = listeners.length-2; i>=0; i-=2) {147if (listeners[i]==CellEditorListener.class) {148// Lazily create the event:149if (changeEvent == null)150changeEvent = new ChangeEvent(this);151((CellEditorListener)listeners[i+1]).editingStopped(changeEvent);152}153}154}155156/**157* Notifies all listeners that have registered interest for158* notification on this event type. The event instance159* is created lazily.160*161* @see EventListenerList162*/163protected void fireEditingCanceled() {164// Guaranteed to return a non-null array165Object[] listeners = listenerList.getListenerList();166// Process the listeners last to first, notifying167// those that are interested in this event168for (int i = listeners.length-2; i>=0; i-=2) {169if (listeners[i]==CellEditorListener.class) {170// Lazily create the event:171if (changeEvent == null)172changeEvent = new ChangeEvent(this);173((CellEditorListener)listeners[i+1]).editingCanceled(changeEvent);174}175}176}177}178179180