File: Startup.java

package info (click to toggle)
imagej 1.52j-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,604 kB
  • sloc: java: 120,017; sh: 279; xml: 161; makefile: 6
file content (98 lines) | stat: -rw-r--r-- 2,706 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package ij.plugin;
import ij.*;
import ij.gui.*;
import ij.macro.Interpreter;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Vector;

/** This plugin implements the Edit/Options/Startup command. */
	public class Startup implements PlugIn, ItemListener {
		private static String NAME = "RunAtStartup.ijm";
		private GenericDialog gd;
		private static final String[] code = {
			"[Select from list]",
			"Black background",
			"Add to overlay",
			"Debug mode",
			"10-bit (0-1023) range",
			"12-bit (0-4095) range"
		};
	private String macro = "";
	private int originalLength;

	public void run(String arg) {
		macro = getStartupMacro();
		String macro2 = macro;
		if (!showDialog())
			return;
		if (!macro.equals(macro2)) {
			if (!runMacro(macro))
				return;
			saveStartupMacro(macro);
		}
	}
	
	public String getStartupMacro() {
		String macro = IJ.openAsString(IJ.getDirectory("macros")+NAME);
		if (macro==null || macro.startsWith("Error:"))
			return null;
		else
			return macro;
	}
		
	private void saveStartupMacro(String macro) {
		IJ.saveString(macro, IJ.getDirectory("macros")+NAME);
	}

	private boolean showDialog() {
		gd = new GenericDialog("Startup Macro");
		String text = "Macro code contained in this text area\nexecutes when ImageJ starts up.";
		Font font = new Font("SansSerif", Font.PLAIN, 14);
		gd.setInsets(5,15,0);
		gd.addMessage(text, font);
		gd.setInsets(5, 10, 0);
		gd.addTextAreas(macro, null, 12, 50);
		gd.addChoice("Add code:", code, code[0]);
		Vector choices = gd.getChoices();
		if (choices!=null) {
			Choice choice = (Choice)choices.elementAt(0);
			choice.addItemListener(this);
		}
		gd.showDialog();
		macro = gd.getNextText();
		return !gd.wasCanceled();
	}
	
	private boolean runMacro(String macro) {
		Interpreter interp = new Interpreter();
		interp.run(macro, null);
		if (interp.wasError())
			return false;
		else
			return true;
	}
				
	public void itemStateChanged(ItemEvent e) {
		Choice choice = (Choice)e.getSource();
		String item = choice.getSelectedItem();
		String statement = null;
		if (item.equals(code[1]))
			statement = "setOption(\"BlackBackground\", true);\n";
		else if (item.equals(code[2]))
			statement = "setOption(\"Add to overlay\", true);\n";
		else if (item.equals(code[3]))
			statement = "setOption(\"DebugMode\", true);\n";
		else if (item.equals(code[4]))
			statement = "call(\"ij.ImagePlus.setDefault16bitRange\", 10);\n";
		else if (item.equals(code[5]))
			statement = "call(\"ij.ImagePlus.setDefault16bitRange\", 12);\n";
		if (statement!=null) {
			TextArea ta = gd.getTextArea1();
			ta.insert(statement, ta.getCaretPosition());
			if (IJ.isMacOSX()) ta.requestFocus();
		}
	}

}