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