package ijDoc.reFig; 

import java.applet.*;
import java.awt.*;
import java.net.*;
import ij.*;
import ij.gui.*;
import ij.process.*;
import ijDoc.plugs.*;

/**
 * This applet implements a prototype of the SEP reproducible figure applet 
 * which includes interactive buttons for <tt>View, Build, Info</tt> and 
 * possibly <tt>Play</tt>. The current version uses NIH's imageJ Java API 
 * for graphics. I believe Applets such as this can be automatically generated
 * from SEP's standard makefile, LaTeX environment, if the applicatioin is 
 * programmed in Java. 
 */
public class ReFig extends Applet {

  ImageJ ij = null;
  Image img;
  ImageProcessor ip = null;
  URL docUrl = null; 

  public void init() {
    docUrl = getDocumentBase();

    setLayout(new BorderLayout());
    Panel p = new Panel();
    p.setLayout(new GridLayout(0, 4));
    p.add(new Button("View"));
    p.add(new Button("Build"));
    p.add(new Button("Play"));
    p.add(new Button("Info"));
    add("South", p);

    img = getImage(docUrl, getParameter("img"));
    MediaTracker tracker = new MediaTracker(this);
    tracker.addImage(img, 0);
    try {tracker.waitForID(0);} catch (InterruptedException e){}
  }
  
  public void update(Graphics g) { paint(g); }
  
  public void paint(Graphics g) { g.drawImage(img, 0, 0, this); }

  public boolean action(Event e, Object arg) {
    if (e.target instanceof Button) {
      String label = (String)arg;
      if      (label.equals("View"))    {
	try {
	  String name = getParameter("oput");
	  URL oputUrl = new URL(docUrl, name);
	  URLConnection uc = oputUrl.openConnection();
	  SepCubeHandler sh = new SepCubeHandler();
	  float[][] theData = (float[][]) sh.getContent(uc);
	  SepCube2FloatArray px = new SepCube2FloatArray(theData); 
	  if (ij == null) ij = new ImageJ(this);
	  ImagePlus ip = new ImagePlus("Image File",  px.getPixels(), 
				       px.getWidth(), px.getHeight(), ij);
	  ip.show();
	}
	catch(Exception ex){System.err.println("did not work:" + ex);}
      }
      else if (label.equals("Build")) {
	try {
	  String name = getParameter("iput");
	  URL oputUrl = new URL(docUrl, name);
	  URLConnection uc = oputUrl.openConnection();
	  SepCubeHandler sh = new SepCubeHandler();
	  float[][] theData = (float[][]) sh.getContent(uc);
	  Nmo myNmo = new Nmo();
	  float[][] movedData = myNmo.apply(theData);
	  SepCube2FloatArray px = new SepCube2FloatArray(movedData);
	  if (ij == null) ij = new ImageJ(this); 
	  ImagePlus ip = new ImagePlus("Image File",  px.getPixels(), 
				       px.getWidth(), px.getHeight(), ij);
	  ip.show();
	}
	catch(Exception ex){System.err.println("did not work:" + ex);}
      }
      else if (label.equals("Info"))    {
	if (ij == null) ij = new ImageJ(this);
	StringBuffer msgs = new StringBuffer();
	msgs.append("Applet: ijDoc.reFig.ReFig\n");
	msgs.append("Author: Matthias Schwab\n");
	msgs.append("This figure is easily reproducible.\n");
        msgs.append("I wish these links were active:\n");
        msgs.append("Javadoc: http://sepwww.stanford.edu/sep/matt/jest/jdocs/api\n");
        msgs.append("Document: http://sepwww.stanford.edu/sep/matt/join/paper");
	Info.showMessage("Info",msgs.toString());
      }
      else if (label.equals("Play")) {
	try {
	  String name = getParameter("iput");
	  URL oputUrl = new URL(docUrl, name);
	  URLConnection uc = oputUrl.openConnection();
	  SepCubeHandler sh = new SepCubeHandler();
	  float[][] theData = (float[][]) sh.getContent(uc);
	  if (ij == null) ij = new ImageJ(this); 
	  Nmo myNmo = new Nmo();
	  GetNumberDialog velGui = new GetNumberDialog(ij,"Velocity?",1.44);
	  try {Thread.sleep(100);} catch (InterruptedException ex) { }
	  myNmo.setVelocity((float) velGui.getNumber());
	  float[][] movedData = myNmo.apply(theData);
	  SepCube2FloatArray px = new SepCube2FloatArray(movedData);
	  ImagePlus ip = new ImagePlus("Image File",  px.getPixels(), 
				       px.getWidth(), px.getHeight(), ij);
	  ip.show();
	}
	catch(Exception ex){System.err.println("did not work:" + ex);}
      }
      return true;
    }
    return false;
  }
  
  void updateAndDraw() {
    img.flush();
    img = ip.createImage();
    getGraphics().drawImage(img, 0, 0, this);
  }
 
}
