Пример #1
0
  /** 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);
        }
      }
    }
  }