KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > preferences > WorkingCopyPreferences


1 /*******************************************************************************
2  * Copyright (c) 2005, 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  *******************************************************************************/

11
12 package org.eclipse.ui.internal.preferences;
13
14 import java.util.Arrays JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.core.commands.common.EventManager;
21 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
22 import org.eclipse.core.runtime.preferences.IPreferenceNodeVisitor;
23 import org.osgi.service.prefs.BackingStoreException;
24 import org.osgi.service.prefs.Preferences;
25
26 /**
27  * Represents a working copy of a preference node, backed by the real node.
28  * <p>
29  * Note: Working copy nodes do not fire node change events.
30  * </p>
31  * <p>
32  * Note: Preference change listeners registered on this node will only receive
33  * events from this node and not events based on the original backing node.
34  * </p>
35  * @since 3.1
36  */

37 public class WorkingCopyPreferences extends EventManager implements
38         IEclipsePreferences {
39
40     private static final String JavaDoc TRUE = "true"; //$NON-NLS-1$
41

42     private final Map JavaDoc temporarySettings;
43     private final IEclipsePreferences original;
44     private boolean removed = false;
45     private org.eclipse.ui.preferences.WorkingCopyManager manager;
46
47     /**
48      * @param original the underlying preference node
49      * @param manager the working copy manager
50      */

51     public WorkingCopyPreferences(IEclipsePreferences original, org.eclipse.ui.preferences.WorkingCopyManager manager) {
52         super();
53         this.original = original;
54         this.manager = manager;
55         this.temporarySettings = new HashMap JavaDoc();
56     }
57
58     /*
59      * Convenience method for throwing an exception when methods
60      * are called on a removed node.
61      */

62     private void checkRemoved() {
63         if (removed) {
64             String JavaDoc message = "Preference node: " + absolutePath() + " has been removed."; //$NON-NLS-1$ //$NON-NLS-2$
65
throw new IllegalStateException JavaDoc(message);
66         }
67     }
68
69     /* (non-Javadoc)
70      * @see org.eclipse.core.runtime.preferences.IEclipsePreferences#addNodeChangeListener(org.eclipse.core.runtime.preferences.IEclipsePreferences.INodeChangeListener)
71      */

72     public void addNodeChangeListener(INodeChangeListener listener) {
73         // no-op - working copy nodes don't fire node change events
74
}
75
76     /* (non-Javadoc)
77      * @see org.eclipse.core.runtime.preferences.IEclipsePreferences#removeNodeChangeListener(org.eclipse.core.runtime.preferences.IEclipsePreferences.INodeChangeListener)
78      */

79     public void removeNodeChangeListener(INodeChangeListener listener) {
80         // no-op - working copy nodes don't fire node change events
81
}
82
83     /* (non-Javadoc)
84      * @see org.eclipse.core.runtime.preferences.IEclipsePreferences#addPreferenceChangeListener(org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener)
85      */

86     public void addPreferenceChangeListener(IPreferenceChangeListener listener) {
87         checkRemoved();
88         addListenerObject(listener);
89     }
90
91     /* (non-Javadoc)
92      * @see org.eclipse.core.runtime.preferences.IEclipsePreferences#removePreferenceChangeListener(org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener)
93      */

94     public void removePreferenceChangeListener(IPreferenceChangeListener listener) {
95         checkRemoved();
96         removeListenerObject(listener);
97     }
98
99     /* (non-Javadoc)
100      * @see org.osgi.service.prefs.Preferences#removeNode()
101      */

102     public void removeNode() throws BackingStoreException {
103         checkRemoved();
104
105         // clear all values (long way so people get notified)
106
String JavaDoc[] keys = keys();
107         for (int i = 0; i < keys.length; i++) {
108             remove(keys[i]);
109         }
110
111         // remove children
112
String JavaDoc[] childNames = childrenNames();
113         for (int i = 0; i < childNames.length; i++) {
114             node(childNames[i]).removeNode();
115         }
116
117         // mark as removed
118
removed = true;
119     }
120
121
122     /* (non-Javadoc)
123      * @see org.osgi.service.prefs.Preferences#node(java.lang.String)
124      */

125     public Preferences node(String JavaDoc path) {
126         checkRemoved();
127         return manager.getWorkingCopy((IEclipsePreferences) getOriginal().node(path));
128     }
129
130     /* (non-Javadoc)
131      * @see org.eclipse.core.runtime.preferences.IEclipsePreferences#accept(org.eclipse.core.runtime.preferences.IPreferenceNodeVisitor)
132      */

133     public void accept(IPreferenceNodeVisitor visitor) throws BackingStoreException {
134         checkRemoved();
135         if (!visitor.visit(this)) {
136             return;
137         }
138         String JavaDoc[] childNames = childrenNames();
139         for (int i = 0; i < childNames.length; i++) {
140             ((IEclipsePreferences) node(childNames[i])).accept(visitor);
141         }
142     }
143
144     /* (non-Javadoc)
145      * @see org.osgi.service.prefs.Preferences#put(java.lang.String, java.lang.String)
146      */

147     public void put(String JavaDoc key, String JavaDoc value) {
148         checkRemoved();
149         if (key == null || value == null) {
150             throw new NullPointerException JavaDoc();
151         }
152         String JavaDoc oldValue = null;
153         if (temporarySettings.containsKey(key)) {
154             oldValue = (String JavaDoc) temporarySettings.get(key);
155         } else {
156             oldValue = getOriginal().get(key, null);
157         }
158         temporarySettings.put(key, value);
159         if (!value.equals(oldValue)) {
160             firePropertyChangeEvent(key, oldValue, value);
161         }
162     }
163
164     private void firePropertyChangeEvent(String JavaDoc key, Object JavaDoc oldValue, Object JavaDoc newValue) {
165         Object JavaDoc[] listeners = getListeners();
166         if (listeners.length == 0) {
167             return;
168         }
169         PreferenceChangeEvent event = new PreferenceChangeEvent(this, key, oldValue, newValue);
170         for (int i = 0; i < listeners.length; i++) {
171             ((IPreferenceChangeListener) listeners[i]).preferenceChange(event);
172         }
173     }
174
175     /* (non-Javadoc)
176      * @see org.osgi.service.prefs.Preferences#get(java.lang.String, java.lang.String)
177      */

178     public String JavaDoc get(String JavaDoc key, String JavaDoc defaultValue) {
179         checkRemoved();
180         return internalGet(key, defaultValue);
181     }
182
183     private String JavaDoc internalGet(String JavaDoc key, String JavaDoc defaultValue) {
184         if (key == null) {
185             throw new NullPointerException JavaDoc();
186         }
187         if (temporarySettings.containsKey(key)) {
188             Object JavaDoc value = temporarySettings.get(key);
189             return value == null ? defaultValue : (String JavaDoc) value;
190         }
191         return getOriginal().get(key, defaultValue);
192     }
193
194     /* (non-Javadoc)
195      * @see org.osgi.service.prefs.Preferences#remove(java.lang.String)
196      */

197     public void remove(String JavaDoc key) {
198         checkRemoved();
199         if (key == null) {
200             throw new NullPointerException JavaDoc();
201         }
202         Object JavaDoc oldValue = null;
203         if (temporarySettings.containsKey(key)) {
204             oldValue = temporarySettings.get(key);
205         } else {
206             oldValue = original.get(key, null);
207         }
208         if (oldValue == null) {
209             return;
210         }
211         temporarySettings.put(key, null);
212         firePropertyChangeEvent(key, oldValue, null);
213     }
214
215     /* (non-Javadoc)
216      * @see org.osgi.service.prefs.Preferences#clear()
217      */

218     public void clear() {
219         checkRemoved();
220         for (Iterator JavaDoc i = temporarySettings.keySet().iterator(); i.hasNext();) {
221             String JavaDoc key = (String JavaDoc) i.next();
222             Object JavaDoc value = temporarySettings.get(key);
223             if (value != null) {
224                 temporarySettings.put(key, null);
225                 firePropertyChangeEvent(key, value, null);
226             }
227         }
228     }
229
230     /* (non-Javadoc)
231      * @see org.osgi.service.prefs.Preferences#putInt(java.lang.String, int)
232      */

233     public void putInt(String JavaDoc key, int value) {
234         checkRemoved();
235         if (key == null) {
236             throw new NullPointerException JavaDoc();
237         }
238         String JavaDoc oldValue = null;
239         if (temporarySettings.containsKey(key)) {
240             oldValue = (String JavaDoc) temporarySettings.get(key);
241         } else {
242             oldValue = getOriginal().get(key, null);
243         }
244         String JavaDoc newValue = Integer.toString(value);
245         temporarySettings.put(key, newValue);
246         if (!newValue.equals(oldValue)) {
247             firePropertyChangeEvent(key, oldValue, newValue);
248         }
249     }
250
251     /* (non-Javadoc)
252      * @see org.osgi.service.prefs.Preferences#getInt(java.lang.String, int)
253      */

254     public int getInt(String JavaDoc key, int defaultValue) {
255         checkRemoved();
256         String JavaDoc value = internalGet(key, null);
257         int result = defaultValue;
258         if (value != null) {
259             try {
260                 result = Integer.parseInt(value);
261             } catch (NumberFormatException JavaDoc e) {
262                 // use default
263
}
264         }
265         return result;
266     }
267
268     /* (non-Javadoc)
269      * @see org.osgi.service.prefs.Preferences#putLong(java.lang.String, long)
270      */

271     public void putLong(String JavaDoc key, long value) {
272         checkRemoved();
273         if (key == null) {
274             throw new NullPointerException JavaDoc();
275         }
276         String JavaDoc oldValue = null;
277         if (temporarySettings.containsKey(key)) {
278             oldValue = (String JavaDoc) temporarySettings.get(key);
279         } else {
280             oldValue = getOriginal().get(key, null);
281         }
282         String JavaDoc newValue = Long.toString(value);
283         temporarySettings.put(key, newValue);
284         if (!newValue.equals(oldValue)) {
285             firePropertyChangeEvent(key, oldValue, newValue);
286         }
287     }
288
289     /* (non-Javadoc)
290      * @see org.osgi.service.prefs.Preferences#getLong(java.lang.String, long)
291      */

292     public long getLong(String JavaDoc key, long defaultValue) {
293         checkRemoved();
294         String JavaDoc value = internalGet(key, null);
295         long result = defaultValue;
296         if (value != null) {
297             try {
298                 result = Long.parseLong(value);
299             } catch (NumberFormatException JavaDoc e) {
300                 // use default
301
}
302         }
303         return result;
304     }
305
306     /* (non-Javadoc)
307      * @see org.osgi.service.prefs.Preferences#putBoolean(java.lang.String, boolean)
308      */

309     public void putBoolean(String JavaDoc key, boolean value) {
310         checkRemoved();
311         if (key == null) {
312             throw new NullPointerException JavaDoc();
313         }
314         String JavaDoc oldValue = null;
315         if (temporarySettings.containsKey(key)) {
316             oldValue = (String JavaDoc) temporarySettings.get(key);
317         } else {
318             oldValue = getOriginal().get(key, null);
319         }
320         String JavaDoc newValue = String.valueOf(value);
321         temporarySettings.put(key, newValue);
322         if (!newValue.equalsIgnoreCase(oldValue)) {
323             firePropertyChangeEvent(key, oldValue, newValue);
324         }
325     }
326
327     /* (non-Javadoc)
328      * @see org.osgi.service.prefs.Preferences#getBoolean(java.lang.String, boolean)
329      */

330     public boolean getBoolean(String JavaDoc key, boolean defaultValue) {
331         checkRemoved();
332         String JavaDoc value = internalGet(key, null);
333         return value == null ? defaultValue : TRUE.equalsIgnoreCase(value);
334     }
335
336     /* (non-Javadoc)
337      * @see org.osgi.service.prefs.Preferences#putFloat(java.lang.String, float)
338      */

339     public void putFloat(String JavaDoc key, float value) {
340         checkRemoved();
341         if (key == null) {
342             throw new NullPointerException JavaDoc();
343         }
344         String JavaDoc oldValue = null;
345         if (temporarySettings.containsKey(key)) {
346             oldValue = (String JavaDoc) temporarySettings.get(key);
347         } else {
348             oldValue = getOriginal().get(key, null);
349         }
350         String JavaDoc newValue = Float.toString(value);
351         temporarySettings.put(key, newValue);
352         if (!newValue.equals(oldValue)) {
353             firePropertyChangeEvent(key, oldValue, newValue);
354         }
355     }
356
357     /* (non-Javadoc)
358      * @see org.osgi.service.prefs.Preferences#getFloat(java.lang.String, float)
359      */

360     public float getFloat(String JavaDoc key, float defaultValue) {
361         checkRemoved();
362         String JavaDoc value = internalGet(key, null);
363         float result = defaultValue;
364         if (value != null) {
365             try {
366                 result = Float.parseFloat(value);
367             } catch (NumberFormatException JavaDoc e) {
368                 // use default
369
}
370         }
371         return result;
372     }
373
374     /* (non-Javadoc)
375      * @see org.osgi.service.prefs.Preferences#putDouble(java.lang.String, double)
376      */

377     public void putDouble(String JavaDoc key, double value) {
378         checkRemoved();
379         if (key == null) {
380             throw new NullPointerException JavaDoc();
381         }
382         String JavaDoc oldValue = null;
383         if (temporarySettings.containsKey(key)) {
384             oldValue = (String JavaDoc) temporarySettings.get(key);
385         } else {
386             oldValue = getOriginal().get(key, null);
387         }
388         String JavaDoc newValue = Double.toString(value);
389         temporarySettings.put(key, newValue);
390         if (!newValue.equals(oldValue)) {
391             firePropertyChangeEvent(key, oldValue, newValue);
392         }
393     }
394
395     /* (non-Javadoc)
396      * @see org.osgi.service.prefs.Preferences#getDouble(java.lang.String, double)
397      */

398     public double getDouble(String JavaDoc key, double defaultValue) {
399         checkRemoved();
400         String JavaDoc value = internalGet(key, null);
401         double result = defaultValue;
402         if (value != null) {
403             try {
404                 result = Double.parseDouble(value);
405             } catch (NumberFormatException JavaDoc e) {
406                 // use default
407
}
408         }
409         return result;
410     }
411
412     /* (non-Javadoc)
413      * @see org.osgi.service.prefs.Preferences#putByteArray(java.lang.String, byte[])
414      */

415     public void putByteArray(String JavaDoc key, byte[] value) {
416         checkRemoved();
417         if (key == null || value == null) {
418             throw new NullPointerException JavaDoc();
419         }
420         String JavaDoc oldValue = null;
421         if (temporarySettings.containsKey(key)) {
422             oldValue = (String JavaDoc) temporarySettings.get(key);
423         } else {
424             oldValue = getOriginal().get(key, null);
425         }
426         String JavaDoc newValue = new String JavaDoc(Base64.encode(value));
427         temporarySettings.put(key, newValue);
428         if (!newValue.equals(oldValue)) {
429             firePropertyChangeEvent(key, oldValue, newValue);
430         }
431     }
432
433     /* (non-Javadoc)
434      * @see org.osgi.service.prefs.Preferences#getByteArray(java.lang.String, byte[])
435      */

436     public byte[] getByteArray(String JavaDoc key, byte[] defaultValue) {
437         checkRemoved();
438         String JavaDoc value = internalGet(key, null);
439         return value == null ? defaultValue : Base64.decode(value.getBytes());
440     }
441
442     /* (non-Javadoc)
443      * @see org.osgi.service.prefs.Preferences#keys()
444      */

445     public String JavaDoc[] keys() throws BackingStoreException {
446         checkRemoved();
447         HashSet JavaDoc allKeys = new HashSet JavaDoc(Arrays.asList(getOriginal().keys()));
448         allKeys.addAll(temporarySettings.keySet());
449         return (String JavaDoc[]) allKeys.toArray(new String JavaDoc[allKeys.size()]);
450     }
451
452     /* (non-Javadoc)
453      * @see org.osgi.service.prefs.Preferences#childrenNames()
454      */

455     public String JavaDoc[] childrenNames() throws BackingStoreException {
456         checkRemoved();
457         return getOriginal().childrenNames();
458     }
459
460     /* (non-Javadoc)
461      * @see org.osgi.service.prefs.Preferences#parent()
462      */

463     public Preferences parent() {
464         checkRemoved();
465         return manager.getWorkingCopy((IEclipsePreferences) getOriginal().parent());
466     }
467
468     /* (non-Javadoc)
469      * @see org.osgi.service.prefs.Preferences#nodeExists(java.lang.String)
470      */

471     public boolean nodeExists(String JavaDoc pathName) throws BackingStoreException {
472         // short circuit for this node
473
if (pathName.length() == 0) {
474             return removed ? false : getOriginal().nodeExists(pathName);
475         }
476         return getOriginal().nodeExists(pathName);
477     }
478
479     /* (non-Javadoc)
480      * @see org.osgi.service.prefs.Preferences#name()
481      */

482     public String JavaDoc name() {
483         return getOriginal().name();
484     }
485
486     /* (non-Javadoc)
487      * @see org.osgi.service.prefs.Preferences#absolutePath()
488      */

489     public String JavaDoc absolutePath() {
490         return getOriginal().absolutePath();
491     }
492
493     /* (non-Javadoc)
494      * @see org.osgi.service.prefs.Preferences#flush()
495      */

496     public void flush() throws BackingStoreException {
497         if (removed) {
498             getOriginal().removeNode();
499             return;
500         }
501         checkRemoved();
502         // update underlying preferences
503
for (Iterator JavaDoc i = temporarySettings.keySet().iterator(); i.hasNext();) {
504             String JavaDoc key = (String JavaDoc) i.next();
505             String JavaDoc value = (String JavaDoc) temporarySettings.get(key);
506             if (value == null) {
507                 getOriginal().remove(key);
508             } else {
509                 getOriginal().put(key, value);
510             }
511         }
512         // clear our settings
513
temporarySettings.clear();
514
515         // save the underlying preference store
516
getOriginal().flush();
517     }
518
519     /* (non-Javadoc)
520      * @see org.osgi.service.prefs.Preferences#sync()
521      */

522     public void sync() throws BackingStoreException {
523         checkRemoved();
524         // forget our settings
525
temporarySettings.clear();
526         // load the underlying preference store
527
getOriginal().sync();
528     }
529
530     /**
531      * @return Returns the original preference node.
532      */

533     private IEclipsePreferences getOriginal() {
534         return original;
535     }
536 }
537
Popular Tags