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