download PreferenceStore.java
Language: Java
Copyright: (c) 2000, 2005 IBM Corporation and others.
LOC: 315
Project Info
Technology project(technology)
Server: Eclipse
Type: cvs
...g\eclipse\jface\preference\
   BooleanFieldEditor.java
   BooleanPropertyAction.java
   ColorFieldEditor.java
   ColorSelector.java
   DirectoryFieldEditor.java
   FieldEditor.java
   ...itorPreferencePage.java
   FileFieldEditor.java
   FontFieldEditor.java
   IntegerFieldEditor.java
   ...entPreferenceStore.java
   IPreferenceNode.java
   IPreferencePage.java
   ...rencePageContainer.java
   IPreferenceStore.java
   JFacePreferences.java
   ListEditor.java
   PathEditor.java
   ...nceContentProvider.java
   PreferenceConverter.java
   PreferenceDialog.java
   ...renceLabelProvider.java
   PreferenceManager.java
   PreferenceNode.java
   PreferencePage.java
   PreferenceStore.java
   RadioGroupFieldEditor.java
   ScaleFieldEditor.java
   ...gButtonFieldEditor.java
   StringFieldEditor.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.preference;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.util.Assert;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.ListenerList;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.util.SafeRunnable;

/**
 * A concrete preference store implementation based on an internal
 * <code>java.util.Properties</code> object, with support for persisting the
 * non-default preference values to files or streams.
 * <p>
 * This class was not designed to be subclassed.
 * </p>
 * 
 * @see IPreferenceStore
 */
public class PreferenceStore implements IPersistentPreferenceStore {
    /**
     * List of registered listeners (element type:
     * <code>IPropertyChangeListener</code>). These listeners are to be
     * informed when the current value of a preference changes.
     */
    private ListenerList listeners = new ListenerList();

    /**
     * The mapping from preference name to preference value (represented as
     * strings).
     */
    private Properties properties;

    /**
     * The mapping from preference name to default preference value (represented
     * as strings); <code>null</code> if none.
     */
    private Properties defaultProperties;

    /**
     * Indicates whether a value as been changed by <code>setToDefault</code>
     * or <code>setValue</code>; initially <code>false</code>.
     */
    private boolean dirty = false;

    /**
     * The file name used by the <code>load</code> method to load a property
     * file. This filename is used to save the properties file when
     * <code>save</code> is called.
     */
    private String filename;

    /**
     * Creates an empty preference store.
     * <p>
     * Use the methods <code>load(InputStream)</code> and
     * <code>save(InputStream)</code> to load and store this preference store.
     * </p>
     * 
     * @see #load(InputStream)
     * @see #save(OutputStream, String)
     */
    public PreferenceStore() {
        defaultProperties = new Properties();
        properties = new Properties(defaultProperties);
    }

