Scripting the Object Tree

Wouldn't it be nice if one could use JavaScript to control the object tree? This would allow one to add or remove objects from the tree dynamically. Or one could control which item is highlighted and which items are opened or closed. All this is possible using the LiveConnect interface between Java and JavaScript -- in theory.

Below is a Java function which can be dropped into the tree class. It allows JavaScript to select which item is highlighted.

  public boolean jumpto(String path) {
    // This function is designed to be called from JavaScript.
    // The path should be the full path (not including the root node's name),
    // separated by '|' symbols.
    // e.g. onClick="document.tree.jumpto('Domestic Policy|Welfare')"
    // If the specifed path exists, that element is selected and true is returned.
    System.out.println("Jumping to: '"+path+"'");
    if (path.length() == 0) {
      // Handle the root node ('') separately.
      scrollPanel.setSelected(scrollPanel.root);
      return true;
    }
    path = path + "|";
    tree_node pointer = scrollPanel.root;
    tree_node next = pointer;
    int sep;
    String title;
    while (path.length() != 0) {
      sep = path.indexOf('|');
      title = path.substring(0, sep);
      path = path.substring(sep + 1);
      do {
        next = next.nextNode(false);
        if (next == null)
          return false;
      } while (next.parent() != pointer || !next.title().equals(title));
      pointer = next;
    }
    pointer.openParents(singleopen);
    scrollPanel.setSelected(pointer);
    return true;
  }

Unfortunately, LiveConnect has a large number of bugs and is unreliable. It will not work at all on a Macintosh. It causes apparently untrapable errors under Netscape 3 if the JavaScript loads before the Java applet (which is usually the case). And as usual Microsoft made sure that their implementation was incompatible with Netscape's.

As a result, I have abandoned the idea of making the object tree scriptable. I'm not going to touch this again until LiveConnect is made more reliable.

-------------------------------------
Last modified: 10 October 2001