1. /*
  2. * @(#)AbstractCellEditor.java 1.11 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import javax.swing.event.*;
  9. import java.util.EventObject;
  10. import java.io.Serializable;
  11. /**
  12. * @version 1.11 12/19/03
  13. *
  14. * A base class for <code>CellEditors</code>, providing default
  15. * implementations for the methods in the <code>CellEditor</code>
  16. * interface except <code>getCellEditorValue()</code>.
  17. * Like the other abstract implementations in Swing, also manages a list
  18. * of listeners.
  19. *
  20. * <p>
  21. * <strong>Warning:</strong>
  22. * Serialized objects of this class will not be compatible with
  23. * future Swing releases. The current serialization support is
  24. * appropriate for short term storage or RMI between applications running
  25. * the same version of Swing. As of 1.4, support for long term storage
  26. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  27. * has been added to the <code>java.beans</code> package.
  28. * Please see {@link java.beans.XMLEncoder}.
  29. *
  30. * @author Philip Milne
  31. */
  32. public abstract class AbstractCellEditor implements CellEditor, Serializable {
  33. protected EventListenerList listenerList = new EventListenerList();
  34. transient protected ChangeEvent changeEvent = null;
  35. // Force this to be implemented.
  36. // public Object getCellEditorValue()
  37. /**
  38. * Returns true.
  39. * @param e an event object
  40. * @return true
  41. */
  42. public boolean isCellEditable(EventObject e) {
  43. return true;
  44. }
  45. /**
  46. * Returns true.
  47. * @param anEvent an event object
  48. * @return true
  49. */
  50. public boolean shouldSelectCell(EventObject anEvent) {
  51. return true;
  52. }
  53. /**
  54. * Calls <code>fireEditingStopped</code> and returns true.
  55. * @return true
  56. */
  57. public boolean stopCellEditing() {
  58. fireEditingStopped();
  59. return true;
  60. }
  61. /**
  62. * Calls <code>fireEditingCanceled</code>.
  63. */
  64. public void cancelCellEditing() {
  65. fireEditingCanceled();
  66. }
  67. /**
  68. * Adds a <code>CellEditorListener</code> to the listener list.
  69. * @param l the new listener to be added
  70. */
  71. public void addCellEditorListener(CellEditorListener l) {
  72. listenerList.add(CellEditorListener.class, l);
  73. }
  74. /**
  75. * Removes a <code>CellEditorListener</code> from the listener list.
  76. * @param l the listener to be removed
  77. */
  78. public void removeCellEditorListener(CellEditorListener l) {
  79. listenerList.remove(CellEditorListener.class, l);
  80. }
  81. /**
  82. * Returns an array of all the <code>CellEditorListener</code>s added
  83. * to this AbstractCellEditor with addCellEditorListener().
  84. *
  85. * @return all of the <code>CellEditorListener</code>s added or an empty
  86. * array if no listeners have been added
  87. * @since 1.4
  88. */
  89. public CellEditorListener[] getCellEditorListeners() {
  90. return (CellEditorListener[])listenerList.getListeners(
  91. CellEditorListener.class);
  92. }
  93. /**
  94. * Notifies all listeners that have registered interest for
  95. * notification on this event type. The event instance
  96. * is created lazily.
  97. *
  98. * @see EventListenerList
  99. */
  100. protected void fireEditingStopped() {
  101. // Guaranteed to return a non-null array
  102. Object[] listeners = listenerList.getListenerList();
  103. // Process the listeners last to first, notifying
  104. // those that are interested in this event
  105. for (int i = listeners.length-2; i>=0; i-=2) {
  106. if (listeners[i]==CellEditorListener.class) {
  107. // Lazily create the event:
  108. if (changeEvent == null)
  109. changeEvent = new ChangeEvent(this);
  110. ((CellEditorListener)listeners[i+1]).editingStopped(changeEvent);
  111. }
  112. }
  113. }
  114. /**
  115. * Notifies all listeners that have registered interest for
  116. * notification on this event type. The event instance
  117. * is created lazily.
  118. *
  119. * @see EventListenerList
  120. */
  121. protected void fireEditingCanceled() {
  122. // Guaranteed to return a non-null array
  123. Object[] listeners = listenerList.getListenerList();
  124. // Process the listeners last to first, notifying
  125. // those that are interested in this event
  126. for (int i = listeners.length-2; i>=0; i-=2) {
  127. if (listeners[i]==CellEditorListener.class) {
  128. // Lazily create the event:
  129. if (changeEvent == null)
  130. changeEvent = new ChangeEvent(this);
  131. ((CellEditorListener)listeners[i+1]).editingCanceled(changeEvent);
  132. }
  133. }
  134. }
  135. }