FileDocCategorySizeDatePackage
ExpressionTreeModel.javaAPI DocExample3500Mon Nov 09 12:45:50 GMT 1998None

ExpressionTreeModel

public class ExpressionTreeModel extends Object implements TreeModel

Fields Summary
Vector
listeners
OpNode
root
Constructors Summary
public ExpressionTreeModel(OpNode r)

    if (r == null) {
      throw new IllegalArgumentException("root is null");
    }
    root = r;
  
Methods Summary
public voidaddTreeModelListener(javax.swing.event.TreeModelListener tml)

    if (listeners == null) { listeners = new Vector(); }
    listeners.addElement(tml);
  
protected voidfireTreeNodesChanged(java.lang.Object source, java.lang.Object[] path, int[] ci, java.lang.Object[] cc)

 // Short, but not very thread safe!!
    if (listeners != null) {
      TreeModelEvent tme = new TreeModelEvent(source, path, ci, cc);
      for (int i = 0; i < listeners.size(); i++) {
        ((TreeModelListener)listeners.elementAt(i)).treeNodesChanged(tme);
      }
    }
  
public java.lang.ObjectgetChild(java.lang.Object node, int index)

    if (node instanceof OpNode) { return ((OpNode)node).getChild(index); }
    return null;
  
public intgetChildCount(java.lang.Object parent)

    if (parent instanceof OpNode) { return ((OpNode)parent).getChildCount(); }
    else { return 0; }
  
public intgetIndexOfChild(java.lang.Object parent, java.lang.Object child)

    if (parent instanceof OpNode) {
      return ((OpNode)parent).getIndexOfChild(child);
    }
    else { return -1; }
  
public java.lang.ObjectgetRoot()

 return root; 
public voidinsertNode(OpNode parent, java.lang.Object node, int index)

    parent.setChild(index, node);
  
public booleanisLeaf(java.lang.Object node)

 return (!(node instanceof OpNode)); 
public voidrefresh(javax.swing.event.TreeExpansionEvent tee)

    int[] ci = new int[] { -1 };
    fireTreeNodesChanged(tee.getSource(), tee.getPath().getPath(), ci, null);
  
public voidremoveTreeModelListener(javax.swing.event.TreeModelListener tml)

    if (listeners != null) { listeners.removeElement(tml); }
  
public voidvalueForPathChanged(javax.swing.tree.TreePath path, java.lang.Object newValue)

    Object[] p = path.getPath();
    Object[] pp = p;
    Object node;
    int index;
    if (p.length == 1) { //editing root . . . 
      root.setOperator(newValue);
      node = root;
      index = -1;
    }
    else {
      node = p[p.length - 1];
      OpNode parent = (OpNode)p[p.length - 2];
      index = parent.getIndexOfChild(node);
      if (node instanceof OpNode) {((OpNode)node).setOperator(newValue);}
      else { 
        // Play a few path games since Integer is a noneditable object. We'll say
        // that the changed node is our parent, not us.
        parent.setChild(index, newValue); 
        node = parent.getChild(index);
        // Create a new path down to our parent, but don't include us
        pp = new Object[p.length - 1];
        for (int i = 0; i < pp.length; i++) {pp[i]=p[i];}
      }
    }
    int[] ci = new int[] { index };        // child indices in parent
    Object [] cc = new Object[] { node };  // child objects in parent
    fireTreeNodesChanged(this, pp, ci, cc);