/* Toolset to quickly rename and save ROIs
 * Tiago Ferreira 01.2008
 *
 * This toolset was thought for analyses of brain autoradiograms (in situ hybridization/receptor binding experiments) 
 * which typically require manual contouring of multiple brain regions but can be easily customized for other purposes.
 *
 * It speeds up drawing, renaming and saving of selections.
 *
 * Installation:
 * Save this file in the ImageJ/macros/toolsets folder. The toolset WILL ONLY INSTALL in this directory.
 * You can then use the '>>' drop down menu in IJ window to activate it.
 * The same drop down menu can be used to return to your default toolset;
 *
 * Usage:
 * 	- Use 'Settings Menu>Help...' to see the available key shortcuts or to edit this file;
 *	- Draw ROIs with the aid of the pop-up menu (right click on image) and/or the shortcut keys;
 *	- Press [t] to add the active selection to the ROI Manager (ImageJ's default);
 *	- Press [E] to restore selection (ImageJ's default);
 *	- When using images with bilateral features, use the 'contralateral' options to retrieve symmetric selections;
 *	- Use the 'Highlight drawn' option to know which regions have been drawn without having to switch to the Manager;
 *	- Once all the contouring is complete use the 'Group Labeling Menus' to rename selections traversing the Manager's list;
 *	- You can also add an extra tag to an already labeled selection with the 'Add suffix Menu';
 *	- Shift-clicking on the 'Labeling Menus' appends the label to ROI name without renaming the selection and without appending
 *	   the respective group tag on the selection name.
 *	   Please note that with this feature and the 'Add suffix Menu' can significantly increase the combination of labels and group-tags.
 *	   Run 'Define labels' for more details on this;
 *	- Shift-clicking on the 'Add suffix Menu' has the opposite effect and renames the selection;
 *	- Use the 'Save All Regions' tool to save and 'associate' the ROIs to the active image;
 *	- You can define a 'ROIset directory' in which to save all the ROISets;
 *	- Inversion of gray values can be performed at any time;
 *
 * Customization:
 * Pop-up Menu and shortcuts
 *	To deactivate the pop-up menu type '//' before 'var pmCmds' (Macros will still be present via shortcuts or 'Plugins> Macros' Menu);
 *	To modify shortcuts edit the character between [ ] on the macro name (case sensitive), you should also do the same 
 *	alteration in the 'var pmCmds' list so that the command keeps to be available via the popup menu.
 *	To know which shortcuts keys are already being used in your installation run 'Plugins> Utilities> List Shortcuts...';
 *	
 * Labels
 *	Labels can be modified at any time via the 'Settings Menu'. They are stored in the ImageJ preferences file;
 *
 * Version history:
 * 01.2008
 *	- Settings are saved in the IJ preferences file;
 *	- Added a 'settings Menu';
 *	- Added the option to save ROIs in a 'ROIset folder', useful for e.g. if images are saved remotely;
 *	- Added the macro 'change selection color' that cycles through the 9 available colors in IJ;
 *	- Added a 'Add suffix Menu';
 *	- Minor changes (Shortcuts; Key modifiers for action and menu tools, Explanations in dialogs);
 * 12.2007
 *	- Makes sure index number is kept in the ROI label; 
 *	- Added ROI operations via pop-up menu;
 *	- Added two commands that 'mirror' the selected ROI (bilateral equivalent);
 *	- Added a 'Refine' mode in which the Brush Selection Tool is deactivated upon mouse release;
 *	- Added 'Highlight drawn', that shows temporarily all the Rois;
 *	- 'Invert gray values' macro works when a selection is present;
 * 10.2007
 *	- Creates custom drop-down menus for labeling ROIs;
 *	- Saves ROIs in a .zip file named after the active image at image's location; 
 *	- Option to invert non-RGB images;
 *
 * Known Issues:
 * 1)
 * Most of ImageJ's operations on ROIs do not work when the selection is a composite (e.g. the kind of
 * selections originated by the Brush Selection Tool). The ROIconverter() function used here is just a
 * kludge to obtain a non-complex ROI from any selection as long as it is just one piece without internal
 * holes... For a more efficient approach have a look at the ROI2PolylineROI macro by G. Landini at the
 * ImageJ documentation wiki [http://imagejdocu.tudor.lu/imagej-documentation-wiki/].
 * 2)
 * At least in certain configurations of Mac OS 10.3 and Java 1.4 the toolset seems to behave errantly.
 * Please let me know if you have any problems.
 * 
 * Acknowledgments:
 * Kota Miura and Wayne Rasband for code corrections;
 * E. Audero and V. Carola for suggestions;
 * T. Tsetsenis for trial images;
 *
 * Comments and suggestions:
 * ferreira < at > embl < dot > it
 */

