Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
Executer.java
1 package ij;
2 
3 import java.awt.*;
4 import java.net.URL;
5 import java.awt.image.*;
6 import java.io.*;
7 import java.util.*;
8 import java.awt.event.KeyEvent;
9 import ij.io.*;
10 import ij.process.*;
11 import ij.gui.*;
12 import ij.util.*;
13 import ij.text.TextWindow;
14 import ij.plugin.frame.*;
15 import javax.swing.*;
16 
18 public class Executer implements Runnable {
19 
20  private static String previousCommand;
21 
22  private String command;
23  private ImagePlus iplus;
24  private ImageJ ij;
25  private Thread thread;
26 
29  public Executer(String cmd) {
30  command = cmd;
31  iplus = IJ.getInstance().getImagePlus();
32  ij = IJ.getInstance();
33  }
34 
37  public Executer(String cmd, ImagePlus imp) {
38  iplus = imp;
39  if (cmd.startsWith("Repeat"))
40  command = previousCommand;
41  else {
42  command = cmd;
43  if (!(cmd.equals("Undo")||cmd.equals("Close")))
44  previousCommand = cmd;
45  }
46 
47  ij = IJ.getInstance();
48  thread = new Thread(this, cmd);
49  thread.setPriority(Math.max(thread.getPriority()-2, Thread.MIN_PRIORITY));
50  thread.start();
51  }
52 
53  public void run() {
54  if (command==null) return;
55  ImagePlus imp = iplus;
56  iplus = null; // maybe this will help get the image GC'd
57 
58  try {
59  runCommand(command, imp);
60  } catch(Throwable e) {
61  IJ.showStatus("");
62  IJ.showProgress(1.0);
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"))
68  ; //do nothing
69  else {
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)
76  return;
77  s = Tools.fixNewLines(s);
78  }
79  JOptionPane.showMessageDialog(IJ.getInstance(), s, "Exception", JOptionPane.ERROR_MESSAGE);
80  }
81  }
82  }
83 
84 
85  void save(String s) {
86  PrintWriter pw = null;
87  try {
88  FileOutputStream fos = new FileOutputStream("exception.txt");
89  BufferedOutputStream bos = new BufferedOutputStream(fos);
90  pw = new PrintWriter(bos);
91  }
92  catch (IOException e) {
93  IJ.error("" + e);
94  return;
95  }
96  pw.println(s);
97  pw.close();
98  }
99 
100 
101  public void runCommand(String cmd, ImagePlus imp) {
102 
103  if (cmd.equals("New..."))
104  new NewImage();
105  else if (cmd.equals("Open Local")) {
106  new Opener().open();
107  } else if (cmd.equals("Close"))
108  close(imp);
109  else if (cmd.equals("Cut"))
110  copy(imp, true);
111  else if (cmd.equals("Copy"))
112  copy(imp, false);
113  else {
114  Hashtable table = Menus.getCommands();
115  String plugIn = (String)table.get(cmd);
116  if (plugIn!=null)
117  runPlugIn(cmd, plugIn);
118  else
119  runImageCommand(cmd, imp);
120  }
121  ij.getContentPane().repaint();
122  }
123 
125  public void runImageCommand(String cmd, ImagePlus imp) {
126 
127  if (imp!=null) {
128  if (!imp.lock())
129  return; // exit if image is in use
130  ImageCanvas canvas = imp.getCanvas();
131 
132  if (cmd.equals("Revert"))
133  {imp.revert(); }
134  else if (cmd.equals("Save Local"))
135  {new FileSaver(imp).save(); }
136  else if (cmd.equals("Paste"))
137  { canvas.paste(); }
138  else if (cmd.equals("Undo"))
139  { Undo.undo(); }
140  else
141  IJ.error("Unrecognized command: " + cmd);
142 
143  imp.unlock();
144  }
145  }
146 
147  void runPlugIn(String cmd, String className) {
148  String arg = "";
149  if (className.endsWith("\")")) {
150  // extract string argument (e.g. className("arg"))
151  int argStart = className.lastIndexOf("(\"");
152  if (argStart>0) {
153  arg = className.substring(argStart+2, className.length()-2);
154  className = className.substring(0, argStart);
155  }
156  }
157  IJ.runPlugIn(cmd, className, arg);
158  }
159 
160  void roiRequired() {
161  IJ.error("Selection required");
162  }
163 
164  void copy(ImagePlus imp, boolean cut) {
165  if (imp==null) {
166  IJ.noImage();
167  return;
168  } else
169  imp.getCanvas().copy(cut);
170  }
171 
172  void close(ImagePlus imp) {
173  }
174 
177  public static String getCommand() {
178  return previousCommand;
179  }
180 
181 }
182 
183