001 /*
002 * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing;
027
028 import javax.swing.event.*;
029 import java.util.EventObject;
030 import java.io.Serializable;
031
032 /**
033 * @version 1.19 05/05/07
034 *
035 * A base class for <code>CellEditors</code>, providing default
036 * implementations for the methods in the <code>CellEditor</code>
037 * interface except <code>getCellEditorValue()</code>.
038 * Like the other abstract implementations in Swing, also manages a list
039 * of listeners.
040 *
041 * <p>
042 * <strong>Warning:</strong>
043 * Serialized objects of this class will not be compatible with
044 * future Swing releases. The current serialization support is
045 * appropriate for short term storage or RMI between applications running
046 * the same version of Swing. As of 1.4, support for long term storage
047 * of all JavaBeans<sup><font size="-2">TM</font></sup>
048 * has been added to the <code>java.beans</code> package.
049 * Please see {@link java.beans.XMLEncoder}.
050 *
051 * @author Philip Milne
052 * @since 1.3
053 */
054
055 public abstract class AbstractCellEditor implements CellEditor,
056 Serializable {
057
058 protected EventListenerList listenerList = new EventListenerList();
059 transient protected ChangeEvent changeEvent = null;
060
061 // Force this to be implemented.
062 // public Object getCellEditorValue()
063
064 /**
065 * Returns true.
066 * @param e an event object
067 * @return true
068 */
069 public boolean isCellEditable(EventObject e) {
070 return true;
071 }
072
073 /**
074 * Returns true.
075 * @param anEvent an event object
076 * @return true
077 */
078 public boolean shouldSelectCell(EventObject anEvent) {
079 return true;
080 }
081
082 /**
083 * Calls <code>fireEditingStopped</code> and returns true.
084 * @return true
085 */
086 public boolean stopCellEditing() {
087 fireEditingStopped();
088 return true;
089 }
090
091 /**
092 * Calls <code>fireEditingCanceled</code>.
093 */
094 public void cancelCellEditing() {
095 fireEditingCanceled();
096 }
097
098 /**
099 * Adds a <code>CellEditorListener</code> to the listener list.
100 * @param l the new listener to be added
101 */
102 public void addCellEditorListener(CellEditorListener l) {
103 listenerList.add(CellEditorListener.class, l);
104 }
105
106 /**
107 * Removes a <code>CellEditorListener</code> from the listener list.
108 * @param l the listener to be removed
109 */
110 public void removeCellEditorListener(CellEditorListener l) {
111 listenerList.remove(CellEditorListener.class, l);
112 }
113
114 /**
115 * Returns an array of all the <code>CellEditorListener</code>s added
116 * to this AbstractCellEditor with addCellEditorListener().
117 *
118 * @return all of the <code>CellEditorListener</code>s added or an empty
119 * array if no listeners have been added
120 * @since 1.4
121 */
122 public CellEditorListener[] getCellEditorListeners() {
123 return (CellEditorListener[]) listenerList
124 .getListeners(CellEditorListener.class);
125 }
126
127 /**
128 * Notifies all listeners that have registered interest for
129 * notification on this event type. The event instance
130 * is created lazily.
131 *
132 * @see EventListenerList
133 */
134 protected void fireEditingStopped() {
135 // Guaranteed to return a non-null array
136 Object[] listeners = listenerList.getListenerList();
137 // Process the listeners last to first, notifying
138 // those that are interested in this event
139 for (int i = listeners.length - 2; i >= 0; i -= 2) {
140 if (listeners[i] == CellEditorListener.class) {
141 // Lazily create the event:
142 if (changeEvent == null)
143 changeEvent = new ChangeEvent(this );
144 ((CellEditorListener) listeners[i + 1])
145 .editingStopped(changeEvent);
146 }
147 }
148 }
149
150 /**
151 * Notifies all listeners that have registered interest for
152 * notification on this event type. The event instance
153 * is created lazily.
154 *
155 * @see EventListenerList
156 */
157 protected void fireEditingCanceled() {
158 // Guaranteed to return a non-null array
159 Object[] listeners = listenerList.getListenerList();
160 // Process the listeners last to first, notifying
161 // those that are interested in this event
162 for (int i = listeners.length - 2; i >= 0; i -= 2) {
163 if (listeners[i] == CellEditorListener.class) {
164 // Lazily create the event:
165 if (changeEvent == null)
166 changeEvent = new ChangeEvent(this );
167 ((CellEditorListener) listeners[i + 1])
168 .editingCanceled(changeEvent);
169 }
170 }
171 }
172 }
|