File: SimpleCommands.java

package info (click to toggle)
imagej 1.54g-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,520 kB
  • sloc: java: 132,209; sh: 286; xml: 255; makefile: 6
file content (277 lines) | stat: -rw-r--r-- 7,523 bytes parent folder | download
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package ij.plugin;
import ij.*;
import ij.process.*;
import ij.gui.*;
import ij.io.Opener;
import ij.text.TextWindow;
import ij.measure.ResultsTable;
import ij.plugin.frame.Editor;
import java.awt.Desktop;
import java.awt.Frame;
import java.io.File;

/** This plugin implements the Plugins/Utilities/Unlock, Image/Rename
	and Plugins/Utilities/Search commands. */
public class SimpleCommands implements PlugIn {
	static String searchArg;
    private static String[] choices = {"Locked Image", "Clipboard", "Undo Buffer"};
    private static int choiceIndex = 0;

	public void run(String arg) {
		if (arg.equals("search"))
			search();
		else if (arg.equals("import")) 
			Opener.openResultsTable("");
		else if (arg.equals("table")) 
			Opener.openTable("");
		else if (arg.equals("rename"))
			rename();	
		else if (arg.equals("reset"))
			reset();
		else if (arg.equals("about"))
			aboutPluginsHelp();
		else if (arg.equals("install"))
			installation();
		else if (arg.equals("set"))
			setSliceLabel();
		else if (arg.equals("remove"))
			removeStackLabels();
		else if (arg.equals("itor"))
			imageToResults();
		else if (arg.equals("rtoi"))
			resultsToImage();
		else if (arg.equals("display"))
			IJ.runMacroFile("ij.jar:ShowAllLuts", null);
		else if (arg.equals("missing"))
			showMissingPluginsMessage();
		else if (arg.equals("fonts"))
			showFonts();
		else if (arg.equals("opencp"))
			openControlPanel();
		else if (arg.equals("magic"))
			installMagicMontageTools();
		else if (arg.equals("interactive"))
			openInteractiveModeEditor();
		else if (arg.startsWith("showdir"))
			showDirectory(arg.replace("showdir", ""));
		else if (arg.equals("measure"))
			measureStack();
		else if (arg.equals("invert"))
			IJ.runMacroFile("ij.jar:InvertAllLuts", null);

	}
	
	private synchronized void showFonts() {
		Thread t = new Thread(new Runnable() {
			public void run() {IJ.runPlugIn("ij.plugin.Text", "");}
		});
		t.start();
	}

	private void reset() {
		GenericDialog gd = new GenericDialog("");
		gd.addChoice("Reset:", choices, choices[choiceIndex]);
		gd.showDialog();
		if (gd.wasCanceled()) return;
		choiceIndex = gd.getNextChoiceIndex();
		switch (choiceIndex) {
			case 0: unlock(); break;
			case 1: resetClipboard(); break;
			case 2: resetUndo(); break;
		}
	}
	
	private void unlock() {
		ImagePlus imp = IJ.getImage();
		boolean wasUnlocked = imp.lockSilently();
		if (wasUnlocked)
			IJ.showStatus("\""+imp.getTitle()+"\" is not locked");
		else {
			IJ.showStatus("\""+imp.getTitle()+"\" is now unlocked");
			IJ.beep();
		}
		imp.unlock();
	}

	private void resetClipboard() {
		ImagePlus.resetClipboard();
		IJ.showStatus("Clipboard reset");
	}
	
	private void resetUndo() {
		Undo.setup(Undo.NOTHING, null);
		IJ.showStatus("Undo reset");
	}
	
	private void rename() {
		ImagePlus imp = IJ.getImage();
		GenericDialog gd = new GenericDialog("Rename");
		gd.addStringField("Title:", imp.getTitle(), 30);
		gd.showDialog();
		if (!gd.wasCanceled())
			imp.setTitle(gd.getNextString());
	}
		
	private void search() {
		searchArg = IJ.runMacroFile("ij.jar:Search", searchArg);
	}
		
	private void installation() {
		String url = IJ.URL2+"/docs/install/";
		if (IJ.isMacintosh())
			url += "osx.html";
		else if (IJ.isWindows())
			url += "windows.html";
		else if (IJ.isLinux())
			url += "linux.html";
		IJ.runPlugIn("ij.plugin.BrowserLauncher", url);
	}
	
	private void aboutPluginsHelp() {
		IJ.showMessage("\"About Plugins\" Submenu", 
			"Plugins packaged as JAR files can add entries\n"+
			"to this submenu. There is an example at\n \n"+
			IJ.URL2+"/plugins/jar-demo.html");
	}
	
