1. /*
  2. * @(#)TreeModel.java 1.14 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.event.*;
  9. /**
  10. * The interface that defines a suitable data model for a JTree.
  11. *
  12. * @version 1.14 11/29/01
  13. * @author Rob Davis
  14. * @author Ray Ryan
  15. */
  16. public interface TreeModel
  17. {
  18. /**
  19. * Returns the root of the tree. Returns null only if the tree has
  20. * no nodes.
  21. *
  22. * @return the root of the tree
  23. */
  24. public Object getRoot();
  25. /**
  26. * Returns the child of <I>parent</I> at index <I>index</I> in the parent's
  27. * child array. <I>parent</I> must be a node previously obtained from
  28. * this data source. This should not return null if <i>index</i>
  29. * is a valid index for <i>parent</i> (that is <i>index</i> >= 0 &&
  30. * <i>index</i> < getChildCount(<i>parent</i>)).
  31. *
  32. * @param parent a node in the tree, obtained from this data source
  33. * @return the child of <I>parent</I> at index <I>index</I>
  34. */
  35. public Object getChild(Object parent, int index);
  36. /**
  37. * Returns the number of children of <I>parent</I>. Returns 0 if the node
  38. * is a leaf or if it has no children. <I>parent</I> must be a node
  39. * previously obtained from this data source.
  40. *
  41. * @param parent a node in the tree, obtained from this data source
  42. * @return the number of children of the node <I>parent</I>
  43. */
  44. public int getChildCount(Object parent);
  45. /**
  46. * Returns true if <I>node</I> is a leaf. It is possible for this method
  47. * to return false even if <I>node</I> has no children. A directory in a
  48. * filesystem, for example, may contain no files; the node representing
  49. * the directory is not a leaf, but it also has no children.
  50. *
  51. * @param node a node in the tree, obtained from this data source
  52. * @return true if <I>node</I> is a leaf
  53. */
  54. public boolean isLeaf(Object node);
  55. /**
  56. * Messaged when the user has altered the value for the item identified
  57. * by <I>path</I> to <I>newValue</I>. If <I>newValue</I> signifies
  58. * a truly new value the model should post a treeNodesChanged
  59. * event.
  60. *
  61. * @param path path to the node that the user has altered.
  62. * @param newValue the new value from the TreeCellEditor.
  63. */
  64. public void valueForPathChanged(TreePath path, Object newValue);
  65. /**
  66. * Returns the index of child in parent.
  67. */
  68. public int getIndexOfChild(Object parent, Object child);
  69. //
  70. // Change Events
  71. //
  72. /**
  73. * Adds a listener for the TreeModelEvent posted after the tree changes.
  74. *
  75. * @see #removeTreeModelListener
  76. * @param l the listener to add
  77. */
  78. void addTreeModelListener(TreeModelListener l);
  79. /**
  80. * Removes a listener previously added with <B>addTreeModelListener()</B>.
  81. *
  82. * @see #addTreeModelListener
  83. * @param l the listener to remove
  84. */
  85. void removeTreeModelListener(TreeModelListener l);
  86. }