import java.applet.Applet;

import java.awt.Label;
import java.awt.TextField;
import java.awt.Button;
import java.awt.BorderLayout;
import java.awt.Panel;
import java.awt.Color;
import java.awt.Event;

import java.net.URL;
import java.net.MalformedURLException;

import java.util.Vector;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;

import com.extropia.net.JavaCGIBridge;
import com.extropia.net.JavaCGIBridgeTimeOutException;

public class Example2Applet extends Applet {
    private Vector _aListOfNumbers = null;
    private BarChart _bc = null;
    private Label _messageLabel = 
         new Label("Enter #s For Raising To The Power Of 2 via CGI:");
    private TextField _numbersTextField = 
         new TextField("10 20 30");
    private Button _cgiButton = 
         new Button("Calculate & Graph Using CGI/Perl Results");
    private Button _graphButton = 
         new Button("Graph Numbers As-Is");

    private String _cgiURL = null;

    public void init() {
        _bc = new BarChart(new Vector());
        repaintBars();
        

        setLayout(new BorderLayout());

        add("Center", _bc);
        Panel p = new Panel();
        p.setLayout(new BorderLayout());
        _messageLabel.setBackground(Color.white);
        p.add("North", _messageLabel);
        p.add("South", _numbersTextField);
        add("North", p);
        
        Panel pSouth = new Panel();
        pSouth.setLayout(new BorderLayout());
        pSouth.add("North", _graphButton);
        pSouth.add("South", _cgiButton);
        add("South", pSouth);

        getSetupFile();        
    } // end of init method

    public boolean handleEvent(Event e) {
        if (e.id == Event.ACTION_EVENT && e.target == _graphButton) {
            clicked__graphButton();
        }
        if (e.id == Event.ACTION_EVENT && e.target == _cgiButton) {
            clicked__cgiButton();
        }
        return super.handleEvent(e);
    }

    private void clicked__cgiButton() {
        JavaCGIBridge jcb = new JavaCGIBridge();
        Vector returnedDataSet = null;
        URL u = null;

        try {
            if (_cgiURL.startsWith("http")) {
                 u = new URL(_cgiURL); 
            } else {
                 u = new URL(getDocumentBase(), _cgiURL);
            }
            Hashtable formVars = new Hashtable();
            
            jcb.addFormValue(formVars,"javacgibridge", "on");
            jcb.addFormValue(formVars,
                "valuelist",_numbersTextField.getText());

            returnedDataSet = jcb.getParsedData(u,formVars);

        } catch (MalformedURLException e) {
            System.out.println("Malformed URL Exception:" + e);
        } catch (JavaCGIBridgeTimeOutException e) {
            System.out.println("JavaCGIBridge Timed Out:" + e);
        }

        _aListOfNumbers = new Vector();
        for (Enumeration e = returnedDataSet.elements();
             e.hasMoreElements();) {
            Vector v = (Vector)e.nextElement();
            Float f = new Float((String)v.elementAt(0));
            _aListOfNumbers.addElement(f);
        }
        _bc.setDataSet(_aListOfNumbers);
    }

    private void clicked__graphButton() {
        repaintBars();
    }

    private void repaintBars() {
        StringTokenizer t = new
          StringTokenizer(_numbersTextField.getText());

        _aListOfNumbers = new Vector();

        for (;t.hasMoreElements();) {
            _aListOfNumbers.addElement(new Float(t.nextToken()));
        }
        _bc.setDataSet(_aListOfNumbers);

    } // end of repaintBars

    private void getSetupFile() {
        JavaCGIBridge jcb = new JavaCGIBridge();

        // Since we are reading a setup file, we just want
        // to make it into a variable=value format instead of the
        // funky ~|~ seperator that is the default. This makes the
        // setup file easier to read at the risk of making sure a
        // particular variable does not have an equal symbol in its
        // value.
        jcb.setFieldSeparator("=");

        Vector returnedDataSet = null;
        Hashtable keyValuePairs = null;

        try {
            URL u = new URL(getDocumentBase(), "setup.html");
 
            returnedDataSet = jcb.getParsedData(u);

            // We call the function below since the vectors that 
            // are returned contain setup information in the form of
            // the first field being a key (variable) and the second
            // field being a value to the key

            keyValuePairs = jcb.getKeyValuePairs(returnedDataSet);

        } catch (MalformedURLException e) {
            System.out.println("Malformed URL Exception:" + e);
        } catch (JavaCGIBridgeTimeOutException e) {
            System.out.println("JavaCGIBridge Timed Out:" + e);
        }
 
        _cgiURL = (String)keyValuePairs.get("cgiURL");

    } // end of getSetupFile

} // end of ExampleApplet