	private void setSliceLabel() {
		ImagePlus imp = IJ.getImage();
		ImageStack stack = imp.getStack();
		int n = imp.getCurrentSlice();
		String label = stack.getSliceLabel(n);
		String label2 = label;
		if (label2==null)
			label2 = "";
		GenericDialog gd = new GenericDialog("Set Slice Label ("+n+")");
		gd.addStringField("Label:", label2, 30);
		gd.showDialog();
		if (!gd.wasCanceled()) {
			label2 = gd.getNextString();
			if (label2!=null && !label2.equals(label)) {
				if (label2.length()==0)
					label2 = null;
				stack.setSliceLabel(label2, n);
				imp.setProp("Slice_Label", label2);	
				imp.repaintWindow();
			}
		}
	}

	private void removeStackLabels() {
		ImagePlus imp = IJ.getImage();
		ImageStack stack = imp.getStack();
		int size = imp.getStackSize();
		for (int i=1; i<=size; i++)
			stack.setSliceLabel(null, i);
		if (size==1)
			imp.setProp("Slice_Label", null);				
		imp.repaintWindow();
	}
	
	private void imageToResults() {
		ImagePlus imp = IJ.getImage();
		ImageProcessor ip = imp.getProcessor();
		ResultsTable rt = ResultsTable.createTableFromImage(ip);
		rt.show("Results");
	}
	
	private void resultsToImage() {
		ResultsTable rt = ResultsTable.getResultsTable();
		if (rt==null || rt.size()==0) {
			IJ.error("Results to Image", "The Results table is empty");
			return;
		}
		ImageProcessor ip = rt.getTableAsImage();
		if (ip==null) return;
		new ImagePlus("Results Table", ip).show();
	}
	
	private void openControlPanel() {
		Prefs.set("Control_Panel.@Main", "51 22 92 426");
		Prefs.set("Control_Panel.Help.Examples", "144 107 261 373");
		IJ.run("Control Panel...", "");
	}

	private void showMissingPluginsMessage() {
		IJ.showMessage("Path Randomization", 
			"Plugins were not loaded due to macOS Path Randomization.\n"+
			"To work around this problem, move ImageJ.app out of the\n"+
			"ImageJ folder and then copy it back. More information is at\n \n"+
			IJ.URL2+"/docs/install/osx.html#randomization");
	}
	
	private void installMagicMontageTools() {
		String name = "MagicMontageTools.txt";
		String path = "/macros/"+name;
		MacroInstaller mi = new MacroInstaller();
		if (IJ.shiftKeyDown())
			 Toolbar.showCode(name, mi.openFromIJJar(path));
		else
			try {
				mi.installFromIJJar(path);
			} catch (Exception e) {}
	}
	
	private void openInteractiveModeEditor() {
		Editor ed = new Editor();
		ed.setSize(600, 500);
		ed.create(Editor.INTERACTIVE_NAME, "");
	}
	
	private void showDirectory(String arg) {
		arg = arg.toLowerCase();
		String path = IJ.getDir(arg);
		if (path == null) {
			if (arg.equals("image")) {
				if (WindowManager.getCurrentImage()==null)
					IJ.noImage();
				else
					IJ.error("No file is associated with front image");
			} else
				IJ.error("Folder not found: " + arg);
			return;
		}		
		File dir = new File(path);
		if (!dir.exists()) {
			IJ.error("Folder not found: " + arg);
			return;
		}
		if (arg.equals("image")&& IJ.getImage()!=null) {
			File imgPath = new File(IJ.getDir("image"));
			if (!imgPath.exists()) {
				IJ.error("Image not found");
				return;
			}
		}
		if (IJ.debugMode) IJ.log("Show Folder: arg="+arg+", path="+path);
		String msg1 = "";
		if (IJ.isLinux()) try {
			if (IJ.debugMode) IJ.log("  trying xdg-open "+path);
			Runtime.getRuntime().exec(new String[] {"xdg-open", path} );
			return;
		} catch (Exception e2) {
			msg1 = "xdg-open error: "+e2;
		}
		try {
			if (IJ.debugMode) IJ.log("  trying Desktop.open "+dir);
			Desktop desktop = Desktop.getDesktop();
			desktop.open(dir);
		} catch (Exception e) {
			String msg2 = "Desktop.open error: "+e;
			if (msg1.length()>0)
				msg2 = msg1+"\n"+msg2;
			IJ.error("Show Folder", msg2);
		}
	}

	private void measureStack() {
		ImagePlus imp = IJ.getImage();
		if (imp.isLocked()) {
			IJ.showStatus("Image is locked: \""+imp.getTitle()+"\"");
			IJ.beep();
		} else
			IJ.runMacroFile("ij.jar:MeasureStack", null);
		return;
	}

}