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

Generated on Thu Jul 15 09:55:47 2004 for SCT DAQ/DCS Software - Java by doxygen 1.3.5