import javax.swing.JApplet;
import java.awt.BorderLayout;
import java.util.StringTokenizer;
import java.io.*;
import java.net.*;
import ptolemy.plot.*;

public class DataPlotter extends JApplet {

  private boolean DEBUG = false;
  private Plot plot;

  public String getAppletInfo() {
    return "DataPlotter 1.0: by Alan Munter.\n"+
           "alan.munter@nist.gov";
  }

  public void init() {
    super.init();
    int height = this.getHeight();
    height = (height/2) - 50;
    int width = this.getWidth();
    width = width - 20;

    plot = new Plot();
    plot.setSize(width,height);
    plot.setMarksStyle("dots");
    plot.setButtons(true);

    this.getContentPane().setLayout(new BorderLayout(10,10));

    parseParameters();
    
    this.getContentPane().add(plot,BorderLayout.CENTER);


  }

  private void parseParameters() {
    int i = 0;
    String paramName = "DATA" + i;
    String paramValue;
    String titleParam;

    String xLogText = getParameter("XLOG");
    if (xLogText.trim().equalsIgnoreCase("true")) {
      plot.setXLog(true);
      if (DEBUG) System.out.println("XLOG: true");
    }
    String yLogText = getParameter("YLOG");
    if (yLogText.trim().equalsIgnoreCase("true")) {
      plot.setYLog(true);
      if (DEBUG) System.out.println("YLOG: true");
    }
    if ((titleParam = getParameter("TITLE")) != null) {
      plot.setTitle(titleParam);
      if (DEBUG) System.out.println("TITLE: "+titleParam);
    }
    if ((titleParam = getParameter("YLABEL")) != null) {
      plot.setYLabel(titleParam);
      if (DEBUG) System.out.println("YLABEL: "+titleParam);
    }
    if ((titleParam = getParameter("XLABEL")) != null) {
      plot.setXLabel(titleParam);
      if (DEBUG) System.out.println("XLABEL: "+titleParam);
    }

    while((paramValue = getParameter(paramName)) != null) {
      if (DEBUG) System.out.println("DATA"+i+": "+paramValue.trim());
      getData(paramValue.trim(),i);
      i++;
      paramName = "DATA" + i;
    }

  }

  private void getData(String dataFile,int dataSet) {
    BufferedReader in;
    try { 
      URL url = new URL(dataFile);
      URLConnection connection = url.openConnection();
      connection.connect();
      in = new BufferedReader( 
             new InputStreamReader(
               connection.getInputStream() 
             )
           );
    } catch (Exception e) {
      this.showStatus("Error with data file: "+dataFile+"! Could not open connection to load data");
      System.out.println("Error with data file: "+dataFile+"! Could not open connection to load data");
      System.err.println(e);
      return;
    }
    int error = 0;
    double xVal = 0.0;
    double yVal = 0.0;
    double errVal = 0.0;
    double yTop = 0.0;
    double yBot = 0.0;
    String thisLine;
    StringTokenizer st;
    
    while (error == 0) {
      try {
        errVal = 0.0;
        thisLine = in.readLine();
        if (DEBUG) System.out.println(thisLine);
        if (thisLine != null) {
          st = new StringTokenizer(thisLine);
          int numTokens = st.countTokens();
          if((numTokens != 2)&&(numTokens != 3)) {
            this.showStatus("Error with data file! Incorrect number of columns: "+numTokens);
            System.out.println("Problem with data file! Incorrect number of columns: "+numTokens);
            return;
          }
          xVal = Double.parseDouble(st.nextToken());
          yVal = Double.parseDouble(st.nextToken());
          if (numTokens == 3) {
            errVal = Double.parseDouble(st.nextToken());
            yTop = yVal + errVal;
            yBot = yVal - errVal;
            plot.addPointWithErrorBars(dataSet,xVal,yVal,yBot,yTop,true);
          } else {
            plot.addPoint(dataSet,xVal,yVal,true);
          }
        } else {
          error = 1;
        }
      } catch (IOException e) {
        error = 1;
      }
    }
    plot.fillPlot();
  }

  public void start(){
    super.stop();
  }
 
  public void stop(){
    super.stop();
  }

}