package ca.phcri;



import ij.*;
import ij.gui.*;
import java.awt.*;
import java.awt.event.*;
import ij.plugin.*;
import ij.plugin.frame.PlugInFrame;


public class Grid_Switch implements PlugIn, ActionListener, WindowListener {
	private ImagePlus imp;
	private boolean gridOn = false;
	private Button b1;
	private static Overlay layer;
	private static Roi gridRoi;
	
	public void run(String arg) {
		if (IJ.versionLessThan("1.47"))	 		return;
		gridSwitch();
	}
	
	void gridSwitch(){
		PlugInFrame gs = new PlugInFrame("Grid Switch");
		gs.setSize(200, 100);
		gs.addWindowListener(this);
		b1 = new Button("Grid On");
		b1.addActionListener(this);
		gs.add(b1);

		gs.setVisible(true);
	}
	
	public void windowActivated(WindowEvent e) {
		imp = WindowManager.getCurrentImage();
		if(imp != null){
			layer = imp.getOverlay();
			if(layer == null || layer.getIndex("grid") == -1) IJ.showStatus("Grid Switch \"" + imp.getTitle() + "\" does not have a grid.");
			else gridRoi = layer.get(layer.getIndex("grid"));
		}
	}
	public void windowClosed(WindowEvent e) {}
	public void windowClosing(WindowEvent e) {}
	public void windowDeactivated(WindowEvent e) {
		enableGrid();
		b1.setLabel("Grid On");
		gridOn = false;
	}
	public void windowDeiconified(WindowEvent e) {}
	public void windowIconified(WindowEvent e) {}
	public void windowOpened(WindowEvent e) {}
	
	public void actionPerformed(ActionEvent e) {
		if(gridOn) {
			enableGrid();
			b1.setLabel("Grid On");
		} else {
			layer.remove(layer.getIndex("grid"));
			imp.setOverlay(layer);
			b1.setLabel("Grid Off");
		}
		gridOn = !gridOn;
	}
	
	void enableGrid() {
		if(layer.getIndex("grid") != -1) layer.remove(layer.getIndex("grid"));
		layer.add(gridRoi);
		imp.setOverlay(layer);
	}
}
@@ -316,16 +316,64 @@
 on b1;%0D%0A
+%09private static String gridStatus = %22Grid On%22;%0D%0A
 %09private
@@ -396,16 +396,16 @@
 layer;%0D%0A
-
 %09private
@@ -678,25 +678,26 @@
  Button(
-%22Grid On%22
+gridStatus
 );%0D%0A%09%09b1
@@ -1265,28 +1265,29 @@
 d();%0D%0A%09%09
-b1.setLabel(
+gridStatus = 
 %22Grid On
@@ -1283,25 +1283,24 @@
  = %22Grid On%22
-)
 ;%0D%0A%09%09gridOn 
@@ -1539,36 +1539,37 @@
 Grid();%0D%0A%09%09%09
-b1.setLabel(
+gridStatus = 
 %22Grid On%22);%0D
@@ -1565,17 +1565,16 @@
 Grid On%22
-)
 ;%0D%0A%09%09%7D e
@@ -1656,20 +1656,21 @@
 %0A%09%09%09
-b1.setLabel(
+gridStatus = 
 %22Gri
@@ -1675,17 +1675,16 @@
 rid Off%22
-)
 ;%0D%0A%09%09%7D%0D%0A
@@ -1767,17 +1767,22 @@
 ) != -1)
- 
+%0D%0A%09%09%09%09
 layer.re

Use static String gridStatus for button instead of "Grid on" and "Grid off" in Grid_Switch.java