
// Includes some on line resources on the tool bar:

// Author : Gilles Carpentier
// Faculte des Sciences et Technologies,
// Universite Paris 12 Val de Marne, France.

// - On Line Notice Tool: open an url containing a notice.
// - Download & Install Macro Tool: download and install a macro tool on line.
// - Download Sample Image Tool: Download a demo Image.
// - On Line Kit Tool: Install a macro, open the on line notice and downolad a sample image.

var distantmacrolink = "http://rsb.info.nih.gov/ij/macros/tools/3FluoLablelingExploringTools.txt";
var onlinedoclink = "http://image.bio.methods.free.fr/Fluotooldoc.html";
var demoimagelink = "http://rsb.info.nih.gov/ij/macros/images/myotube.tif.zip";
var macroname = "3FluoLablelingExploringTools.txt";
var shortmacroname = macroname;
var demoimagename = "myotube.tif";


macro "Download Sample Image Tool -  "{
}
macro "Download Sample Image Tool Selected" {
	requires("1.35r");
	OpenImageLink(1);
}

macro "On Line Notice Tool -  "{
}

macro "On Line Notice Tool Selected" {
	showMessageWithCancel  ("A notice is avaible on line. Open it with your default web browser?");
	run("URL...", "url=["+onlinedoclink +"/"+"]");
} 

macro "Download & Install Macro Tool -  "{
}
macro "Download & Install Macro Tool Selected" {
	requires("1.37i");
	if (endsWith(macroname, ".txt")) shortmacroname = substring(macroname, 0, indexOf(macroname, ".txt"));
	showMessageWithCancel ("On line Install the \"" +shortmacroname+ "\"?");
	run("URL...", "url=["+distantmacrolink+"]");
}

macro "On Line Kit Tool -  "{
}
macro "On Line Kit Tool Selected" {
	requires("1.37i");
	if (endsWith(macroname, ".txt")) shortmacroname = substring(macroname, 0, indexOf(macroname, ".txt"));
	showMessageWithCancel ("Run on line \"kit\" \"" +shortmacroname+ "\"?");
	demoimalocation = getDirectory("startup");
	run("URL...", "url=["+onlinedoclink +"/"+"]");
	OpenImageLink(0);
	run("URL...", "url=["+distantmacrolink+"]");
	selectWindow (distantmacrolink);
	run('Close');
	// message dedicated to this macro example
	showMessageWithCancel ("Start using \"" +shortmacroname+ "\" by making a panel:\nPress \"p\", or use (Plugins -> Macros -> Presentation Panel) submenu.");
}

macro "On Line Macro Update Tool-  "{
}
macro "On Line Macro Update Tool Selected" {
	requires("1.37i");
	if (endsWith(macroname, ".txt")) shortmacroname = substring(macroname, 0, indexOf(macroname, ".txt"));
	showMessageWithCancel ("Looking for \"" +shortmacroname+ "\" update ?");
	run("URL...", "url=["+distantmacrolink+"]");
	selectWindow(distantmacrolink);
	macrotextnih = getInfo();
	run('Close');
	if (getDirectory("macros") == "") exit ("Unable to find the Macros directory; something wrong in the ImageJ folder.");
	macrostoolslocation = getDirectory("macros") + "tools/";
	if (File.isDirectory(macrostoolslocation) == 0) exit ("Unable to find the tools directory on the Macros directory; something wrong in the ImageJ folder.");
	macropath = macrostoolslocation + macroname;
	if (File.exists(macropath)) {
			thelocalmacro = File.openAsString(macropath);
			if (thelocalmacro == macrotextnih) {
			showMessage ("Your version of \"" +shortmacroname+ "\" is up to date.");
			exit;
		} else {
			//update:
			// Create an Archived Tools directory in tools repertory
			ArchDir = macrostoolslocation+"Archived Tools"+File.separator;
			File.makeDirectory(ArchDir);
			if (!File.exists(ArchDir)) exit("Unable to create directory, something wrong in the ImageJ folder");
			showMessageWithCancel ("An update for the macro \"" +shortmacroname+ "\" is available.\nDo you want to install the new version?");
			//Archive the local macro
			f= File.open(ArchDir + macroname);
			print (f,thelocalmacro);
			File.close(f);
			//Write the new macro
			f= File.open(macropath);
			print (f,macrotextnih);
			File.close(f);
			showMessage ("Installation of the macro \"" +shortmacroname+ "\" is completed.");
		}
	} else {
		showMessageWithCancel ("The macro \"" +shortmacroname+ "\" isn't in you Macros repertory.\nDo you want to install it?");
		f= File.open(macropath);
		print (f,macrotextnih);
		File.close(f);
		showMessage ("Installation of the macro \"" +shortmacroname+ "\" is completed.");
	}
}

function OpenImageLink(question) {
	// Check if already downloaded.
	demoimalocation = getDirectory("startup");	
	fildestination = demoimalocation+ "Downloaded Demo Images/" + demoimagename;
	if (File.exists(fildestination)) {
		if (question ==1 ) showMessageWithCancel ("The \"" + demoimagename + "\" has already been downloaded. Open it?");
		open(fildestination);
	}
	else {
		showMessageWithCancel ("ImageJ will download a demo image. Continue?");
		run("URL...", "url=["+demoimagelink+"]");
		imageid = getImageID();
		nomdimage = getTitle;
		// Create a <Downloaded Demo Images> repertory in ImageJ folder.
		ImaDemo = demoimalocation+"Downloaded Demo Images"+File.separator;
		File.makeDirectory(ImaDemo);
		if (!File.exists(ImaDemo)) exit("Unable to create directory, something wrong in the ImageJ folder");
		selectWindow(nomdimage);
		save(""+ImaDemo+""+ nomdimage +"");
	}
}
