KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > Preferences


1 /*
2  * Preferences.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: Preferences.java,v 1.5 2003/07/03 01:56:40 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.awt.Color JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 public final class Preferences
33 {
34     private Properties JavaDoc properties = new Properties JavaDoc();
35     private ArrayList JavaDoc listeners;
36
37     public static final File getPreferencesFile()
38     {
39         return File.getInstance(Directories.getEditorDirectory(), "prefs");
40     }
41
42     public synchronized void reload()
43     {
44         reloadInternal();
45         firePreferencesChanged();
46     }
47
48     private void reloadInternal()
49     {
50         File file = getPreferencesFile();
51         if (file == null || !file.isFile()) {
52             // No preferences file.
53
properties = new Properties JavaDoc();
54             return;
55         }
56
57         // Load preferences file into a temporary Properties object so we can
58
// see if the user has specified a theme.
59
Properties JavaDoc temp = new Properties JavaDoc();
60         try {
61             InputStream JavaDoc in = file.getInputStream();
62             temp.load(in);
63             in.close();
64         }
65         catch (IOException JavaDoc e) {
66             Log.error(e);
67         }
68         // Convert keys to lower case.
69
temp = canonicalize(temp);
70
71         String JavaDoc themeName = temp.getProperty(Property.THEME.key());
72         if (themeName == null || themeName.length() == 0) {
73             // No theme specified.
74
properties = temp;
75             return;
76         }
77
78         String JavaDoc themePath = temp.getProperty(Property.THEME_PATH.key());
79
80         // User has specified a theme. Load theme into a new Properties object.
81
properties = loadTheme(themeName, themePath);
82
83         // User preferences from temporary Properties object override theme.
84
properties.putAll(temp);
85     }
86
87     // Returns new Properties with keys converted to lower case.
88
private static Properties JavaDoc canonicalize(Properties JavaDoc properties)
89     {
90         Properties JavaDoc newProperties = new Properties JavaDoc();
91         for (Enumeration JavaDoc e = properties.keys(); e.hasMoreElements();) {
92             String JavaDoc key = (String JavaDoc) e.nextElement();
93             newProperties.put(key.toLowerCase(), properties.get(key));
94         }
95         return newProperties;
96     }
97
98     // FIXME This is far from ideal (but it does work).
99
public synchronized void killTheme()
100     {
101         Iterator JavaDoc it = properties.keySet().iterator();
102         while (it.hasNext()) {
103             String JavaDoc key = (String JavaDoc) it.next();
104             if (key.startsWith("color."))
105                 it.remove();
106             else if (key.indexOf(".color.") >= 0)
107                 it.remove();
108             else if (key.startsWith("style."))
109                 it.remove();
110             else if (key.indexOf(".style.") >= 0)
111                 it.remove();
112         }
113     }
114
115     private static Properties JavaDoc loadTheme(String JavaDoc themeName, String JavaDoc themePath)
116     {
117         Properties JavaDoc properties = new Properties JavaDoc();
118         File file = getThemeFile(themeName, themePath);
119         if (file != null && file.isFile()) {
120             try {
121                 InputStream JavaDoc in = file.getInputStream();
122                 properties.load(in);
123                 in.close();
124             }
125             catch (IOException JavaDoc e) {
126                 Log.error(e);
127             }
128         }
129         return canonicalize(properties);
130     }
131
132     private static File getThemeFile(String JavaDoc themeName, String JavaDoc themePath)
133     {
134         if (themeName == null)
135             return null;
136         themeName = stripQuotes(themeName);
137
138         // The string passed in is either the name of a theme ("Anokha") or
139
// the full pathname of the file ("/home/peter/Anokha").
140
if (Utilities.isFilenameAbsolute(themeName))
141             return File.getInstance(themeName);
142
143         // It's not an absolute filename. Check theme path.
144
if (themePath != null) {
145             Path path = new Path(stripQuotes(themePath));
146             String JavaDoc[] array = path.list();
147             if (array != null) {
148                 for (int i = 0; i < array.length; i++) {
149                     File dir = File.getInstance(array[i]);
150                     if (dir != null && dir.isDirectory()) {
151                         File themeFile = File.getInstance(dir, themeName);
152                         if (themeFile != null && themeFile.isFile())
153                             return themeFile;
154                     }
155                 }
156             }
157         }
158
159         // We haven't found it yet. Look in default locations.
160
String JavaDoc classPath = System.getProperty("java.class.path");
161         if (classPath != null) {
162             Path path = new Path(classPath);
163             String JavaDoc[] array = path.list();
164             if (array == null)
165                 return null;
166             final File userDir = File.getInstance(System.getProperty("user.dir"));
167             for (int i = 0; i < array.length; i++) {
168                 String JavaDoc pathComponent = array[i];
169                 if (pathComponent.endsWith("src")) {
170                     // "~/j/src"
171
File srcDir = File.getInstance(pathComponent);
172                     if (srcDir != null && srcDir.isDirectory()) {
173                         File parentDir = srcDir.getParentFile(); // "~/j"
174
if (parentDir != null && parentDir.isDirectory()) {
175                             File themeDir = File.getInstance(parentDir, "themes"); // "~/j/themes"
176
if (themeDir != null && themeDir.isDirectory()) {
177                                 File themeFile = File.getInstance(themeDir, themeName);
178                                 if (themeFile != null && themeFile.isFile())
179                                     return themeFile;
180                             }
181                         }
182                     }
183                 } else {
184                     String JavaDoc suffix = "j.jar";
185                     if (pathComponent.endsWith(suffix)) {
186                         // "/usr/local/share/j/j.jar"
187
String JavaDoc prefix = pathComponent.substring(0, pathComponent.length() - suffix.length());
188                         // "/usr/local/share/j/"
189
File prefixDir;
190                         if (prefix.length() == 0) {
191                             // j.jar is in working directory ("java -jar j.jar").
192
prefixDir = userDir;
193                         } else {
194                             // Prefix might be relative to working directory ("java -jar ../j.jar").
195
prefixDir = File.getInstance(userDir, prefix);
196                         }
197                         if (prefixDir != null && prefixDir.isDirectory()) {
198                             // Look for a "themes" subdirectory under prefix directory.
199
File themeDir = File.getInstance(prefixDir, "themes");
200                             // "/usr/local/share/j/themes"
201
if (themeDir != null && themeDir.isDirectory()) {
202                                 File themeFile = File.getInstance(themeDir, themeName);
203                                 if (themeFile != null && themeFile.isFile())
204                                     return themeFile;
205                             }
206                         }
207                     }
208                 }
209             }
210         }
211         return null;
212     }
213
214     public synchronized void setProperty(Property property, String JavaDoc value)
215     {
216         properties.setProperty(property.key(), value);
217     }
218
219     public synchronized void setProperty(String JavaDoc key, String JavaDoc value)
220     {
221         properties.setProperty(key.toLowerCase(), value);
222     }
223
224     public synchronized void removeProperty(String JavaDoc key)
225     {
226         properties.remove(key.toLowerCase());
227     }
228
229     // Strips quotes if present.
230
public synchronized String JavaDoc getStringProperty(Property property)
231     {
232         String JavaDoc value = getProperty(property.key());
233         if (value != null)
234             return stripQuotes(value);
235         else
236             return (String JavaDoc) property.getDefaultValue(); // May be null.
237
}
238
239     // Strips quotes if present.
240
public synchronized String JavaDoc getStringProperty(String JavaDoc key)
241     {
242         String JavaDoc value = getProperty(key);
243         if (value != null)
244             return stripQuotes(value);
245         else
246             return null;
247     }
248
249     public synchronized boolean getBooleanProperty(Property property)
250     {
251         String JavaDoc value = getProperty(property.key());
252         if (value != null) {
253             value = value.trim();
254             if (value.equals("true") || value.equals("1"))
255                 return true;
256             if (value.equals("false") || value.equals("0"))
257                 return false;
258         }
259         return ((Boolean JavaDoc)property.getDefaultValue()).booleanValue();
260     }
261
262     public synchronized boolean getBooleanProperty(Property property, boolean defaultValue)
263     {
264         return getBooleanProperty(property.key(), defaultValue);
265     }
266
267     public synchronized boolean getBooleanProperty(String JavaDoc key, boolean defaultValue)
268     {
269         String JavaDoc value = getProperty(key);
270         if (value != null) {
271             value = value.trim();
272             if (value.equals("true") || value.equals("1"))
273                 return true;
274             if (value.equals("false") || value.equals("0"))
275                 return false;
276         }
277         return defaultValue;
278     }
279
280     public synchronized int getIntegerProperty(Property property)
281     {
282         String JavaDoc value = getProperty(property.key());
283         if (value != null) {
284             value = value.trim();
285             if (value.length() > 0) {
286                 // Integer.parseInt() doesn't understand a plus sign.
287
if (value.charAt(0) == '+')
288                     value = value.substring(1).trim();
289                 try {
290                     return Integer.parseInt(value);
291                 }
292                 catch (NumberFormatException JavaDoc e) {}
293             }
294         }
295         return ((Integer JavaDoc)property.getDefaultValue()).intValue();
296     }
297
298     public synchronized Color JavaDoc getColorProperty(String JavaDoc key)
299     {
300         String JavaDoc value = getStringProperty(key);
301         if (value != null)
302             return Utilities.getColor(value);
303         return null;
304     }
305
306     private String JavaDoc getProperty(String JavaDoc key)
307     {
308         return properties.getProperty(key.toLowerCase());
309     }
310
311     private static String JavaDoc stripQuotes(String JavaDoc s)
312     {
313         final int length = s.length();
314         if (length >= 2) {
315             if (s.charAt(0) == '"' && s.charAt(length-1) == '"')
316                 return s.substring(1, length-1);
317             else if (s.charAt(0) == '\'' && s.charAt(length-1) == '\'')
318                 return s.substring(1, length-1);
319         }
320         // Not quoted.
321
return s.trim();
322     }
323
324     public synchronized void addPreferencesChangeListener(PreferencesChangeListener listener)
325     {
326         if (listeners == null)
327             listeners = new ArrayList JavaDoc();
328         listeners.add(listener);
329     }
330
331     public synchronized void firePreferencesChanged()
332     {
333         if (listeners != null)
334             for (int i = 0; i < listeners.size(); i++)
335                 ((PreferencesChangeListener)listeners.get(i)).preferencesChanged();
336     }
337 }
338
Popular Tags