AbstractCellEditor.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* AbstractCellEditor.java --
  2. Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.swing;
  32. import java.io.Serializable;
  33. import java.util.EventObject;
  34. import javax.swing.event.CellEditorListener;
  35. import javax.swing.event.ChangeEvent;
  36. import javax.swing.event.EventListenerList;
  37. /**
  38. * An abstract superclass for table and tree cell editors. This provides some
  39. * common shared functionality.
  40. *
  41. * @author Andrew Selkirk
  42. */
  43. public abstract class AbstractCellEditor
  44. implements CellEditor, Serializable
  45. {
  46. private static final long serialVersionUID = -1048006551406220959L;
  47. /**
  48. * Our Swing event listeners.
  49. */
  50. protected EventListenerList listenerList;
  51. /**
  52. * The cached ChangeEvent.
  53. */
  54. protected transient ChangeEvent changeEvent;
  55. /**
  56. * Creates a new instance of AbstractCellEditor.
  57. */
  58. public AbstractCellEditor()
  59. {
  60. listenerList = new EventListenerList();
  61. changeEvent = new ChangeEvent(this);
  62. }
  63. /**
  64. * Returns <code>true</code> if the cell is editable using
  65. * <code>event</code>, <code>false</code>
  66. * if it's not. The default behaviour is to return <code>true</code>.
  67. *
  68. * @param event an event
  69. *
  70. * @return <code>true</code> if the cell is editable using
  71. * <code>event</code>, <code>false</code> if it's not
  72. */
  73. public boolean isCellEditable(EventObject event)
  74. {
  75. return true;
  76. }
  77. /**
  78. * Returns <code>true</code> if the editing cell should be selected,
  79. * <code>false</code> otherwise. This is usually returning <code>true</code>,
  80. * but in some special cases it might be useful not to switch cell selection
  81. * when editing one cell.
  82. *
  83. * @param event an event
  84. *
  85. * @return <code>true</code> if the editing cell should be selected,
  86. * <code>false</code> otherwise
  87. */
  88. public boolean shouldSelectCell(EventObject event)
  89. {
  90. return true;
  91. }
  92. /**
  93. * Stop editing the cell and accept any partial value that has been entered
  94. * into the cell.
  95. *
  96. * @return <code>true</code> if editing has been stopped successfully,
  97. * <code>false</code>otherwise
  98. */
  99. public boolean stopCellEditing()
  100. {
  101. fireEditingStopped();
  102. return true;
  103. }
  104. /**
  105. * Stop editing the cell and do not accept any partial value that has
  106. * been entered into the cell.
  107. */
  108. public void cancelCellEditing()
  109. {
  110. fireEditingCanceled();
  111. }
  112. /**
  113. * Adds a CellEditorListener to the list of CellEditorListeners of this
  114. * CellEditor.
  115. *
  116. * @param listener the CellEditorListener to add
  117. */
  118. public void addCellEditorListener(CellEditorListener listener)
  119. {
  120. listenerList.add(CellEditorListener.class, listener);
  121. }
  122. /**
  123. * Removes the specified CellEditorListener from the list of the
  124. * CellEditorListeners of this CellEditor.
  125. *
  126. * @param listener the CellEditorListener to remove
  127. */
  128. public void removeCellEditorListener(CellEditorListener listener)
  129. {
  130. listenerList.remove(CellEditorListener.class, listener);
  131. }
  132. /**
  133. * Returns the list of CellEditorListeners that have been registered
  134. * in this CellEditor.
  135. *
  136. * @return the list of CellEditorListeners that have been registered
  137. * in this CellEditor
  138. *
  139. * @since 1.4
  140. */
  141. public CellEditorListener[] getCellEditorListeners()
  142. {
  143. return (CellEditorListener[]) listenerList.getListeners(
  144. CellEditorListener.class);
  145. }
  146. /**
  147. * Notifies all registered listeners that the editing of the cell has has been
  148. * stopped.
  149. */
  150. protected void fireEditingStopped()
  151. {
  152. CellEditorListener[] listeners = getCellEditorListeners();
  153. for (int index = 0; index < listeners.length; index++)
  154. {
  155. listeners[index].editingStopped(changeEvent);
  156. }
  157. }
  158. /**
  159. * Notifies all registered listeners that the editing of the cell has
  160. * has been canceled.
  161. */
  162. protected void fireEditingCanceled()
  163. {
  164. CellEditorListener[] listeners = getCellEditorListeners();
  165. for (int index = 0; index < listeners.length; index++)
  166. {
  167. listeners[index].editingCanceled(changeEvent);
  168. }
  169. }
  170. }