Mercurial > projects > dwt-addons
annotate dwtx/jface/preference/PreferenceStore.d @ 193:a785420fe9ca
PreferenceStore char escaping for \r.
Thanks yidabu
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 15 Nov 2008 09:20:02 +0100 |
parents | 04b47443bb01 |
children | 0ea0c9f9008f |
rev | line source |
---|---|
34 | 1 /******************************************************************************* |
90 | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
34 | 3 * All rights reserved. This program and the accompanying materials |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.preference.PreferenceStore; | |
14 | |
15 import dwtx.jface.preference.IPersistentPreferenceStore; | |
16 import dwtx.jface.preference.IPreferenceStore; | |
17 | |
18 // import java.io.FileInputStream; | |
19 // import java.io.FileOutputStream; | |
20 // import java.io.IOException; | |
21 // import java.io.InputStream; | |
22 // import java.io.OutputStream; | |
23 // import java.io.PrintStream; | |
24 // import java.io.PrintWriter; | |
25 // import java.util.ArrayList; | |
26 // import java.util.Enumeration; | |
27 // import java.util.Properties; | |
28 | |
29 import dwtx.core.commands.common.EventManager; | |
30 import dwtx.core.runtime.Assert; | |
31 import dwtx.jface.resource.JFaceResources; | |
32 import dwtx.jface.util.IPropertyChangeListener; | |
33 import dwtx.jface.util.PropertyChangeEvent; | |
34 import dwtx.jface.util.SafeRunnable; | |
35 | |
36 import dwt.dwthelper.utils; | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
90
diff
changeset
|
37 // import dwtx.dwtxhelper.Collection; |
34 | 38 import tango.io.Print; |
39 import tango.io.stream.FileStream; | |
40 | |
41 import tango.io.model.IConduit; | |
42 import tango.text.Util; | |
43 public class Properties { | |
44 | |
45 protected Properties defaults; | |
46 | |
47 private String[ String ] map; | |
48 | |
49 public this (){ | |
50 } | |
51 | |
52 public this ( Properties defaults ){ | |
53 this.defaults = defaults; | |
54 } | |
55 | |
56 public synchronized String setProperty( String key, String value ){ | |
57 String res; | |
58 if( auto v = key in map ){ | |
59 res = *v; | |
60 } | |
61 map[ key ] = value; | |
62 return res; | |
63 } | |
64 | |
65 public synchronized void load( InputStream inStream ){ | |
66 char[] line; | |
67 bool eof = false; | |
68 //tango.io.Stdout.Stdout.formatln( "properties put ..." ); | |
69 void readLine(){ | |
70 line.length = 0; | |
71 char[1] rdbuf; | |
72 int i = inStream.read( rdbuf ); | |
73 while( i is 1 && rdbuf[0] != '\n' && rdbuf[0] != '\r' ){ | |
74 line ~= rdbuf[0]; | |
75 i = inStream.read( rdbuf ); | |
76 } | |
77 eof = i !is 1; | |
78 } | |
79 | |
80 //tango.io.Stdout.Stdout.formatln( "properties put {}", __LINE__ ); | |
81 bool linecontinue = false; | |
82 bool iskeypart = true; | |
83 char[] key; | |
84 char[] value; | |
85 nextline: | |
86 while( !eof ){ | |
87 //tango.io.Stdout.Stdout.formatln( "properties put {} startline", __LINE__ ); | |
88 readLine(); | |
89 line = tango.text.Util.trim( line ); | |
90 if( line.length == 0 ){ | |
91 //tango.io.Stdout.Stdout.formatln( "properties put {} was 0 length", __LINE__ ); | |
92 continue; | |
93 } | |
94 if( line[0] == '#' ){ | |
95 //tango.io.Stdout.Stdout.formatln( "properties put {} was comment", __LINE__ ); | |
96 continue; | |
97 } | |
98 int pos = 0; | |
99 bool esc = false; | |
100 if( !linecontinue ){ | |
101 iskeypart = true; | |
102 key = null; | |
103 value = null; | |
104 } | |
105 else{ | |
106 linecontinue = false; | |
107 } | |
108 while( pos < line.length ){ | |
109 char c = line[pos]; | |
110 if( esc ){ | |
111 esc = false; | |
112 switch( c ){ | |
113 case 't': c = '\t'; break; | |
193
a785420fe9ca
PreferenceStore char escaping for \r.
Frank Benoit <benoit@tionex.de>
parents:
104
diff
changeset
|
114 case 'r': c = '\r'; break; |
34 | 115 case 'n': c = '\n'; break; |
116 case '\\': c = '\\'; break; | |
47
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
117 default: break; |
34 | 118 } |
119 } | |
120 else{ | |
121 if( c == '\\' ){ | |
122 if( pos == line.length -1 ){ | |
123 linecontinue = true; | |
124 goto nextline; | |
125 } | |
126 esc = true; | |
127 pos++; | |
128 continue; | |
129 } | |
130 else if( iskeypart && c == '=' ){ | |
131 pos++; | |
132 iskeypart = false; | |
133 continue; | |
134 } | |
135 } | |
136 pos++; | |
137 if( iskeypart ){ | |
138 key ~= c; | |
139 } | |
140 else{ | |
141 value ~= c; | |
142 } | |
143 } | |
144 if( iskeypart ){ | |
145 tango.io.Stdout.Stdout.formatln( "dejavu.util.Properties put cannot find '='." ); | |
146 continue; | |
147 } | |
148 key = tango.text.Util.trim( key ); | |
149 value = tango.text.Util.trim(value); | |
150 //tango.io.Stdout.Stdout.formatln( "properties put {}=>{}", key, value ); | |
151 | |
152 map[ key.dup ] = value.dup; | |
153 //tango.io.Stdout.Stdout.formatln( "properties put {}", __LINE__ ); | |
154 } | |
155 } | |
156 | |
47
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
157 public synchronized void store( OutputStream ostr, String comments ){ |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
158 void append(char[] s){ |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
159 foreach( c; s ){ |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
160 switch( c ){ |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
161 case '\t': ostr.write( "\\t" ); break; |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
162 case '\n': ostr.write( "\\n" ); break; |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
163 case '\r': ostr.write( "\\r" ); break; |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
164 case '\\': ostr.write( "\\\\" ); break; |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
165 case ':' : ostr.write( "\\:" ); break; |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
166 default: { |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
167 char[1] b; |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
168 b[0] = c; |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
169 ostr.write( b ); break; |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
170 } |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
171 } |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
172 } |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
173 } |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
174 if( comments.length ){ |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
175 bool lineStart = true; |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
176 for( int idx = 0; idx < comments.length; idx++ ){ |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
177 char[1] b; |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
178 if( lineStart ){ |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
179 b[0] = '#'; |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
180 ostr.write(b); |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
181 } |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
182 append( comments[ idx .. idx+1 ] ); |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
183 lineStart = false; |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
184 if( comments[idx] is '\n' ){ |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
185 lineStart = true; |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
186 } |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
187 } |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
188 ostr.write( "\n" ); |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
189 } |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
190 foreach( k, v; map ){ |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
191 append(k); |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
192 append("="); |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
193 append(v); |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
194 ostr.write( "\n" ); |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
195 } |
34 | 196 } |
197 | |
198 // public synchronized void save( dejavu.io.OutputStream.OutputStream out_KEYWORDESCAPE, dejavu.lang.String.String comments ){ | |
199 // implMissing( __FILE__, __LINE__ ); | |
200 // } | |
201 // | |
202 // public synchronized void loadFromXML( dejavu.io.InputStream.InputStream in_KEYWORDESCAPE ){ | |
203 // implMissing( __FILE__, __LINE__ ); | |
204 // } | |
205 // | |
206 // public synchronized void storeToXML( dejavu.io.OutputStream.OutputStream os, dejavu.lang.String.String comment ){ | |
207 // implMissing( __FILE__, __LINE__ ); | |
208 // } | |
209 // | |
210 // public synchronized void storeToXML( dejavu.io.OutputStream.OutputStream os, dejavu.lang.String.String comment, dejavu.lang.String.String encoding ){ | |
211 // implMissing( __FILE__, __LINE__ ); | |
212 // } | |
213 | |
214 public String getProperty( String aKey ){ | |
215 if( auto res = aKey in map ){ | |
216 return *res; | |
217 } | |
218 if( defaults !is null ){ | |
219 return defaults.getProperty( aKey ); | |
220 } | |
221 return null; | |
222 } | |
223 | |
224 public String get( String aKey ){ | |
225 if( auto res = aKey in map ){ | |
226 return *res; | |
227 } | |
228 return null; | |
229 } | |
230 public String put( String aKey, String aValue ){ | |
231 if( auto pres = aKey in map ){ | |
232 String res = *pres; | |
233 map[ aKey ] = aValue; | |
234 return res; | |
235 } | |
236 map[ aKey ] = aValue; | |
237 return null; | |
238 } | |
239 public String remove( String aKey ){ | |
240 if( auto res = aKey in map ){ | |
241 map.remove(aKey); | |
242 return *res; | |
243 } | |
244 return null; | |
245 } | |
246 | |
247 public String getProperty( String key, String defaultValue ){ | |
248 if( auto res = key in map ){ | |
249 return *res; | |
250 } | |
251 return defaultValue; | |
252 } | |
253 | |
254 public void list(Print!(char) print){ | |
255 foreach( k, v; map ){ | |
256 print( k )( '=' )( v ).newline; | |
257 } | |
258 } | |
259 public bool containsKey( String key ){ | |
260 return ( key in map ) !is null; | |
261 } | |
262 public String[] propertyNames(){ | |
263 String[] res = new String[ map.length ]; | |
264 int idx; | |
265 foreach( key, val; map ){ | |
266 res[idx] = key; | |
267 idx++; | |
268 } | |
269 return res; | |
270 } | |
271 | |
272 // public dejavu.util.Enumeration.Enumeration propertyNames(){ | |
273 // implMissing( __FILE__, __LINE__ ); | |
274 // return null; | |
275 // } | |
276 // | |
277 // public void list( dejavu.io.PrintStream.PrintStream out_KEYWORDESCAPE ){ | |
278 // implMissing( __FILE__, __LINE__ ); | |
279 // } | |
280 // | |
281 // public void list( dejavu.io.PrintWriter.PrintWriter out_KEYWORDESCAPE ){ | |
282 // implMissing( __FILE__, __LINE__ ); | |
283 // } | |
284 // | |
285 // public override char[] toUtf8(){ | |
286 // return ""; | |
287 // } | |
288 | |
289 } | |
290 | |
291 | |
292 | |
293 /** | |
294 * A concrete preference store implementation based on an internal | |
295 * <code>java.util.Properties</code> object, with support for persisting the | |
296 * non-default preference values to files or streams. | |
297 * <p> | |
298 * This class was not designed to be subclassed. | |
299 * </p> | |
300 * | |
301 * @see IPreferenceStore | |
90 | 302 * @noextend This class is not intended to be subclassed by clients. |
34 | 303 */ |
304 public class PreferenceStore : EventManager, | |
305 IPersistentPreferenceStore { | |
306 | |
307 /** | |
308 * The mapping from preference name to preference value (represented as | |
309 * strings). | |
310 */ | |
311 private Properties properties; | |
312 | |
313 /** | |
314 * The mapping from preference name to default preference value (represented | |
315 * as strings); <code>null</code> if none. | |
316 */ | |
317 private Properties defaultProperties; | |
318 | |
319 /** | |
320 * Indicates whether a value as been changed by <code>setToDefault</code> | |
321 * or <code>setValue</code>; initially <code>false</code>. | |
322 */ | |
323 private bool dirty = false; | |
324 | |
325 /** | |
326 * The file name used by the <code>load</code> method to load a property | |
327 * file. This filename is used to save the properties file when | |
328 * <code>save</code> is called. | |
329 */ | |
330 private String filename; | |
331 | |
332 /** | |
333 * Creates an empty preference store. | |
334 * <p> | |
335 * Use the methods <code>load(InputStream)</code> and | |
336 * <code>save(InputStream)</code> to load and store this preference store. | |
337 * </p> | |
338 * | |
339 * @see #load(InputStream) | |
340 * @see #save(OutputStream, String) | |
341 */ | |
342 public this() { | |
343 defaultProperties = new Properties(); | |
344 properties = new Properties(defaultProperties); | |
345 } | |
346 | |
347 /** | |
348 * Creates an empty preference store that loads from and saves to the a | |
349 * file. | |
350 * <p> | |
351 * Use the methods <code>load()</code> and <code>save()</code> to load | |
352 * and store this preference store. | |
353 * </p> | |
354 * | |
355 * @param filename | |
356 * the file name | |
357 * @see #load() | |
358 * @see #save() | |
359 */ | |
360 public this(String filename) { | |
361 this(); | |
362 Assert.isNotNull(filename); | |
363 this.filename = filename; | |
364 } | |
365 | |
366 /* | |
367 * (non-Javadoc) Method declared on IPreferenceStore. | |
368 */ | |
369 public void addPropertyChangeListener(IPropertyChangeListener listener) { | |
370 addListenerObject(cast(Object)listener); | |
371 } | |
372 | |
373 /* | |
374 * (non-Javadoc) Method declared on IPreferenceStore. | |
375 */ | |
376 public bool contains(String name) { | |
377 return (properties.containsKey(name) || defaultProperties | |
378 .containsKey(name)); | |
379 } | |
380 | |
381 /* | |
382 * (non-Javadoc) Method declared on IPreferenceStore. | |
383 */ | |
384 public void firePropertyChangeEvent(String name, Object oldValue, | |
385 Object newValue) { | |
386 final Object[] finalListeners = getListeners(); | |
387 // Do we need to fire an event. | |
388 if (finalListeners.length > 0 | |
389 && (oldValue is null || !oldValue.opEquals(newValue))) { | |
390 final PropertyChangeEvent pe = new PropertyChangeEvent(this, name, | |
391 oldValue, newValue); | |
392 for (int i = 0; i < finalListeners.length; ++i) { | |
39 | 393 SafeRunnable.run(new class(JFaceResources.getString("PreferenceStore.changeError"),cast(IPropertyChangeListener) finalListeners[i]) SafeRunnable { //$NON-NLS-1$ |
34 | 394 IPropertyChangeListener l; |
39 | 395 this(char[] s,IPropertyChangeListener b){ |
34 | 396 super(s); |
39 | 397 l = b; |
34 | 398 } |
399 public void run() { | |
400 l.propertyChange(pe); | |
401 } | |
402 }); | |
403 } | |
404 } | |
405 } | |
406 | |
407 /* | |
408 * (non-Javadoc) Method declared on IPreferenceStore. | |
409 */ | |
410 public bool getBoolean(String name) { | |
411 return getBoolean(properties, name); | |
412 } | |
413 | |
414 /** | |
415 * Helper function: gets bool for a given name. | |
416 * | |
417 * @param p | |
418 * @param name | |
419 * @return bool | |
420 */ | |
421 private bool getBoolean(Properties p, String name) { | |
422 String value = p !is null ? p.getProperty(name) : null; | |
423 if (value is null) { | |
424 return BOOLEAN_DEFAULT_DEFAULT; | |
425 } | |
426 if (value.equals(IPreferenceStore.TRUE)) { | |
427 return true; | |
428 } | |
429 return false; | |
430 } | |
431 | |
432 /* | |
433 * (non-Javadoc) Method declared on IPreferenceStore. | |
434 */ | |
435 public bool getDefaultBoolean(String name) { | |
436 return getBoolean(defaultProperties, name); | |
437 } | |
438 | |
439 /* | |
440 * (non-Javadoc) Method declared on IPreferenceStore. | |
441 */ | |
442 public double getDefaultDouble(String name) { | |
443 return getDouble(defaultProperties, name); | |
444 } | |
445 | |
446 /* | |
447 * (non-Javadoc) Method declared on IPreferenceStore. | |
448 */ | |
449 public float getDefaultFloat(String name) { | |
450 return getFloat(defaultProperties, name); | |
451 } | |
452 | |
453 /* | |
454 * (non-Javadoc) Method declared on IPreferenceStore. | |
455 */ | |
456 public int getDefaultInt(String name) { | |
457 return getInt(defaultProperties, name); | |
458 } | |
459 | |
460 /* | |
461 * (non-Javadoc) Method declared on IPreferenceStore. | |
462 */ | |
463 public long getDefaultLong(String name) { | |
464 return getLong(defaultProperties, name); | |
465 } | |
466 | |
467 /* | |
468 * (non-Javadoc) Method declared on IPreferenceStore. | |
469 */ | |
470 public String getDefaultString(String name) { | |
471 return getString(defaultProperties, name); | |
472 } | |
473 | |
474 /* | |
475 * (non-Javadoc) Method declared on IPreferenceStore. | |
476 */ | |
477 public double getDouble(String name) { | |
478 return getDouble(properties, name); | |
479 } | |
480 | |
481 /** | |
482 * Helper function: gets double for a given name. | |
483 * | |
484 * @param p | |
485 * @param name | |
486 * @return double | |
487 */ | |
488 private double getDouble(Properties p, String name) { | |
489 String value = p !is null ? p.getProperty(name) : null; | |
490 if (value is null) { | |
491 return DOUBLE_DEFAULT_DEFAULT; | |
492 } | |
493 double ival = DOUBLE_DEFAULT_DEFAULT; | |
494 try { | |
495 ival = (new Double(value)).doubleValue(); | |
496 } catch (NumberFormatException e) { | |
497 } | |
498 return ival; | |
499 } | |
500 | |
501 /* | |
502 * (non-Javadoc) Method declared on IPreferenceStore. | |
503 */ | |
504 public float getFloat(String name) { | |
505 return getFloat(properties, name); | |
506 } | |
507 | |
508 /** | |
509 * Helper function: gets float for a given name. | |
510 * | |
511 * @param p | |
512 * @param name | |
513 * @return float | |
514 */ | |
515 private float getFloat(Properties p, String name) { | |
516 String value = p !is null ? p.getProperty(name) : null; | |
517 if (value is null) { | |
518 return FLOAT_DEFAULT_DEFAULT; | |
519 } | |
520 float ival = FLOAT_DEFAULT_DEFAULT; | |
521 try { | |
522 ival = (new Float(value)).floatValue(); | |
523 } catch (NumberFormatException e) { | |
524 } | |
525 return ival; | |
526 } | |
527 | |
528 /* | |
529 * (non-Javadoc) Method declared on IPreferenceStore. | |
530 */ | |
531 public int getInt(String name) { | |
532 return getInt(properties, name); | |
533 } | |
534 | |
535 /** | |
536 * Helper function: gets int for a given name. | |
537 * | |
538 * @param p | |
539 * @param name | |
540 * @return int | |
541 */ | |
542 private int getInt(Properties p, String name) { | |
543 String value = p !is null ? p.getProperty(name) : null; | |
544 if (value is null) { | |
545 return INT_DEFAULT_DEFAULT; | |
546 } | |
547 int ival = 0; | |
548 try { | |
549 ival = Integer.parseInt(value); | |
550 } catch (NumberFormatException e) { | |
551 } | |
552 return ival; | |
553 } | |
554 | |
555 /* | |
556 * (non-Javadoc) Method declared on IPreferenceStore. | |
557 */ | |
558 public long getLong(String name) { | |
559 return getLong(properties, name); | |
560 } | |
561 | |
562 /** | |
563 * Helper function: gets long for a given name. | |
564 * | |
565 * @param p | |
566 * the properties storage (may be <code>null</code>) | |
567 * @param name | |
568 * the name of the property | |
569 * @return the long or a default value of if: | |
570 * <ul> | |
571 * <li>properties storage is <code>null</code></li> | |
572 * <li>property is not found</li> | |
573 * <li>property value is not a number</li> | |
574 * </ul> | |
575 * @see IPreferenceStore#LONG_DEFAULT_DEFAULT | |
576 */ | |
577 private long getLong(Properties p, String name) { | |
578 String value = p !is null ? p.getProperty(name) : null; | |
579 if (value is null) { | |
580 return LONG_DEFAULT_DEFAULT; | |
581 } | |
582 long ival = LONG_DEFAULT_DEFAULT; | |
583 try { | |
584 ival = Long.parseLong(value); | |
585 } catch (NumberFormatException e) { | |
586 } | |
587 return ival; | |
588 } | |
589 | |
590 /* | |
591 * (non-Javadoc) Method declared on IPreferenceStore. | |
592 */ | |
593 public String getString(String name) { | |
594 return getString(properties, name); | |
595 } | |
596 | |
597 /** | |
598 * Helper function: gets string for a given name. | |
599 * | |
600 * @param p | |
601 * the properties storage (may be <code>null</code>) | |
602 * @param name | |
603 * the name of the property | |
604 * @return the value or a default value of if: | |
605 * <ul> | |
606 * <li>properties storage is <code>null</code></li> | |
607 * <li>property is not found</li> | |
608 * <li>property value is not a number</li> | |
609 * </ul> | |
610 * @see IPreferenceStore#STRING_DEFAULT_DEFAULT | |
611 */ | |
612 private String getString(Properties p, String name) { | |
613 String value = p !is null ? p.getProperty(name) : null; | |
614 if (value is null) { | |
615 return STRING_DEFAULT_DEFAULT; | |
616 } | |
617 return value; | |
618 } | |
619 | |
620 /* | |
621 * (non-Javadoc) Method declared on IPreferenceStore. | |
622 */ | |
623 public bool isDefault(String name) { | |
624 return (!properties.containsKey(name) && defaultProperties | |
625 .containsKey(name)); | |
626 } | |
627 | |
628 /** | |
629 * Prints the contents of this preference store to the given print stream. | |
630 * | |
631 * @param out | |
632 * the print stream | |
633 */ | |
634 public void list(Print!(char) out_) { | |
635 properties.list(out_); | |
636 } | |
637 | |
638 // /** | |
639 // * Prints the contents of this preference store to the given print writer. | |
640 // * | |
641 // * @param out | |
642 // * the print writer | |
643 // */ | |
644 // public void list(PrintWriter out_) { | |
645 // properties.list(out_); | |
646 // } | |
647 | |
648 /** | |
649 * Loads this preference store from the file established in the constructor | |
650 * <code>PreferenceStore(java.lang.String)</code> (or by | |
651 * <code>setFileName</code>). Default preference values are not affected. | |
652 * | |
653 * @exception java.io.IOException | |
654 * if there is a problem loading this store | |
655 */ | |
656 public void load() { | |
657 if (filename is null) { | |
658 throw new IOException("File name not specified");//$NON-NLS-1$ | |
659 } | |
660 FileInput in_ = new FileInput(filename); | |
661 load(in_.input); | |
662 in_.close(); | |
663 } | |
664 | |
665 /** | |
666 * Loads this preference store from the given input stream. Default | |
667 * preference values are not affected. | |
668 * | |
669 * @param in | |
670 * the input stream | |
671 * @exception java.io.IOException | |
672 * if there is a problem loading this store | |
673 */ | |
674 public void load(InputStream in_) { | |
675 properties.load(in_); | |
676 dirty = false; | |
677 } | |
678 | |
679 /* | |
680 * (non-Javadoc) Method declared on IPreferenceStore. | |
681 */ | |
682 public bool needsSaving() { | |
683 return dirty; | |
684 } | |
685 | |
686 /** | |
687 * Returns an enumeration of all preferences known to this store which have | |
688 * current values other than their default value. | |
689 * | |
690 * @return an array of preference names | |
691 */ | |
692 public String[] preferenceNames() { | |
693 String[] list; | |
694 foreach( prop; properties.propertyNames() ){ | |
695 list ~= prop; | |
696 } | |
697 return list; | |
698 } | |
699 | |
700 /* | |
701 * (non-Javadoc) Method declared on IPreferenceStore. | |
702 */ | |
703 public void putValue(String name, String value) { | |
704 String oldValue = getString(name); | |
705 if (oldValue is null || !oldValue.equals(value)) { | |
706 setValue(properties, name, value); | |
707 dirty = true; | |
708 } | |
709 } | |
710 | |
711 /* | |
712 * (non-Javadoc) Method declared on IPreferenceStore. | |
713 */ | |
714 public void removePropertyChangeListener(IPropertyChangeListener listener) { | |
715 removeListenerObject(cast(Object)listener); | |
716 } | |
717 | |
718 /** | |
719 * Saves the non-default-valued preferences known to this preference store | |
720 * to the file from which they were originally loaded. | |
721 * | |
722 * @exception java.io.IOException | |
723 * if there is a problem saving this store | |
724 */ | |
725 public void save() { | |
726 if (filename is null) { | |
727 throw new IOException("File name not specified");//$NON-NLS-1$ | |
728 } | |
729 FileOutput out_ = null; | |
730 try { | |
731 out_ = new FileOutput(filename); | |
732 save(out_, null); | |
733 } finally { | |
734 if (out_ !is null) { | |
735 out_.close(); | |
736 } | |
737 } | |
738 } | |
739 | |
740 /** | |
741 * Saves this preference store to the given output stream. The given string | |
742 * is inserted as header information. | |
743 * | |
744 * @param out | |
745 * the output stream | |
746 * @param header | |
747 * the header | |
748 * @exception java.io.IOException | |
749 * if there is a problem saving this store | |
750 */ | |
751 public void save(OutputStream out_, String header) { | |
752 properties.store(out_, header); | |
753 dirty = false; | |
754 } | |
755 | |
756 /* | |
757 * (non-Javadoc) Method declared on IPreferenceStore. | |
758 */ | |
759 public void setDefault(String name, double value) { | |
760 setValue(defaultProperties, name, value); | |
761 } | |
762 | |
763 /* | |
764 * (non-Javadoc) Method declared on IPreferenceStore. | |
765 */ | |
766 public void setDefault(String name, float value) { | |
767 setValue(defaultProperties, name, value); | |
768 } | |
769 | |
770 /* | |
771 * (non-Javadoc) Method declared on IPreferenceStore. | |
772 */ | |
773 public void setDefault(String name, int value) { | |
774 setValue(defaultProperties, name, value); | |
775 } | |
776 | |
777 /* | |
778 * (non-Javadoc) Method declared on IPreferenceStore. | |
779 */ | |
780 public void setDefault(String name, long value) { | |
781 setValue(defaultProperties, name, value); | |
782 } | |
783 | |
784 /* | |
785 * (non-Javadoc) Method declared on IPreferenceStore. | |
786 */ | |
787 public void setDefault(String name, String value) { | |
788 setValue(defaultProperties, name, value); | |
789 } | |
790 | |
791 /* | |
792 * (non-Javadoc) Method declared on IPreferenceStore. | |
793 */ | |
794 public void setDefault(String name, bool value) { | |
795 setValue(defaultProperties, name, value); | |
796 } | |
797 | |
798 /** | |
799 * Sets the name of the file used when loading and storing this preference | |
800 * store. | |
801 * <p> | |
802 * Afterward, the methods <code>load()</code> and <code>save()</code> | |
803 * can be used to load and store this preference store. | |
804 * </p> | |
805 * | |
806 * @param name | |
807 * the file name | |
808 * @see #load() | |
809 * @see #save() | |
810 */ | |
811 public void setFilename(String name) { | |
812 filename = name; | |
813 } | |
814 | |
815 /* | |
816 * (non-Javadoc) Method declared on IPreferenceStore. | |
817 */ | |
818 public void setToDefault(String name) { | |
819 String oldValue = properties.get(name); | |
820 properties.remove(name); | |
821 dirty = true; | |
822 String newValue; | |
823 if (defaultProperties !is null) { | |
824 newValue = defaultProperties.get(name); | |
825 } | |
826 firePropertyChangeEvent(name, stringcast(oldValue), stringcast(newValue)); | |
827 } | |
828 | |
829 /* | |
830 * (non-Javadoc) Method declared on IPreferenceStore. | |
831 */ | |
832 public void setValue(String name, double value) { | |
833 double oldValue = getDouble(name); | |
834 if (oldValue !is value) { | |
835 setValue(properties, name, value); | |
836 dirty = true; | |
837 firePropertyChangeEvent(name, new Double(oldValue), new Double( | |
838 value)); | |
839 } | |
840 } | |
841 | |
842 /* | |
843 * (non-Javadoc) Method declared on IPreferenceStore. | |
844 */ | |
845 public void setValue(String name, float value) { | |
846 float oldValue = getFloat(name); | |
847 if (oldValue !is value) { | |
848 setValue(properties, name, value); | |
849 dirty = true; | |
850 firePropertyChangeEvent(name, new Float(oldValue), new Float(value)); | |
851 } | |
852 } | |
853 | |
854 /* | |
855 * (non-Javadoc) Method declared on IPreferenceStore. | |
856 */ | |
857 public void setValue(String name, int value) { | |
858 int oldValue = getInt(name); | |
859 if (oldValue !is value) { | |
860 setValue(properties, name, value); | |
861 dirty = true; | |
862 firePropertyChangeEvent(name, new Integer(oldValue), new Integer( | |
863 value)); | |
864 } | |
865 } | |
866 | |
867 /* | |
868 * (non-Javadoc) Method declared on IPreferenceStore. | |
869 */ | |
870 public void setValue(String name, long value) { | |
871 long oldValue = getLong(name); | |
872 if (oldValue !is value) { | |
873 setValue(properties, name, value); | |
874 dirty = true; | |
875 firePropertyChangeEvent(name, new Long(oldValue), new Long(value)); | |
876 } | |
877 } | |
878 | |
879 /* | |
880 * (non-Javadoc) Method declared on IPreferenceStore. | |
881 */ | |
882 public void setValue(String name, String value) { | |
883 String oldValue = getString(name); | |
884 if (oldValue is null || !oldValue.equals(value)) { | |
885 setValue(properties, name, value); | |
886 dirty = true; | |
887 firePropertyChangeEvent(name, stringcast(oldValue), stringcast(value)); | |
888 } | |
889 } | |
890 | |
891 /* | |
892 * (non-Javadoc) Method declared on IPreferenceStore. | |
893 */ | |
894 public void setValue(String name, bool value) { | |
895 bool oldValue = getBoolean(name); | |
896 if (oldValue !is value) { | |
897 setValue(properties, name, value); | |
898 dirty = true; | |
899 firePropertyChangeEvent(name, oldValue ? Boolean.TRUE | |
900 : Boolean.FALSE, value ? Boolean.TRUE : Boolean.FALSE); | |
901 } | |
902 } | |
903 | |
904 /** | |
905 * Helper method: sets value for a given name. | |
906 * | |
907 * @param p | |
908 * @param name | |
909 * @param value | |
910 */ | |
911 private void setValue(Properties p, String name, double value) { | |
912 Assert.isTrue(p !is null); | |
913 p.put(name, Double.toString(value)); | |
914 } | |
915 | |
916 /** | |
917 * Helper method: sets value for a given name. | |
918 * | |
919 * @param p | |
920 * @param name | |
921 * @param value | |
922 */ | |
923 private void setValue(Properties p, String name, float value) { | |
924 Assert.isTrue(p !is null); | |
925 p.put(name, Float.toString(value)); | |
926 } | |
927 | |
928 /** | |
929 * Helper method: sets value for a given name. | |
930 * | |
931 * @param p | |
932 * @param name | |
933 * @param value | |
934 */ | |
935 private void setValue(Properties p, String name, int value) { | |
936 Assert.isTrue(p !is null); | |
937 p.put(name, Integer.toString(value)); | |
938 } | |
939 | |
940 /** | |
941 * Helper method: sets the value for a given name. | |
942 * | |
943 * @param p | |
944 * @param name | |
945 * @param value | |
946 */ | |
947 private void setValue(Properties p, String name, long value) { | |
948 Assert.isTrue(p !is null); | |
949 p.put(name, Long.toString(value)); | |
950 } | |
951 | |
952 /** | |
953 * Helper method: sets the value for a given name. | |
954 * | |
955 * @param p | |
956 * @param name | |
957 * @param value | |
958 */ | |
959 private void setValue(Properties p, String name, String value) { | |
47
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
960 // DWT: allow null value |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
961 Assert.isTrue(p !is null /+&& value !is null+/); |
34 | 962 p.put(name, value); |
963 } | |
964 | |
965 /** | |
966 * Helper method: sets the value for a given name. | |
967 * | |
968 * @param p | |
969 * @param name | |
970 * @param value | |
971 */ | |
972 private void setValue(Properties p, String name, bool value) { | |
973 Assert.isTrue(p !is null); | |
974 p.put(name, value is true ? IPreferenceStore.TRUE | |
975 : IPreferenceStore.FALSE); | |
976 } | |
977 } |