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