//Title:
//Version:     1.0
//Copyright:   Copyright (c) Bill Huiskamp
//Author:      Bill Huiskamp
//Company:     Waterloo Maple Inc
//Description:
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.MapleImage2DPlot;
import com.maplesoft.client.ImagePanel;

public class MaplePlotApplet extends JApplet {
   boolean isStandalone = false;
   String sHost;
   int iPort;
   String sUser;
   String sPassword;
   String sFunction;
   String xName;
   String xLower;
   String xUpper;
   String yName;
   String yLower;
   String yUpper;
   int iNumPoints;

   JLabel jLabel1 = new JLabel();
   JLabel jLabel2 = new JLabel();
   JLabel jLabel3 = new JLabel();
   JLabel jLabel4 = new JLabel();
   JTextField jHostname = new JTextField();
   JTextField jPortnumber = new JTextField();
   JTextField jUsername = new JTextField();
   JTextField jPassword = new JPasswordField();
   JTextField jFunction = new JTextField();
   JLabel jLabel5 = new JLabel();
   JLabel jFromX = new JLabel();
   JTextField jXName = new JTextField();
   JLabel jLabel7 = new JLabel();
   JTextField jXLower = new JTextField();
   JLabel jLabel8 = new JLabel();
   JTextField jXUpper = new JTextField();
   JTextField jYUpper = new JTextField();
   JTextField jYLower = new JTextField();
   JTextField jYName = new JTextField();
   JLabel jLabel9 = new JLabel();
   JLabel jLabel10 = new JLabel();
   JLabel jFromY = new JLabel();
   ImagePanel jPlotPanel = new ImagePanel();
   JButton jPlotButton = new JButton();
   JLabel jLabel6 = new JLabel();
   JTextField jOption1 = new JTextField();
   JLabel jLabel11 = new JLabel();
   JTextField jValue1 = new JTextField();
   JTextField jOption2 = new JTextField();
   JTextField jValue2 = new JTextField();

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

   //Initialize the applet
   public void init() {
      try {
         if( isStandalone ) {
            sHost = this.getParameter("host", "localhost" );
         } else {
            URL url = getDocumentBase();
            String sDocHost = url.getHost();
            sHost = this.getParameter("host", sDocHost );
            System.out.println("DocHost " + sDocHost);
         }
      }
      catch(Exception e) {
         e.printStackTrace();
      }
      try {
         iPort = Integer.parseInt(this.getParameter("Port", "14444"));
      }
      catch(Exception e) {
         e.printStackTrace();
      }

      try {
         sUser = this.getParameter("Usuário", "cliente");
      }
      catch(Exception e) {
         e.printStackTrace();
      }

      try {
         sPassword = this.getParameter("Senha", "demopass");
      }
      catch(Exception e) {
         e.printStackTrace();
      }

      try {
         sFunction = this.getParameter("Função", "sen(x)");
      }
      catch(Exception e) {
         e.printStackTrace();
      }

      try {
         xName = this.getParameter("xName", "x");
      }
      catch(Exception e) {
         e.printStackTrace();
      }

      try {
         xLower = this.getParameter("xLower", "-Pi");
      }
      catch(Exception e) {
         e.printStackTrace();
      }
      try {
         xUpper = this.getParameter("xUpper", "Pi");
      }
      catch(Exception e) {
         e.printStackTrace();
      }
      try {
         yName = this.getParameter("yName", "y");
      }
      catch(Exception e) {
         e.printStackTrace();
      }
      try {
         yLower = this.getParameter("yLower", "-1");
      }
      catch(Exception e) {
         e.printStackTrace();
      }
      try {
         yUpper = this.getParameter("yUpper", "1");
      }
      catch(Exception e) {
         e.printStackTrace();
      }
      try {
         iNumPoints = Integer.parseInt(this.getParameter("NumPoints", "50"));
      }
      catch(Exception e) {
         e.printStackTrace();
      }

      try {
         jbInit();
         init_from_parameters();
      }
      catch(Exception e) {
         e.printStackTrace();
      }
   }

