import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;
import ij.plugin.frame.*;
import ij.io.*;
import ij.measure.*;
import ij.plugin.filter.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.FileWriter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
	
public class Measure_ extends ImagePlus implements PlugIn, MouseListener {

	public void mouseExited(MouseEvent e)
	{
	}
	public void mouseEntered(MouseEvent e)
	{
	}
	public void mouseReleased(MouseEvent e)
	{
	}

	public void mousePressed(MouseEvent e)
	{
	}
	boolean click = false;
	Point clickPoint = new Point();
	ImagePlus imp = new ImagePlus();
	ImageCanvas canvas;
	String path;
	String img_title;
	//int [] PointArrayX;
	//int [] PointArrayY;
	int x;
	int y;
	//int cnt_click=0;
	public void openImage(){
		Opener open = new Opener();
		open.open();
	}	


	public void setScale(){
		Scaler scale = new Scaler();
	
	}
	public void Imageenv(){
	ImagePlus im = IJ.getImage();
	ImageProcessor ip = im.getProcessor();
	canvas.addMouseListener(this);
	}
	
	public void mouseClicked(MouseEvent e){
		click=true;
		x = e.getX();
		y = e.getY();
		int offscreenX = canvas.offScreenX(x);
		int offscreenY = canvas.offScreenY(y);
		//PointArrayX[cnt_click]=offscreenX;
		//PointArrayY[cnt_click]=offscreenY;
		//cnt_click++;
	}

	public void waitForClick() {
		while (!click) IJ.wait(300);
		click=false;
	}
	
public void setup(String arg, ImagePlus imp) {
	this.imp = imp;
}

public void html(String input){
String [] tmp = input.split(".");
try{
	FileWriter htmlFile = new FileWriter(path+"report.html");
	PrintWriter htmlPrint = new PrintWriter (htmlFile);
	htmlPrint.println("<html>");
	htmlPrint.println("<head>");
	htmlPrint.println("<title>ImageJ Measurement PlugIn - Dev by Andrea Cannella - University of Catania - 2011</title>");
	htmlPrint.println("</head>");
	htmlPrint.println("<body>");
	htmlPrint.println("<b><h1>ImageJ Measurement PlugIn - Dev by Andrea Cannella - University of Catania - 2011</b></h1>");
	htmlPrint.println(input);
	htmlPrint.println("</body>");
	htmlPrint.println("</html>");
	htmlPrint.close();
	} catch (Exception e){};
}
	
public void run(String arg) {
	Opener op = new Opener();
	op.open();
	ImagePlus im = IJ.getImage();
	img_title = im.getTitle();
	path = IJ.getDirectory("image");
	ImageProcessor ip = im.getProcessor();
	ImageWindow win = im.getWindow();
	canvas = win.getCanvas();
	canvas.addMouseListener(this);
	waitForClick();
	int x1=x;
	int y1=y;
	waitForClick();
	int x2=x;
	int y2=y;
	IJ.makeLine(x1, y1, x2, y2);
	ScaleDialog sd1 = new ScaleDialog();
	sd1.setup("",im);
	sd1.run(ip);
	canvas.addMouseListener(this);
	waitForClick();
	int x3=x;
	int y3=y;
	waitForClick();
	int x4=x;
	int y4=y;
	IJ.makeLine(x3, y3, x4, y4);
	ScaleDialog sd2 = new ScaleDialog();
	sd2.setup("",im);
	sd2.run(ip);
	canvas.addMouseListener(this);
	waitForClick();
	int x5=x;
	int y5=y;
	waitForClick();
	int x6=x;
	int y6=y;
	IJ.makeLine(x5, y5, x6, y6);
	Analyzer analyze = new Analyzer(im);
	analyze.run(ip);
	ResultsTable table = analyze.getResultsTable();
	double result = table.getValue("Length",0);
	html("<h2>Results<h2><br /><p>Image Path ="+path+img_title+"<br />Init points:<br />x1="+x1+"<br />y1="+y1+"<br />x2="+x2+"<br />y2="+y2+"<br />x3="+x3+"<br />y3="+y3+"<br />x4="+x4+"<br />y4="+y4+"</p><br /><h2>Measurements</h2><br /><h3>The distance between ("+x5+", "+y5+") and ("+x6+", "+y6+") is <b>"+result+"</b></h3>");

	}

}
