1 /* AbstractCellEditor.java --
2    Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37 
38 
39 package javax.swing;
40 
41 import java.io.Serializable;
42 import java.util.EventObject;
43 
44 import javax.swing.event.CellEditorListener;
45 import javax.swing.event.ChangeEvent;
46 import javax.swing.event.EventListenerList;
47 
48 /**
49  * An abstract superclass for table and tree cell editors. This provides some
50  * common shared functionality.
51  *
52  * @author Andrew Selkirk
53  */
54 public abstract class AbstractCellEditor
55   implements CellEditor, Serializable
56 {
57   private static final long serialVersionUID = -1048006551406220959L;
58 
59   /**
60    * Our Swing event listeners.
61    */
62   protected EventListenerList listenerList;
63 
64   /**
65    * The cached ChangeEvent.
66    */
67   protected transient ChangeEvent changeEvent;
68 
69   /**
70    * Creates a new instance of AbstractCellEditor.
71    */
AbstractCellEditor()72   public AbstractCellEditor()
73   {
74     listenerList = new EventListenerList();
75     changeEvent = new ChangeEvent(this);
76   }
77 
78   /**
79    * Returns <code>true</code> if the cell is editable using
80    * <code>event</code>, <code>false</code>
81    * if it's not. The default behaviour is to return <code>true</code>.
82    *
83    * @param event an event
84    *
85    * @return <code>true</code> if the cell is editable using
86    *     <code>event</code>, <code>false</code> if it's not
87    */
isCellEditable(EventObject event)88   public boolean isCellEditable(EventObject event)
89   {
90     return true;
91   }
92 
93   /**
94    * Returns <code>true</code> if the editing cell should be selected,
95    * <code>false</code> otherwise. This is usually returning <code>true</code>,
96    * but in some special cases it might be useful not to switch cell selection
97    * when editing one cell.
98    *
99    * @param event an event
100    *
101    * @return <code>true</code> if the editing cell should be selected,
102    *     <code>false</code> otherwise
103    */
shouldSelectCell(EventObject event)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    */
stopCellEditing()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    */
cancelCellEditing()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    */
addCellEditorListener(CellEditorListener listener)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    */
removeCellEditorListener(CellEditorListener listener)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    */
getCellEditorListeners()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    */
fireEditingStopped()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    */
fireEditingCanceled()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 }
196