Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
Prefs.java
1 package ij;
2 import java.io.*;
3 import java.util.*;
4 import java.applet.*;
5 import java.net.URL;
6 import java.awt.Color;
7 import java.applet.Applet;
8 import ij.io.*;
9 import ij.util.Tools;
10 import ij.gui.*;
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;
16 
22 public class Prefs {
23 
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 = ".";
35 
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";
39 
41  public static String separator = System.getProperty("file.separator");
43  public static boolean usePointerCursor;
45  public static boolean antialiasedText;
47  public static boolean interpolateScaledImages;
49  public static boolean open100Percent;
51  public static boolean blackBackground;
53  public static boolean unweightedColor;
55  public static boolean blackCanvas;
56 
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; // ImageJ folder
62 
66  public static String load(Object ij, Applet applet) {
67  InputStream f = ij.getClass().getResourceAsStream("/"+PROPS_NAME);
68  if (applet!=null)
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)
74  prefsDir = homeDir; //ImageJ folder on Windows
75  else {
76  prefsDir = userHome; // Mac Preferences folder or Unix home dir
77  if (IJ.isMacOSX())
78  prefsDir += "/Library/Preferences";
79  }
80  if (f==null) {
81  try {f = new FileInputStream(homeDir+"/"+PROPS_NAME);}
82  catch (FileNotFoundException e) {f=null;}
83  }
84  if (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");
90  loadPreferences();
91  loadOptions();
92  return null;
93  }
94 
95  /*
96  static void dumpPrefs(String title) {
97  IJ.log("");
98  IJ.log(title);
99  Enumeration e = ijPrefs.keys();
100  while (e.hasMoreElements()) {
101  String key = (String) e.nextElement();
102  IJ.log(key+": "+ijPrefs.getProperty(key));
103  }
104  }
105  */
106 
107  static String loadAppletProps(InputStream f, Applet applet) {
108  if (f==null)
109  return PROPS_NAME+" not found in ij.jar";
110  try {
111  props.load(f);
112  f.close();
113  }
114  catch (IOException e) {return("Error loading "+PROPS_NAME);}
115  try {
116  URL url = new URL(applet.getDocumentBase(), "images/");
117  imagesURL = url.toString();
118  }
119  catch (Exception e) {}
120  return null;
121  }
122 
124  public static String getImagesURL() {
125  return imagesURL;
126  }
127 
129  public static String getHomeDir() {
130  return homeDir;
131  }
132 
134  public static String getString(String key) {
135  return props.getProperty(key);
136  }
137 
139  public static String getString(String key, String defaultString) {
140  if (props==null)
141  return defaultString;
142  String s = props.getProperty(key);
143  if (s==null)
144  return defaultString;
145  else
146  return s;
147  }
148 
150  public static boolean getBoolean(String key, boolean defaultValue) {
151  if (props==null) return defaultValue;
152  String s = props.getProperty(key);
153  if (s==null)
154  return defaultValue;
155  else
156  return s.equals("true");
157  }
158 
160  public static int getInt(String key, int defaultValue) {
161  if (props==null) //workaround for Netscape JIT bug
162  return defaultValue;
163  String s = props.getProperty(key);
164  if (s!=null) {
165  try {
166  return Integer.decode(s).intValue();
167  } catch (NumberFormatException e) {IJ.write(""+e);}
168  }
169  return defaultValue;
170  }
171 
173  public static double getDouble(String key, double defaultValue) {
174  if (props==null)
175  return defaultValue;
176  String s = props.getProperty(key);
177  Double d = null;
178  if (s!=null) {
179  try {d = new Double(s);}
180  catch (NumberFormatException e){d = null;}
181  if (d!=null)
182  return(d.doubleValue());
183  }
184  return defaultValue;
185  }
186 
188  public static Color getColor(String key, Color defaultColor) {
189  int i = getInt(key, 0xaaa);
190  if (i == 0xaaa)
191  return defaultColor;
192  return new Color((i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF);
193  }
194 
196  public static String getFileSeparator() {
197  return separator;
198  }
199 
201  static void loadPreferences() {
202  String path = prefsDir+separator+PREFS_NAME;
203  boolean ok = loadPrefs(path);
204  if (!ok && IJ.isMacOSX()) {
205  path = System.getProperty("user.home")+separator+PREFS_NAME;
206  ok = loadPrefs(path); // look in home dir
207  if (ok)
208  new File(path).delete();
209  }
210 
211  }
212 
213  static boolean loadPrefs(String path) {
214  try {
215  InputStream is = new BufferedInputStream(new FileInputStream(path));
216  ijPrefs.load(is);
217  is.close();
218  return true;
219  } catch (Exception e) {
220  return false;
221  }
222  }
223 
225  static void savePreferences() {
226  }
227 
228  static void loadOptions() {
229  int options = getInt(OPTIONS, ANTIALIASING);
230  usePointerCursor = (options&USE_POINTER)!=0;
231  antialiasedText = (options&ANTIALIASING)!=0;
232  interpolateScaledImages = (options&INTERPOLATE)!=0;
233  open100Percent = (options&ONE_HUNDRED_PERCENT)!=0;
234  open100Percent = (options&ONE_HUNDRED_PERCENT)!=0;
235  blackBackground = (options&BLACK_BACKGROUND)!=0;
236  unweightedColor = (options&UNWEIGHTED)!=0;
237  if (unweightedColor)
238  ColorProcessor.setWeightingFactors(1d/3d, 1d/3d, 1d/3d);
239  blackCanvas = (options&BLACK_CANVAS)!=0;
240  }
241 
242  static void saveOptions(Properties prefs) {
243  int options = (usePointerCursor?USE_POINTER:0) + (antialiasedText?ANTIALIASING:0)
244  + (interpolateScaledImages?INTERPOLATE:0) + (open100Percent?ONE_HUNDRED_PERCENT:0)
245  + (blackBackground?BLACK_BACKGROUND:0)
246  + (unweightedColor?UNWEIGHTED:0) + (blackCanvas?BLACK_CANVAS:0);
247  prefs.put(OPTIONS, Integer.toString(options));
248  }
249 
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);
257  }
258 
262  public static void set(String key, double value) {
263  set(key, ""+value);
264  }
265 
269  public static void set(String key, boolean value) {
270  set (key, ""+value);
271  }
272 
276  public static String get(String key, String defaultValue) {
277  String value = ijPrefs.getProperty(KEY_PREFIX+key);
278  if (value == null)
279  return defaultValue;
280  else
281  return value;
282  }
283 
287  public static double get(String key, double defaultValue) {
288  String s = ijPrefs.getProperty(KEY_PREFIX+key);
289  Double d = null;
290  if (s!=null) {
291  try {d = new Double(s);}
292  catch (NumberFormatException e) {d = null;}
293  if (d!=null)
294  return(d.doubleValue());
295  }
296  return defaultValue;
297  }
298 
302  public static boolean get(String key, boolean defaultValue) {
303  String value = ijPrefs.getProperty(KEY_PREFIX+key);
304  if (value==null)
305  return defaultValue;
306  else
307  return value.equals("true");
308  }
309 
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));
317  }
318  }
319 
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());
326  pw.println("");
327  for (Enumeration e=prefs.keys(); e.hasMoreElements();) {
328  String key = (String)e.nextElement();
329  pw.print(key);
330  pw.write('=');
331  pw.println((String)prefs.get(key));
332  }
333  pw.close();
334  }
335 
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++) {
340  sb.append(chars[i]);
341  if (chars[i]=='\\')
342  sb.append('\\');
343  }
344  return sb.toString();
345  }
346 
347 }
348