KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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 java.net.URL JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Set JavaDoc;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
19
20 /**
21  * @since 3.0
22  */

23 public class ConfigurationPreferences extends EclipsePreferences {
24
25     // cached values
26
private int segmentCount;
27     private String JavaDoc qualifier;
28     private IPath location;
29     private IEclipsePreferences loadLevel;
30     // cache which nodes have been loaded from disk
31
private static Set JavaDoc loadedNodes = new HashSet JavaDoc();
32     private static boolean initialized = false;
33     private static IPath baseLocation;
34
35     static {
36         URL JavaDoc url = PreferencesOSGiUtils.getDefault().getConfigurationLocation().getURL();
37         if (url != null)
38             baseLocation = new Path(url.getFile());
39     }
40
41     /**
42      * Default constructor. Should only be called by #createExecutableExtension.
43      */

44     public ConfigurationPreferences() {
45         this(null, null);
46     }
47
48     private ConfigurationPreferences(EclipsePreferences parent, String JavaDoc name) {
49         super(parent, name);
50
51         initializeChildren();
52
53         // cache the segment count
54
String JavaDoc path = absolutePath();
55         segmentCount = getSegmentCount(path);
56         if (segmentCount < 2)
57             return;
58
59         // cache the qualifier
60
qualifier = getSegment(path, 1);
61
62         // cache the location
63
if (qualifier == null)
64             return;
65         if (baseLocation != null)
66             location = computeLocation(baseLocation, qualifier);
67     }
68
69     protected IPath getLocation() {
70         return location;
71     }
72
73     protected boolean isAlreadyLoaded(IEclipsePreferences node) {
74         return loadedNodes.contains(node.name());
75     }
76
77     protected void loaded() {
78         loadedNodes.add(name());
79     }
80
81     /*
82      * Return the node at which these preferences are loaded/saved.
83      */

84     protected IEclipsePreferences getLoadLevel() {
85         if (loadLevel == null) {
86             if (qualifier == null)
87                 return null;
88             // Make it relative to this node rather than navigating to it from the root.
89
// Walk backwards up the tree starting at this node.
90
// This is important to avoid a chicken/egg thing on startup.
91
IEclipsePreferences node = this;
92             for (int i = 2; i < segmentCount; i++)
93                 node = (EclipsePreferences) node.parent();
94             loadLevel = node;
95         }
96         return loadLevel;
97     }
98
99     protected void initializeChildren() {
100         if (initialized || parent == null)
101             return;
102         try {
103             synchronized (this) {
104                 if (baseLocation == null)
105                     return;
106                 String JavaDoc[] names = computeChildren(baseLocation);
107                 for (int i = 0; i < names.length; i++)
108                     addChild(names[i], null);
109             }
110         } finally {
111             initialized = true;
112         }
113     }
114
115     protected EclipsePreferences internalCreate(EclipsePreferences nodeParent, String JavaDoc nodeName, Object JavaDoc context) {
116         return new ConfigurationPreferences(nodeParent, nodeName);
117     }
118 }
119
Popular Tags