Source code: jpl2/common/Preference.java
1 /***********************************************************************
2 * JavaPsionLink 2.0, a java implementation of the psion link protocol
3 * Copyright (C) 2001-2003 John S Montgomery (john.montgomery@lineone.net)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation; either version 2.1 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ************************************************************************/
19 package jpl2.common;
20
21 import java.io.*;
22 import java.util.Properties;
23 import java.util.Enumeration;
24 import java.util.Vector;
25
26
27 /** A simple preference class, that stores preferences in a file in "user.home".
28 * This file will only be written if a preference is actually set and will only contain actually
29 * set preferences. See java.util.Properties for a description of the file format.
30 **/
31
32 public class Preference {
33 private Properties prefs = new Properties();
34 private File prefsFile = null;
35 private String description = "";
36 private Vector listeners = new Vector();
37
38 /** Make a preference object, that stores prefs in a prefsName, with
39 * a description stored in the file.
40 **/
41 public Preference( String prefsName, String description ) {
42 this.description = description;
43 try {
44 String userHome = System.getProperty( "user.home" );
45 //System.out.println( "user.home = " + userHome );
46 prefsFile = new File( userHome, prefsName );
47 if ( prefsFile.exists() && prefsFile.canRead() ) {
48 // if it exists read in the previously stored properties
49 InputStream in = new BufferedInputStream( new FileInputStream( prefsFile ) );
50 prefs.load( in );
51 in.close();
52 }
53 }
54 catch( Exception e ) {
55 e.printStackTrace();
56 }
57 }
58
59 /** Attempt to save the prefs to file.
60 **/
61 private void savePrefs() {
62 try {
63 if ( prefsFile.canWrite() || !prefsFile.exists() ) {
64 OutputStream out = new BufferedOutputStream( new FileOutputStream( prefsFile ) );
65 prefs.save( out, description );
66 out.flush();
67 out.close();
68 }
69 }
70 catch( Exception e ) {
71 e.printStackTrace();
72 }
73 }
74
75 /** Get a preference, or return defaultValue if it was not specified.
76 **/
77 public synchronized String getPreference( String key, String defaultValue ) {
78 return prefs.getProperty( key, defaultValue );
79 }
80
81 /** Set a preference and attempt to write it to file too.
82 **/
83 public synchronized void setPreference( String key, String value ) {
84 //System.out.println( "setPreference( " + key + ", " + value + " )" );
85
86 Object prev = prefs.put( key, value );
87 if ( prev == null || !prev.equals( value ) ) {
88 //System.out.println( "saving" );
89 savePrefs();
90 preferenceChanged( key, value );
91 }
92 }
93
94 /** Set a preference as an integer.
95 **/
96 public synchronized void setPreference( String key, int value ) {
97 setPreference( key, Integer.toString( value ) );
98 }
99
100 /** Set a preference as a long.
101 **/
102 public synchronized void setPreference( String key, long value ) {
103 setPreference( key, Long.toString( value ) );
104 }
105
106 /** Set a preference as a boolean.
107 **/
108 public synchronized void setPreference( String key, boolean value ) {
109 setPreference( key, String.valueOf( value ) );
110 }
111
112 /** Get a preference as an integer.
113 **/
114 public int getPreference( String key, int defaultValue ) {
115 String num = getPreference( key, null );
116 if ( num == null ) return defaultValue;
117 try {
118 return Integer.parseInt( num );
119 }
120 catch( Exception e ) {
121 }
122 return defaultValue;
123 }
124
125 /** Get a preference as a long.
126 **/
127 public long getPreference( String key, long defaultValue ) {
128 String num = getPreference( key, null );
129 if ( num == null ) return defaultValue;
130 try {
131 return Long.parseLong( num );
132 }
133 catch( Exception e ) {
134 }
135 return defaultValue;
136 }
137
138 /** Get a preference as boolean.
139 **/
140 public boolean getPreference( String key, boolean defaultValue ) {
141 String bool = getPreference( key, null );
142 if ( bool == null )
143 return defaultValue;
144 return Boolean.valueOf( bool ).booleanValue();
145 }
146
147 public void deletePreference( String key ) {
148 prefs.remove( key );
149 savePrefs();
150 // TODO notify listeners of deleted pref
151 }
152
153 public Enumeration getPreferenceKeys() {
154 return prefs.keys();
155 }
156
157
158 /** Tell all of the Preference.Listener objects that a preference has
159 * changed value.
160 **/
161 protected void preferenceChanged( String pref, String value ) {
162 for ( int i = 0; i < listeners.size(); i++ )
163 ((Listener)listeners.elementAt( i )).preferenceChanged( pref, value );
164 }
165
166 /** Add a preference listener.
167 **/
168 public void addPreferenceListener( Listener l ) {
169 listeners.addElement( l );
170 }
171
172 /** Remove a preference listener.
173 **/
174 public void removePreferenceListener( Listener l ) {
175 listeners.removeElement( l );
176 }
177
178 /** Listener interface. Classes shoudl implement this interface so they can be notfied
179 * when preferences change.
180 **/
181 public static interface Listener {
182
183 /** This method will be called on each registered Preference.Listener objects
184 * when a preference has been changed.
185 **/
186 public void preferenceChanged( String pref, String value );
187
188 }
189
190 /*public static void main( String[] args ) {
191 System.out.println( "running" );
192 Preference p = new Preference( ".testpref", "test preference" );
193 String value = p.getPreference( "key", "default" );
194 System.out.println( value );
195 p.setPreference( "saved", "test" );
196 }*/
197
198 }