// ----------------------------	Variables		----------------------------

	var install	=checkInstallation();
	var version	=requires139l();
	var labels	=getLabelList();
	var groups	=getGroupList();
	var suffixs	=getSuffixList();
	var yy		=startManager();
	var aCmds	=newMenu("Group *1* labeling Menu Tool", labels);
	var bCmds	=newMenu("Group *2* labeling Menu Tool", labels);
	var cCmds	=newMenu("Group *3* labeling Menu Tool", labels);
	var cCmds	=newMenu("Add suffix Menu Tool", suffixs);
	var dCmds	=newMenu("Settings Menu Tool", newArray("Define labels", "Define groups","Define suffix tags","Select ROIset directory","-","Invert gray values","Paste Control...","Set Measurements...","-","Delete all selections","-","Help...","Reset settings"));
	var pmCmds	=newMenu("Popup Menu", newArray("Rectangle [1]", "Ellipse [2]", "Brush [3]", "Polygon [4]","Freehand [5]", "Refine selection [6]","-", "Contralateral in X [F1]", "Contralateral in Y [F2]", "Highlight drawn [F3]", "-", "Enlarge...", "Rotate...","-","Fit Spline","Fit Ellipse","-","Restore Selection","Undo"));

// ----------------------------	Menus and Tools	----------------------------
macro "Unused Tool - " {}

