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