1. /*
  2. * @(#)DefaultCellEditor.java 1.38 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing;
  11. import java.awt.Component;
  12. import java.awt.event.*;
  13. import java.awt.AWTEvent;
  14. import java.lang.Boolean;
  15. import javax.swing.table.*;
  16. import javax.swing.event.*;
  17. import java.util.EventObject;
  18. import javax.swing.tree.*;
  19. import java.io.Serializable;
  20. /**
  21. * The default editor for table and tree cells.
  22. * <p>
  23. * <strong>Warning:</strong>
  24. * Serialized objects of this class will not be compatible with
  25. * future Swing releases. The current serialization support is appropriate
  26. * for short term storage or RMI between applications running the same
  27. * version of Swing. A future release of Swing will provide support for
  28. * long term persistence.
  29. *
  30. * @version 1.38 02/02/00
  31. * @author Alan Chung
  32. * @author Philip Milne
  33. */
  34. public class DefaultCellEditor extends AbstractCellEditor
  35. implements TableCellEditor, TreeCellEditor {
  36. //
  37. // Instance Variables
  38. //
  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. public 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. // Override the implementations of the superclass, forwarding all methods
  141. // from the CellEditor interface to our delegate.
  142. //
  143. public Object getCellEditorValue() {
  144. return delegate.getCellEditorValue();
  145. }
  146. public boolean isCellEditable(EventObject anEvent) {
  147. return delegate.isCellEditable(anEvent);
  148. }
  149. public boolean shouldSelectCell(EventObject anEvent) {
  150. return delegate.shouldSelectCell(anEvent);
  151. }
  152. public boolean stopCellEditing() {
  153. return delegate.stopCellEditing();
  154. }
  155. public void cancelCellEditing() {
  156. delegate.cancelCellEditing();
  157. }
  158. //
  159. // Implementing the TreeCellEditor Interface
  160. //
  161. public Component getTreeCellEditorComponent(JTree tree, Object value,
  162. boolean isSelected,
  163. boolean expanded,
  164. boolean leaf, int row) {
  165. String stringValue = tree.convertValueToText(value, isSelected,
  166. expanded, leaf, row, false);
  167. delegate.setValue(stringValue);
  168. return editorComponent;
  169. }
  170. //
  171. // Implementing the CellEditor Interface
  172. //
  173. public Component getTableCellEditorComponent(JTable table, Object value,
  174. boolean isSelected,
  175. int row, int column) {
  176. delegate.setValue(value);
  177. return editorComponent;
  178. }
  179. //
  180. // Protected EditorDelegate class
  181. //
  182. protected class EditorDelegate implements ActionListener, ItemListener, Serializable {
  183. protected Object value;
  184. public Object getCellEditorValue() {
  185. return value;
  186. }
  187. public void setValue(Object value) {
  188. this.value = value;
  189. }
  190. public boolean isCellEditable(EventObject anEvent) {
  191. if (anEvent instanceof MouseEvent) {
  192. return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart;
  193. }
  194. return true;
  195. }
  196. public boolean shouldSelectCell(EventObject anEvent) {
  197. return true;
  198. }
  199. public boolean startCellEditing(EventObject anEvent) {
  200. return true;
  201. }
  202. public boolean stopCellEditing() {
  203. fireEditingStopped();
  204. return true;
  205. }
  206. public void cancelCellEditing() {
  207. fireEditingCanceled();
  208. }
  209. public void actionPerformed(ActionEvent e) {
  210. DefaultCellEditor.this.stopCellEditing();
  211. }
  212. public void itemStateChanged(ItemEvent e) {
  213. DefaultCellEditor.this.stopCellEditing();
  214. }
  215. }
  216. } // End of class JCellEditor