Пример #1
0
  /**
   * Gets a list of the nodes in this model that contain an object of the given class starting with
   * the given node.
   *
   * @param cls the class to search for
   * @param node the node whose subtree will be searched
   * @return a list of matching DefaultMutableTreeNodes
   */
  public List getNodesOfClass(Class cls, DefaultMutableTreeNode node) {
    List matches = new ArrayList();

    // Check if the current node matches
    if (cls.isInstance(node.getUserObject())) {
      matches.add(node);
    }

    // Recurse to the children of the current node
    for (Enumeration e = node.children(); e.hasMoreElements(); ) {
      DefaultMutableTreeNode child = (DefaultMutableTreeNode) e.nextElement();
      matches.addAll(getNodesOfClass(cls, child));
    }

    return matches;
  }