3e2d01cea140b0c6acc2d6986a27a05a7847672b
[imageja.git] / ij / Macro.java
1 package ij;
2 import ij.process.*;
3 import ij.gui.*;
4 import ij.io.*;
5 import ij.measure.*;
6 import ij.plugin.filter.*;
7 import ij.macro.Interpreter;
8 import java.awt.*;
9 import java.awt.image.*;
10 import java.io.*;
11 import java.util.Locale;
12 import java.util.Hashtable;
13
14 /** The class contains static methods that perform macro operations. */
15 public class Macro {
16
17         public static final String MACRO_CANCELED = "Macro canceled";
18
19         // A table of Thread as keys and String as values, so  
20         // Macro options are local to each calling thread.
21         static private Hashtable table = new Hashtable();
22         static boolean abort;
23
24         public static boolean open(String path) {
25                 if (path==null || path.equals("")) {
26                         Opener o = new Opener();
27                         return true;
28                 }
29                 Opener o = new Opener();
30                 ImagePlus img = o.openImage(path);
31                 if (img==null)
32                         return false;
33                 img.show();     
34                 return true;
35         }
36
37         public static boolean saveAs(String path) {
38                 ImagePlus imp = WindowManager.getCurrentImage();
39                 if (imp==null)
40                         return false;
41                 FileSaver fs = new FileSaver(imp);
42                 if (path==null || path.equals(""))
43                         return fs.saveAsTiff();
44                 if (imp.getStackSize()>1)
45                         return fs.saveAsTiffStack(path);
46                 else
47                         return fs.saveAsTiff(path);
48         }
49
50         public static String getName(String path) {
51                 int i = path.lastIndexOf('/');
52                 if (i==-1)
53                         i = path.lastIndexOf('\\');
54                 if (i>0)
55                         return path.substring(i+1);
56                 else
57                         return path;
58         }
59         
60         public static String getDir(String path) {
61                 int i = path.lastIndexOf('/');
62                 if (i==-1)
63                         i = path.lastIndexOf('\\');
64                 if (i>0)
65                         return path.substring(0, i+1);
66                 else
67                         return "";
68         }
69         
70         /** Aborts the currently running macro or any plugin using IJ.run(). */
71         public static void abort() {
72                 abort = true;
73                 //IJ.log("Abort: "+Thread.currentThread().getName());
74                 if (Thread.currentThread().getName().endsWith("Macro$")) {
75                         table.remove(Thread.currentThread());
76                         throw new RuntimeException(MACRO_CANCELED);
77                 }
78         }
79
80         /** If a command started using run(name, options) is running,
81                 and the current thread is the same thread,
82                 returns the options string, otherwise, returns null.
83                 @see ij.gui.GenericDialog
84                 @see ij.io.OpenDialog
85         */
86         public static String getOptions() {
87                 //IJ.log("getOptions: "+Thread.currentThread().hashCode()); //ts
88                 if (Thread.currentThread().getName().startsWith("Run$_")) {
89                         Object options = table.get(Thread.currentThread());
90                         return options==null?null:options+" ";
91                 } else
92                         return null;
93         }
94
95         /** Define a set of Macro options for the current Thread. */
96         public static void setOptions(String options) {
97                 //IJ.log("setOptions: "+Thread.currentThread().hashCode()+" "+options); //ts
98                 if (options==null || options.equals(""))
99                         table.remove(Thread.currentThread());
100                 else
101                         table.put(Thread.currentThread(), options);
102         }
103
104         /** Define a set of Macro options for a Thread. */
105         public static void setOptions(Thread thread, String options) {
106                 if (null==thread)
107                         throw new RuntimeException("Need a non-null thread instance");
108                 if (null==options)
109                         table.remove(thread);
110                 else
111                         table.put(thread, options);
112         }
113
114         public static String getValue(String options, String key, String defaultValue) {
115                 key = trimKey(key);
116         key += '=';
117                 int index=-1;
118                 do {
119                         index = options.indexOf(key, ++index);
120                         if (index<0) return defaultValue;
121                 } while (index!=0&&options.charAt(index-1)!=' ');
122                 options = options.substring(index+key.length(), options.length());
123                 if (options.charAt(0)=='\'') {
124                         index = options.indexOf("'",1);
125                         if (index<0)
126                                 return defaultValue;
127                         else
128                                 return options.substring(1, index);
129                 } else if (options.charAt(0)=='[') {
130                         index = options.indexOf("]",1);
131                         if (index<0)
132                                 return defaultValue;
133                         else
134                                 return options.substring(1, index);
135                 } else {
136                         //if (options.indexOf('=')==-1) {
137                         //      options = options.trim();
138                         //      IJ.log("getValue: "+key+"  |"+options+"|");
139                         //      if (options.length()>0)
140                         //              return options;
141                         //      else
142                         //              return defaultValue;
143                         //}
144                         index = options.indexOf(" ");
145                         if (index<0)
146                                 return defaultValue;
147                         else
148                                 return options.substring(0, index);
149                 }
150         }
151         
152         public static String trimKey(String key) {
153                 int index = key.indexOf(" ");
154                 if (index>-1)
155                         key = key.substring(0,index);
156                 index = key.indexOf(":");
157                 if (index>-1)
158                         key = key.substring(0,index);
159                 key = key.toLowerCase(Locale.US);
160                 return key;
161         }
162
163         public static void setLocalVariable(String key,String value) {
164                 Interpreter i=Interpreter.getInstance();
165                 if(i==null)
166                         return;
167                 i.setLocalVariable(key,value);
168         }
169 }
170