    /**
     * Creates an empty preference store that loads from and saves to the a
     * file.
     * <p>
     * Use the methods <code>load()</code> and <code>save()</code> to load
     * and store this preference store.
     * </p>
     * 
     * @param filename
     *            the file name
     * @see #load()
     * @see #save()
     */
    public PreferenceStore(String filename) {
        this();
        Assert.isNotNull(filename);
        this.filename = filename;
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public void addPropertyChangeListener(IPropertyChangeListener listener) {
        listeners.add(listener);
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public boolean contains(String name) {
        return (properties.containsKey(name) || defaultProperties
                .containsKey(name));
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public void firePropertyChangeEvent(String name, Object oldValue,
            Object newValue) {
        final Object[] finalListeners = this.listeners.getListeners();
        // Do we need to fire an event.
        if (finalListeners.length > 0
                && (oldValue == null || !oldValue.equals(newValue))) {
            final PropertyChangeEvent pe = new PropertyChangeEvent(this, name,
                    oldValue, newValue);
            for (int i = 0; i < finalListeners.length; ++i) {
                final IPropertyChangeListener l = (IPropertyChangeListener) finalListeners[i];
                SafeRunnable.run(new SafeRunnable(JFaceResources.getString("PreferenceStore.changeError")) { //$NON-NLS-1$
                		public void run() {
  			                l.propertyChange(pe);
                		}
                });
            }
        }
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public boolean getBoolean(String name) {
        return getBoolean(properties, name);
    }

    /**
     * Helper function: gets boolean for a given name.
     * 
     * @param p
     * @param name
     * @return boolean
     */
    private boolean getBoolean(Properties p, String name) {
        String value = p != null ? p.getProperty(name) : null;
        if (value == null)
            return BOOLEAN_DEFAULT_DEFAULT;
        if (value.equals(IPreferenceStore.TRUE))
            return true;
        return false;
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public boolean getDefaultBoolean(String name) {
        return getBoolean(defaultProperties, name);
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public double getDefaultDouble(String name) {
        return getDouble(defaultProperties, name);
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public float getDefaultFloat(String name) {
        return getFloat(defaultProperties, name);
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public int getDefaultInt(String name) {
        return getInt(defaultProperties, name);
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public long getDefaultLong(String name) {
        return getLong(defaultProperties, name);
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public String getDefaultString(String name) {
        return getString(defaultProperties, name);
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public double getDouble(String name) {
        return getDouble(properties, name);
    }

    /**
     * Helper function: gets double for a given name.
     * 
     * @param p
     * @param name
     * @return double
     */
    private double getDouble(Properties p, String name) {
        String value = p != null ? p.getProperty(name) : null;
        if (value == null)
            return DOUBLE_DEFAULT_DEFAULT;
        double ival = DOUBLE_DEFAULT_DEFAULT;
        try {
            ival = new Double(value).doubleValue();
        } catch (NumberFormatException e) {
        }
        return ival;
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public float getFloat(String name) {
        return getFloat(properties, name);
    }

    /**
     * Helper function: gets float for a given name.
     * @param p
     * @param name
     * @return float
     */
    private float getFloat(Properties p, String name) {
        String value = p != null ? p.getProperty(name) : null;
        if (value == null)
            return FLOAT_DEFAULT_DEFAULT;
        float ival = FLOAT_DEFAULT_DEFAULT;
        try {
            ival = new Float(value).floatValue();
        } catch (NumberFormatException e) {
        }
        return ival;
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public int getInt(String name) {
        return getInt(properties, name);
    }

    /**
     * Helper function: gets int for a given name.
     * @param p
     * @param name
     * @return int
     */
    private int getInt(Properties p, String name) {
        String value = p != null ? p.getProperty(name) : null;
        if (value == null)
            return INT_DEFAULT_DEFAULT;
        int ival = 0;
        try {
            ival = Integer.parseInt(value);
        } catch (NumberFormatException e) {
        }
        return ival;
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public long getLong(String name) {
        return getLong(properties, name);
    }

    /**
     * Helper function: gets long for a given name.
     * @param p
     * @param name
     * @return
     */
    private long getLong(Properties p, String name) {
        String value = p != null ? p.getProperty(name) : null;
        if (value == null)
            return LONG_DEFAULT_DEFAULT;
        long ival = LONG_DEFAULT_DEFAULT;
        try {
            ival = Long.parseLong(value);
        } catch (NumberFormatException e) {
        }
        return ival;
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public String getString(String name) {
        return getString(properties, name);
    }

    /**
     * Helper function: gets string for a given name.
     * @param p
     * @param name
     * @return
     */
    private String getString(Properties p, String name) {
        String value = p != null ? p.getProperty(name) : null;
        if (value == null)
            return STRING_DEFAULT_DEFAULT;
        return value;
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public boolean isDefault(String name) {
        return (!properties.containsKey(name) && defaultProperties
                .containsKey(name));
    }

    /**
     * Prints the contents of this preference store to the given print stream.
     * 
     * @param out
     *            the print stream
     */
    public void list(PrintStream out) {
        properties.list(out);
    }

    /**
     * Prints the contents of this preference store to the given print writer.
     * 
     * @param out
     *            the print writer
     */
    public void list(PrintWriter out) {
        properties.list(out);
    }

    /**
     * Loads this preference store from the file established in the constructor
     * <code>PreferenceStore(java.lang.String)</code> (or by
     * <code>setFileName</code>). Default preference values are not affected.
     * 
     * @exception java.io.IOException
     *                if there is a problem loading this store
     */
    public void load() throws IOException {
        if (filename == null)
            throw new IOException("File name not specified");//$NON-NLS-1$
        FileInputStream in = new FileInputStream(filename);
        load(in);
        in.close();
    }

    /**
     * Loads this preference store from the given input stream. Default
     * preference values are not affected.
     * 
     * @param in
     *            the input stream
     * @exception java.io.IOException
     *                if there is a problem loading this store
     */
    public void load(InputStream in) throws IOException {
        properties.load(in);
        dirty = false;
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public boolean needsSaving() {
        return dirty;
    }

    /**
     * Returns an enumeration of all preferences known to this store which have
     * current values other than their default value.
     * 
     * @return an array of preference names
     */
    public String[] preferenceNames() {
        ArrayList list = new ArrayList();
        Enumeration it = properties.propertyNames();
        while (it.hasMoreElements()) {
            list.add(it.nextElement());
        }
        return (String[]) list.toArray(new String[list.size()]);
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public void putValue(String name, String value) {
        String oldValue = getString(name);
        if (oldValue == null || !oldValue.equals(value)) {
            setValue(properties, name, value);
            dirty = true;
        }
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public void removePropertyChangeListener(IPropertyChangeListener listener) {
        listeners.remove(listener);
    }

    /**
     * Saves the non-default-valued preferences known to this preference store
     * to the file from which they were originally loaded.
     * 
     * @exception java.io.IOException
     *                if there is a problem saving this store
     */
    public void save() throws IOException {
        if (filename == null)
            throw new IOException("File name not specified");//$NON-NLS-1$
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(filename);
            save(out, null);
        } finally {
            if (out != null)
                out.close();
        }
    }

    /**
     * Saves this preference store to the given output stream. The given string
     * is inserted as header information.
     * 
     * @param out
     *            the output stream
     * @param header
     *            the header
     * @exception java.io.IOException
     *                if there is a problem saving this store
     */
    public void save(OutputStream out, String header) throws IOException {
        properties.store(out, header);
        dirty = false;
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public void setDefault(String name, double value) {
        setValue(defaultProperties, name, value);
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public void setDefault(String name, float value) {
        setValue(defaultProperties, name, value);
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public void setDefault(String name, int value) {
        setValue(defaultProperties, name, value);
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public void setDefault(String name, long value) {
        setValue(defaultProperties, name, value);
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public void setDefault(String name, String value) {
        setValue(defaultProperties, name, value);
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public void setDefault(String name, boolean value) {
        setValue(defaultProperties, name, value);
    }

    /**
     * Sets the name of the file used when loading and storing this preference
     * store.
     * <p>
     * Afterward, the methods <code>load()</code> and <code>save()</code>
     * can be used to load and store this preference store.
     * </p>
     * 
     * @param name the file name
     * @see #load()
     * @see #save()
     */
    public void setFilename(String name) {
        filename = name;
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public void setToDefault(String name) {
        Object oldValue = properties.get(name);
        properties.remove(name);
        dirty = true;
        Object newValue = null;
        if (defaultProperties != null)
            newValue = defaultProperties.get(name);
        firePropertyChangeEvent(name, oldValue, newValue);
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public void setValue(String name, double value) {
        double oldValue = getDouble(name);
        if (oldValue != value) {
            setValue(properties, name, value);
            dirty = true;
            firePropertyChangeEvent(name, new Double(oldValue), new Double(
                    value));
        }
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public void setValue(String name, float value) {
        float oldValue = getFloat(name);
        if (oldValue != value) {
            setValue(properties, name, value);
            dirty = true;
            firePropertyChangeEvent(name, new Float(oldValue), new Float(value));
        }
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public void setValue(String name, int value) {
        int oldValue = getInt(name);
        if (oldValue != value) {
            setValue(properties, name, value);
            dirty = true;
            firePropertyChangeEvent(name, new Integer(oldValue), new Integer(
                    value));
        }
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public void setValue(String name, long value) {
        long oldValue = getLong(name);
        if (oldValue != value) {
            setValue(properties, name, value);
            dirty = true;
            firePropertyChangeEvent(name, new Long(oldValue), new Long(value));
        }
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public void setValue(String name, String value) {
        String oldValue = getString(name);
        if (oldValue == null || !oldValue.equals(value)) {
            setValue(properties, name, value);
            dirty = true;
            firePropertyChangeEvent(name, oldValue, value);
        }
    }

    /*
     * (non-Javadoc) Method declared on IPreferenceStore.
     */
    public void setValue(String name, boolean value) {
        boolean oldValue = getBoolean(name);
        if (oldValue != value) {
            setValue(properties, name, value);
            dirty = true;
            firePropertyChangeEvent(name, new Boolean(oldValue), new Boolean(
                    value));
        }
    }

    /**
     * Helper method: sets value for a given name.
     * @param p
     * @param name
     * @param value
     */
    private void setValue(Properties p, String name, double value) {
        Assert.isTrue(p != null);
        p.put(name, Double.toString(value));
    }

    /**
     * Helper method: sets value for a given name.
     * @param p
     * @param name
     * @param value
     */
    private void setValue(Properties p, String name, float value) {
        Assert.isTrue(p != null);
        p.put(name, Float.toString(value));
    }

    /**
     * Helper method: sets value for a given name.
     * @param p
     * @param name
     * @param value
     */
    private void setValue(Properties p, String name, int value) {
        Assert.isTrue(p != null);
        p.put(name, Integer.toString(value));
    }

    /**
     * Helper method: sets the value for a given name.
     * @param p
     * @param name
     * @param value
     */
    private void setValue(Properties p, String name, long value) {
        Assert.isTrue(p != null);
        p.put(name, Long.toString(value));
    }

    /**
     * Helper method: sets the value for a given name.
     * @param p
     * @param name
     * @param value
     */
    private void setValue(Properties p, String name, String value) {
        Assert.isTrue(p != null && value != null);
        p.put(name, value);
    }

    /**
     * Helper method: sets the value for a given name.
     * @param p
     * @param name
     * @param value
     */
    private void setValue(Properties p, String name, boolean value) {
        Assert.isTrue(p != null);
        p.put(name, value == true ? IPreferenceStore.TRUE
                : IPreferenceStore.FALSE);
    }
}

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us