macro "Settings Menu Tool - C037D3eD4eD5eD6bD6cD6dD7aD89D98Da7Db6Dc6Dd6De4De5C037D2aD5dDa2Dd5C037C037D59D68D69D77D78D86D87D96C037D1aD1bD1cD29D2bD39D49D4bD4cD4dD58D67D76D85D92D93D94Da1Db1Db2Db4Dc1Dc4Dd4De3C037D5aD6aD79D88D95D97Da5Da6C037D19D91C037D4aD5bDa4Db5C037D3aD5cDa3Dc5" {
	cmd = getArgument();
	if(cmd=="Define labels") createNewLabelList();
	else
	if(cmd=="Define groups") createNewGroupList();
	else
	if(cmd=="Define suffix tags") createNewSuffixList();
	else
	if(cmd=="Select ROIset directory") {
		dir = getDirectory(" Select or create a ROIset directory");
		call("ij.Prefs.set", "renameroisets.dir", dir);
		showStatus("ROIset directory's patth saved in IJ preferences");}
	else
	if(cmd=="Delete all selections") {
		showMessageWithCancel("Confirm clean Manager", "Are you sure you want to delete all ROIs?");
		roiManager("reset");}
	else
	if(cmd=="Reset settings") {
		showMessageWithCancel("Confirm reset settings", "This will reset all labels, groups and suffix-tags.\nYou might need this if for e.g.:\n \n1) You typed nothing in one of the dialog choices;\n \n2) You typed ' | ' (used to concatenate strings);\n \n3) The IJ Prefs file has been modified;");
		call("ij.Prefs.set",  "renameroisets.labels", "nolabels");
		call("ij.Prefs.set",  "renameroisets.groups", "nogroups");
		call("ij.Prefs.set",  "renameroisets.suffixes", "nosuffixes");
		checkInstallation();
		reInstall(); }
	else
	if (cmd!="-") run(cmd);
}
macro "Group *1* labeling Menu Tool - C037D10D1aD20D21D22D23D24D25D26D27D28D29D2aD30D3aC000C111C222C333C444C555C666C777C888C999CaaaCbbbCcccCdddCeeeCfff" {
	cmd = getArgument();
	i =roiManager("index");
	a=call("ij.plugin.frame.RoiManager.getName", i);
	if (roiManager("count")==0) exit("ROI Manager is empty.");
	else
	if (isKeyDown("shift")) roiManager("Rename", a+":"+cmd);
	else
	roiManager('Rename',i+":"+cmd+":"+groups[0]);
	MoveToNext();
}
macro "Group *2* labeling Menu Tool - C037D10D1aD20D21D22D23D24D25D26D27D28D29D2aD30D3aD50D5aD60D61D62D63D64D65D66D67D68D69D6aD70D7aC000C111C222C333C444C555C666C777C888C999CaaaCbbbCcccCdddCeeeCfff" {
	cmd = getArgument();
	i =roiManager("index");
	a=call("ij.plugin.frame.RoiManager.getName", i);
	if (roiManager("count")==0) exit("ROI Manager is empty.");
	else
	if (isKeyDown("shift")) roiManager("Rename", a+":"+cmd);
	else
	roiManager('Rename',i+":"+cmd+":"+groups[1]);
	MoveToNext();
}
macro "Group *3* labeling Menu Tool - C037D10D1aD20D21D22D23D24D25D26D27D28D29D2aD30D3aD50D5aD60D61D62D63D64D65D66D67D68D69D6aD70D7aD90D9aDa0Da1Da2Da3Da4Da5Da6Da7Da8Da9DaaDb0DbaC000C111C222C333C444C555C666C777C888C999CaaaCbbbCcccCdddCeeeCfff" {
	cmd = getArgument();
	i =roiManager("index");
	a=call("ij.plugin.frame.RoiManager.getName", i);
	if (roiManager("count")==0) exit("ROI Manager is empty.");
	else
	if (isKeyDown("shift")) roiManager("Rename", a+":"+cmd);
	else
	roiManager('Rename',i+":"+cmd+":"+groups[2]);
	MoveToNext();
}
macro "Add suffix Menu Tool - C037D10D11D12D13D14D15D16D17D18D19D1aD20D2aD30D35D3aD45D65D75D95Da0Da5DaaDb0DbaDc0Dc1Dc2Dc3Dc4Dc5Dc6Dc7Dc8Dc9DcaC000C111C222C333C444C555C666C777C888C999CaaaCbbbCcccCdddCeeeCfff" {
	cmd = getArgument();
	i =roiManager("index");
	a=call("ij.plugin.frame.RoiManager.getName", i);
	if (roiManager("count")==0) exit("ROI Manager is empty.");
	else
	if (isKeyDown("shift")) roiManager('Rename',i+":"+cmd);
	else 
	roiManager("Rename", a+":"+cmd);
	MoveToNext();
}
macro "Selection cycler [shift+click: previous] [alt+click: first] Action Tool - C037D13D14D15D16D17D18D19D1aD1bD1cD44D45D46D47D48D49D4aD4bD55D56D57D58D59D5aD66D67D68D69D77D78D97D98Da6Da7Da8Da9Db5Db6Db7Db8Db9DbaDc4Dc5Dc6Dc7Dc8Dc9DcaDcbDf3Df4Df5Df6Df7Df8Df9DfaDfbDfcC000C111C222C333C444C555C666C777C888C999CaaaCbbbCcccCdddCeeeCfff"{
	if (roiManager("count")<=1) showStatus('Requires at least 2 ROIs in Manager');
	else
	if (isKeyDown("shift")) MoveToPrevious();
	else 
	if (isKeyDown("alt")) roiManager('select', 0);
	else
	MoveToNext();
}
macro "Save All Regions Action Tool - CfffCeeeCdddCcccCbbbCaaaC999C888C777C666C555C444C333C222C111C037D2aD2bD2cD2dD2eD3eD4eD58D5eD68D69D6eD71D72D73D74D75D76D77D78D79D7aD7eD81D82D83D84D85D86D87D88D89D8aD8bD8eD91D92D93D94D95D96D97D98D99D9aD9bD9eDa1Da2Da3Da4Da5Da6Da7Da8Da9DaaDaeDb8Db9DbeDc8DceDdeDeeDfaDfbDfcDfdDfe"{
	if (roiManager("count")==0) showStatus("ROI Manager is empty. No ROIs to save.");
	else
	if (nImages==0 && roiManager("count")!=0) showStatus("This tool requires an open image.");
	else {	dir= call("ij.Prefs.get",  "renameroisets.dir", "nodir");
		if (dir=="nodir") {lb="Other location"; foot2="The 'ROIset directory' has not been defined yet";}
		else
		if(!File.isDirectory(dir)) {
			lb="Other Location";
			foot2="The 'ROIset folder' could not been found.\nMake sure it has not been moved or deleted";}
		else {	if(File.exists(dir+getTitle+'-ROIset.zip')) {
			lb='ROIset directory (overwrite)';
				foot2="A ROIset.zip file for this image has been found\nin the 'ROIset folder'";}
			else {	lb='ROIset directory';
				foot2= "No ROIset.zip file for this image found\nin the 'ROIset folder'";} 
			}
		if (getDirectory("image")==""){
			options=newArray("Other location",""+lb);
			foot =getTitle+' is either on a remote server or\nit has not been saved yet...'; }
		else
		if (File.exists(getDirectory("image")+getTitle+'-ROIset.zip')) {
			options=newArray('Image folder (overwrite)',""+lb,"Other location");
			foot ="A ["+getTitle+"-ROIset.zip] file already exists in\n"+getDirectory("image"); }
		else {
			foot ="No ROIset.zip file found for "+getTitle+" in\n"+getDirectory("image");
			options=newArray('Image folder',""+lb,'Other location'); }
	
		Dialog.create("Saving all ROIs");
		Dialog.addMessage(foot);
		Dialog.addMessage("\n"+foot2);
		Dialog.addChoice('      Save set in',options);
		Dialog.show();
		target = Dialog.getChoice();
		if (startsWith(target, 'Image folder')) roiManager('save',getDirectory("image")+getTitle+'-ROIset.zip');
		else
		if (startsWith(target, 'ROIset directory')) roiManager('save',dir+getTitle+'-ROIset.zip');
		else
		if (target=='Other location') roiManager('save', getDirectory("Select a Directory")+getTitle+'-ROIset.zip');
		showStatus("All ROIs in Manager have been saved...");
		beep();
	}
}

