Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
Recorder.java
1 package ij.plugin.frame;
2 import java.awt.*;
3 import java.awt.event.*;
4 import java.util.*;
5 import java.io.*;
6 import ij.*;
7 import ij.plugin.*;
8 import ij.plugin.frame.*;
9 import ij.text.*;
10 import ij.gui.*;
11 import ij.util.*;
12 import ij.io.*;
13 import ij.process.*;
14 import ij.measure.*;
15 
17 public class Recorder extends PlugInFrame implements PlugIn, ActionListener {
18 
20  public static boolean record;
21 
23  public static boolean recordInMacros;
24 
25  private Button makeMacro, help;
26  private TextField macroName;
27  private String fitTypeStr = CurveFitter.fitList[0];
28  private static TextArea textArea;
29  private static Frame instance;
30  private static String commandName;
31  private static String commandOptions;
32  private static String defaultName = "Macro";
33 
34  public Recorder() {
35  super("Recorder");
36  if (instance!=null) {
37  instance.toFront();
38  return;
39  }
40  instance = this;
41  record = true;
42  Panel panel = new Panel(new FlowLayout(FlowLayout.CENTER, 2, 0));
43  panel.add(new Label("Name:"));
44  macroName = new TextField(defaultName,15);
45  panel.add(macroName);
46  panel.add(new Label(" "));
47  makeMacro = new Button("Create");
48  makeMacro.addActionListener(this);
49  panel.add(makeMacro);
50  panel.add(new Label(" "));
51  help = new Button("?");
52  help.addActionListener(this);
53  panel.add(help);
54  add("North", panel);
55  textArea = new TextArea("",15,60,TextArea.SCROLLBARS_VERTICAL_ONLY);
56  textArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
57  //textArea.setBackground(Color.white);
58  add("Center", textArea);
59  pack();
60  GUI.center(this);
61  show();
62  IJ.register(Recorder.class);
63  }
64 
65  public static void record(String method) {
66  if (textArea==null)
67  return;
68  textArea.append(method+"();\n");
69  }
70 
74  public static void setCommand(String command) {
75  if (textArea==null || (Thread.currentThread().getName().startsWith("Run$_")&&!recordInMacros))
76  return;
77  commandName = command;
78  commandOptions = null;
79  //IJ.log("setCommand: "+command+" "+Thread.currentThread().getName());
80  }
81 
82  static String fixPath (String path) {
83  StringBuffer sb = new StringBuffer();
84  char c;
85  for (int i=0; i<path.length(); i++) {
86  sb.append(c=path.charAt(i));
87  if (c=='\\')
88  sb.append("\\");
89  }
90  return new String(sb);
91  }
92 
93  public static void record(String method, String arg) {
94  if (textArea==null) return;
95  textArea.append(method+"(\""+arg+"\");\n");
96  }
97 
98  public static void record(String method, int a1) {
99  if (textArea==null) return;
100  textArea.append(method+"("+a1+");\n");
101  }
102 
103  public static void record(String method, int a1, int a2) {
104  if (textArea==null) return;
105  textArea.append(method+"("+a1+", "+a2+");\n");
106  }
107 
108  public static void record(String method, double a1, double a2) {
109  if (textArea==null) return;
110  textArea.append(method+"("+a1+", "+a2+");\n");
111  }
112 
113  public static void record(String method, int a1, int a2, int a3) {
114  if (textArea==null) return;
115  textArea.append(method+"("+a1+", "+a2+", "+a3+");\n");
116  }
117 
118  public static void record(String method, String args, int a1, int a2) {
119  if (textArea==null) return;
120  method = "//"+method;
121  textArea.append(method+"(\""+args+"\", "+a1+", "+a2+");\n");
122  }
123 
124  public static void record(String method, int a1, int a2, int a3, int a4) {
125  if (textArea==null) return;
126  textArea.append(method+"("+a1+", "+a2+", "+a3+", "+a4+");\n");
127  }
128 
129  public static void record(String method, String path, String args, int a1, int a2, int a3, int a4, int a5) {
130  if (textArea==null) return;
131  path = fixPath(path);
132  method = "//"+method;
133  textArea.append(method+"(\""+path+"\", "+"\""+args+"\", "+a1+", "+a2+", "+a3+", "+a4+", "+a5+");\n");
134  }
135 
136  public static void recordOption(String key, String value) {
137  key = trimKey(key);
138  value = addQuotes(value);
139  if (commandOptions==null)
140  commandOptions = key+"="+value;
141  else
142  commandOptions += " "+key+"="+value;
143  //IJ.write(" "+key+"="+value);
144  }
145 
146  public static void recordPath(String key, String path) {
147  if (key==null) return;
148  key = trimKey(key);
149  path = fixPath(path);
150  path = addQuotes(path);
151  if (commandOptions==null)
152  commandOptions = key+"="+path;
153  else
154  commandOptions += " "+key+"="+path;
155  //IJ.write(" "+key+"="+value);
156  }
157 
158  public static void recordOption(String key) {
159  if (commandOptions==null && key.equals(" "))
160  commandOptions = " ";
161  else {
162  key = trimKey(key);
163  if (commandOptions==null)
164  commandOptions = key;
165  else
166  commandOptions += " "+key;
167  }
168  }
169 
170  static String trimKey(String key) {
171  int index = key.indexOf(" ");
172  if (index>-1)
173  key = key.substring(0,index);
174  index = key.indexOf(":");
175  if (index>-1)
176  key = key.substring(0,index);
177  key = key.toLowerCase(Locale.US);
178  return key;
179  }
180 
182  public static void saveCommand() {
183  if (commandName!=null) {
184  if (commandOptions!=null)
185  textArea.append("run(\""+commandName+"\", \""+commandOptions+"\");\n");
186  else
187  textArea.append("run(\""+commandName+"\");\n");
188  }
189  commandName = null;
190  commandOptions = null;
191  }
192 
193  static String addQuotes(String value) {
194  int index = value.indexOf(' ');
195  if (index>-1)
196  value = "'"+value+"'";
197  return value;
198  }
199 
200  void createMacro() {
201 
202  }
203 
204  public void actionPerformed(ActionEvent e) {
205  if (e.getSource()==makeMacro)
206  createMacro();
207  else if (e.getSource()==help)
208  showHelp();
209  }
210 
211  void showHelp() {
212  IJ.showMessage("Recorder",
213  "Click \"Create\" to open recorded commands\n"
214  +"as a macro in an editor window.\n"
215  +" \n"
216  +"In the editor:\n"
217  +" \n"
218  +" Type ctrl+R (File>Run Macro) to\n"
219  +" run the macro.\n"
220  +" \n"
221  +" Use File>Save As to save it and\n"
222  +" ImageJ's Open command to open it.\n"
223  +" \n"
224  +" To create a command, use File>Save As,\n"
225  +" add a '_' to the name, save in the \n"
226  +" plugins folder, and restart ImageJ.\n"
227  +" \n"
228  +" Use Edit>Convert to Plugin to convert\n"
229  +" the macro to a plugin."
230  );
231  }
232 
233  public void windowClosing(WindowEvent e) {
234  close();
235  }
236 
237  public void close() {
238  super.close();
239  record = false;
240  textArea = null;
241  commandName = null;
242  instance = null;
243  }
244 
245 }