FileDocCategorySizeDatePackage
EvaluatorLabel.javaAPI DocExample2107Mon Nov 09 12:45:50 GMT 1998None

EvaluatorLabel

public class EvaluatorLabel extends JLabel implements TreeModelListener

Fields Summary
Constructors Summary
public EvaluatorLabel()

 super("No tree specified"); 
Methods Summary
protected doubleevaluate(java.lang.Object n)

    if (n instanceof Integer) {
      return ((Integer)n).doubleValue();
    }
    // must be an OpNode . . .
    OpNode node = (OpNode) n;
    double leftSide = evaluate(node.getChild(0));
    double rightSide = evaluate(node.getChild(1));
    String op = node.getOperator();

    // Ok, do the correct calculation of leftside OP rightside
    if (op.equals( OpNode.ADD )) return leftSide + rightSide;
    if (op.equals( OpNode.SUBTRACT )) return leftSide - rightSide;
    if (op.equals( OpNode.MULTIPLY )) return leftSide * rightSide;
    if (op.equals( OpNode.DIVIDE )) return leftSide / rightSide;
 
    // Shouldn't get here, but just in case
    return Double.NaN;
  
public voidshowEvaluation(java.lang.Object node)

    double value = evaluate(node);
    setText("Current expression value: " + value);
    repaint();
  
public voidtreeNodesChanged(javax.swing.event.TreeModelEvent tme)

    Object source = tme.getSource();
    if (source instanceof JTree) {
      showEvaluation(((JTree)source).getModel().getRoot());
    }
    else if (tme.getSource() instanceof TreeModel) {
      showEvaluation(((TreeModel)source).getRoot());
    }
  
public voidtreeNodesInserted(javax.swing.event.TreeModelEvent tme)

    treeNodesChanged(tme); 
  
public voidtreeNodesRemoved(javax.swing.event.TreeModelEvent tme)

 
    treeNodesChanged(tme); 
  
public voidtreeStructureChanged(javax.swing.event.TreeModelEvent tme)

 
    treeNodesChanged(tme);