<commit_before>package ca.phcri;

import ij.IJ;
import ij.ImagePlus;
import ij.WindowManager;
import ij.gui.DialogListener;
import ij.gui.GenericDialog;
import ij.gui.Overlay;
import ij.gui.Roi;
import ij.gui.ShapeRoi;
import ij.measure.Calibration;
import ij.plugin.PlugIn;
import ij.text.TextWindow;

import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Shape;
import java.awt.geom.GeneralPath;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;


public class CombinedGridsPlugin implements PlugIn, DialogListener {
	private final static String[] colors = { "Red", "Green", "Blue", "Magenta", "Cyan", "Yellow", "Orange", "Black", "White" };
	private static String color = "Blue";
	private final static int COMBINED = 0, DOUBLE_LATTICE = 1, LINES = 2,
			HLINES = 3, CROSSES = 4, POINTS = 5;
	private final static String[] types = { "Combined Point", "Double Lattice", "Lines", "Horizontal Lines", "Crosses", "Points" };
	private static String type = types[COMBINED];
	private static double areaPerPoint;

	private final static int ONE_TO_FOUR = 0, ONE_TO_NINE = 1, ONE_TO_SIXTEEN = 2, ONE_TO_TWENTYFIVE = 3, ONE_TO_THIRTYSIX = 4;
	private final static String[] ratioChoices = { "1:4", "1:9", "1:16", "1:25", "1:36" };
	private static String gridRatio = ratioChoices[ONE_TO_FOUR];
	private final static String[] radiobuttons = { "Random Offset", "Fixed Position", "Manual Input" };
	private final static int RANDOM = 0, FIXED = 1, MANUAL = 2;
	private static String radiochoice = radiobuttons[RANDOM];
	private static Component[] components; // this is to select components in the dialog box
	private final static int[] ratioField = { 4, 5 };
	private final static int[] combinedGridFields = { 14, 15, 16, 17 };
	private final static int[] parameterFieldsOff = { 10, 11, 12, 13, 14, 15, 16, 17 };
	private final static int[] xstartField = { 10, 11 };
	private final static int[] ystartField = { 12, 13 };
	private static boolean showGridSwitch = true;

	private Random random = new Random(System.currentTimeMillis());
	private ImagePlus imp;
	private double tileWidth, tileHeight;
	private int xstart, ystart;
	private int xstartCoarse, ystartCoarse, coarseGridX, coarseGridY;
	private int linesV, linesH;
	private double pixelWidth = 1.0, pixelHeight = 1.0;
	private String units;
	private String err = "";

	public void run(String arg) {
		if (IJ.versionLessThan("1.47"))
			return;
		imp = IJ.getImage();
		showDialog();
	}

	void showGrid(Shape shape) {
		Overlay layer = imp.getOverlay();
		if (shape == null) {
			if (layer != null) layer.remove(layer.getIndex("grid"));
		} else {
			Roi roi = new ShapeRoi(shape);
			roi.setStrokeColor(getColor());
			roi.setName("grid");
			if (layer != null) {
				if (layer.getIndex("grid") != -1) layer.remove(layer.getIndex("grid"));
				layer.add(roi);
			} else
				layer = new Overlay(roi);
		}
		imp.setOverlay(layer);
	}

	// methods to form grids
	void drawPoints() {
		int one = 1;
		int two = 2;
		GeneralPath path = new GeneralPath();
		for (int h = 0; h < linesV; h++) {
			for (int v = 0; v < linesH; v++) {
				float x = (float) (xstart + h * tileWidth);
				float y = (float) (ystart + v * tileHeight);
				path.moveTo(x - two, y - one); path.lineTo(x - two, y + one);
				path.moveTo(x + two, y - one); path.lineTo(x + two, y + one);
				path.moveTo(x - one, y - two); path.lineTo(x + one, y - two);
				path.moveTo(x - one, y + two); path.lineTo(x + one, y + two);
			}
		}
		showGrid(path);
	}

	void drawCrosses() {
		GeneralPath path = new GeneralPath();
		float arm = 5;
		for (int h = 0; h < linesV; h++) {
			for (int v = 0; v < linesH; v++) {
				float x = (float) (xstart + h * tileWidth);
				float y = (float) (ystart + v * tileHeight);
				path.moveTo(x - arm, y); path.lineTo(x + arm, y);
				path.moveTo(x, y - arm); path.lineTo(x, y + arm);
			}
		}
		showGrid(path);
	}