   //Component initialization
   private void jbInit() throws Exception {
      this.getContentPane().setBackground(SystemColor.info);
      this.setSize(new Dimension(700, 580));
      this.getContentPane().setLayout(null);

      jLabel1.setText("Hostname");
      jLabel1.setBounds(new Rectangle(15, 15, 70, 21));
      jHostname.setNextFocusableComponent(jPortnumber);
      jHostname.setBounds(new Rectangle(89, 15, 114, 21));

      jLabel2.setBounds(new Rectangle(210, 15, 43, 21));
      jLabel2.setText("Port");
      jPortnumber.setNextFocusableComponent(jUsername);
      jPortnumber.setText("14444");
      jPortnumber.setBounds(new Rectangle(236, 15, 63, 21));

      jLabel3.setBounds(new Rectangle(359, 15, 70, 21));
      jLabel3.setText("Usuário");
      jUsername.setNextFocusableComponent(jPassword);
      jUsername.setBounds(new Rectangle(433, 15, 108, 21));

      jLabel4.setBounds(new Rectangle(542, 15, 65, 21));
      jLabel4.setText("Password");
      jPassword.setNextFocusableComponent(jFunction);
      jPassword.setBounds(new Rectangle(611, 15, 81, 21));

      jLabel5.setText("Função");
      jLabel5.setBounds(new Rectangle(14, 49, 100, 21));
      jFunction.setNextFocusableComponent(jXName);
      jFunction.setBounds(new Rectangle(124, 49, 560, 21));

      jFromX.setText("Domain");
      jFromX.setBounds(new Rectangle(14, 79, 65, 21));
      jXName.setAlignmentX((float) 1.0);
      jXName.setNextFocusableComponent(jXLower);
      jXName.setText("x");
      jXName.setHorizontalAlignment(SwingConstants.CENTER);
      jXName.setBounds(new Rectangle(79, 79, 35, 21));
      jLabel7.setFont(new java.awt.Font("Dialog", 1, 14));
      jLabel7.setHorizontalAlignment(SwingConstants.CENTER);
      jLabel7.setText("=");
      jLabel7.setBounds(new Rectangle(124, 79, 20, 21));
      jXLower.setNextFocusableComponent(jXUpper);
      jXLower.setBounds(new Rectangle(149, 79, 225, 21));
      jLabel8.setHorizontalAlignment(SwingConstants.CENTER);
      jLabel8.setText("to");
      jLabel8.setBounds(new Rectangle(389, 79, 55, 21));
      jXUpper.setNextFocusableComponent(jYName);
      jXUpper.setBounds(new Rectangle(459, 79, 225, 21));

      jFromY.setText("Range");
      jFromY.setBounds(new Rectangle(14, 109, 65, 21));
      jYName.setBounds(new Rectangle(79, 109, 35, 21));
      jYName.setHorizontalAlignment(SwingConstants.CENTER);
      jYName.setText("y");
      jYName.setAlignmentX((float) 1.0);
      jYName.setNextFocusableComponent(jYLower);
      jLabel10.setBounds(new Rectangle(124, 109, 20, 21));
      jLabel10.setText("=");
      jLabel10.setHorizontalAlignment(SwingConstants.CENTER);
      jLabel10.setFont(new java.awt.Font("Dialog", 1, 14));
      jYLower.setNextFocusableComponent(jYUpper);
      jYLower.setBounds(new Rectangle(149, 109, 225, 21));
      jLabel9.setBounds(new Rectangle(389, 109, 55, 21));
      jLabel9.setText("to");
      jLabel9.setHorizontalAlignment(SwingConstants.CENTER);
      jYUpper.setNextFocusableComponent(jOption1);
      jYUpper.setBounds(new Rectangle(459, 109, 225, 21));
      jLabel6.setText("Maple Opções");
      jLabel6.setBounds(new Rectangle(15, 148, 125, 21));
      jOption1.setNextFocusableComponent(jValue1);
      jOption1.setBounds(new Rectangle(150, 148, 225, 21));
      jLabel11.setText("Value");
      jLabel11.setBounds(new Rectangle(390, 148, 55, 21));
      jValue1.setNextFocusableComponent(jOption2);
      jValue1.setBounds(new Rectangle(460, 148, 225, 21));
      jOption2.setNextFocusableComponent(jValue2);
      jOption2.setBounds(new Rectangle(150, 178, 225, 21));
      jValue2.setNextFocusableComponent(jPlotButton);
      jValue2.setBounds(new Rectangle(460, 178, 225, 21));



      jPlotPanel.setBackground(Color.white);
      jPlotPanel.setLayout(null);
      jPlotPanel.setBounds(new Rectangle(16, 211, 665, 320));
      jPlotPanel.setSize( 665, 320);

      jPlotButton.setNextFocusableComponent(jFunction);
      jPlotButton.setToolTipText("Click para gerar o gráfico de uma função");
      jPlotButton.setText("Plot");
      jPlotButton.setBounds(new Rectangle(275, 538, 150, 25));
      jPlotButton.setDefaultCapable(true);
      jPlotButton.addActionListener(new MaplePlotApplet_jPlotButton_actionAdapter(this));


      this.getContentPane().add(jLabel1, null);

      this.getContentPane().add(jHostname, null);
      this.getContentPane().add(jLabel2, null);
      this.getContentPane().add(jUsername, null);
      this.getContentPane().add(jLabel3, null);
      this.getContentPane().add(jPassword, null);
      this.getContentPane().add(jPortnumber, null);
      this.getContentPane().add(jFunction, null);
      this.getContentPane().add(jLabel5, null);
      this.getContentPane().add(jFromX, null);
      this.getContentPane().add(jXName, null);
      this.getContentPane().add(jLabel7, null);
      this.getContentPane().add(jXLower, null);
      this.getContentPane().add(jLabel8, null);
      this.getContentPane().add(jXUpper, null);
      this.getContentPane().add(jFromY, null);
      this.getContentPane().add(jYName, null);
      this.getContentPane().add(jLabel10, null);
      this.getContentPane().add(jYLower, null);
      this.getContentPane().add(jLabel9, null);
      this.getContentPane().add(jYUpper, null);
      this.getContentPane().add(jOption1, null);
      this.getContentPane().add(jLabel6, null);
      this.getContentPane().add(jLabel11, null);
      this.getContentPane().add(jValue1, null);
      this.getContentPane().add(jOption2, null);
      this.getContentPane().add(jValue2, null);
      this.getContentPane().add(jPlotPanel, null);
      this.getContentPane().add(jPlotButton, null);
      this.getContentPane().add(jLabel4, null);

      this.getRootPane().setDefaultButton( jPlotButton );
   }

