1. /*
  2. * @(#)DefaultCellEditor.java 1.38 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.awt.Component;
  9. import java.awt.event.*;
  10. import java.awt.AWTEvent;
  11. import java.lang.Boolean;
  12. import javax.swing.table.*;
  13. import javax.swing.event.*;
  14. import java.util.EventObject;
  15. import javax.swing.tree.*;
  16. import java.io.Serializable;
  17. /**
  18. * The default editor for table and tree cells.
  19. * <p>
  20. * <strong>Warning:</strong>
  21. * Serialized objects of this class will not be compatible with
  22. * future Swing releases. The current serialization support is appropriate
  23. * for short term storage or RMI between applications running the same
  24. * version of Swing. A future release of Swing will provide support for
  25. * long term persistence.
  26. *
  27. * @version 1.38 11/29/01
  28. * @author Philip Milne
  29. * @author Alan Chung
  30. */
  31. public class DefaultCellEditor implements TableCellEditor, TreeCellEditor,
  32. Serializable {
  33. //
  34. // Instance Variables
  35. //
  36. /** Event listeners */
  37. protected EventListenerList listenerList = new EventListenerList();
  38. transient protected ChangeEvent changeEvent = null;
  39. protected JComponent editorComponent;
  40. protected EditorDelegate delegate;
  41. protected int clickCountToStart = 1;
  42. //
  43. // Constructors
  44. //
  45. /**
  46. * Constructs a DefaultCellEditor that uses a text field.
  47. *
  48. * @param x a JTextField object ...
  49. */
  50. public DefaultCellEditor(final JTextField textField) {
  51. editorComponent = textField;
  52. this.clickCountToStart = 2;
  53. delegate = new EditorDelegate() {
  54. public void setValue(Object value) {
  55. textField.setText((value != null) ? value.toString() : "");
  56. }
  57. public Object getCellEditorValue() {
  58. return textField.getText();
  59. }
  60. };
  61. textField.addActionListener(delegate);
  62. }
  63. /**
  64. * Constructs a DefaultCellEditor object that uses a check box.
  65. *
  66. * @param x a JCheckBox object ...
  67. */
  68. public DefaultCellEditor(final JCheckBox checkBox) {
  69. editorComponent = checkBox;
  70. delegate = new EditorDelegate() {
  71. public void setValue(Object value) {
  72. boolean selected = false;
  73. if (value instanceof Boolean) {
  74. selected = ((Boolean)value).booleanValue();
  75. }
  76. else if (value instanceof String) {
  77. selected = value.equals("true");
  78. }
  79. checkBox.setSelected(selected);
  80. }
  81. public Object getCellEditorValue() {
  82. return new Boolean(checkBox.isSelected());
  83. }
  84. };
  85. checkBox.addActionListener(delegate);
  86. }
  87. /**
  88. * Constructs a DefaultCellEditor object that uses a combo box.
  89. *
  90. * @param x a JComboBox object ...
  91. */
  92. public DefaultCellEditor(final JComboBox comboBox) {
  93. editorComponent = comboBox;
  94. comboBox.putClientProperty("JComboBox.lightweightKeyboardNavigation", "Lightweight");
  95. delegate = new EditorDelegate() {
  96. public void setValue(Object value) {
  97. comboBox.setSelectedItem(value);
  98. }
  99. public Object getCellEditorValue() {
  100. return comboBox.getSelectedItem();
  101. }
  102. boolean shouldSelectCell(EventObject anEvent) {
  103. if (anEvent instanceof MouseEvent) {
  104. MouseEvent e = (MouseEvent)anEvent;
  105. return e.getID() != MouseEvent.MOUSE_DRAGGED;
  106. }
  107. return true;
  108. }
  109. };
  110. comboBox.addActionListener(delegate);
  111. }
  112. /**
  113. * Returns the a reference to the editor component.
  114. *
  115. * @return the editor Component
  116. */
  117. public Component getComponent() {
  118. return editorComponent;
  119. }
  120. //
  121. // Modifying
  122. //
  123. /**
  124. * Specifies the number of clicks needed to start editing.
  125. *
  126. * @param count an int specifying the number of clicks needed to start editing
  127. * @see #getClickCountToStart
  128. */
  129. public void setClickCountToStart(int count) {
  130. clickCountToStart = count;
  131. }
  132. /**
  133. * ClickCountToStart controls the number of clicks required to start
  134. * editing.
  135. */
  136. public int getClickCountToStart() {
  137. return clickCountToStart;
  138. }
  139. //
  140. // Implementing the CellEditor Interface
  141. //
  142. // implements javax.swing.CellEditor
  143. public Object getCellEditorValue() {
  144. return delegate.getCellEditorValue();
  145. }
  146. // implements javax.swing.CellEditor
  147. public boolean isCellEditable(EventObject anEvent) {
  148. if (anEvent instanceof MouseEvent) {
  149. return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart;
  150. }
  151. return true;
  152. }
  153. // implements javax.swing.CellEditor
  154. public boolean shouldSelectCell(EventObject anEvent) {
  155. return delegate.shouldSelectCell(anEvent);
  156. }
  157. // implements javax.swing.CellEditor
  158. public boolean stopCellEditing() {
  159. fireEditingStopped();
  160. return true;
  161. }
  162. // implements javax.swing.CellEditor
  163. public void cancelCellEditing() {
  164. fireEditingCanceled();
  165. }
  166. //
  167. // Handle the event listener bookkeeping
  168. //
  169. // implements javax.swing.CellEditor
  170. public void addCellEditorListener(CellEditorListener l) {
  171. listenerList.add(CellEditorListener.class, l);
  172. }
  173. // implements javax.swing.CellEditor
  174. public void removeCellEditorListener(CellEditorListener l) {
  175. listenerList.remove(CellEditorListener.class, l);
  176. }
  177. /*
  178. * Notify all listeners that have registered interest for
  179. * notification on this event type. The event instance
  180. * is lazily created using the parameters passed into
  181. * the fire method.
  182. * @see EventListenerList
  183. */
  184. protected void fireEditingStopped() {
  185. // Guaranteed to return a non-null array
  186. Object[] listeners = listenerList.getListenerList();
  187. // Process the listeners last to first, notifying
  188. // those that are interested in this event
  189. for (int i = listeners.length-2; i>=0; i-=2) {
  190. if (listeners[i]==CellEditorListener.class) {
  191. // Lazily create the event:
  192. if (changeEvent == null)
  193. changeEvent = new ChangeEvent(this);
  194. ((CellEditorListener)listeners[i+1]).editingStopped(changeEvent);
  195. }
  196. }
  197. }
  198. /*
  199. * Notify all listeners that have registered interest for
  200. * notification on this event type. The event instance
  201. * is lazily created using the parameters passed into
  202. * the fire method.
  203. * @see EventListenerList
  204. */
  205. protected void fireEditingCanceled() {
  206. // Guaranteed to return a non-null array
  207. Object[] listeners = listenerList.getListenerList();
  208. // Process the listeners last to first, notifying
  209. // those that are interested in this event
  210. for (int i = listeners.length-2; i>=0; i-=2) {
  211. if (listeners[i]==CellEditorListener.class) {
  212. // Lazily create the event:
  213. if (changeEvent == null)
  214. changeEvent = new ChangeEvent(this);
  215. ((CellEditorListener)listeners[i+1]).editingCanceled(changeEvent);
  216. }
  217. }
  218. }
  219. //
  220. // Implementing the TreeCellEditor Interface
  221. //
  222. // implements javax.swing.tree.TreeCellEditor
  223. public Component getTreeCellEditorComponent(JTree tree, Object value,
  224. boolean isSelected,
  225. boolean expanded,
  226. boolean leaf, int row) {
  227. String stringValue = tree.convertValueToText(value, isSelected,
  228. expanded, leaf, row, false);
  229. delegate.setValue(stringValue);
  230. return editorComponent;
  231. }
  232. //
  233. // Implementing the CellEditor Interface
  234. //
  235. // implements javax.swing.table.TableCellEditor
  236. public Component getTableCellEditorComponent(JTable table, Object value,
  237. boolean isSelected,
  238. int row, int column) {
  239. delegate.setValue(value);
  240. return editorComponent;
  241. }
  242. //
  243. // Protected EditorDelegate class
  244. //
  245. protected class EditorDelegate implements ActionListener, ItemListener, Serializable {
  246. /** Not implemented. */
  247. protected Object value;
  248. /** Not implemented. */
  249. public Object getCellEditorValue() {
  250. return null;
  251. }
  252. /** Not implemented. */
  253. public void setValue(Object x) {}
  254. /** Not implemented. */
  255. public boolean isCellEditable(EventObject anEvent) {
  256. return true;
  257. }
  258. /** Unfortunately, restrictions on API changes force us to
  259. * declare this method package private.
  260. */
  261. boolean shouldSelectCell(EventObject anEvent) {
  262. return true;
  263. }
  264. /** Not implemented. */
  265. public boolean startCellEditing(EventObject anEvent) {
  266. return true;
  267. }
  268. /** Not implemented. */
  269. public boolean stopCellEditing() {
  270. return true;
  271. }
  272. /** Not implemented. */
  273. public void cancelCellEditing() {
  274. }
  275. // Implementing ActionListener interface
  276. public void actionPerformed(ActionEvent e) {
  277. fireEditingStopped();
  278. }
  279. // Implementing ItemListener interface
  280. public void itemStateChanged(ItemEvent e) {
  281. fireEditingStopped();
  282. }
  283. }
  284. } // End of class JCellEditor