1 import java.io.File; 2 import java.io.FileReader; 3 import java.io.FileInputStream; 4 import java.io.FileOutputStream; 5 import java.util.Properties; 6 7 /** Load from and save options into a file. 8 */ 9 class Options 10 extends Properties 11 { 12 static public Options Instance () 13 { 14 if (saOptions == null) 15 saOptions = new Options (); 16 return saOptions; 17 } 18 19 static public void SetString (String sName, String sValue) 20 { 21 Instance().setProperty (sName, sValue); 22 } 23 24 static public String GetString (String sName) 25 { 26 return Instance().getProperty (sName); 27 } 28 29 static public void SetBoolean (String sName, boolean bValue) 30 { 31 Instance().setProperty (sName, Boolean.toString(bValue)); 32 } 33 34 static public boolean GetBoolean (String sName) 35 { 36 return Boolean.getBoolean(Instance().getProperty (sName)); 37 } 38 39 static public void SetInteger (String sName, int nValue) 40 { 41 Instance().setProperty (sName, Integer.toString(nValue)); 42 } 43 44 static public int GetInteger (String sName, int nDefault) 45 { 46 String sValue = Instance().getProperty (sName); 47 if (sValue == null) 48 return nDefault; 49 else 50 return Integer.parseInt (sValue); 51 } 52 53 public void Load (String sBaseName) 54 { 55 try 56 { 57 load (new FileInputStream (ProvideFile(sBaseName))); 58 } 59 catch (java.io.IOException e) 60 { 61 // Ignore a non-existing options file. 62 } 63 } 64 65 public void Save (String sBaseName) 66 { 67 try 68 { 69 store (new FileOutputStream (ProvideFile(sBaseName)), null); 70 } 71 catch (java.io.IOException e) 72 { 73 } 74 } 75 76 private Options () 77 { 78 } 79 80 private File ProvideFile (String sBaseName) 81 { 82 return new File ( 83 System.getProperty ("user.home"), 84 sBaseName); 85 } 86 87 static private Options saOptions = null; 88 } 89