001 /* AbstractCellEditor.java --
002 Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package javax.swing;
040
041 import java.io.Serializable;
042 import java.util.EventObject;
043
044 import javax.swing.event.CellEditorListener;
045 import javax.swing.event.ChangeEvent;
046 import javax.swing.event.EventListenerList;
047
048 /**
049 * An abstract superclass for table and tree cell editors. This provides some
050 * common shared functionality.
051 *
052 * @author Andrew Selkirk
053 */
054 public abstract class AbstractCellEditor
055 implements CellEditor, Serializable
056 {
057 private static final long serialVersionUID = -1048006551406220959L;
058
059 /**
060 * Our Swing event listeners.
061 */
062 protected EventListenerList listenerList;
063
064 /**
065 * The cached ChangeEvent.
066 */
067 protected transient ChangeEvent changeEvent;
068
069 /**
070 * Creates a new instance of AbstractCellEditor.
071 */
072 public AbstractCellEditor()
073 {
074 listenerList = new EventListenerList();
075 changeEvent = new ChangeEvent(this);
076 }
077
078 /**
079 * Returns <code>true</code> if the cell is editable using
080 * <code>event</code>, <code>false</code>
081 * if it's not. The default behaviour is to return <code>true</code>.
082 *
083 * @param event an event
084 *
085 * @return <code>true</code> if the cell is editable using
086 * <code>event</code>, <code>false</code> if it's not
087 */
088 public boolean isCellEditable(EventObject event)
089 {
090 return true;
091 }
092
093 /**
094 * Returns <code>true</code> if the editing cell should be selected,
095 * <code>false</code> otherwise. This is usually returning <code>true</code>,
096 * but in some special cases it might be useful not to switch cell selection
097 * when editing one cell.
098 *
099 * @param event an event
100 *
101 * @return <code>true</code> if the editing cell should be selected,
102 * <code>false</code> otherwise
103 */
104 public boolean shouldSelectCell(EventObject event)
105 {
106 return true;
107 }
108
109 /**
110 * Stop editing the cell and accept any partial value that has been entered
111 * into the cell.
112 *
113 * @return <code>true</code> if editing has been stopped successfully,
114 * <code>false</code>otherwise
115 */
116 public boolean stopCellEditing()
117 {
118 fireEditingStopped();
119 return true;
120 }
121
122 /**
123 * Stop editing the cell and do not accept any partial value that has
124 * been entered into the cell.
125 */
126 public void cancelCellEditing()
127 {
128 fireEditingCanceled();
129 }
130
131 /**
132 * Adds a CellEditorListener to the list of CellEditorListeners of this
133 * CellEditor.
134 *
135 * @param listener the CellEditorListener to add
136 */
137 public void addCellEditorListener(CellEditorListener listener)
138 {
139 listenerList.add(CellEditorListener.class, listener);
140 }
141
142 /**
143 * Removes the specified CellEditorListener from the list of the
144 * CellEditorListeners of this CellEditor.
145 *
146 * @param listener the CellEditorListener to remove
147 */
148 public void removeCellEditorListener(CellEditorListener listener)
149 {
150 listenerList.remove(CellEditorListener.class, listener);
151 }
152
153 /**
154 * Returns the list of CellEditorListeners that have been registered
155 * in this CellEditor.
156 *
157 * @return the list of CellEditorListeners that have been registered
158 * in this CellEditor
159 *
160 * @since 1.4
161 */
162 public CellEditorListener[] getCellEditorListeners()
163 {
164 return (CellEditorListener[]) listenerList.getListeners(
165 CellEditorListener.class);
166 }
167
168 /**
169 * Notifies all registered listeners that the editing of the cell has has been
170 * stopped.
171 */
172 protected void fireEditingStopped()
173 {
174 CellEditorListener[] listeners = getCellEditorListeners();
175
176 for (int index = 0; index < listeners.length; index++)
177 {
178 listeners[index].editingStopped(changeEvent);
179 }
180 }
181
182 /**
183 * Notifies all registered listeners that the editing of the cell has
184 * has been canceled.
185 */
186 protected void fireEditingCanceled()
187 {
188 CellEditorListener[] listeners = getCellEditorListeners();
189
190 for (int index = 0; index < listeners.length; index++)
191 {
192 listeners[index].editingCanceled(changeEvent);
193 }
194 }
195 }