Mercurial > projects > dwt-addons
annotate dwtx/jface/preference/PreferenceStore.d @ 90:7ffeace6c47f
Update 3.4M7 to 3.4
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 06 Jul 2008 23:30:07 +0200 |
parents | be19b235c6c4 |
children | 04b47443bb01 |
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 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 | |
90 | 301 * @noextend This class is not intended to be subclassed by clients. |
34 | 302 */ |
303 public class PreferenceStore : EventManager, | |
304 IPersistentPreferenceStore { | |
305 | |
306 /** | |
307 * The mapping from preference name to preference value (represented as | |
308 * strings). | |
309 */ | |
310 private Properties properties; | |
311 | |
312 /** | |
313 * The mapping from preference name to default preference value (represented | |
314 * as strings); <code>null</code> if none. | |
315 */ | |
316 private Properties defaultProperties; | |
317 | |
318 /** | |
319 * Indicates whether a value as been changed by <code>setToDefault</code> | |
320 * or <code>setValue</code>; initially <code>false</code>. | |
321 */ | |
322 private bool dirty = false; | |
323 | |
324 /** | |
325 * The file name used by the <code>load</code> method to load a property | |
326 * file. This filename is used to save the properties file when | |
327 * <code>save</code> is called. | |
328 */ | |
329 private String filename; | |
330 | |
331 /** | |
332 * Creates an empty preference store. | |
333 * <p> | |
334 * Use the methods <code>load(InputStream)</code> and | |
335 * <code>save(InputStream)</code> to load and store this preference store. | |
336 * </p> | |
337 * | |
338 * @see #load(InputStream) | |
339 * @see #save(OutputStream, String) | |
340 */ | |
341 public this() { | |
342 defaultProperties = new Properties(); | |
343 properties = new Properties(defaultProperties); | |
344 } | |
345 | |
346 /** | |
347 * Creates an empty preference store that loads from and saves to the a | |
348 * file. | |
349 * <p> | |
350 * Use the methods <code>load()</code> and <code>save()</code> to load | |
351 * and store this preference store. | |
352 * </p> | |
353 * | |
354 * @param filename | |
355 * the file name | |
356 * @see #load() | |
357 * @see #save() | |
358 */ | |
359 public this(String filename) { | |
360 this(); | |
361 Assert.isNotNull(filename); | |
362 this.filename = filename; | |
363 } | |
364 | |
365 /* | |
366 * (non-Javadoc) Method declared on IPreferenceStore. | |
367 */ | |
368 public void addPropertyChangeListener(IPropertyChangeListener listener) { | |
369 addListenerObject(cast(Object)listener); | |
370 } | |
371 | |
372 /* | |
373 * (non-Javadoc) Method declared on IPreferenceStore. | |
374 */ | |
375 public bool contains(String name) { | |
376 return (properties.containsKey(name) || defaultProperties | |
377 .containsKey(name)); | |
378 } | |
379 | |
380 /* | |
381 * (non-Javadoc) Method declared on IPreferenceStore. | |
382 */ | |
383 public void firePropertyChangeEvent(String name, Object oldValue, | |
384 Object newValue) { | |
385 final Object[] finalListeners = getListeners(); | |
386 // Do we need to fire an event. | |
387 if (finalListeners.length > 0 | |
388 && (oldValue is null || !oldValue.opEquals(newValue))) { | |
389 final PropertyChangeEvent pe = new PropertyChangeEvent(this, name, | |
390 oldValue, newValue); | |
391 for (int i = 0; i < finalListeners.length; ++i) { | |
39 | 392 SafeRunnable.run(new class(JFaceResources.getString("PreferenceStore.changeError"),cast(IPropertyChangeListener) finalListeners[i]) SafeRunnable { //$NON-NLS-1$ |
34 | 393 IPropertyChangeListener l; |
39 | 394 this(char[] s,IPropertyChangeListener b){ |
34 | 395 super(s); |
39 | 396 l = b; |
34 | 397 } |
398 public void run() { | |
399 l.propertyChange(pe); | |
400 } | |
401 }); | |
402 } | |
403 } | |
404 } | |
405 | |
406 /* | |
407 * (non-Javadoc) Method declared on IPreferenceStore. | |
408 */ | |
409 public bool getBoolean(String name) { | |
410 return getBoolean(properties, name); | |
411 } | |
412 | |
413 /** | |
414 * Helper function: gets bool for a given name. | |
415 * | |
416 * @param p | |
417 * @param name | |
418 * @return bool | |
419 */ | |
420 private bool getBoolean(Properties p, String name) { | |
421 String value = p !is null ? p.getProperty(name) : null; | |
422 if (value is null) { | |
423 return BOOLEAN_DEFAULT_DEFAULT; | |
424 } | |
425 if (value.equals(IPreferenceStore.TRUE)) { | |
426 return true; | |
427 } | |
428 return false; | |
429 } | |
430 | |
431 /* | |
432 * (non-Javadoc) Method declared on IPreferenceStore. | |
433 */ | |
434 public bool getDefaultBoolean(String name) { | |
435 return getBoolean(defaultProperties, name); | |
436 } | |
437 | |
438 /* | |
439 * (non-Javadoc) Method declared on IPreferenceStore. | |
440 */ | |
441 public double getDefaultDouble(String name) { | |
442 return getDouble(defaultProperties, name); | |
443 } | |
444 | |
445 /* | |
446 * (non-Javadoc) Method declared on IPreferenceStore. | |
447 */ | |
448 public float getDefaultFloat(String name) { | |
449 return getFloat(defaultProperties, name); | |
450 } | |
451 | |
452 /* | |
453 * (non-Javadoc) Method declared on IPreferenceStore. | |
454 */ | |
455 public int getDefaultInt(String name) { | |
456 return getInt(defaultProperties, name); | |
457 } | |
458 | |
459 /* | |
460 * (non-Javadoc) Method declared on IPreferenceStore. | |
461 */ | |
462 public long getDefaultLong(String name) { | |
463 return getLong(defaultProperties, name); | |
464 } | |
465 | |
466 /* | |
467 * (non-Javadoc) Method declared on IPreferenceStore. | |
468 */ | |
469 public String getDefaultString(String name) { | |
470 return getString(defaultProperties, name); | |
471 } | |
472 | |
473 /* | |
474 * (non-Javadoc) Method declared on IPreferenceStore. | |
475 */ | |
476 public double getDouble(String name) { | |
477 return getDouble(properties, name); | |
478 } | |
479 | |
480 /** | |
481 * Helper function: gets double for a given name. | |
482 * | |
483 * @param p | |
484 * @param name | |
485 * @return double | |
486 */ | |
487 private double getDouble(Properties p, String name) { | |
488 String value = p !is null ? p.getProperty(name) : null; | |
489 if (value is null) { | |
490 return DOUBLE_DEFAULT_DEFAULT; | |
491 } | |
492 double ival = DOUBLE_DEFAULT_DEFAULT; | |
493 try { | |
494 ival = (new Double(value)).doubleValue(); | |
495 } catch (NumberFormatException e) { | |
496 } | |
497 return ival; | |
498 } | |
499 | |
500 /* | |
501 * (non-Javadoc) Method declared on IPreferenceStore. | |
502 */ | |
503 public float getFloat(String name) { | |
504 return getFloat(properties, name); | |
505 } | |
506 | |
507 /** | |
508 * Helper function: gets float for a given name. | |
509 * | |
510 * @param p | |
511 * @param name | |
512 * @return float | |
513 */ | |
514 private float getFloat(Properties p, String name) { | |
515 String value = p !is null ? p.getProperty(name) : null; | |
516 if (value is null) { | |
517 return FLOAT_DEFAULT_DEFAULT; | |
518 } | |
519 float ival = FLOAT_DEFAULT_DEFAULT; | |
520 try { | |
521 ival = (new Float(value)).floatValue(); | |
522 } catch (NumberFormatException e) { | |
523 } | |
524 return ival; | |
525 } | |
526 | |
527 /* | |
528 * (non-Javadoc) Method declared on IPreferenceStore. | |
529 */ | |
530 public int getInt(String name) { | |
531 return getInt(properties, name); | |
532 } | |
533 | |
534 /** | |
535 * Helper function: gets int for a given name. | |
536 * | |
537 * @param p | |
538 * @param name | |
539 * @return int | |
540 */ | |
541 private int getInt(Properties p, String name) { | |
542 String value = p !is null ? p.getProperty(name) : null; | |
543 if (value is null) { | |
544 return INT_DEFAULT_DEFAULT; | |
545 } | |
546 int ival = 0; | |
547 try { | |
548 ival = Integer.parseInt(value); | |
549 } catch (NumberFormatException e) { | |
550 } | |
551 return ival; | |
552 } | |
553 | |
554 /* | |
555 * (non-Javadoc) Method declared on IPreferenceStore. | |
556 */ | |
557 public long getLong(String name) { | |
558 return getLong(properties, name); | |
559 } | |
560 | |
561 /** | |
562 * Helper function: gets long for a given name. | |
563 * | |
564 * @param p | |
565 * the properties storage (may be <code>null</code>) | |
566 * @param name | |
567 * the name of the property | |
568 * @return the long or a default value of if: | |
569 * <ul> | |
570 * <li>properties storage is <code>null</code></li> | |
571 * <li>property is not found</li> | |
572 * <li>property value is not a number</li> | |
573 * </ul> | |
574 * @see IPreferenceStore#LONG_DEFAULT_DEFAULT | |
575 */ | |
576 private long getLong(Properties p, String name) { | |
577 String value = p !is null ? p.getProperty(name) : null; | |
578 if (value is null) { | |
579 return LONG_DEFAULT_DEFAULT; | |
580 } | |
581 long ival = LONG_DEFAULT_DEFAULT; | |
582 try { | |
583 ival = Long.parseLong(value); | |
584 } catch (NumberFormatException e) { | |
585 } | |
586 return ival; | |
587 } | |
588 | |
589 /* | |
590 * (non-Javadoc) Method declared on IPreferenceStore. | |
591 */ | |
592 public String getString(String name) { | |
593 return getString(properties, name); | |
594 } | |
595 | |
596 /** | |
597 * Helper function: gets string for a given name. | |
598 * | |
599 * @param p | |
600 * the properties storage (may be <code>null</code>) | |
601 * @param name | |
602 * the name of the property | |
603 * @return the value or a default value of if: | |
604 * <ul> | |
605 * <li>properties storage is <code>null</code></li> | |
606 * <li>property is not found</li> | |
607 * <li>property value is not a number</li> | |
608 * </ul> | |
609 * @see IPreferenceStore#STRING_DEFAULT_DEFAULT | |
610 */ | |
611 private String getString(Properties p, String name) { | |
612 String value = p !is null ? p.getProperty(name) : null; | |
613 if (value is null) { | |
614 return STRING_DEFAULT_DEFAULT; | |
615 } | |
616 return value; | |
617 } | |
618 | |
619 /* | |
620 * (non-Javadoc) Method declared on IPreferenceStore. | |
621 */ | |
622 public bool isDefault(String name) { | |
623 return (!properties.containsKey(name) && defaultProperties | |
624 .containsKey(name)); | |
625 } | |
626 | |
627 /** | |
628 * Prints the contents of this preference store to the given print stream. | |
629 * | |
630 * @param out | |
631 * the print stream | |
632 */ | |
633 public void list(Print!(char) out_) { | |
634 properties.list(out_); | |
635 } | |
636 | |
637 // /** | |
638 // * Prints the contents of this preference store to the given print writer. | |
639 // * | |
640 // * @param out | |
641 // * the print writer | |
642 // */ | |
643 // public void list(PrintWriter out_) { | |
644 // properties.list(out_); | |
645 // } | |
646 | |
647 /** | |
648 * Loads this preference store from the file established in the constructor | |
649 * <code>PreferenceStore(java.lang.String)</code> (or by | |
650 * <code>setFileName</code>). Default preference values are not affected. | |
651 * | |
652 * @exception java.io.IOException | |
653 * if there is a problem loading this store | |
654 */ | |
655 public void load() { | |
656 if (filename is null) { | |
657 throw new IOException("File name not specified");//$NON-NLS-1$ | |
658 } | |
659 FileInput in_ = new FileInput(filename); | |
660 load(in_.input); | |
661 in_.close(); | |
662 } | |
663 | |
664 /** | |
665 * Loads this preference store from the given input stream. Default | |
666 * preference values are not affected. | |
667 * | |
668 * @param in | |
669 * the input stream | |
670 * @exception java.io.IOException | |
671 * if there is a problem loading this store | |
672 */ | |
673 public void load(InputStream in_) { | |
674 properties.load(in_); | |
675 dirty = false; | |
676 } | |
677 | |
678 /* | |
679 * (non-Javadoc) Method declared on IPreferenceStore. | |
680 */ | |
681 public bool needsSaving() { | |
682 return dirty; | |
683 } | |
684 | |
685 /** | |
686 * Returns an enumeration of all preferences known to this store which have | |
687 * current values other than their default value. | |
688 * | |
689 * @return an array of preference names | |
690 */ | |
691 public String[] preferenceNames() { | |
692 String[] list; | |
693 foreach( prop; properties.propertyNames() ){ | |
694 list ~= prop; | |
695 } | |
696 return list; | |
697 } | |
698 | |
699 /* | |
700 * (non-Javadoc) Method declared on IPreferenceStore. | |
701 */ | |
702 public void putValue(String name, String value) { | |
703 String oldValue = getString(name); | |
704 if (oldValue is null || !oldValue.equals(value)) { | |
705 setValue(properties, name, value); | |
706 dirty = true; | |
707 } | |
708 } | |
709 | |
710 /* | |
711 * (non-Javadoc) Method declared on IPreferenceStore. | |
712 */ | |
713 public void removePropertyChangeListener(IPropertyChangeListener listener) { | |
714 removeListenerObject(cast(Object)listener); | |
715 } | |
716 | |
717 /** | |
718 * Saves the non-default-valued preferences known to this preference store | |
719 * to the file from which they were originally loaded. | |
720 * | |
721 * @exception java.io.IOException | |
722 * if there is a problem saving this store | |
723 */ | |
724 public void save() { | |
725 if (filename is null) { | |
726 throw new IOException("File name not specified");//$NON-NLS-1$ | |
727 } | |
728 FileOutput out_ = null; | |
729 try { | |
730 out_ = new FileOutput(filename); | |
731 save(out_, null); | |
732 } finally { | |
733 if (out_ !is null) { | |
734 out_.close(); | |
735 } | |
736 } | |
737 } | |
738 | |
739 /** | |
740 * Saves this preference store to the given output stream. The given string | |
741 * is inserted as header information. | |
742 * | |
743 * @param out | |
744 * the output stream | |
745 * @param header | |
746 * the header | |
747 * @exception java.io.IOException | |
748 * if there is a problem saving this store | |
749 */ | |
750 public void save(OutputStream out_, String header) { | |
751 properties.store(out_, header); | |
752 dirty = false; | |
753 } | |
754 | |
755 /* | |
756 * (non-Javadoc) Method declared on IPreferenceStore. | |
757 */ | |
758 public void setDefault(String name, double value) { | |
759 setValue(defaultProperties, name, value); | |
760 } | |
761 | |
762 /* | |
763 * (non-Javadoc) Method declared on IPreferenceStore. | |
764 */ | |
765 public void setDefault(String name, float value) { | |
766 setValue(defaultProperties, name, value); | |
767 } | |
768 | |
769 /* | |
770 * (non-Javadoc) Method declared on IPreferenceStore. | |
771 */ | |
772 public void setDefault(String name, int value) { | |
773 setValue(defaultProperties, name, value); | |
774 } | |
775 | |
776 /* | |
777 * (non-Javadoc) Method declared on IPreferenceStore. | |
778 */ | |
779 public void setDefault(String name, long value) { | |
780 setValue(defaultProperties, name, value); | |
781 } | |
782 | |
783 /* | |
784 * (non-Javadoc) Method declared on IPreferenceStore. | |
785 */ | |
786 public void setDefault(String name, String value) { | |
787 setValue(defaultProperties, name, value); | |
788 } | |
789 | |
790 /* | |
791 * (non-Javadoc) Method declared on IPreferenceStore. | |
792 */ | |
793 public void setDefault(String name, bool value) { | |
794 setValue(defaultProperties, name, value); | |
795 } | |
796 | |
797 /** | |
798 * Sets the name of the file used when loading and storing this preference | |
799 * store. | |
800 * <p> | |
801 * Afterward, the methods <code>load()</code> and <code>save()</code> | |
802 * can be used to load and store this preference store. | |
803 * </p> | |
804 * | |
805 * @param name | |
806 * the file name | |
807 * @see #load() | |
808 * @see #save() | |
809 */ | |
810 public void setFilename(String name) { | |
811 filename = name; | |
812 } | |
813 | |
814 /* | |
815 * (non-Javadoc) Method declared on IPreferenceStore. | |
816 */ | |
817 public void setToDefault(String name) { | |
818 String oldValue = properties.get(name); | |
819 properties.remove(name); | |
820 dirty = true; | |
821 String newValue; | |
822 if (defaultProperties !is null) { | |
823 newValue = defaultProperties.get(name); | |
824 } | |
825 firePropertyChangeEvent(name, stringcast(oldValue), stringcast(newValue)); | |
826 } | |
827 | |
828 /* | |
829 * (non-Javadoc) Method declared on IPreferenceStore. | |
830 */ | |
831 public void setValue(String name, double value) { | |
832 double oldValue = getDouble(name); | |
833 if (oldValue !is value) { | |
834 setValue(properties, name, value); | |
835 dirty = true; | |
836 firePropertyChangeEvent(name, new Double(oldValue), new Double( | |
837 value)); | |
838 } | |
839 } | |
840 | |
841 /* | |
842 * (non-Javadoc) Method declared on IPreferenceStore. | |
843 */ | |
844 public void setValue(String name, float value) { | |
845 float oldValue = getFloat(name); | |
846 if (oldValue !is value) { | |
847 setValue(properties, name, value); | |
848 dirty = true; | |
849 firePropertyChangeEvent(name, new Float(oldValue), new Float(value)); | |
850 } | |
851 } | |
852 | |
853 /* | |
854 * (non-Javadoc) Method declared on IPreferenceStore. | |
855 */ | |
856 public void setValue(String name, int value) { | |
857 int oldValue = getInt(name); | |
858 if (oldValue !is value) { | |
859 setValue(properties, name, value); | |
860 dirty = true; | |
861 firePropertyChangeEvent(name, new Integer(oldValue), new Integer( | |
862 value)); | |
863 } | |
864 } | |
865 | |
866 /* | |
867 * (non-Javadoc) Method declared on IPreferenceStore. | |
868 */ | |
869 public void setValue(String name, long value) { | |
870 long oldValue = getLong(name); | |
871 if (oldValue !is value) { | |
872 setValue(properties, name, value); | |
873 dirty = true; | |
874 firePropertyChangeEvent(name, new Long(oldValue), new Long(value)); | |
875 } | |
876 } | |
877 | |
878 /* | |
879 * (non-Javadoc) Method declared on IPreferenceStore. | |
880 */ | |
881 public void setValue(String name, String value) { | |
882 String oldValue = getString(name); | |
883 if (oldValue is null || !oldValue.equals(value)) { | |
884 setValue(properties, name, value); | |
885 dirty = true; | |
886 firePropertyChangeEvent(name, stringcast(oldValue), stringcast(value)); | |
887 } | |
888 } | |
889 | |
890 /* | |
891 * (non-Javadoc) Method declared on IPreferenceStore. | |
892 */ | |
893 public void setValue(String name, bool value) { | |
894 bool oldValue = getBoolean(name); | |
895 if (oldValue !is value) { | |
896 setValue(properties, name, value); | |
897 dirty = true; | |
898 firePropertyChangeEvent(name, oldValue ? Boolean.TRUE | |
899 : Boolean.FALSE, value ? Boolean.TRUE : Boolean.FALSE); | |
900 } | |
901 } | |
902 | |
903 /** | |
904 * Helper method: sets value for a given name. | |
905 * | |
906 * @param p | |
907 * @param name | |
908 * @param value | |
909 */ | |
910 private void setValue(Properties p, String name, double value) { | |
911 Assert.isTrue(p !is null); | |
912 p.put(name, Double.toString(value)); | |
913 } | |
914 | |
915 /** | |
916 * Helper method: sets value for a given name. | |
917 * | |
918 * @param p | |
919 * @param name | |
920 * @param value | |
921 */ | |
922 private void setValue(Properties p, String name, float value) { | |
923 Assert.isTrue(p !is null); | |
924 p.put(name, Float.toString(value)); | |
925 } | |
926 | |
927 /** | |
928 * Helper method: sets value for a given name. | |
929 * | |
930 * @param p | |
931 * @param name | |
932 * @param value | |
933 */ | |
934 private void setValue(Properties p, String name, int value) { | |
935 Assert.isTrue(p !is null); | |
936 p.put(name, Integer.toString(value)); | |
937 } | |
938 | |
939 /** | |
940 * Helper method: sets the value for a given name. | |
941 * | |
942 * @param p | |
943 * @param name | |
944 * @param value | |
945 */ | |
946 private void setValue(Properties p, String name, long value) { | |
947 Assert.isTrue(p !is null); | |
948 p.put(name, Long.toString(value)); | |
949 } | |
950 | |
951 /** | |
952 * Helper method: sets the value for a given name. | |
953 * | |
954 * @param p | |
955 * @param name | |
956 * @param value | |
957 */ | |
958 private void setValue(Properties p, String name, String value) { | |
47
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
959 // DWT: allow null value |
be19b235c6c4
implement PreferenceStore.store
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
960 Assert.isTrue(p !is null /+&& value !is null+/); |
34 | 961 p.put(name, value); |
962 } | |
963 | |
964 /** | |
965 * Helper method: sets the value for a given name. | |
966 * | |
967 * @param p | |
968 * @param name | |
969 * @param value | |
970 */ | |
971 private void setValue(Properties p, String name, bool value) { | |
972 Assert.isTrue(p !is null); | |
973 p.put(name, value is true ? IPreferenceStore.TRUE | |
974 : IPreferenceStore.FALSE); | |
975 } | |
976 } |