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