KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > preferences > RootPreferences


1 /*******************************************************************************
2  * Copyright (c) 2004, 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 package org.eclipse.core.internal.preferences;
12
13 import org.eclipse.core.runtime.IPath;
14 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
15 import org.osgi.service.prefs.BackingStoreException;
16 import org.osgi.service.prefs.Preferences;
17
18 /**
19  * @since 3.0
20  */

21 public class RootPreferences extends EclipsePreferences {
22
23     /**
24      * Default constructor.
25      */

26     public RootPreferences() {
27         super(null, ""); //$NON-NLS-1$
28
}
29
30     /*
31      * @see org.osgi.service.prefs.Preferences#flush()
32      */

33     public void flush() throws BackingStoreException {
34         // flush all children
35
BackingStoreException exception = null;
36         String JavaDoc[] names = childrenNames();
37         for (int i = 0; i < names.length; i++) {
38             try {
39                 node(names[i]).flush();
40             } catch (BackingStoreException e) {
41                 // store the first exception we get and still try and flush
42
// the rest of the children.
43
if (exception == null)
44                     exception = e;
45             }
46         }
47         if (exception != null)
48             throw exception;
49     }
50
51     /*
52      * @see EclipsePreferences#getChild(String, Plugin)
53      */

54     protected synchronized IEclipsePreferences getChild(String JavaDoc key, Object JavaDoc context) {
55         Object JavaDoc value = null;
56         IEclipsePreferences child = null;
57         if (children != null)
58             value = children.get(key);
59         if (value != null) {
60             if (value instanceof IEclipsePreferences)
61                 return (IEclipsePreferences) value;
62             //lazy initialization
63
child = PreferencesService.getDefault().createNode(key);
64             addChild(key, child);
65         }
66         return child;
67     }
68
69     /*
70      * @see EclipsePreferences#getChildren()
71      */

72     protected synchronized IEclipsePreferences[] getChildren() {
73         //must perform lazy initialization of child nodes
74
String JavaDoc[] childNames = childrenNames();
75         IEclipsePreferences[] childNodes = new IEclipsePreferences[childNames.length];
76         for (int i = 0; i < childNames.length; i++)
77             childNodes[i] = getChild(childNames[i], null);
78         return childNodes;
79     }
80
81     /*
82      * @see Preferences#node(String)
83      */

84     public Preferences node(String JavaDoc path) {
85         return getNode(path, true); // create if not found
86
}
87
88     public Preferences getNode(String JavaDoc path, boolean create) {
89         if (path.length() == 0 || (path.length() == 1 && path.charAt(0) == IPath.SEPARATOR))
90             return this;
91         int startIndex = path.charAt(0) == IPath.SEPARATOR ? 1 : 0;
92         int endIndex = path.indexOf(IPath.SEPARATOR, startIndex + 1);
93         String JavaDoc scope = path.substring(startIndex, endIndex == -1 ? path.length() : endIndex);
94         IEclipsePreferences child;
95         if (create) {
96             child = getChild(scope, null);
97             if (child == null) {
98                 child = new EclipsePreferences(this, scope);
99                 addChild(scope, child);
100             }
101         } else {
102             child = getChild(scope, null, false);
103             if (child == null)
104                 return null;
105         }
106         return child.node(endIndex == -1 ? "" : path.substring(endIndex + 1)); //$NON-NLS-1$
107
}
108
109     /*
110      * @see org.osgi.service.prefs.Preferences#sync()
111      */

112     public void sync() throws BackingStoreException {
113         // sync all children
114
BackingStoreException exception = null;
115         String JavaDoc[] names = childrenNames();
116         for (int i = 0; i < names.length; i++) {
117             try {
118                 node(names[i]).sync();
119             } catch (BackingStoreException e) {
120                 // store the first exception we get and still try and sync
121
// the rest of the children.
122
if (exception == null)
123                     exception = e;
124             }
125         }
126         if (exception != null)
127             throw exception;
128     }
129 }
130
Popular Tags