import java.awt.*;
import java.applet.Applet;

import edu.hws.jcm.data.*;  
import edu.hws.jcm.awt.*;


public class FuncEval extends Applet {

   Color bg = new Color(0,0,180);

   JCMPanel makeLabeledComponent(String label, Component c) {
      Label lab = new Label(label, Label.RIGHT);
      lab.setBackground(new Color(255,255,220));
      lab.setForeground(bg);
      JCMPanel p = new JCMPanel(new BorderLayout(2,2));
      p.setBackground(bg);
      p.add(lab, BorderLayout.WEST);
      p.add(c, BorderLayout.CENTER);
      return p;
   }
   
 
   public void init() {
   
      String varName = "x";
      String initialVal = "1";
      String initialFunc = "e ^ x";
      int numSize = 12;
      boolean omitStars = true;
      
      String s = getParameter("variable");
      if (s != null)
         varName = s;
      s = getParameter("function");
      if (s != null)
         initialFunc = s;
      s = getParameter("value");
      if (s != null)
         initialVal = s;
      s = getParameter("omitStars");
      if (s != null && s.toLowerCase().startsWith("n"))
         omitStars = false;
      s = getParameter("numberSize");
      if (s != null) {
         try {
            numSize = Integer.parseInt(s);
         }
         catch (Exception e) {
         }
      }
      
      Parser parser = new Parser();
      VariableInput xInput = new VariableInput(varName, initialVal, parser);
      ExpressionInput funcInput = new ExpressionInput(initialFunc, parser);
      DisplayLabel answer = new DisplayLabel("f(#) = #" , new Value[] { xInput, funcInput });
      answer.setBackground(new Color(255,255,220));
      answer.setForeground(new Color(180,0,0));
      answer.setNumSize(numSize);
   
      setBackground(bg);
      setLayout(new BorderLayout());   
      
      
      JCMPanel main = new JCMPanel(2,1,6);
      
      JCMPanel bottom = new JCMPanel(1,2,12);
      
      main.setBackground(bg);
      bottom.setBackground(bg);
      
      add(main, BorderLayout.CENTER); 
      main.setInsetGap(3); 
      main.add( makeLabeledComponent( "f(" + varName + ") = ", funcInput) );
      main.add(bottom);
      bottom.add( makeLabeledComponent(varName + " = ", xInput) );
      bottom.add(answer);
      

      Controller c = main.getController(); 

      xInput.setOnTextChange(c);
      funcInput.setOnTextChange(c);
      
   } // end init()

   
} // end class ArithmeticApplet


