Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

PreferencesInterface.java

00001 /*
00002  * PreferencesInterface.java
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%"};
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"};
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 
00034     public static final String unDefinedString = "Not defined";
00035     
00037     private PreferencesInterface() {
00038         Refresh();
00039     }
00040     
00041     public static PreferencesInterface getInstance() {
00042         return instance;
00043     }
00044 
00045     public void Refresh() {
00046        values = new String[keys.length];
00047        for(int i=0;i<values.length;i++) values[i]=new String(unDefinedString);
00048        readpreferencesFile();
00049        if(values[AUTO_TEST_SELECT].equals(unDefinedString)) setPreference(AUTO_TEST_SELECT,"TRUE"); 
00050        if(values[BOOKKEEPING].equals(unDefinedString)) setPreference(BOOKKEEPING,"TRUE"); 
00051        if(values[RUN_PROMPT].equals(unDefinedString)) setPreference(RUN_PROMPT,"TRUE");
00052        if(values[TEST_PROMPT].equals(unDefinedString)) setPreference(TEST_PROMPT,"FALSE");
00053        if(values[SCRATCH_DIR].equals(unDefinedString)) setPreference(SCRATCH_DIR,System.getProperty("user.home")); 
00054        if(values[UPLOAD_DIR].equals(unDefinedString)) setPreference(UPLOAD_DIR,System.getProperty("user.home")); 
00055        if(values[DCS_SERVER_NAME].equals(unDefinedString)) setPreference(DCS_SERVER_NAME,"IS_Server");
00056        String scratchDir;
00057        if((scratchDir = System.getProperty("Sct.Scratch.Dir", null))!=null) setPreference(SCRATCH_DIR,scratchDir);      
00058     }
00059     public String getPreference(int parameter) {
00060        return values[parameter];
00061        }
00062     public String getDescription(int parameter) {
00063         return titles[parameter];
00064         }
00065     public void setPreference(int parameter, String value) {
00066        values[parameter]=value;
00067        writepreferencesFile();
00068        }
00069    public int getNoPreferences() {
00070        return keys.length;
00071        }
00072 
00073    public File getPrefsFile() {
00074        return preferencesFile;
00075        }
00076 
00077     public void readpreferencesFile() {
00078        String line;
00079        
00080        try {
00081          if(!preferencesFile.exists()) return;
00082          BufferedReader in = new BufferedReader(new FileReader(preferencesFile));
00083          while((line=in.readLine())!=null) {
00084               for(int i=0;i<keys.length;i++) {
00085                    if(line.indexOf(keys[i])!=-1) {
00086                           values[i] = line.substring(keys[i].length());
00087                           break;
00088                           }
00089                    }
00090          }
00091          in.close();
00092         } catch(Exception e) {System.out.println("Failed to read Preferences file "+preferencesFile.toString()+" properly. It appears to be corrupted!.");};
00093      }
00094 
00095     public void writepreferencesFile() {
00096 // if any config data has changed, save it to file.
00097 // first save any nonPref data
00098            java.util.List nonPrefList = new java.util.ArrayList();
00099            File tempFile = new File(System.getProperty("user.home"),"temp.dat");
00100            try {
00101 //************************
00102               if(preferencesFile.exists()) {
00103                 BufferedReader in = new BufferedReader(new FileReader(preferencesFile));
00104                 String line;
00105                 while((line=in.readLine())!=null) {
00106                    if(!line.startsWith("PREF_")) nonPrefList.add(line);
00107                    }
00108                 in.close();
00109               }
00110 //*********************
00111               BufferedWriter out = new BufferedWriter(new FileWriter(tempFile));
00112               String theNewLine;
00113               for(int i=0;i<keys.length;i++) {
00114                out.write(keys[i]+values[i]);
00115                out.newLine();
00116                }
00117               for(int i=0;i<nonPrefList.size();i++) {
00118                  out.write((String)nonPrefList.get(i));
00119                  out.newLine();
00120                  }
00121               out.close();
00122               boolean deleted = true;
00123               if(preferencesFile.exists()) deleted = preferencesFile.delete();
00124               if(deleted) {
00125                     tempFile.renameTo(preferencesFile);
00126                     if(preferencesFile.exists()) tempFile.delete();
00127                     else System.out.println("Preferences file lost!! Backup is at "+tempFile.toString());
00128                     }
00129            } catch (Exception e2) { System.out.println("Cant update preferences file");}; 
00130       }
00131 
00132 
00133 }

Generated on Sat Jul 31 14:20:58 2004 for SCT DAQ/DCS Software - Java by doxygen 1.3.5