	void drawCombined() {
		GeneralPath path = new GeneralPath();
		float arm = 5;
		float pointSizeCoarse = 10;
		float armCoarse = pointSizeCoarse / 2;

		for (int h = 0; h < linesV; h++) {
			for (int v = 0; v < linesH; v++) {
				float x = (float) (xstart + h * tileWidth);
				float y = (float) (ystart + v * tileHeight);
				path.moveTo(x - arm, y); path.lineTo(x + arm, y);
				path.moveTo(x, y - arm); path.lineTo(x, y + arm);

				if ((h % coarseGridX == 0) && (v % coarseGridY == 0)) {
					float centerX = (float) (xstart + xstartCoarse * tileWidth  + h * tileWidth);
					float centerY = (float) (ystart + ystartCoarse * tileHeight + v * tileHeight);

					// drawing a coarse point by lines
					path.moveTo(centerX - pointSizeCoarse, centerY - armCoarse);
					path.lineTo(centerX - pointSizeCoarse, centerY + armCoarse);
					path.moveTo(centerX + pointSizeCoarse, centerY - 0);
					path.lineTo(centerX + pointSizeCoarse, centerY + armCoarse);
					path.moveTo(centerX - armCoarse, centerY - pointSizeCoarse);
					path.lineTo(centerX + 0,		 centerY - pointSizeCoarse);
					path.moveTo(centerX - armCoarse, centerY + pointSizeCoarse);
					path.lineTo(centerX + armCoarse, centerY + pointSizeCoarse);
				}
			}
		}
		showGrid(path);
	}

	void drawDoubleLattice() {
		GeneralPath path = new GeneralPath();
		int width = imp.getWidth();
		int height = imp.getHeight();
		float rad = 14;
		float radkappa = (float) (rad * 0.5522847498); // ref
														// https://www.java.net/node/660133

		for (int i = 0; i < linesV; i++) {
			float xoff = (float) (xstart + i * tileWidth);
			path.moveTo(xoff, 0f); path.lineTo(xoff, height);
		}
		for (int i = 0; i < linesH; i++) {
			float yoff = (float) (ystart + i * tileHeight);
			path.moveTo(0f, yoff); path.lineTo(width, yoff);
		}

		for (int h = 0; h < linesV; h++) {
			for (int v = 0; v < linesH; v++) {
				if ((h % coarseGridX == 0) && (v % coarseGridY == 0)) {
					float centerX = (float) (xstart + xstartCoarse * tileWidth + h * tileWidth);
					float centerY = (float) (ystart + ystartCoarse * tileHeight + v * tileHeight);
					// drawing curve for coarse grid
					path.moveTo(centerX, centerY - rad);
					path.curveTo(centerX - radkappa, centerY - rad, centerX - rad, centerY - radkappa, centerX - rad, centerY);
					path.curveTo(centerX - rad, centerY + radkappa, centerX - radkappa, centerY + rad, centerX, centerY + rad);
					path.curveTo(centerX + radkappa, centerY + rad, centerX + rad, centerY + radkappa, centerX + rad, centerY);
				}
			}
		}
		showGrid(path);
	}

	void drawLines() {
		GeneralPath path = new GeneralPath();
		int width = imp.getWidth();
		int height = imp.getHeight();
		for (int i = 0; i < linesV; i++) {
			float xoff = (float) (xstart + i * tileWidth);
			path.moveTo(xoff, 0f);
			path.lineTo(xoff, height);
		}
		for (int i = 0; i < linesH; i++) {
			float yoff = (float) (ystart + i * tileHeight);
			path.moveTo(0f, yoff);
			path.lineTo(width, yoff);
		}
		showGrid(path);
	}

	void drawHorizontalLines() {
		GeneralPath path = new GeneralPath();
		int width = imp.getWidth();
//		int height = imp.getHeight();
		for (int i = 0; i < linesH; i++) {
			float yoff = (float) (ystart + i * tileHeight);
			path.moveTo(0f, yoff);
			path.lineTo(width, yoff);
		}
		showGrid(path);
	}

	// end of methods for drawing grids

