7 import java.applet.Applet;
11 import ij.plugin.filter.*;
12 import ij.process.ImageConverter;
13 import ij.process.FloatBlitter;
14 import ij.plugin.JpegWriter;
15 import ij.process.ColorProcessor;
24 public static final String PROPS_NAME =
"IJ_Props.txt";
25 public static final String PREFS_NAME =
"IJ_Prefs.txt";
26 public static final String DIR_IMAGE =
"dir.image";
27 public static final String FCOLOR =
"fcolor";
28 public static final String BCOLOR =
"bcolor";
29 public static final String ROICOLOR =
"roicolor";
30 public static final String JPEG =
"jpeg";
31 public static final String FPS =
"fps";
32 public static final String DIV_BY_ZERO_VALUE =
"div-by-zero";
33 public static final String NOISE_SD =
"noise.sd";
34 public static final String KEY_PREFIX =
".";
36 private static final int USE_POINTER=1, ANTIALIASING=2, INTERPOLATE=4, ONE_HUNDRED_PERCENT=8,
37 BLACK_BACKGROUND=16, JFILE_CHOOSER=32, UNWEIGHTED=64, BLACK_CANVAS=128;
38 public static final String OPTIONS =
"prefs.options";
41 public static String
separator = System.getProperty(
"file.separator");
57 static Properties ijPrefs =
new Properties();
58 static Properties props =
new Properties(ijPrefs);
59 static String prefsDir;
60 static String imagesURL;
61 static String homeDir;
66 public static String
load(Object ij, Applet applet) {
67 InputStream f = ij.getClass().getResourceAsStream(
"/"+PROPS_NAME);
69 return loadAppletProps(f,applet);
70 homeDir = System.getProperty(
"user.dir");
71 String userHome = System.getProperty(
"user.home");
72 String osName = System.getProperty(
"os.name");
73 if (osName.indexOf(
"Windows",0)>-1)
78 prefsDir +=
"/Library/Preferences";
81 try {f =
new FileInputStream(homeDir+
"/"+PROPS_NAME);}
82 catch (FileNotFoundException e) {f=null;}
85 return PROPS_NAME+
" not found in ij.jar or in "+homeDir;
86 f =
new BufferedInputStream(f);
87 try {props.load(f); f.close();}
88 catch (IOException e) {
return(
"Error loading "+PROPS_NAME);}
89 imagesURL = props.getProperty(
"images.location");
107 static String loadAppletProps(InputStream f, Applet applet) {
109 return PROPS_NAME+
" not found in ij.jar";
114 catch (IOException e) {
return(
"Error loading "+PROPS_NAME);}
116 URL url =
new URL(applet.getDocumentBase(),
"images/");
117 imagesURL = url.toString();
119 catch (Exception e) {}
135 return props.getProperty(key);
139 public static String
getString(String key, String defaultString) {
141 return defaultString;
142 String s = props.getProperty(key);
144 return defaultString;
150 public static boolean getBoolean(String key,
boolean defaultValue) {
151 if (props==null)
return defaultValue;
152 String s = props.getProperty(key);
156 return s.equals(
"true");
160 public static int getInt(String key,
int defaultValue) {
163 String s = props.getProperty(key);
166 return Integer.decode(s).intValue();
167 }
catch (NumberFormatException e) {
IJ.
write(
""+e);}
173 public static double getDouble(String key,
double defaultValue) {
176 String s = props.getProperty(key);
179 try {d =
new Double(s);}
180 catch (NumberFormatException e){d = null;}
182 return(d.doubleValue());
188 public static Color
getColor(String key, Color defaultColor) {
189 int i =
getInt(key, 0xaaa);
192 return new Color((i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF);
201 static void loadPreferences() {
202 String path = prefsDir+
separator+PREFS_NAME;
203 boolean ok = loadPrefs(path);
205 path = System.getProperty(
"user.home")+
separator+PREFS_NAME;
206 ok = loadPrefs(path);
208 new File(path).delete();
213 static boolean loadPrefs(String path) {
215 InputStream is =
new BufferedInputStream(
new FileInputStream(path));
219 }
catch (Exception e) {
225 static void savePreferences() {
228 static void loadOptions() {
229 int options =
getInt(OPTIONS, ANTIALIASING);
238 ColorProcessor.setWeightingFactors(1d/3d, 1d/3d, 1d/3d);
242 static void saveOptions(Properties prefs) {
247 prefs.put(OPTIONS, Integer.toString(options));
253 public static void set(String key, String text) {
254 if (key.indexOf(
'.')<1)
255 throw new IllegalArgumentException(
"Key must have a prefix");
256 ijPrefs.put(KEY_PREFIX+key, text);
262 public static void set(String key,
double value) {
269 public static void set(String key,
boolean value) {
276 public static String
get(String key, String defaultValue) {
277 String value = ijPrefs.getProperty(KEY_PREFIX+key);
287 public static double get(String key,
double defaultValue) {
288 String s = ijPrefs.getProperty(KEY_PREFIX+key);
291 try {d =
new Double(s);}
292 catch (NumberFormatException e) {d = null;}
294 return(d.doubleValue());
302 public static boolean get(String key,
boolean defaultValue) {
303 String value = ijPrefs.getProperty(KEY_PREFIX+key);
307 return value.equals(
"true");
311 static void savePluginPrefs(Properties prefs) {
312 Enumeration e = ijPrefs.keys();
313 while (e.hasMoreElements()) {
314 String key = (String) e.nextElement();
315 if (key.indexOf(KEY_PREFIX) == 0)
316 prefs.put(key, ijPrefs.getProperty(key));
320 public static void savePrefs(Properties prefs, String path)
throws IOException{
321 FileOutputStream fos =
new FileOutputStream(path);
322 BufferedOutputStream bos =
new BufferedOutputStream(fos);
323 PrintWriter pw =
new PrintWriter(bos);
324 pw.println(
"# ImageJ "+ImageJ.VERSION+
" Preferences");
325 pw.println(
"# "+
new Date());
327 for (Enumeration e=prefs.keys(); e.hasMoreElements();) {
328 String key = (String)e.nextElement();
331 pw.println((String)prefs.get(key));
336 static String escapeBackSlashes (String s) {
337 StringBuffer sb =
new StringBuffer(s.length()+10);
338 char[] chars = s.toCharArray();
339 for (
int i=0; i<chars.length; i++) {
344 return sb.toString();