5 import java.awt.image.*;
8 import java.awt.event.KeyEvent;
13 import ij.text.TextWindow;
14 import ij.plugin.frame.*;
20 private static String previousCommand;
22 private String command;
25 private Thread thread;
39 if (cmd.startsWith(
"Repeat"))
40 command = previousCommand;
43 if (!(cmd.equals(
"Undo")||cmd.equals(
"Close")))
44 previousCommand = cmd;
48 thread =
new Thread(
this, cmd);
49 thread.setPriority(Math.max(thread.getPriority()-2, Thread.MIN_PRIORITY));
54 if (command==null)
return;
59 runCommand(command, imp);
60 }
catch(Throwable e) {
63 if (imp!=null) imp.
unlock();
64 String msg = e.getMessage();
65 if (e instanceof OutOfMemoryError)
66 IJ.outOfMemory(command);
67 else if (e instanceof RuntimeException && msg!=null && msg.equals(
"Macro canceled"))
70 CharArrayWriter caw =
new CharArrayWriter();
71 PrintWriter pw =
new PrintWriter(caw);
72 e.printStackTrace(pw);
73 String s = caw.toString();
74 if (IJ.isMacintosh()) {
75 if (s.indexOf(
"ThreadDeath")>0)
77 s =
Tools.fixNewLines(s);
79 JOptionPane.showMessageDialog(IJ.getInstance(), s,
"Exception", JOptionPane.ERROR_MESSAGE);
86 PrintWriter pw = null;
88 FileOutputStream fos =
new FileOutputStream(
"exception.txt");
89 BufferedOutputStream bos =
new BufferedOutputStream(fos);
90 pw =
new PrintWriter(bos);
92 catch (IOException e) {
101 public void runCommand(String cmd, ImagePlus imp) {
103 if (cmd.equals(
"New..."))
105 else if (cmd.equals(
"Open Local")) {
107 }
else if (cmd.equals(
"Close"))
109 else if (cmd.equals(
"Cut"))
111 else if (cmd.equals(
"Copy"))
114 Hashtable table = Menus.getCommands();
115 String plugIn = (String)table.get(cmd);
117 runPlugIn(cmd, plugIn);
121 ij.getContentPane().repaint();
130 ImageCanvas canvas = imp.getCanvas();
132 if (cmd.equals(
"Revert"))
134 else if (cmd.equals(
"Save Local"))
135 {
new FileSaver(imp).save(); }
136 else if (cmd.equals(
"Paste"))
138 else if (cmd.equals(
"Undo"))
141 IJ.
error(
"Unrecognized command: " + cmd);
147 void runPlugIn(String cmd, String className) {
149 if (className.endsWith(
"\")")) {
151 int argStart = className.lastIndexOf(
"(\"");
153 arg = className.substring(argStart+2, className.length()-2);
154 className = className.substring(0, argStart);
157 IJ.runPlugIn(cmd, className, arg);
161 IJ.error(
"Selection required");
164 void copy(ImagePlus imp,
boolean cut) {
169 imp.getCanvas().copy(cut);
172 void close(ImagePlus imp) {
178 return previousCommand;