	void showDialog() {
		int width = imp.getWidth();
		int height = imp.getHeight();
		Calibration cal = imp.getCalibration();
		int places;
		if (cal.scaled()) {
			pixelWidth = cal.pixelWidth;
			pixelHeight = cal.pixelHeight;
			units = cal.getUnits();
			places = 2;
		} else {
			pixelWidth = 1.0;
			pixelHeight = 1.0;
			units = "pixels";
			places = 0;
		}
		if (areaPerPoint == 0.0) // default to 9x9 grid
			areaPerPoint = (width * cal.pixelWidth * height * cal.pixelHeight) / 81.0;

		// get values in a dialog box
		GenericDialog gd = new GenericDialog("Grid...");
		gd.addChoice("Grid Type:", types, type);
		gd.addNumericField("Area per Point:", areaPerPoint, places, 6, units + "^2");
		gd.addChoice("Ratio:", ratioChoices, gridRatio);
		gd.addChoice("Color:", colors, color);
		gd.addRadioButtonGroup("Grid Location", radiobuttons, 3, 1, radiobuttons[RANDOM]);
		gd.addNumericField("xstart:", 0, 0);
		gd.addNumericField("ystart:", 0, 0);
		gd.addNumericField("xstartCoarse:", 0, 0);
		gd.addNumericField("ystartCoarse:", 0, 0);
		gd.addCheckbox("Show a Grid Switch if none exists", showGridSwitch);

		// to switch enable/disable parameter input boxes
		components = gd.getComponents();
		for (int i : parameterFieldsOff) components[i].setEnabled(false);
		if (!(types[COMBINED].equals(type) || types[DOUBLE_LATTICE].equals(type)))
			for (int i : ratioField) components[i].setEnabled(false);

		gd.addDialogListener(this);
		gd.showDialog();

		if (gd.wasCanceled())
			showGrid(null);
		if (gd.wasOKed()) {
			if ("".equals(err)) {
				showParameterList();
				if (showGridSwitch && !gridSwitchExist()){
					Grid_Switch gs = new Grid_Switch();
					gs.gridSwitch();
				}
			} else {
				IJ.error("Grid", err);
				showGrid(null);
			}
		}
	}

