package ij;
import ij.util.Java2;
import java.io.*;
import java.util.*;
import java.applet.*;
import java.net.URL;
import java.awt.*;
import java.applet.Applet;
import ij.io.*;
import ij.util.Tools;
import ij.gui.*;
import ij.plugin.filter.*;
import ij.process.ImageConverter;
import ij.plugin.Animator;
import ij.process.FloatBlitter;
import ij.plugin.GelAnalyzer;
import ij.process.ColorProcessor;
import ij.text.TextWindow;
public class Prefs {
public static final String PROPS_NAME = "IJ_Props.txt";
public static final String PREFS_NAME = "IJ_Prefs.txt";
public static final String DIR_IMAGE = "dir.image";
public static final String FCOLOR = "fcolor";
public static final String BCOLOR = "bcolor";
public static final String ROICOLOR = "roicolor";
public static final String SHOW_ALL_COLOR = "showcolor";
public static final String JPEG = "jpeg";
public static final String FPS = "fps";
public static final String DIV_BY_ZERO_VALUE = "div-by-zero";
public static final String NOISE_SD = "noise.sd";
public static final String MENU_SIZE = "menu.size";
public static final String THREADS = "threads";
public static final String KEY_PREFIX = ".";
private static final int USE_POINTER=1<<0, ANTIALIASING=1<<1, INTERPOLATE=1<<2, ONE_HUNDRED_PERCENT=1<<3,
BLACK_BACKGROUND=1<<4, JFILE_CHOOSER=1<<5, UNUSED=1<<6, BLACK_CANVAS=1<<7, WEIGHTED=1<<8,
AUTO_MEASURE=1<<9, REQUIRE_CONTROL=1<<10, USE_INVERTING_LUT=1<<11, ANTIALIASED_TOOLS=1<<12,
INTEL_BYTE_ORDER=1<<13, DOUBLE_BUFFER=1<<14, NO_POINT_LABELS=1<<15, NO_BORDER=1<<16,
SHOW_ALL_SLICE_ONLY=1<<17, COPY_HEADERS=1<<18, NO_ROW_NUMBERS=1<<19,
MOVE_TO_MISC=1<<20, ADD_TO_MANAGER=1<<21, RUN_SOCKET_LISTENER=1<<22,
MULTI_POINT_MODE=1<<23, ROTATE_YZ=1<<24, FLIP_XZ=1<<25,
DONT_SAVE_HEADERS=1<<26, DONT_SAVE_ROW_NUMBERS=1<<27, NO_CLICK_TO_GC=1<<28,
AVOID_RESLICE_INTERPOLATION=1<<29, KEEP_UNDO_BUFFERS=1<<30;
public static final String OPTIONS = "prefs.options";
public static final String vistaHint = "";
private static final int USE_SYSTEM_PROXIES=1<<0, USE_FILE_CHOOSER=1<<1,
SUBPIXEL_RESOLUTION=1<<2, ENHANCED_LINE_TOOL=1<<3, SKIP_RAW_DIALOG=1<<4,
REVERSE_NEXT_PREVIOUS_ORDER=1<<5, AUTO_RUN_EXAMPLES=1<<6, SHOW_ALL_POINTS=1<<7,
DO_NOT_SAVE_WINDOW_LOCS=1<<8, JFILE_CHOOSER_CHANGED=1<<9;
public static final String OPTIONS2 = "prefs.options2";
public static String separator = System.getProperty("file.separator");
public static boolean usePointerCursor;
public static boolean antialiasedText;
public static boolean interpolateScaledImages;
public static boolean open100Percent;
public static boolean blackBackground;
public static boolean useJFileChooser;
public static boolean weightedColor;
public static boolean blackCanvas;
public static boolean pointAutoMeasure;
public static boolean pointAutoNextSlice;
public static boolean requireControlKey;
public static boolean useInvertingLut;
public static boolean antialiasedTools = true;
public static boolean intelByteOrder;
public static boolean doubleBuffer = true;
public static boolean noPointLabels;
public static boolean disableUndo;
public static boolean noBorder;
public static boolean showAllSliceOnly;
public static boolean copyColumnHeaders;
public static boolean noRowNumbers;
public static boolean moveToMisc;
public static boolean pointAddToManager;
public static boolean pointAddToOverlay;
public static boolean padEdges;
public static boolean runSocketListener;
public static boolean multiPointMode;
public static boolean openDicomsAsFloat;
public static boolean verticalProfile;
public static boolean rotateYZ;
public static boolean flipXZ;
public static boolean dontSaveHeaders;
public static boolean dontSaveRowNumbers;
public static boolean noClickToGC;
public static boolean reflexAngle;
public static boolean avoidResliceInterpolation;
public static boolean keepUndoBuffers;
public static boolean useNamesAsLabels;
public static boolean useSystemProxies;
public static boolean useFileChooser;
public static boolean subPixelResolution;
public static boolean autoContrast;
public static boolean enhancedLineTool;
public static boolean keepArrowSelections;
public static boolean paintDoubleBuffered;
public static boolean skipRawDialog;
public static boolean reverseNextPreviousOrder;
public static boolean autoRunExamples = true;
public static boolean showAllPoints;
public static boolean setIJMenuBar = IJ.isMacOSX();
public static boolean alwaysOnTop;
public static boolean splineFitLines;
public static boolean doNotSaveWindowLocations = true;
public static boolean jFileChooserSettingChanged;
public static boolean convertToMicrons = true;
public static boolean smoothWand;
public static boolean closingAll;
static Properties ijPrefs = new Properties();
static Properties props = new Properties(ijPrefs);
static String prefsDir;
static String imagesURL;
static String homeDir; static int threads;
static int transparentIndex = -1;
static boolean commandLineMacro;
private static boolean resetPreferences;
public static String load(Object ij, Applet applet) {
InputStream f = ij.getClass().getResourceAsStream("/"+PROPS_NAME);
if (applet!=null)
return loadAppletProps(f, applet);
if (homeDir==null)
homeDir = System.getProperty("user.dir");
if (f==null) {
try {f = new FileInputStream(homeDir+"/"+PROPS_NAME);}
catch (FileNotFoundException e) {f=null;}
}
if (f==null)
return PROPS_NAME+" not found in ij.jar or in "+homeDir;
f = new BufferedInputStream(f);
try {props.load(f); f.close();}
catch (IOException e) {return("Error loading "+PROPS_NAME);}
imagesURL = props.getProperty("images.location");
loadPreferences();
loadOptions();
return null;
}
static String loadAppletProps(InputStream f, Applet applet) {
if (f==null)
return PROPS_NAME+" not found in ij.jar";
try {
props.load(f);
f.close();
}
catch (IOException e) {return("Error loading "+PROPS_NAME);}
try {
URL url = new URL(applet.getDocumentBase(), "images/");
imagesURL = url.toString();
}
catch (Exception e) {}
return null;
}
public static String getImagesURL() {
return imagesURL;
}
public static void setImagesURL(String url) {
imagesURL = url;
}
public static String getHomeDir() {
return homeDir;
}
public static String getImageJDir() {
String path = Menus.getImageJPath();
if (path==null)
return homeDir + File.separator;
else
return path;
}
public static String getPrefsDir() {
if (prefsDir==null) {
String dir = System.getProperty("user.home");
if (IJ.isMacOSX())
dir += "/Library/Preferences";
else
dir += File.separator+".imagej";
prefsDir = dir;
}
return prefsDir;
}
static void setHomeDir(String path) {
if (path.endsWith(File.separator))
path = path.substring(0, path.length()-1);
homeDir = path;
}
public static String getDefaultDirectory() {
if (commandLineMacro)
return null;
else
return getString(DIR_IMAGE);
}
public static String getString(String key) {
return props.getProperty(key);
}
public static String getString(String key, String defaultString) {
if (props==null)
return defaultString;
String s = props.getProperty(key);
if (s==null)
return defaultString;
else
return s;
}
public static boolean getBoolean(String key, boolean defaultValue) {
if (props==null) return defaultValue;
String s = props.getProperty(key);
if (s==null)
return defaultValue;
else
return s.equals("true");
}
public static int getInt(String key, int defaultValue) {
if (props==null) return defaultValue;
String s = props.getProperty(key);
if (s!=null) {
try {
return Integer.decode(s).intValue();
} catch (NumberFormatException e) {IJ.write(""+e);}
}
return defaultValue;
}
public static double getDouble(String key, double defaultValue) {
if (props==null)
return defaultValue;
String s = props.getProperty(key);
Double d = null;
if (s!=null) {
try {d = new Double(s);}
catch (NumberFormatException e){d = null;}
if (d!=null)
return(d.doubleValue());
}
return defaultValue;
}
public static Color getColor(String key, Color defaultColor) {
int i = getInt(key, 0xaaa);
if (i == 0xaaa)
return defaultColor;
return new Color((i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF);
}
public static String getFileSeparator() {
return separator;
}
static void loadPreferences() {
String path = getPrefsDir()+separator+PREFS_NAME;
boolean ok = loadPrefs(path);
if (!ok) { if (IJ.isWindows())
path = homeDir +separator+PREFS_NAME; else
path = System.getProperty("user.home")+separator+PREFS_NAME; ok = loadPrefs(path);
if (ok)
new File(path).delete();
}
}
static boolean loadPrefs(String path) {
try {
InputStream is = new BufferedInputStream(new FileInputStream(path));
ijPrefs.load(is);
is.close();
return true;
} catch (Exception e) {
return false;
}
}
public static void savePreferences() {
String path = null;
try {
Properties prefs = new Properties();
String dir = OpenDialog.getDefaultDirectory();
if (dir!=null)
prefs.put(DIR_IMAGE, dir);
prefs.put(ROICOLOR, Tools.c2hex(Roi.getColor()));
prefs.put(SHOW_ALL_COLOR, Tools.c2hex(ImageCanvas.getShowAllColor()));
prefs.put(FCOLOR, Tools.c2hex(Toolbar.getForegroundColor()));
prefs.put(BCOLOR, Tools.c2hex(Toolbar.getBackgroundColor()));
prefs.put(JPEG, Integer.toString(FileSaver.getJpegQuality()));
prefs.put(FPS, Double.toString(Animator.getFrameRate()));
prefs.put(DIV_BY_ZERO_VALUE, Double.toString(FloatBlitter.divideByZeroValue));
prefs.put(NOISE_SD, Double.toString(Filters.getSD()));
if (threads>1) prefs.put(THREADS, Integer.toString(threads));
if (IJ.isMacOSX()) useJFileChooser = false;
saveOptions(prefs);
savePluginPrefs(prefs);
IJ.getInstance().savePreferences(prefs);
Menus.savePreferences(prefs);
ParticleAnalyzer.savePreferences(prefs);
Analyzer.savePreferences(prefs);
ImportDialog.savePreferences(prefs);
PlotWindow.savePreferences(prefs);
NewImage.savePreferences(prefs);
String prefsDir = getPrefsDir();
path = prefsDir+separator+PREFS_NAME;
if (prefsDir.endsWith(".imagej")) {
File f = new File(prefsDir);
if (!f.exists()) f.mkdir(); }
if (resetPreferences) {
File f = new File(path);
if (!f.exists())
IJ.error("Edit>Options>Reset", "Unable to reset preferences. File not found at\n"+path);
boolean rtn = f.delete();
resetPreferences = false;
} else
savePrefs(prefs, path);
} catch (Throwable t) {
String msg = t.getMessage();
if (msg==null) msg = ""+t;
int delay = 4000;
try {
new TextWindow("Error Saving Preferences:\n"+path, msg, 500, 200);
IJ.wait(delay);
} catch (Throwable t2) {}
}
}
public static void resetPreferences() {
resetPreferences = true;
}
static void loadOptions() {
int defaultOptions = ANTIALIASING+AVOID_RESLICE_INTERPOLATION+ANTIALIASED_TOOLS+MULTI_POINT_MODE
+(!IJ.isMacOSX()?RUN_SOCKET_LISTENER:0);
int options = getInt(OPTIONS, defaultOptions);
usePointerCursor = (options&USE_POINTER)!=0;
antialiasedText = false;
interpolateScaledImages = (options&INTERPOLATE)!=0;
open100Percent = (options&ONE_HUNDRED_PERCENT)!=0;
blackBackground = (options&BLACK_BACKGROUND)!=0;
useJFileChooser = (options&JFILE_CHOOSER)!=0;
weightedColor = (options&WEIGHTED)!=0;
if (weightedColor)
ColorProcessor.setWeightingFactors(0.299, 0.587, 0.114);
blackCanvas = (options&BLACK_CANVAS)!=0;
requireControlKey = (options&REQUIRE_CONTROL)!=0;
useInvertingLut = (options&USE_INVERTING_LUT)!=0;
antialiasedTools = (options&ANTIALIASED_TOOLS)!=0;
intelByteOrder = (options&INTEL_BYTE_ORDER)!=0;
noBorder = (options&NO_BORDER)!=0;
showAllSliceOnly = (options&SHOW_ALL_SLICE_ONLY)!=0;
copyColumnHeaders = (options©_HEADERS)!=0;
noRowNumbers = (options&NO_ROW_NUMBERS)!=0;
moveToMisc = (options&MOVE_TO_MISC)!=0;
runSocketListener = (options&RUN_SOCKET_LISTENER)!=0;
multiPointMode = (options&MULTI_POINT_MODE)!=0;
rotateYZ = (options&ROTATE_YZ)!=0;
flipXZ = (options&FLIP_XZ)!=0;
noClickToGC = (options&NO_CLICK_TO_GC)!=0;
avoidResliceInterpolation = (options&AVOID_RESLICE_INTERPOLATION)!=0;
keepUndoBuffers = (options&KEEP_UNDO_BUFFERS)!=0;
defaultOptions = (!IJ.isMacOSX()?USE_FILE_CHOOSER:0);
int options2 = getInt(OPTIONS2, defaultOptions);
useSystemProxies = (options2&USE_SYSTEM_PROXIES)!=0;
useFileChooser = (options2&USE_FILE_CHOOSER)!=0;
subPixelResolution = (options2&SUBPIXEL_RESOLUTION)!=0;
enhancedLineTool = (options2&ENHANCED_LINE_TOOL)!=0;
skipRawDialog = (options2&SKIP_RAW_DIALOG)!=0;
reverseNextPreviousOrder = (options2&REVERSE_NEXT_PREVIOUS_ORDER)!=0;
autoRunExamples = (options2&AUTO_RUN_EXAMPLES)!=0;
showAllPoints = (options2&SHOW_ALL_POINTS)!=0;
doNotSaveWindowLocations = (options2&DO_NOT_SAVE_WINDOW_LOCS)!=0;
jFileChooserSettingChanged = (options2&JFILE_CHOOSER_CHANGED)!=0;
}
static void saveOptions(Properties prefs) {
int options = (usePointerCursor?USE_POINTER:0) + (antialiasedText?ANTIALIASING:0)
+ (interpolateScaledImages?INTERPOLATE:0) + (open100Percent?ONE_HUNDRED_PERCENT:0)
+ (blackBackground?BLACK_BACKGROUND:0) + (useJFileChooser?JFILE_CHOOSER:0)
+ (blackCanvas?BLACK_CANVAS:0) + (weightedColor?WEIGHTED:0)
+ (requireControlKey?REQUIRE_CONTROL:0)
+ (useInvertingLut?USE_INVERTING_LUT:0) + (antialiasedTools?ANTIALIASED_TOOLS:0)
+ (intelByteOrder?INTEL_BYTE_ORDER:0) + (doubleBuffer?DOUBLE_BUFFER:0)
+ (noPointLabels?NO_POINT_LABELS:0) + (noBorder?NO_BORDER:0)
+ (showAllSliceOnly?SHOW_ALL_SLICE_ONLY:0) + (copyColumnHeaders?COPY_HEADERS:0)
+ (noRowNumbers?NO_ROW_NUMBERS:0) + (moveToMisc?MOVE_TO_MISC:0)
+ (runSocketListener?RUN_SOCKET_LISTENER:0)
+ (multiPointMode?MULTI_POINT_MODE:0) + (rotateYZ?ROTATE_YZ:0)
+ (flipXZ?FLIP_XZ:0) + (dontSaveHeaders?DONT_SAVE_HEADERS:0)
+ (dontSaveRowNumbers?DONT_SAVE_ROW_NUMBERS:0) + (noClickToGC?NO_CLICK_TO_GC:0)
+ (avoidResliceInterpolation?AVOID_RESLICE_INTERPOLATION:0)
+ (keepUndoBuffers?KEEP_UNDO_BUFFERS:0);
prefs.put(OPTIONS, Integer.toString(options));
int options2 = (useSystemProxies?USE_SYSTEM_PROXIES:0)
+ (useFileChooser?USE_FILE_CHOOSER:0) + (subPixelResolution?SUBPIXEL_RESOLUTION:0)
+ (enhancedLineTool?ENHANCED_LINE_TOOL:0) + (skipRawDialog?SKIP_RAW_DIALOG:0)
+ (reverseNextPreviousOrder?REVERSE_NEXT_PREVIOUS_ORDER:0)
+ (autoRunExamples?AUTO_RUN_EXAMPLES:0) + (showAllPoints?SHOW_ALL_POINTS:0)
+ (doNotSaveWindowLocations?DO_NOT_SAVE_WINDOW_LOCS:0)
+ (jFileChooserSettingChanged?JFILE_CHOOSER_CHANGED:0);
prefs.put(OPTIONS2, Integer.toString(options2));
}
public static void set(String key, String text) {
if (key.indexOf('.')<1)
throw new IllegalArgumentException("Key must have a prefix");
if (text==null)
ijPrefs.remove(KEY_PREFIX+key);
else
ijPrefs.put(KEY_PREFIX+key, text);
}
public static void set(String key, int value) {
set(key, Integer.toString(value));
}
public static void set(String key, double value) {
set(key, ""+value);
}
public static void set(String key, boolean value) {
set(key, ""+value);
}
public static String get(String key, String defaultValue) {
String value = ijPrefs.getProperty(KEY_PREFIX+key);
if (value == null)
return defaultValue;
else
return value;
}
public static double get(String key, double defaultValue) {
String s = ijPrefs.getProperty(KEY_PREFIX+key);
Double d = null;
if (s!=null) {
try {d = new Double(s);}
catch (NumberFormatException e) {d = null;}
if (d!=null)
return(d.doubleValue());
}
return defaultValue;
}
public static boolean get(String key, boolean defaultValue) {
String value = ijPrefs.getProperty(KEY_PREFIX+key);
if (value==null)
return defaultValue;
else
return value.equals("true");
}
public static void saveLocation(String key, Point loc) {
if (!doNotSaveWindowLocations)
set(key, loc.x+","+loc.y);
}
public static Point getLocation(String key) {
String value = ijPrefs.getProperty(KEY_PREFIX+key);
if (value==null) return null;
int index = value.indexOf(",");
if (index==-1) return null;
double xloc = Tools.parseDouble(value.substring(0, index));
if (Double.isNaN(xloc) || index==value.length()-1) return null;
double yloc = Tools.parseDouble(value.substring(index+1));
if (Double.isNaN(yloc)) return null;
Point p = new Point((int)xloc, (int)yloc);
Dimension screen = null;
if (IJ.debugMode)
screen = Toolkit.getDefaultToolkit().getScreenSize();
else
screen = IJ.getScreenSize();
if (p.x>screen.width-100 || p.y>screen.height-40)
return null;
else
return p;
}
static void savePluginPrefs(Properties prefs) {
Enumeration e = ijPrefs.keys();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
if (key.indexOf(KEY_PREFIX) == 0)
prefs.put(key, ijPrefs.getProperty(key));
}
}
public static void savePrefs(Properties prefs, String path) throws IOException{
FileOutputStream fos = new FileOutputStream(path);
BufferedOutputStream bos = new BufferedOutputStream(fos);
prefs.store(bos, "ImageJ "+ImageJ.VERSION+" Preferences");
bos.close();
}
public static int getThreads() {
if (threads==0) {
threads = getInt(THREADS, 0);
int processors = Runtime.getRuntime().availableProcessors();
if (threads<1 || threads>processors)
threads = processors;
}
return threads;
}
public static void setThreads(int n) {
if (n<1) n = 1;
threads = n;
}
public static void setTransparentIndex(int index) {
if (index<-1 || index>255) index = -1;
transparentIndex = index;
}
public static int getTransparentIndex() {
return transparentIndex;
}
public static Properties getControlPanelProperties() {
return ijPrefs;
}
public static String defaultResultsExtension() {
return get("options.ext", ".csv");
}
}