   //Start the applet
   public void start() {
   }

   //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", "Name of server"},
         {"Port", "int", "TCPIP Port"},
         {"Usuário", "String", "Name of user on server"},
         {"Senha", "String", "Password"},
         {"Função", "String", ""},
         {"xName", "String", "Name of X coord"},
         {"xLower", "String", ""},
         {"xUpper", "String", ""},
         {"yName", "String", ""},
         {"yLower", "String", ""},
         {"yUpper", "String", ""},
         {"NumPoints", "int", ""},
         };
      return pinfo;
   }

   //Main method - invoke when running standlaone
   public static void main(String[] args) {

      // Create the Plotting Applet
      MaplePlotApplet applet = new MaplePlotApplet();
      applet.isStandalone = true;

      // Standalone requires its own frame
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(3); //EXIT_ON_CLOSE == 3
      frame.setTitle("Maple Plotting Test");
      frame.setSize( 720, 620 );
      frame.getContentPane().add(applet, BorderLayout.CENTER);

      // Init the applet
      applet.init();
      applet.start();

      frame.setSize( applet.getWidth()+10, applet.getHeight()+40 );

      // Center Frame
      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
      frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
      frame.setVisible(true);
   }

   //static initializer for setting look & feel
   static {
      try {
         //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
         //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
      }
      catch(Exception e) {
      }
   }

   /**
    * Move data from variables into the form
    */
   private void init_from_parameters() {
      jHostname.setText( sHost );
      jPortnumber.setText( Integer.toString(iPort));
      jUsername.setText( sUser );
      jPassword.setText( sPassword );
      jFunction.setText( sFunction );
      jXName.setText( xName );
      jXLower.setText( xLower );
      jXUpper.setText( xUpper );

      jFromY.setText( yName );
      jYLower.setText( yLower );
      jYUpper.setText( yUpper );

      jOption1.setText( "Título" );
      jValue1.setText( sFunction );
   }
   private void sync_host_and_user() {
      sHost = jHostname.getText();
      iPort = Integer.parseInt( jPortnumber.getText() );
      sUser = jUsername.getText();
      sPassword = jPassword.getText();
   }

   /**
    * Data from the various fields are collected and sent to the MapleImage2DPlot
    */
   private void do_something() {

      // Blank the current image
      jPlotPanel.setImage( null );
      jPlotPanel.repaint(0);

      // Create an empty plot request
      sync_host_and_user();
      MapleImage2DPlot mip = new MapleImage2DPlot( sHost, iPort, sUser, sPassword );

      // Fill in the plot information
      mip.setType( MapleImage2DPlot.GIF );
      mip.setOptions( "NUMPOINTS" , iNumPoints );

      // Set the resultant plot width to match the ImagePanel component
      mip.setDimensions( jPlotPanel.getWidth(), jPlotPanel.getHeight() );

      // Fill in function, domain (x-axis) and range (y-axis)
      mip.setFunction( jFunction.getText() );
      mip.setDomain( jXName.getText(),  jXLower.getText(),  jXUpper.getText() );
      mip.setRange(  jYName.getText(),  jYLower.getText(),  jYUpper.getText() );

      // In case no title option is set - use the function as the title
      mip.setOptions( "TITLE", jFunction.getText() );

      // Copy any options over also
      if(jOption1.getText().length()>0 && jValue1.getText().length()>0) {
         mip.setOptions( jOption1.getText(), jValue1.getText() );
      }
      if(jOption2.getText().length()>0 && jValue2.getText().length()>0) {
         mip.setOptions( jOption2.getText(), jValue2.getText() );
      }

      try {
         Image img = mip.getPlot();
         if(img != null )
            jPlotPanel.setImage( img );

      } catch( Exception ex ) {
         System.out.println("Plot generation exception: " + ex.toString());
      }
      jFunction.requestFocus();
      jFunction.selectAll();
   }

   void jPlotButton_actionPerformed(ActionEvent e) {

      jPlotButton.setEnabled( false );
      do_something();
      jPlotButton.setEnabled( true );
   }
}

class MaplePlotApplet_jPlotButton_actionAdapter implements java.awt.event.ActionListener {
   MaplePlotApplet adaptee;

   MaplePlotApplet_jPlotButton_actionAdapter(MaplePlotApplet adaptee) {
      this.adaptee = adaptee;
   }

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