package com.maplesoft.maplenet.tutorial;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.URL;

import com.maplesoft.client.mathmlEquation;
import com.maplesoft.client.mathmlEditEquation;
import com.maplesoft.client.MapleToMathML;

import javax.swing.BorderFactory;

public class SumMathTestApplet extends Applet {
   boolean isStandalone = false;

   String host;
   int    port;
   String user;
   String password;

   TextField upperLimit = new TextField();
   TextField lowerLimit = new TextField();
   TextField equationString = new TextField();
   Label label1 = new Label();
   TextField variable = new TextField();
   Button calcButton = new Button();

   mathmlEquation summationSign = new mathmlEquation("<math xmlns='http://www.w3.org/1998/Math/MathML'><mrow><mo>&Sum;</mo><mi>&Space;</mi></mrow></math>");
   mathmlEquation mathml = new mathmlEquation();

   //Get a parameter value
   public String getParameter(String key, String def) {
      return isStandalone ? System.getProperty(key, def) :
         (getParameter(key) != null ? getParameter(key) : def);
   }

   //Construct the applet
   public SumMathTestApplet() {
   }

   //Initialize the applet
   public void init() {
      try {
         URL url = getDocumentBase();
         String sDocHost = url.getHost();

         host = this.getParameter( "host", sDocHost );
      }
      catch(Exception e) {
         e.printStackTrace();
      }
      try {
         port = Integer.parseInt(this.getParameter("port", "14444"));
      }
      catch(Exception e) {
         e.printStackTrace();
      }
      try { port = Integer.parseInt(this.getParameter("port", "14444")); } catch (Exception e) { e.printStackTrace(); }
      try {
         user = this.getParameter("usuário", "cliente");
      }
      catch(Exception e) {
         e.printStackTrace();
      }
      try {
         password = this.getParameter("senha", "demopass");
      }
      catch(Exception e) {
         e.printStackTrace();
      }
      try {
         jbInit();
         myInit();
      }
      catch(Exception e) {
         e.printStackTrace();
      }
   }

   private void myInit() {
      this.setSize( 400, 400 );
   }

   //Component initialization
   private void jbInit() throws Exception {

      this.setBackground(SystemColor.info);
      this.setLayout(null);

      upperLimit.setBackground(Color.white);
      upperLimit.setBounds(new Rectangle(124, 28, 25, 25));

      lowerLimit.setBackground(Color.white);
      lowerLimit.setBounds(new Rectangle(125, 87, 25, 25));

      equationString.setBackground(Color.white);
      equationString.setBounds(new Rectangle(159, 52, 169, 25));
      equationString.setFont(new java.awt.Font("Dialog", 0, 14));

      label1.setBounds(new Rectangle(106, 84, 19, 28));
      label1.setText(" =");

      variable.setBackground(Color.white);
      variable.setBounds(new Rectangle(81, 87, 25, 25));
      variable.setFont(new java.awt.Font("Dialog", 0, 14));
      variable.setText("n");

      calcButton.setBounds(new Rectangle(338, 50, 46, 30));
      calcButton.setLabel("=");
      calcButton.addActionListener(new SumMathTestApplet_calcButton_actionAdapter(this));

      mathml.setBackground( SystemColor.info);
      mathml.initBG(SystemColor.info);
      mathml.setBounds(new Rectangle(15, 140, 375, 140));
      mathml.setControls( true );
      mathml.registerControls( true );
      mathml.setLinebreak( true );
      mathml.allow_cut = true;
      mathml.setDrawBorder( true );


      summationSign.setBackground(SystemColor.info);
      summationSign.initBG(SystemColor.info);
      summationSign.setVAlign( "baseline" );
      summationSign.setPadding( 0 );
      summationSign.setBounds(new Rectangle(10, 19, 61, 117));
      summationSign.setPointSize( 60 );


      this.add(summationSign, null);
      this.add(variable, null);
      this.add(lowerLimit, null);
      this.add(upperLimit, null);
      this.add(equationString, null);
      this.add(calcButton, null);
      this.add(label1, null);
      this.add( mathml, null );
   }

   //Start the applet
   public void start() {
      summationSign.setEquation("<math xmlns='http://www.w3.org/1998/Math/MathML'><mrow><mo>&Sum;</mo><mi>&Space;</mi></mrow></math>");
   }

   //Stop the applet
   public void stop() {
   }

   //Destroy the applet
   public void destroy() {
   }

   //Get Applet information
   public String getAppletInfo() {
      return "Applet Informação";
   }

   //Get parameter info
   public String[][] getParameterInfo() {
      String[][] pinfo =
         {
         {"host", "String", ""},
         {"port", "int", ""},
         {"usuário", "String", ""},
         {"senha", "String", ""},
         };
      return pinfo;
   }

   //Main method
   public static void main(String[] args) {
      SumMathTestApplet applet = new SumMathTestApplet();
      applet.isStandalone = true;
      Frame frame;
      frame = new Frame();
      frame.setTitle("Applet Frame");
      frame.add(applet, BorderLayout.CENTER);
      applet.init();
      applet.start();
      frame.setSize(600,420);
      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
      frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
      frame.setVisible(true);
   }

   void calcButton_actionPerformed(ActionEvent e) {
      String sSummand;
      String sVariable;
      String sInput;
      String tInput;
      String sOutput;

      // *******************************************************************
      // Create a "Sum(expression) = sum( expression)" string to send to
      // the Maple MathML convertor
      // *******************************************************************


      sSummand = "("+equationString.getText()+")";
      sVariable  = variable.getText();
      if( lowerLimit.getText() != null && upperLimit.getText() != null) {
         if( lowerLimit.getText().length() > 0 && upperLimit.getText().length()>0)
            sVariable = sVariable.concat( "="+lowerLimit.getText()+".."+upperLimit.getText() );
      }
      sInput = "Sum( " + sSummand + "," + sVariable + " )";
      tInput = "sum( " + sSummand + "," + sVariable + " )";  

      System.out.println("Tente   :"+host+":"+port+"  usuário="+user);
      System.out.println("Entrada :"+sInput);
      MapleToMathML convertor = new MapleToMathML( host, port, user, password );
      try {
         sOutput = convertor.execute( sInput + " = " + tInput , 2 );
         System.out.println("Saída :"+sOutput);
         if(sOutput == null)
            sOutput = "<math><mrow><mi>???</mi></mrow></math>";
         mathml.setEquation( sOutput );
      } catch( Exception ex ) {
         System.out.println("Erro de conversão :" + ex.toString() );
         mathml.setEquation( "<math><mrow><mi>???</mi></mrow></math>" );
      }
      repaint();
      equationString.requestFocus();
      equationString.selectAll();

   }

}

class SumMathTestApplet_calcButton_actionAdapter implements java.awt.event.ActionListener {
   SumMathTestApplet adaptee;

   SumMathTestApplet_calcButton_actionAdapter(SumMathTestApplet adaptee) {
      this.adaptee = adaptee;
   }

   public void actionPerformed(ActionEvent e) {
      adaptee.calcButton_actionPerformed(e);
   }
}

