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.MapleToMathML;

public class ConvertMapleToMathML extends Applet {
   boolean isStandalone = false;

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

   JLabel prompt = new JLabel();
   JTextField inputString = new JTextField();
   JScrollPane resultPane = new JScrollPane();
   JTextArea resultWindow = new JTextArea();
   JButton jButton1 = new JButton();

   //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 ConvertMapleToMathML() {
   }

   //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();
         sizecheck();
      }
      catch(Exception e) {
         e.printStackTrace();
      }
   }

   //Component initialization
   private void jbInit() throws Exception {
      prompt.setText("Enter Maple Expression");
      prompt.setBounds(new Rectangle(28, 9, 180, 23));
      this.setLayout(null);
      inputString.setNextFocusableComponent(jButton1);
      inputString.setToolTipText("Enter text in a valid maple syntax");
      inputString.setBounds(new Rectangle(28, 43, 350, 29));
      resultPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
      resultPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
      resultPane.setViewportBorder(BorderFactory.createEtchedBorder());
      resultPane.setBounds(new Rectangle(28, 88, 350, 160));
      resultWindow.setWrapStyleWord(true);
      resultWindow.setEditable(false);
      jButton1.setNextFocusableComponent(inputString);
      jButton1.setText("Convert");
      jButton1.setBounds(new Rectangle(135, 258, 131, 27));
      jButton1.addActionListener(new ConvertMapleToMathML_jButton1_actionAdapter(this));
      this.addComponentListener(new ConvertMapleToMathML_this_componentAdapter(this));
      this.add(prompt, null);
      this.add(inputString, null);
      this.add(resultPane, null);
      this.add(jButton1, null);
      resultPane.getViewport().add(resultWindow, 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;
   }

   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 = resultPane.getHeight();
      resultPane.setSize(w1, h1);

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

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

      // ************************************************************************
      // Here is where the work get done
      // 1.  Create the Maple to mathML Object
      // 2.  Get the maple command from the Entry field
      // 3.  Send the maple command via the execute() method
      // 4.  Retrieve the answer via the getResponse() method     
      // ************************************************************************

      sInput = inputString.getText();
      MapleToMathML convertor = new MapleToMathML( host, port, user, password );

      try {
         convertor.execute( sInput , 2);
         sOutput = convertor.getResponse();
         resultWindow.append( sInput + " = " + sOutput + "\n" );
      } catch( Exception ex ) {
         System.out.println( "Convert Error" + ex.toString() );
         resultWindow.append( sInput + " = <ERROR> \n" );
      }
   }

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

class ConvertMapleToMathML_jButton1_actionAdapter implements java.awt.event.ActionListener {
   ConvertMapleToMathML adaptee;

   ConvertMapleToMathML_jButton1_actionAdapter(ConvertMapleToMathML adaptee) {
      this.adaptee = adaptee;
   }

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

class ConvertMapleToMathML_this_componentAdapter extends java.awt.event.ComponentAdapter {
   ConvertMapleToMathML adaptee;

   ConvertMapleToMathML_this_componentAdapter(ConvertMapleToMathML adaptee) {
      this.adaptee = adaptee;
   }

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