	// event control for dialog box
	public boolean dialogItemChanged(GenericDialog gd, AWTEvent e) {
		int width = imp.getWidth();
		int height = imp.getHeight();
		type = gd.getNextChoice();
		areaPerPoint = gd.getNextNumber();
		gridRatio = gd.getNextChoice();
		color = gd.getNextChoice();
		radiochoice = gd.getNextRadioButton();
		xstart = (int) gd.getNextNumber();
		ystart = (int) gd.getNextNumber();
		xstartCoarse = (int) gd.getNextNumber();
		ystartCoarse = (int) gd.getNextNumber();
		showGridSwitch = gd.getNextBoolean();<commit_msg>Try to create a new "automatic save" function<commit_after>package ca.phcri;

import ij.IJ;
import ij.ImagePlus;
import ij.WindowManager;
import ij.gui.DialogListener;
import ij.gui.GenericDialog;
import ij.gui.Overlay;
import ij.gui.Roi;
import ij.gui.ShapeRoi;
import ij.io.OpenDialog;
import ij.measure.Calibration;
import ij.plugin.PlugIn;
import ij.text.TextWindow;

import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Shape;
import java.awt.geom.GeneralPath;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;


public class CombinedGridsPlugin implements PlugIn, DialogListener {
	private final static String[] colors = { "Red", "Green", "Blue", "Magenta", "Cyan", "Yellow", "Orange", "Black", "White" };
	private static String color = "Blue";
	private final static int COMBINED = 0, DOUBLE_LATTICE = 1, LINES = 2,
			HLINES = 3, CROSSES = 4, POINTS = 5;
	private final static String[] types = { "Combined Point", "Double Lattice", "Lines", "Horizontal Lines", "Crosses", "Points" };
	private static String type = types[COMBINED];
	private static double areaPerPoint;

	private final static int ONE_TO_FOUR = 0, ONE_TO_NINE = 1, ONE_TO_SIXTEEN = 2, ONE_TO_TWENTYFIVE = 3, ONE_TO_THIRTYSIX = 4;
	private final static String[] ratioChoices = { "1:4", "1:9", "1:16", "1:25", "1:36" };
	private static String gridRatio = ratioChoices[ONE_TO_FOUR];
	private final static String[] radiobuttons = { "Random Offset", "Fixed Position", "Manual Input" };
	private final static int RANDOM = 0, FIXED = 1, MANUAL = 2;
	private static String radiochoice = radiobuttons[RANDOM];
	private static Component[] components; // this is to select components in the dialog box
	private final static int[] ratioField = { 4, 5 };
	private final static int[] combinedGridFields = { 14, 15, 16, 17 };
	private final static int[] parameterFieldsOff = { 10, 11, 12, 13, 14, 15, 16, 17 };
	private final static int[] xstartField = { 10, 11 };
	private final static int[] ystartField = { 12, 13 };
	private static boolean showGridSwitch = true;

	private Random random = new Random(System.currentTimeMillis());
	private ImagePlus imp;
	private double tileWidth, tileHeight;
	private int xstart, ystart;
	private int xstartCoarse, ystartCoarse, coarseGridX, coarseGridY;
	private int linesV, linesH;
	private double pixelWidth = 1.0, pixelHeight = 1.0;
	private String units;
	private String err = "";

	public void run(String arg) {
		if (IJ.versionLessThan("1.47"))
			return;
		imp = IJ.getImage();
		showDialog();
	}

	void showGrid(Shape shape) {
		Overlay layer = imp.getOverlay();
		if (shape == null) {
			if (layer != null) layer.remove(layer.getIndex("grid"));
		} else {
			Roi roi = new ShapeRoi(shape);
			roi.setStrokeColor(getColor());
			roi.setName("grid");
			if (layer != null) {
				if (layer.getIndex("grid") != -1) layer.remove(layer.getIndex("grid"));
				layer.add(roi);
			} else
				layer = new Overlay(roi);
		}
		imp.setOverlay(layer);
	}

	// methods to form grids
	void drawPoints() {
		int one = 1;
		int two = 2;
		GeneralPath path = new GeneralPath();
		for (int h = 0; h < linesV; h++) {
			for (int v = 0; v < linesH; v++) {
				float x = (float) (xstart + h * tileWidth);
				float y = (float) (ystart + v * tileHeight);
				path.moveTo(x - two, y - one); path.lineTo(x - two, y + one);
				path.moveTo(x + two, y - one); path.lineTo(x + two, y + one);
				path.moveTo(x - one, y - two); path.lineTo(x + one, y - two);
				path.moveTo(x - one, y + two); path.lineTo(x + one, y + two);
			}
		}
		showGrid(path);
	}

	void drawCrosses() {
		GeneralPath path = new GeneralPath();
		float arm = 5;
		for (int h = 0; h < linesV; h++) {
			for (int v = 0; v < linesH; v++) {
				float x = (float) (xstart + h * tileWidth);
				float y = (float) (ystart + v * tileHeight);
				path.moveTo(x - arm, y); path.lineTo(x + arm, y);
				path.moveTo(x, y - arm); path.lineTo(x, y + arm);
			}
		}
		showGrid(path);
	}

	void drawCombined() {
		GeneralPath path = new GeneralPath();
		float arm = 5;
		float pointSizeCoarse = 10;
		float armCoarse = pointSizeCoarse / 2;

		for (int h = 0; h < linesV; h++) {
			for (int v = 0; v < linesH; v++) {
				float x = (float) (xstart + h * tileWidth);
				float y = (float) (ystart + v * tileHeight);
				path.moveTo(x - arm, y); path.lineTo(x + arm, y);
				path.moveTo(x, y - arm); path.lineTo(x, y + arm);

				if ((h % coarseGridX == 0) && (v % coarseGridY == 0)) {
					float centerX = (float) (xstart + xstartCoarse * tileWidth  + h * tileWidth);
					float centerY = (float) (ystart + ystartCoarse * tileHeight + v * tileHeight);

					// drawing a coarse point by lines
					path.moveTo(centerX - pointSizeCoarse, centerY - armCoarse);
					path.lineTo(centerX - pointSizeCoarse, centerY + armCoarse);
					path.moveTo(centerX + pointSizeCoarse, centerY - 0);
					path.lineTo(centerX + pointSizeCoarse, centerY + armCoarse);
					path.moveTo(centerX - armCoarse, centerY - pointSizeCoarse);
					path.lineTo(centerX + 0,		 centerY - pointSizeCoarse);
					path.moveTo(centerX - armCoarse, centerY + pointSizeCoarse);
					path.lineTo(centerX + armCoarse, centerY + pointSizeCoarse);
				}
			}
		}
		showGrid(path);
	}

	void drawDoubleLattice() {
		GeneralPath path = new GeneralPath();
		int width = imp.getWidth();
		int height = imp.getHeight();
		float rad = 14;
		float radkappa = (float) (rad * 0.5522847498); // ref
														// https://www.java.net/node/660133

		for (int i = 0; i < linesV; i++) {
			float xoff = (float) (xstart + i * tileWidth);
			path.moveTo(xoff, 0f); path.lineTo(xoff, height);
		}
		for (int i = 0; i < linesH; i++) {
			float yoff = (float) (ystart + i * tileHeight);
			path.moveTo(0f, yoff); path.lineTo(width, yoff);
		}

		for (int h = 0; h < linesV; h++) {
			for (int v = 0; v < linesH; v++) {
				if ((h % coarseGridX == 0) && (v % coarseGridY == 0)) {
					float centerX = (float) (xstart + xstartCoarse * tileWidth + h * tileWidth);
					float centerY = (float) (ystart + ystartCoarse * tileHeight + v * tileHeight);
					// drawing curve for coarse grid
					path.moveTo(centerX, centerY - rad);
					path.curveTo(centerX - radkappa, centerY - rad, centerX - rad, centerY - radkappa, centerX - rad, centerY);
					path.curveTo(centerX - rad, centerY + radkappa, centerX - radkappa, centerY + rad, centerX, centerY + rad);
					path.curveTo(centerX + radkappa, centerY + rad, centerX + rad, centerY + radkappa, centerX + rad, centerY);
				}
			}
		}
		showGrid(path);
	}

	void drawLines() {
		GeneralPath path = new GeneralPath();
		int width = imp.getWidth();
		int height = imp.getHeight();
		for (int i = 0; i < linesV; i++) {
			float xoff = (float) (xstart + i * tileWidth);
			path.moveTo(xoff, 0f);
			path.lineTo(xoff, height);
		}
		for (int i = 0; i < linesH; i++) {
			float yoff = (float) (ystart + i * tileHeight);
			path.moveTo(0f, yoff);
			path.lineTo(width, yoff);
		}
		showGrid(path);
	}

	void drawHorizontalLines() {
		GeneralPath path = new GeneralPath();
		int width = imp.getWidth();
//		int height = imp.getHeight();
		for (int i = 0; i < linesH; i++) {
			float yoff = (float) (ystart + i * tileHeight);
			path.moveTo(0f, yoff);
			path.lineTo(width, yoff);
		}
		showGrid(path);
	}

	// end of methods for drawing grids

	void showDialog() {
		int width = imp.getWidth();
		int height = imp.getHeight();
		Calibration cal = imp.getCalibration();
		int places;
		if (cal.scaled()) {
			pixelWidth = cal.pixelWidth;
			pixelHeight = cal.pixelHeight;
			units = cal.getUnits();
			places = 2;
		} else {
			pixelWidth = 1.0;
			pixelHeight = 1.0;
			units = "pixels";
			places = 0;
		}
		if (areaPerPoint == 0.0) // default to 9x9 grid
			areaPerPoint = (width * cal.pixelWidth * height * cal.pixelHeight) / 81.0;

		// get values in a dialog box
		GenericDialog gd = new GenericDialog("Grid...");
		gd.addChoice("Grid Type:", types, type);
		gd.addNumericField("Area per Point:", areaPerPoint, places, 6, units + "^2");
		gd.addChoice("Ratio:", ratioChoices, gridRatio);
		gd.addChoice("Color:", colors, color);
		gd.addRadioButtonGroup("Grid Location", radiobuttons, 3, 1, radiobuttons[RANDOM]);
		gd.addNumericField("xstart:", 0, 0);
		gd.addNumericField("ystart:", 0, 0);
		gd.addNumericField("xstartCoarse:", 0, 0);
		gd.addNumericField("ystartCoarse:", 0, 0);
		gd.addCheckbox("Show a Grid Switch if none exists", showGridSwitch);

		// to switch enable/disable parameter input boxes
		components = gd.getComponents();
		for (int i : parameterFieldsOff) components[i].setEnabled(false);
		if (!(types[COMBINED].equals(type) || types[DOUBLE_LATTICE].equals(type)))
			for (int i : ratioField) components[i].setEnabled(false);

		gd.addDialogListener(this);
		gd.showDialog();

		if (gd.wasCanceled())
			showGrid(null);
		if (gd.wasOKed()) {
			if ("".equals(err)) {
				showParameterList();
				IJ.log(OpenDialog.getDefaultDirectory());
				if (showGridSwitch && !gridSwitchExist()){
					Grid_Switch gs = new Grid_Switch();
					gs.gridSwitch();
				}
			} else {
				IJ.error("Grid", err);
				showGrid(null);
			}
		}
	}

	// event control for dialog box
	public boolean dialogItemChanged(GenericDialog gd, AWTEvent e) {
		int width = imp.getWidth();
		int height = imp.getHeight();
		type = gd.getNextChoice();
		areaPerPoint = gd.getNextNumber();
		gridRatio = gd.getNextChoice();
		color = gd.getNextChoice();
		radiochoice = gd.getNextRadioButton();
		xstart = (int) gd.getNextNumber();
		ystart = (int) gd.getNextNumber();
		xstartCoarse = (int) gd.getNextNumber();
		ystartCoarse = (int) gd.getNextNumber();
		showGridSwitch = gd.getNextBoolean();
{'fraction_non_alphanumeric': 0.1495626973037313, 'fraction_numerical': 0.014904517931998137, 'mean_word_length': 3.5564725300636644, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1554418', 'n_tokens_mistral': 7833, 'n_tokens_neox': 6810, 'n_words': 1899}