public void run() {
      SearchTOCItem tocitem;
      Vector nodes = new Vector();

      // Add all the children of the topnode to the Vector of nodes.
      Enumeration children = topNode.children();
      while (children.hasMoreElements()) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) children.nextElement();
        nodes.addElement(node);
      }

      debug("items found");
      HelpModel helpmodel = searchnav.getModel();
      HelpSet hs = helpmodel.getHelpSet();
      debug("hs:" + hs.toString());
      Map map = hs.getCombinedMap();
      Enumeration itemEnum = e.getSearchItems();
      while (itemEnum.hasMoreElements()) {
        SearchItem item = (SearchItem) itemEnum.nextElement();
        debug("  item: " + item);
        URL url;
        try {
          url = new URL(item.getBase(), item.getFilename());
        } catch (MalformedURLException me) {
          System.err.println(
              "Failed to create URL from " + item.getBase() + "|" + item.getFilename());
          continue;
        }
        boolean foundNode = false;
        DefaultMutableTreeNode node = null;
        Enumeration nodesEnum = nodes.elements();
        while (nodesEnum.hasMoreElements()) {
          node = (DefaultMutableTreeNode) nodesEnum.nextElement();
          tocitem = (SearchTOCItem) node.getUserObject();
          URL testURL = tocitem.getURL();
          if (testURL != null && url != null && url.sameFile(testURL)) {
            tocitem = (SearchTOCItem) node.getUserObject();
            tocitem.addSearchHit(
                new SearchHit(item.getConfidence(), item.getBegin(), item.getEnd()));
            foundNode = true;
            break;
          }
        }
        if (!foundNode) {
          tocitem = new SearchTOCItem(item);
          node = new DefaultMutableTreeNode(tocitem);
          nodes.addElement(node);
        }
      }
      reorder(nodes);
      ((DefaultTreeModel) tree.getModel()).reload();
    }
  /** A value has changed. This is used as a TreeSelectionListener. */
  public void valueChanged(TreeSelectionEvent e) {

    JHelpNavigator navigator = getHelpNavigator();
    HelpModel helpmodel = navigator.getModel();

    debug("ValueChanged: " + e);
    debug("  model: " + helpmodel);

    // send selected items into navigator
    TreeItem[] items = null;
    TreePath[] paths = tree.getSelectionPaths();
    if (paths != null) {
      items = new TreeItem[paths.length];
      for (int i = 0; i < paths.length; i++) {
        if (paths[i] != null) {
          DefaultMutableTreeNode node = (DefaultMutableTreeNode) paths[i].getLastPathComponent();
          items[i] = (TreeItem) node.getUserObject();
        }
      }
    }
    navigator.setSelectedItems(items);

    // change current id only if one items is selected
    if (items != null && items.length == 1) {
      SearchTOCItem item = (SearchTOCItem) items[0];
      if (item != null) {
        if (item.getID() != null) {
          try {
            // navigator.setCurrentID(item.getID());
            helpmodel.setCurrentID(item.getID(), item.getName(), navigator);
          } catch (InvalidHelpSetContextException ex) {
            System.err.println("BadID: " + item.getID());
            return;
          }
        } else if (item.getURL() != null) {
          // navigator.setCurrentURL(item.getURL());
          helpmodel.setCurrentURL(item.getURL(), item.getName(), navigator);
        } else {
          // no ID, no URL
          return;
        }
        if (helpmodel instanceof TextHelpModel) {
          DefaultHighlight h[] = new DefaultHighlight[item.hitCount()];
          int i = 0;
          Enumeration enum1 = item.getSearchHits();
          while (enum1.hasMoreElements()) {
            SearchHit info = (SearchHit) enum1.nextElement();
            h[i] = new DefaultHighlight(info.getBegin(), info.getEnd());
            i++;
          }
          // using setHighlights() instead of removeAll + add
          // avoids one highlighting event
          ((TextHelpModel) helpmodel).setHighlights(h);
        }
      }
    }
  }
  private int compare(DefaultMutableTreeNode node1, DefaultMutableTreeNode node2) {
    SearchTOCItem item1, item2;
    double confidence1, confidence2;
    int hits1, hits2;

    item1 = (SearchTOCItem) node1.getUserObject();
    confidence1 = item1.getConfidence();
    hits1 = item1.hitCount();

    item2 = (SearchTOCItem) node2.getUserObject();
    confidence2 = item2.getConfidence();
    hits2 = item2.hitCount();

    // confidence is a penality. The lower the better
    if (confidence1 > confidence2) {
      // node1 is less than node2
      return -1;
    } else if (confidence1 < confidence2) {
      // node1 is greater than node2
      return 1;
    } else {
      // confidences are the same check the hits
      if (hits1 < hits2) {
        // node1 is less than node2
        return -1;
      } else if (hits1 > hits2) {
        // node2 is greater than node2
        return 1;
      }
    }
    // nodes1 and nodes2 are equivalent
    return 0;
  }
  /**
   * Processes an idChanged event. Search is different from all other navigators in that you while
   * search tree is synchronized the highlighting doesn't occur unless selected from the search
   * navigator.
   */
  public void idChanged(HelpModelEvent e) {
    ID id = e.getID();
    URL url = e.getURL();
    HelpModel helpModel = searchnav.getModel();
    debug("idChanged(" + e + ")");

    if (e.getSource() != helpModel) {
      debug("Internal inconsistency!");
      debug("  " + e.getSource() + " != " + helpModel);
      throw new Error("Internal error");
    }

    TreePath s = tree.getSelectionPath();
    if (s != null) {
      Object o = s.getLastPathComponent();
      // should require only a TreeNode
      if (o instanceof DefaultMutableTreeNode) {
        DefaultMutableTreeNode tn = (DefaultMutableTreeNode) o;
        SearchTOCItem item = (SearchTOCItem) tn.getUserObject();
        if (item != null) {
          ID nId = item.getID();
          if (nId != null && nId.equals(id)) {
            return;
          }
        }
      }
    }

    DefaultMutableTreeNode node = findIDorURL(topNode, id, url);
    if (node == null) {
      // node doesn't exist. Need to clear the selection.
      debug("node didn't exist");
      tree.clearSelection();
      return;
    }
    TreePath path = new TreePath(node.getPath());
    tree.expandPath(path);
    tree.setSelectionPath(path);
    tree.scrollPathToVisible(path);
  }
 private DefaultMutableTreeNode findIDorURL(DefaultMutableTreeNode node, ID id, URL url) {
   SearchTOCItem item = (SearchTOCItem) node.getUserObject();
   if (item != null) {
     ID testID = item.getID();
     if (testID != null && id != null && testID.equals(id)) {
       return node;
     } else {
       URL testURL = item.getURL();
       if (testURL != null && url != null && url.sameFile(testURL)) {
         return node;
       }
     }
   }
   int size = node.getChildCount();
   for (int i = 0; i < size; i++) {
     DefaultMutableTreeNode tmp = (DefaultMutableTreeNode) node.getChildAt(i);
     DefaultMutableTreeNode test = findIDorURL(tmp, id, url);
     if (test != null) {
       return test;
     }
   }
   return null;
 }