1. /*
  2. * @(#)DefaultTreeCellEditor.java 1.15 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.tree;
  8. import javax.swing.*;
  9. import javax.swing.border.*;
  10. import javax.swing.event.*;
  11. import javax.swing.plaf.FontUIResource;
  12. import java.awt.*;
  13. import java.awt.event.*;
  14. import java.beans.*;
  15. import java.io.*;
  16. import java.util.*;
  17. /**
  18. * A TreeCellEditor. You need to supply an instance of DefaultTreeCellRenderer
  19. * so that the icons can be obtained. You can optionaly supply a TreeCellEditor
  20. * that will be layed out according to the icon in the DefaultTreeCellRenderer.
  21. * If you do not supply a TreeCellEditor, a TextField will be used. Editing
  22. * is started on a triple mouse click, or after a click, pause, click and
  23. * a delay of 1200 miliseconds.
  24. *<p>
  25. * <strong>Warning:</strong>
  26. * Serialized objects of this class will not be compatible with
  27. * future Swing releases. The current serialization support is appropriate
  28. * for short term storage or RMI between applications running the same
  29. * version of Swing. A future release of Swing will provide support for
  30. * long term persistence.
  31. * @version 1.15 11/29/01
  32. * @author Scott Violet
  33. */
  34. public class DefaultTreeCellEditor implements ActionListener, TreeCellEditor,
  35. TreeSelectionListener {
  36. /** Editor handling the editing. */
  37. protected TreeCellEditor realEditor;
  38. /** Renderer, used to get border and offsets from. */
  39. protected DefaultTreeCellRenderer renderer;
  40. /** Editing container, will contain the editorComponent. */
  41. protected Container editingContainer;
  42. /** Component used in editing, obtained from the editingContainer. */
  43. transient protected Component editingComponent;
  44. /** Should isCellEditable return true? This is set in configure...
  45. * based on the path being edited and the selected selected path. */
  46. protected boolean canEdit;
  47. /** Used in editing. Indicates x position to place editingComponent. */
  48. protected transient int offset;
  49. /** JTree instance listening too. */
  50. protected transient JTree tree;
  51. /** last path that was selected. */
  52. protected transient TreePath lastPath;
  53. /** Used before starting the editing session. */
  54. protected transient Timer timer;
  55. /** Row that was last passed into getTreeCellEditorComponent. */
  56. protected transient int lastRow;
  57. /** True if the border selection color should be drawn. */
  58. protected Color borderSelectionColor;
  59. /** Icon to use when editing. */
  60. protected transient Icon editingIcon;
  61. /** Font to paint with, null indicates font of renderer is to be used. */
  62. protected Font font;
  63. /**
  64. * Constructs a DefaultTreeCellEditor object for a JTree using the
  65. * specified renderer and a default editor. (Use this constructor
  66. * for normal editing.)
  67. *
  68. * @param tree a JTree object
  69. * @param renderer a DefaultTreeCellRenderer object
  70. */
  71. public DefaultTreeCellEditor(JTree tree,
  72. DefaultTreeCellRenderer renderer) {
  73. this(tree, renderer, null);
  74. }
  75. /**
  76. * Constructs a DefaultTreeCellEditor object for a JTree using the
  77. * specified renderer and the specified editor. (Use this constructor
  78. * for specialized editing.)
  79. *
  80. * @param tree a JTree object
  81. * @param renderer a DefaultTreeCellRenderer object
  82. * @param editor a TreeCellEditor object
  83. */
  84. public DefaultTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer,
  85. TreeCellEditor editor) {
  86. this.renderer = renderer;
  87. realEditor = editor;
  88. if(realEditor == null)
  89. realEditor = createTreeCellEditor();
  90. editingContainer = createContainer();
  91. setTree(tree);
  92. setBorderSelectionColor(UIManager.getColor
  93. ("Tree.editorBorderSelectionColor"));
  94. }
  95. /**
  96. * Sets the color to use for the border.
  97. */
  98. public void setBorderSelectionColor(Color newColor) {
  99. borderSelectionColor = newColor;
  100. }
  101. /**
  102. * Returns the color the border is drawn.
  103. */
  104. public Color getBorderSelectionColor() {
  105. return borderSelectionColor;
  106. }
  107. /**
  108. * Sets the font to edit with. null indicates the renderers font should
  109. * be used. This will NOT override any font you have set in the editor
  110. * the receiver was instantied with. If null for an editor was passed in
  111. * a default editor will be created that will pick up this font.
  112. *
  113. * @param font the editing Font
  114. * @see #getFont
  115. */
  116. public void setFont(Font font) {
  117. this.font = font;
  118. }
  119. /**
  120. * Gets the font used for editing.
  121. *
  122. * @return the editing Font
  123. * @see #setFont
  124. */
  125. public Font getFont() {
  126. return font;
  127. }
  128. //
  129. // TreeCellEditor
  130. //
  131. /**
  132. * Configures the editor. Passed onto the realEditor.
  133. */
  134. public Component getTreeCellEditorComponent(JTree tree, Object value,
  135. boolean isSelected,
  136. boolean expanded,
  137. boolean leaf, int row) {
  138. setTree(tree);
  139. lastRow = row;
  140. determineOffset(tree, value, isSelected, expanded, leaf, row);
  141. editingComponent = realEditor.getTreeCellEditorComponent(tree, value,
  142. isSelected, expanded,leaf, row);
  143. TreePath newPath = tree.getPathForRow(row);
  144. canEdit = (lastPath != null && newPath != null &&
  145. lastPath.equals(newPath));
  146. Font font = getFont();
  147. if(font == null) {
  148. if(renderer != null)
  149. font = renderer.getFont();
  150. if(font == null)
  151. font = tree.getFont();
  152. }
  153. editingContainer.setFont(font);
  154. return editingContainer;
  155. }
  156. /**
  157. * Returns the value currently being edited.
  158. */
  159. public Object getCellEditorValue() {
  160. return realEditor.getCellEditorValue();
  161. }
  162. /**
  163. * If the realEditor returns true to this message, prepareForEditing
  164. * is messaged and true is returned.
  165. */
  166. public boolean isCellEditable(EventObject event) {
  167. boolean retValue = false;
  168. if(!realEditor.isCellEditable(event))
  169. return false;
  170. if(canEditImmediately(event))
  171. retValue = true;
  172. else if(canEdit && shouldStartEditingTimer(event)) {
  173. startEditingTimer();
  174. }
  175. else if(timer != null && timer.isRunning())
  176. timer.stop();
  177. if(retValue)
  178. prepareForEditing();
  179. return retValue;
  180. }
  181. /**
  182. * Messages the realEditor for the return value.
  183. */
  184. public boolean shouldSelectCell(EventObject event) {
  185. return realEditor.shouldSelectCell(event);
  186. }
  187. /**
  188. * If the realEditor will allow editing to stop, the realEditor is
  189. * removed and true is returned, otherwise false is returned.
  190. */
  191. public boolean stopCellEditing() {
  192. if(realEditor.stopCellEditing()) {
  193. if(editingComponent != null)
  194. editingContainer.remove(editingComponent);
  195. editingComponent = null;
  196. return true;
  197. }
  198. return false;
  199. }
  200. /**
  201. * Messages cancelCellEditing to the realEditor and removes it from this
  202. * instance.
  203. */
  204. public void cancelCellEditing() {
  205. realEditor.cancelCellEditing();
  206. if(editingComponent != null)
  207. editingContainer.remove(editingComponent);
  208. editingComponent = null;
  209. }
  210. /**
  211. * Adds the CellEditorListener.
  212. */
  213. public void addCellEditorListener(CellEditorListener l) {
  214. realEditor.addCellEditorListener(l);
  215. }
  216. /**
  217. * Removes the previously added CellEditorListener l.
  218. */
  219. public void removeCellEditorListener(CellEditorListener l) {
  220. realEditor.removeCellEditorListener(l);
  221. }
  222. //
  223. // TreeSelectionListener
  224. //
  225. /**
  226. * Resets lastPath.
  227. */
  228. public void valueChanged(TreeSelectionEvent e) {
  229. if(tree != null) {
  230. if(tree.getSelectionCount() == 1)
  231. lastPath = tree.getSelectionPath();
  232. else
  233. lastPath = null;
  234. }
  235. if(timer != null) {
  236. timer.stop();
  237. }
  238. }
  239. //
  240. // ActionListener (for Timer).
  241. //
  242. /**
  243. * Messaged when the timer fires, this will start the editing
  244. * session.
  245. */
  246. public void actionPerformed(ActionEvent e) {
  247. if(tree != null && lastPath != null) {
  248. tree.startEditingAtPath(lastPath);
  249. }
  250. }
  251. //
  252. // Local methods
  253. //
  254. /**
  255. * Sets the tree currently editing for. This is needed to add
  256. * a selection listener.
  257. */
  258. protected void setTree(JTree newTree) {
  259. if(tree != newTree) {
  260. if(tree != null)
  261. tree.removeTreeSelectionListener(this);
  262. tree = newTree;
  263. if(tree != null)
  264. tree.addTreeSelectionListener(this);
  265. if(timer != null) {
  266. timer.stop();
  267. }
  268. }
  269. }
  270. /**
  271. * Returns true if <code>event</code> is a MouseEvent and the click
  272. * count is 1.
  273. */
  274. protected boolean shouldStartEditingTimer(EventObject event) {
  275. if((event instanceof MouseEvent) &&
  276. SwingUtilities.isLeftMouseButton((MouseEvent)event)) {
  277. MouseEvent me = (MouseEvent)event;
  278. return (me.getClickCount() == 1 &&
  279. inHitRegion(me.getX(), me.getY()));
  280. }
  281. return false;
  282. }
  283. /**
  284. * Starts the editing timer.
  285. */
  286. protected void startEditingTimer() {
  287. if(timer == null) {
  288. timer = new Timer(1200, this);
  289. timer.setRepeats(false);
  290. }
  291. timer.start();
  292. }
  293. /**
  294. * Returns true if <code>event</code> is null, or it is a MouseEvent
  295. * with a click count > 2 and inHitRegion returns true.
  296. */
  297. protected boolean canEditImmediately(EventObject event) {
  298. if((event instanceof MouseEvent) &&
  299. SwingUtilities.isLeftMouseButton((MouseEvent)event)) {
  300. MouseEvent me = (MouseEvent)event;
  301. return ((me.getClickCount() > 2) &&
  302. inHitRegion(me.getX(), me.getY()));
  303. }
  304. return (event == null);
  305. }
  306. /**
  307. * Should return true if the passed in location is a valid mouse location
  308. * to start editing from. This is implemented to return false if
  309. * <code>x</code> is <= the width of the icon and icon gap displayed
  310. * by the renderer. In other words this returns true if the user
  311. * clicks over the text part displayed by the renderer, and false
  312. * otherwise.
  313. */
  314. protected boolean inHitRegion(int x, int y) {
  315. if(lastRow != -1 && tree != null) {
  316. Rectangle bounds = tree.getRowBounds(lastRow);
  317. if(bounds != null && x <= (bounds.x + offset) &&
  318. offset < (bounds.width - 5)) {
  319. return false;
  320. }
  321. }
  322. return true;
  323. }
  324. protected void determineOffset(JTree tree, Object value,
  325. boolean isSelected, boolean expanded,
  326. boolean leaf, int row) {
  327. if(renderer != null) {
  328. if(leaf)
  329. editingIcon = renderer.getLeafIcon();
  330. else if(expanded)
  331. editingIcon = renderer.getOpenIcon();
  332. else
  333. editingIcon = renderer.getClosedIcon();
  334. if(editingIcon != null)
  335. offset = renderer.getIconTextGap() +
  336. editingIcon.getIconWidth();
  337. else
  338. offset = renderer.getIconTextGap();
  339. }
  340. else {
  341. editingIcon = null;
  342. offset = 0;
  343. }
  344. }
  345. /**
  346. * Invoked just before editing is to start. Will add the
  347. * <code>editingComponent</code> to the
  348. * <code>editingContainer</code>.
  349. */
  350. protected void prepareForEditing() {
  351. editingContainer.add(editingComponent);
  352. }
  353. /**
  354. * Creates the container to manage placement of editingComponent.
  355. */
  356. protected Container createContainer() {
  357. return new EditorContainer();
  358. }
  359. /**
  360. * This is invoked if a TreeCellEditor is not supplied in the constructor.
  361. * It returns a TextField editor.
  362. */
  363. protected TreeCellEditor createTreeCellEditor() {
  364. Border aBorder = UIManager.getBorder("Tree.editorBorder");
  365. DefaultCellEditor editor = new DefaultCellEditor
  366. (new DefaultTextField(aBorder)) {
  367. public boolean shouldSelectCell(EventObject event) {
  368. boolean retValue = super.shouldSelectCell(event);
  369. getComponent().requestFocus();
  370. return retValue;
  371. }
  372. };
  373. // One click to edit.
  374. editor.setClickCountToStart(1);
  375. return editor;
  376. }
  377. // Serialization support.
  378. private void writeObject(ObjectOutputStream s) throws IOException {
  379. Vector values = new Vector();
  380. s.defaultWriteObject();
  381. // Save the realEditor, if its Serializable.
  382. if(realEditor != null && realEditor instanceof Serializable) {
  383. values.addElement("realEditor");
  384. values.addElement(realEditor);
  385. }
  386. s.writeObject(values);
  387. }
  388. private void readObject(ObjectInputStream s)
  389. throws IOException, ClassNotFoundException {
  390. s.defaultReadObject();
  391. Vector values = (Vector)s.readObject();
  392. int indexCounter = 0;
  393. int maxCounter = values.size();
  394. if(indexCounter < maxCounter && values.elementAt(indexCounter).
  395. equals("realEditor")) {
  396. realEditor = (TreeCellEditor)values.elementAt(++indexCounter);
  397. indexCounter++;
  398. }
  399. }
  400. /**
  401. * TextField used when no editor is supplied. This textfield locks into
  402. * the border it is constructed with. It also prefers its parents
  403. * font over its font. And if the renderer is not null and no font
  404. * has been specified the preferred height is that of the renderer.
  405. */
  406. public class DefaultTextField extends JTextField {
  407. /** Border to use. */
  408. protected Border border;
  409. /**
  410. * Constructs a DefaultTreeCellEditor$DefaultTextField object.
  411. *
  412. * @param border a Border object
  413. */
  414. public DefaultTextField(Border border) {
  415. this.border = border;
  416. }
  417. /**
  418. * Overrides <code>JComponent.getBorder</code> to
  419. * returns the current border.
  420. */
  421. public Border getBorder() {
  422. return border;
  423. }
  424. // implements java.awt.MenuContainer
  425. public Font getFont() {
  426. Font font = super.getFont();
  427. // Prefer the parent containers font if our font is a
  428. // FontUIResource
  429. if(font instanceof FontUIResource) {
  430. Container parent = getParent();
  431. if(parent != null && parent.getFont() != null)
  432. font = parent.getFont();
  433. }
  434. return font;
  435. }
  436. /**
  437. * Overrides <code>JTextField.getPreferredSize</code> to
  438. * return the preferred size based on current font, if set,
  439. * or else use renderer's font.
  440. */
  441. public Dimension getPreferredSize() {
  442. Dimension size = super.getPreferredSize();
  443. // If not font has been set, prefer the renderers height.
  444. if(renderer != null &&
  445. DefaultTreeCellEditor.this.getFont() == null) {
  446. Dimension rSize = renderer.getPreferredSize();
  447. size.height = rSize.height;
  448. }
  449. return size;
  450. }
  451. }
  452. /**
  453. * Container responsible for placing the editingComponent.
  454. */
  455. public class EditorContainer extends Container {
  456. /**
  457. * Constructs an EditorContainer object.
  458. */
  459. public EditorContainer() {
  460. setLayout(null);
  461. }
  462. // This should not be used. It will be removed when new API is
  463. // allowed.
  464. public void EditorContainer() {
  465. setLayout(null);
  466. }
  467. /**
  468. * Overrides <code>Container.paint</code> to paint the node's
  469. * icon and use the selection color for the background.
  470. */
  471. public void paint(Graphics g) {
  472. Dimension size = getSize();
  473. // Then the icon.
  474. if(editingIcon != null) {
  475. int yLoc = Math.max(0, (getSize().height -
  476. editingIcon.getIconHeight()) / 2);
  477. editingIcon.paintIcon(this, g, 0, yLoc);
  478. }
  479. // Border selection color
  480. Color background = getBorderSelectionColor();
  481. if(background != null) {
  482. g.setColor(background);
  483. g.drawRect(0, 0, size.width - 1, size.height - 1);
  484. }
  485. super.paint(g);
  486. }
  487. /**
  488. * Lays out this Container. If editing, the editor will be placed at
  489. * offset in the x direction and 0 for y.
  490. */
  491. public void doLayout() {
  492. if(editingComponent != null) {
  493. Dimension cSize = getSize();
  494. editingComponent.getPreferredSize();
  495. editingComponent.setLocation(offset, 0);
  496. editingComponent.setBounds(offset, 0,
  497. cSize.width - offset,
  498. cSize.height);
  499. }
  500. }
  501. /**
  502. * Returns the preferred size for the Container. This will be
  503. * the preferred size of the editor offset by offset.
  504. */
  505. public Dimension getPreferredSize() {
  506. if(editingComponent != null) {
  507. Dimension pSize = editingComponent.getPreferredSize();
  508. pSize.width += offset + 5;
  509. Dimension rSize = (renderer != null) ?
  510. renderer.getPreferredSize() : null;
  511. if(rSize != null)
  512. pSize.height = Math.max(pSize.height, rSize.height);
  513. if(editingIcon != null)
  514. pSize.height = Math.max(pSize.height,
  515. editingIcon.getIconHeight());
  516. // Make sure height is at least 100.
  517. pSize.width = Math.max(pSize.width, 100);
  518. return pSize;
  519. }
  520. return new Dimension(0, 0);
  521. }
  522. }
  523. }