// ----------------------------	Pop-up Menu		----------------------------
macro "Popup Menu" {
	cmd = getArgument();
	if (cmd=="Rotate...") { ROIconverter();run(cmd); }
	else
	run(cmd);
}

// ----------------------------	Macros			----------------------------

macro "Rectangle [1]" {setTool("rectangle");}

macro "Ellipse [2]" {setTool("elliptical");}

macro "Brush [3]" {setTool("brush");}

macro "Polygon [4]" {setTool("polygon");}

macro "Freehand [5]" {setTool("freehand");}

macro "Refine selection [6]" {
	if (nImages==0) showStatus("'Refine selection mode' requires an open image");
	else {
	setTool("brush");
	while (true) {
	getCursorLoc(x, y, z, flags);
	if (flags&16!=0) {restorePreviousTool; exit;}
		}
	}
}
macro "Select next [n]" {MoveToNext();}

macro "Select previous [p]" {MoveToPrevious();}

macro "Change selection color [q]" {
	a =newArray('red','green','blue','magenta','cyan','yellow','orange','black','white');
	b ="Selection color is now ";
	c =call("ij.gui.Roi.getColor");
	if(c=='java.awt.Color[r=255,g=0,b=0]') {run("Colors...", "selection="+a[1]); showStatus(b+a[1]);}
	else
	if(c=='java.awt.Color[r=0,g=255,b=0]') {run("Colors...", "selection="+a[2]); showStatus(b+a[2]);}
	else
	if(c=='java.awt.Color[r=0,g=0,b=255]') {run("Colors...", "selection="+a[3]); showStatus(b+a[3]);}
	else
	if(c=='java.awt.Color[r=255,g=0,b=255]') {run("Colors...", "selection="+a[4]); showStatus(b+a[4]);}
	else
	if(c=='java.awt.Color[r=0,g=255,b=255]') {run("Colors...", "selection="+a[5]); showStatus(b+a[5]);}
	else
	if(c=='java.awt.Color[r=255,g=255,b=0]') {run("Colors...", "selection="+a[6]); showStatus(b+a[6]);}
	else
	if(c=='java.awt.Color[r=255,g=200,b=0]')  {run("Colors...", "selection="+a[7]); showStatus(b+a[7]);}
	else
	if(c=='java.awt.Color[r=0,g=0,b=0]') {run("Colors...", "selection="+a[8]); showStatus(b+a[8]);}
	else
	if(c=='java.awt.Color[r=255,g=255,b=255]') {run("Colors...", "selection="+a[0]); showStatus(b+a[0]);}
	else {
 	run("Colors...", "selection="+a[0]); showStatus(b+a[0]); }
}
macro "Contralateral in X [F1]" {
	fx=-1; fy=1;
	mirrorROI();
}
macro "Contralateral in Y [F2]" {
	fx=1; fy=-1;
	mirrorROI();
}
macro "Highlight drawn [F3]" {
	while (true) {
	getCursorLoc(x, y, z, flags);
	if (flags&16!=0) {setOption("Show All", false); exit;}
	setOption("Show All", true);}
}
macro "Invert gray values" {	
	if (selectionType()!=-1) {ROIconverter(); s=selectionType(); getSelectionCoordinates(x,y); x2= x; y2=y; }
	else {s='line'; x2= newArray(0,0); y2=newArray(0,0);}
	if(bitDepth!=24) {	if(is("Inverting LUT")==1)
				showMessageWithCancel('Warning', 'Image is already using an inverting LUT.\nInvert image nevertheless?');
				run("Select None"); run("Invert"); run("Invert LUT");} 
	if(bitDepth==24) {
				showMessageWithCancel('Warning', getTitle+' is a RGB Image.\nConvert to 8-bit and invert?');
				run("8-bit"); run("Select None"); run("Invert"); run("Invert LUT");} 
	makeSelection(s, x2, y2);
}
macro "Help..." {
	msg="Settings:\n"+"labels, groups, suffix-tags and a target directory to save\n"+"ROIsets can be defined in the settings menu\n \n"+"Shortcuts:\n"+"[1;2;3;4;5] ......... Area selection tools\n"+"[6] ......................... Refine mode\n"+"[t] ............................. Add to Manager (IJ)\n"+"[E] ............................... Restore selection (IJ)\n"+"[n] .................................. Next ROI\n"+"[p] ..................................... Previous ROI\n"+"[q] ........................................ Change selection color\n"+"[f1] ........................................... Contralateral in X\n"+"[f2] .............................................. Contralateral in Y\n"+"[f3] .................................................. Highlight drawn\n"+"Right-click on image to access most of these commands\n \n"+"Modifiers:\nShift in labeling menus .............. Label as suffix\n"+"Shift in add suffix menu ................ Suffix as label\n"+"Shift in cycler tool ............................. Previous ROI\n"+"Alt in cycler tool ................................... First ROI\n \n"+"Tags are displayed on result tables only if  'Display label'\n"+"from 'Set Measurements...' has been chosen";	msg2="Please check for updates at:\nhttp://rsb.info.nih.gov/ij/macros/toolsets/\n"+"Tiago Ferreira 01.2008";
	msg3="Open the toolset file (contains further information)";
	Dialog.create('Toolset to speed-up ROI measurements');
	Dialog.addMessage(msg);
	Dialog.addCheckbox(msg3, false); 
	Dialog.addMessage(msg2);
	Dialog.show();
	Opfile =Dialog.getCheckbox();
	TSpath	=getDirectory("macros")+"/toolsets/Rename and Save ROI Sets.txt";
	if (Opfile) open(TSpath);
}

