1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package org.openoffice.accessibility.misc;
23 
24 import java.io.File;
25 import java.io.FileReader;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import java.util.Properties;
29 
30 
31 /** Load from and save options into a file.
32 */
33 public class Options
34     extends Properties
35 {
Instance()36     static public Options Instance ()
37     {
38         if (saOptions == null)
39             saOptions = new Options ();
40         return saOptions;
41     }
42 
SetString(String sName, String sValue)43     static public void SetString (String sName, String sValue)
44     {
45         Instance().setProperty (sName, sValue);
46         Instance().Save ();
47     }
48 
GetString(String sName)49     static public String GetString (String sName)
50     {
51         return Instance().getProperty (sName);
52     }
53 
SetBoolean(String sName, boolean bValue)54     static public void SetBoolean (String sName, boolean bValue)
55     {
56         Instance().setProperty (sName, Boolean.toString(bValue));
57         Instance().Save ();
58     }
59 
GetBoolean(String sName)60     static public boolean GetBoolean (String sName)
61     {
62         return Boolean.valueOf(Instance().getProperty (sName)).booleanValue();
63     }
64 
SetInteger(String sName, int nValue)65     static public void SetInteger (String sName, int nValue)
66     {
67         Instance().setProperty (sName, Integer.toString(nValue));
68         Instance().Save ();
69     }
70 
GetInteger(String sName, int nDefault)71     static public int GetInteger (String sName, int nDefault)
72     {
73         String sValue = Instance().getProperty (sName);
74         if (sValue == null)
75             return nDefault;
76         else
77             return Integer.parseInt (sValue);
78     }
79 
Load(String sBaseName)80     public void Load (String sBaseName)
81     {
82         try
83         {
84             load (new FileInputStream (ProvideFile(sBaseName)));
85         }
86         catch (java.io.IOException e)
87         {
88             // Ignore a non-existing options file.
89         }
90     }
91 
Save(String sBaseName)92     public void Save (String sBaseName)
93     {
94         ProvideFile(sBaseName);
95         Save ();
96     }
97 
Save()98     public void Save ()
99     {
100         if (maFile != null)
101         {
102             try
103             {
104                 store (new FileOutputStream (maFile), null);
105             }
106             catch (java.io.IOException e)
107             {
108             }
109         }
110     }
111 
Options()112     private Options ()
113     {
114         maFile = null;
115     }
116 
ProvideFile(String sBaseName)117     private File ProvideFile (String sBaseName)
118     {
119         maFile = new File (
120             System.getProperty ("user.home"),
121             sBaseName);
122         return maFile;
123     }
124 
125     static private Options saOptions = null;
126     private File maFile;
127 }
128