00001
00002
00003
00004
00005
00006
00007 package Preferences;
00008 import java.io.*;
00009
00014 public class PreferencesInterface {
00015 private static PreferencesInterface instance = new PreferencesInterface();
00016 File preferencesFile = new File(System.getProperty("user.home"),"SctGUI.prefs");
00017 private String[] keys = {"PREF_DEFAULT_VIEW%","PREF_INSTITUTE%","PREF_ZDIR%","PREF_UPLOADDIR%","PREF_PSVIEWER%","PREF_SCRATCHDIR%","PREF_DCS_SERVER%","PREF_AUTO_TEST_SELECT%","PREF_IVDIRECTORY%","PREF_LOOKUPTABLE_DIR%","PREF_ENABLE_BOOKKEEPING%","PREF_LOG_RUN%","PREF_LOG_TEST%","PREF_ASSEMBLY_UPLOAD%"};
00018 private String[] titles = {"Default SCT View","MacroAssemly Site Location","SCT Database Password","Database Upload Directory","Postscript file Viewer","Scratch Disk","DCS IS Server Name","Automatically select new test","IV Scan Data Directory","Book-keeping Directory","Enable Bookkeeping","Prompt for comments on new run","Prompt for comments on new test","Enable assembly Uploads"};
00019 private String[] values;
00020 public static final int SCT_VIEW=0;
00021 public static final int INSTITUTE=1;
00022 public static final int PASSWORD=2;
00023 public static final int UPLOAD_DIR=3;
00024 public static final int PSVIEWER=4;
00025 public static final int SCRATCH_DIR=5;
00026 public static final int DCS_SERVER_NAME=6;
00027 public static final int AUTO_TEST_SELECT=7;
00028 public static final int IV_DIRECTORY=8;
00029 public static final int WEB_LOOKUP_DIR=9;
00030 public static final int BOOKKEEPING=10;
00031 public static final int RUN_PROMPT=11;
00032 public static final int TEST_PROMPT=12;
00033 public static final int ASSEMBLY_UPLOADS=13;
00034
00035 public static final String unDefinedString = "Not defined";
00036
00038 private PreferencesInterface() {
00039 Refresh();
00040 }
00041
00042 public static PreferencesInterface getInstance() {
00043 return instance;
00044 }
00045
00046 public void Refresh() {
00047 values = new String[keys.length];
00048 for(int i=0;i<values.length;i++) values[i]=new String(unDefinedString);
00049 readpreferencesFile();
00050 if(values[AUTO_TEST_SELECT].equals(unDefinedString)) setPreference(AUTO_TEST_SELECT,"TRUE");
00051 if(values[BOOKKEEPING].equals(unDefinedString)) setPreference(BOOKKEEPING,"TRUE");
00052 if(values[RUN_PROMPT].equals(unDefinedString)) setPreference(RUN_PROMPT,"TRUE");
00053 if(values[TEST_PROMPT].equals(unDefinedString)) setPreference(TEST_PROMPT,"FALSE");
00054 if(values[SCRATCH_DIR].equals(unDefinedString)) setPreference(SCRATCH_DIR,System.getProperty("user.home"));
00055 if(values[UPLOAD_DIR].equals(unDefinedString)) setPreference(UPLOAD_DIR,System.getProperty("user.home"));
00056 if(values[DCS_SERVER_NAME].equals(unDefinedString)) setPreference(DCS_SERVER_NAME,"IS_Server");
00057 if(values[ASSEMBLY_UPLOADS].equals(unDefinedString)) setPreference(ASSEMBLY_UPLOADS,"FALSE");
00058 String scratchDir;
00059 if((scratchDir = System.getProperty("Sct.Scratch.Dir", null))!=null) setPreference(SCRATCH_DIR,scratchDir);
00060 }
00061 public String getPreference(int parameter) {
00062 return values[parameter];
00063 }
00064 public String getDescription(int parameter) {
00065 return titles[parameter];
00066 }
00067 public void setPreference(int parameter, String value) {
00068 values[parameter]=value;
00069 writepreferencesFile();
00070 }
00071 public int getNoPreferences() {
00072 return keys.length;
00073 }
00074
00075 public File getPrefsFile() {
00076 return preferencesFile;
00077 }
00078
00079 public void readpreferencesFile() {
00080 String line;
00081
00082 try {
00083 if(!preferencesFile.exists()) return;
00084 BufferedReader in = new BufferedReader(new FileReader(preferencesFile));
00085 while((line=in.readLine())!=null) {
00086 for(int i=0;i<keys.length;i++) {
00087 if(line.indexOf(keys[i])!=-1) {
00088 values[i] = line.substring(keys[i].length());
00089 break;
00090 }
00091 }
00092 }
00093 in.close();
00094 } catch(Exception e) {System.out.println("Failed to read Preferences file "+preferencesFile.toString()+" properly. It appears to be corrupted!.");};
00095 }
00096
00097 public void writepreferencesFile() {
00098
00099
00100 java.util.List nonPrefList = new java.util.ArrayList();
00101 File tempFile = new File(System.getProperty("user.home"),"temp.dat");
00102 try {
00103
00104 if(preferencesFile.exists()) {
00105 BufferedReader in = new BufferedReader(new FileReader(preferencesFile));
00106 String line;
00107 while((line=in.readLine())!=null) {
00108 if(!line.startsWith("PREF_")) nonPrefList.add(line);
00109 }
00110 in.close();
00111 }
00112
00113 BufferedWriter out = new BufferedWriter(new FileWriter(tempFile));
00114 String theNewLine;
00115 for(int i=0;i<keys.length;i++) {
00116 out.write(keys[i]+values[i]);
00117 out.newLine();
00118 }
00119 for(int i=0;i<nonPrefList.size();i++) {
00120 out.write((String)nonPrefList.get(i));
00121 out.newLine();
00122 }
00123 out.close();
00124 boolean deleted = true;
00125 if(preferencesFile.exists()) deleted = preferencesFile.delete();
00126 if(deleted) {
00127 tempFile.renameTo(preferencesFile);
00128 if(preferencesFile.exists()) tempFile.delete();
00129 else System.out.println("Preferences file lost!! Backup is at "+tempFile.toString());
00130 }
00131 } catch (Exception e2) { System.out.println("Cant update preferences file");};
00132 }
00133
00134
00135 }