// ----------------------------	Functions		----------------------------

function requires139l() {
	requires("1.39l"); return 0; 
}
function startManager() {
	run("ROI Manager...");return 0;
}
function checkInstallation() {
	TSpath	=getDirectory("macros")+"/toolsets/Rename and Save ROI Sets.txt";
	labels =call("ij.Prefs.get",  "renameroisets.labels", "nolabels");
	groups =call("ij.Prefs.get",  "renameroisets.groups", "nogroups");
	suffixs =call("ij.Prefs.get",  "renameroisets.suffixes", "nosuffixes");
	spacer="|";
	text="'Rename and Save ROI Sets.txt' could not be found at the\nImageJ/macros/toolset folder.\nThis toolset must be saved in this directory with the above\nname or it will not install...";
	
	if (!File.exists(TSpath)) exit(text);
	else {	if (labels=="nolabels") {
			labels="C.Cortex"+spacer+"E.Cortex"+spacer+"DG"+spacer+"CA3"+spacer+"CA1"+spacer+"Subiculum"+spacer+"Striatum"+spacer+"C.Callosum"+spacer+"Thalamus"+spacer+"Hypothalamus"+spacer+"Amigdala"+spacer+"Raphe"+spacer+"-"+spacer+"Background";
			call("ij.Prefs.set", "renameroisets.labels", labels);}
		if(groups=="nogroups") {
			groups="WT"+spacer+"Het"+spacer+"KO";
			call("ij.Prefs.set", "renameroisets.groups", groups);}
		if(suffixs=="nosuffixes") { 
			suffixs="Left"+spacer+"Right"+spacer+"-"+spacer+"Anterior"+spacer+"Posterior"+spacer+"-"+spacer+"Ventral"+spacer+"Medial"+spacer+"Dorsal"+spacer+"Lateral"+spacer+"-"+spacer+"Coronal"+spacer+"Sagittal"+spacer+"Horizontal";
			call("ij.Prefs.set", "renameroisets.suffixes", suffixs);}
		} 
	return 0;
}
function getLabelList() {
	labels = call("ij.Prefs.get",  "renameroisets.labels", "nolabels");
	spacer="|";
	if(labels=="nolabels") checkInstallation();
	else labels=split(labels,spacer);
	return labels;
}
function getGroupList() {
	groups = call("ij.Prefs.get",  "renameroisets.groups", "nogroups");
	spacer="|";
	if(groups=="nogroups") checkInstallation();
	groups=split(groups,spacer);
	return groups;
}
function getSuffixList() {
	suffixs = call("ij.Prefs.get",  "renameroisets.suffixes", "nosuffixes");
	spacer="|";
	if(suffixs=="nosuffixes") checkInstallation();
	suffixs=split(suffixs,spacer);
	return suffixs;
}
function createNewLabelList() {
	getLabelList();
	text="Fill in this list with the tags you will need to label\nyour selections.\nThese labels will be common to all groups.\n \n[shift+click] in any of the three labeling menus\nwill append these labels without the group-tag.\n \nLabels defined by one hyphen are ignored...";
	Dialog.create('Labels definition');
	Dialog.addMessage(text);
	Dialog.addString("               Label 01",labels[0],15);Dialog.addString("Label 02",labels[1],15);
	Dialog.addString("Label 03",labels[2],15);Dialog.addString("Label 04",labels[3],15);
	Dialog.addString("Label 05",labels[4],15);Dialog.addString("Label 06",labels[5],15);
	Dialog.addString("Label 07",labels[6],15);Dialog.addString("Label 08",labels[7],15);
	Dialog.addString("Label 09",labels[8],15);Dialog.addString("Label 10",labels[9],15);
	Dialog.addString("Label 11",labels[10],15);Dialog.addString("Label 12",labels[11],15);
	Dialog.addString("Label 13",labels[12],15);Dialog.addString("Label 14",labels[13],15);
	Dialog.show();
	spacer=""+"|";
	labels =""+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString+spacer+Dialog.getString()+spacer+Dialog.getString();
	call("ij.Prefs.set", "renameroisets.labels", labels);
	showStatus('Labels saved in IJ.prefs file');
	reInstall();
}
function createNewGroupList() {
	getGroupList();
	text="These are your comparision/experimental groups.\nThese tags will be appended to the labels previously\ndefined and assigned to the Labeling Menu Tools:";
	Dialog.create('Groups definition');
	Dialog.addMessage(text);
	Dialog.addString("        Group *1* Labeling Menu",groups[0], 14);
	Dialog.addString("Group *2* Labeling Menu",groups[1], 14);
	Dialog.addString("Group *3* Labeling Menu",groups[2], 14);
	Dialog.show();	
	spacer=""+"|";
	groups =""+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString();
	call("ij.Prefs.set", "renameroisets.groups", groups);
	showStatus('Groups saved in IJ.prefs file');
	reInstall();
}
function createNewSuffixList() {
	getSuffixList();
	text="This list is installed under the [...] Menu.\n \nUse it to append extra notes to selections\n[simple click] or use [shift+click] to rename\nselections with these tags.\nLong strings may not display properly on the\nManager but will ppear in the results table.";
	Dialog.create('Suffixes definition');
	Dialog.addMessage(text);
	Dialog.addString("               Suffix 01",suffixs[0], 13); Dialog.addString("Suffix 02",suffixs[1], 13);
	Dialog.addString("Suffix 03",suffixs[2], 13); Dialog.addString("Suffix 04",suffixs[3], 13);
	Dialog.addString("Suffix 05",suffixs[4], 13); Dialog.addString("Suffix 06",suffixs[5], 13);
	Dialog.addString("Suffix 07",suffixs[6], 13); Dialog.addString("Suffix 08",suffixs[7], 13);
	Dialog.addString("Suffix 09",suffixs[8], 13); Dialog.addString("Suffix 10",suffixs[9], 13);
	Dialog.addString("Suffix 11",suffixs[10], 13); Dialog.addString("Suffix 12",suffixs[11], 13);
	Dialog.addString("Suffix 13",suffixs[12], 13); Dialog.addString("Suffix 14",suffixs[13], 13);
	Dialog.show();
	spacer=""+"|";
	suffixs =""+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString()+spacer+Dialog.getString();
	call("ij.Prefs.set", "renameroisets.suffixes", suffixs);
	showStatus('Suffixes saved in IJ.prefs file');
	reInstall();
}
function reInstall(){
	TSpath	=getDirectory("macros")+"/toolsets/Rename and Save ROI Sets.txt";
	text="'Rename and Save ROI Sets.txt' could not be found at the\nImageJ/macros/toolset folder.\nThis toolset must be saved in this directory with the above\nname or it will not install...";
	if (!File.exists(TSpath)) exit(text);
	else run("Install...", "install="+"["+TSpath+"]");
}
function ROIconverter() {
	run("Enlarge...", "enlarge=0");
}
function mirrorROI() {
	if (selectionType()==9) s='freehand'; else s=selectionType();
	ROIconverter();
	getSelectionBounds(x, y, width, height);
	originalX = x;
	originalY = y;
	getSelectionCoordinates(x,y);
	aX= newArray(x.length);
	aY= newArray(x.length);
	for (i=0; i<x.length; i++) {aX[i]= fx*x[i]; aY[i]= fy*y[i];}
	makeSelection(s, aX, aY);
	setSelectionLocation(originalX , originalY);
}
function MoveToNext() {
	n =roiManager("count");
	i =roiManager("index");
	if(n==i+1) roiManager('select', 0);
	else roiManager('select', i+1);
}
function MoveToPrevious() {
	n =roiManager("count");
	i =roiManager("index");
	if (i<=0) roiManager('select', n-1);
	else roiManager('select', i-1);
}