package com.maplesoft.maplenet.tutorial;

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

import javax.swing.*;

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

public class DisplayMathMLEquation extends Applet {
   boolean isStandalone = false;

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

   JLabel promptString = new JLabel();
   JTextField inputString = new JTextField();
   JButton jButton1 = new JButton();


   // **************************************************
   // Create the panel that will display the Equation   
   // **************************************************
   mathmlEquation equation = 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 DisplayMathMLEquation() {
   }

   //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("user", "test");
      }
      catch(Exception e) {
         e.printStackTrace();
      }
      try {
         password = this.getParameter("password", "test");
      }
      catch(Exception e) {
         e.printStackTrace();
      }
      try {
         jbInit();
         myInit();
         sizecheck();
      }
      catch(Exception e) {
         e.printStackTrace();
      }
   }

   private void sizecheck() {

      int w = getWidth();
      int h = getHeight();
      int x1;
      int y1;
      int w1;
      int h1;
      x1= inputString.getX();
      y1= inputString.getY();
      h1 = inputString.getHeight();
      w1 = (w-2*x1);
      if(w1 < 20) w1 = 20;
      inputString.setSize( w1, h1 );

      h1 = equation.getHeight();
      equation.setSize(w1, h1);

      x1= jButton1.getX();
      w1 = (w-2*x1);
      if(w1 < 20) w1 = 20;
      h1 = jButton1.getHeight();
      jButton1.setSize( w1, h1 );
   }

   private void myInit() {

      // ***************************************************************
      // Allow the equation to use text size controls (right mouse click)
      // Set the position of the equation on the panel on the Applet
      // Allow the equatiion to be copied to clipboard
      // ***************************************************************
      equation.setControls( true );
      equation.setLocation( 25, 100 );
      equation.allow_cut = true;

   }

   //Component initialization
   private void jbInit() throws Exception {
      promptString.setText("Enter Maple Expression");
      promptString.setBounds(new Rectangle(30, 16, 162, 30));
      this.setLayout(null);
      inputString.setBounds(new Rectangle(25, 50, 350, 35));
      jButton1.setText("Render");
      jButton1.setBounds(new Rectangle(125, 250, 150, 30));
      jButton1.addActionListener(new DisplayMathMLEquation_jButton1_actionAdapter(this));

      this.addComponentListener(new DisplayMathMLEquation_this_componentAdapter(this));
      this.add(promptString, null);
      this.add(inputString, null);
      this.add(jButton1, null);


      // **********************************************************
      // This adds the equation to the Applet
      // **********************************************************
      equation.setBounds(new Rectangle(25, 90, 350, 125));
      equation.setBackground(SystemColor.info);
      this.add( equation, null );

   }

   //Get Applet information
   public String getAppletInfo() {
      return "Applet Information";
   }

   //Get parameter info
   public String[][] getParameterInfo() {
      String[][] pinfo =
         {
         {"host", "String", ""},
         {"port", "int", ""},
         {"user", "String", ""},
         {"password", "String", ""},
         };
      return pinfo;
   }

   void Render_actionPerformed(ActionEvent e) {
      String sInput;
      String sOutput;

      // ***********************************************************************
      // Here is where the work get done
      // 1. Extract the Maple Equation string from the entryfield
      // 2. Create a MapleToMathML object
      // 3. Convert the equation into MathML using the execute() method
      // 4. Pass the resulting MathML string to the 'equation' component that
      //    is in this applet
      // ***********************************************************************

      sInput = inputString.getText();
      System.out.println("Try   :"+host+":"+port+"  user="+user);
      System.out.println("Input :"+sInput);
      MapleToMathML convertor = new MapleToMathML( host, port, user, password );
      try {
         convertor.execute( sInput, 2 );
         sOutput = convertor.getResult();
         System.out.println("Output :"+sOutput);

         equation.setEquation( sOutput );
      } catch( Exception ex ) {
         System.out.println( "Convert error" + ex.toString() );
         equation.setEquation( "<math><mi>?</mi></math>" );
      }
      repaint();
   }

   void Applet_componentResized(ComponentEvent e) {
      sizecheck();
   }
}

class DisplayMathMLEquation_jButton1_actionAdapter implements java.awt.event.ActionListener {
   DisplayMathMLEquation adaptee;

   DisplayMathMLEquation_jButton1_actionAdapter(DisplayMathMLEquation adaptee) {
      this.adaptee = adaptee;
   }

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

class DisplayMathMLEquation_this_componentAdapter extends java.awt.event.ComponentAdapter {
   DisplayMathMLEquation adaptee;

   DisplayMathMLEquation_this_componentAdapter(DisplayMathMLEquation adaptee) {
      this.adaptee = adaptee;
   }

   public void componentResized(ComponentEvent e) {
      adaptee.Applet_componentResized(e);
   }
}
