001: package net.refractions.udig.style.sld.editor;
002:
003: import java.io.InputStream;
004: import java.io.OutputStream;
005: import java.util.Collections;
006: import java.util.HashMap;
007: import java.util.Map;
008:
009: import org.eclipse.core.runtime.CoreException;
010: import org.eclipse.core.runtime.IConfigurationElement;
011: import org.eclipse.core.runtime.IExtension;
012: import org.eclipse.core.runtime.IExtensionPoint;
013: import org.eclipse.core.runtime.IRegistryChangeEvent;
014: import org.eclipse.core.runtime.IRegistryChangeListener;
015: import org.eclipse.core.runtime.IStatus;
016: import org.eclipse.core.runtime.Platform;
017: import org.eclipse.core.runtime.preferences.IEclipsePreferences;
018: import org.eclipse.core.runtime.preferences.IExportedPreferences;
019: import org.eclipse.core.runtime.preferences.IPreferenceFilter;
020: import org.eclipse.core.runtime.preferences.IPreferencesService;
021: import org.eclipse.core.runtime.preferences.IScope;
022: import org.eclipse.core.runtime.preferences.IScopeContext;
023: import org.osgi.service.prefs.Preferences;
024:
025: /**
026: * TODO Purpose of
027: * <p>
028: * Yes, I know we aren't supposed to implement IPreferencesService, but this isn't a regular
029: * circumstance; since we want to have the same functionality as the preference dialog without being
030: * the preference dialog, this seems appropriate.
031: * </p>
032: *
033: * @author chorner
034: * @since 1.1
035: */
036: public class SLDPreferencesService implements IPreferencesService,
037: IRegistryChangeListener {
038:
039: private static final String ATTRIBUTE_CLASS = "class"; //$NON-NLS-1$
040: private static final String ATTRIBUTE_NAME = "name"; //$NON-NLS-1$
041: private static final String ELEMENT_SCOPE = "scope"; //$NON-NLS-1$
042:
043: private static SLDPreferencesService instance;
044: static final SLDRootPreferences root = new SLDRootPreferences();
045: private static final Map<String, Object> scopeRegistry = Collections
046: .synchronizedMap(new HashMap<String, Object>());
047:
048: public SLDPreferencesService() {
049: super ();
050: initializeScopes();
051: }
052:
053: public String get(String key, String defaultValue,
054: Preferences[] nodes) {
055: return null;
056: }
057:
058: public boolean getBoolean(String qualifier, String key,
059: boolean defaultValue, IScopeContext[] contexts) {
060: return false;
061: }
062:
063: public byte[] getByteArray(String qualifier, String key,
064: byte[] defaultValue, IScopeContext[] contexts) {
065: return null;
066: }
067:
068: public double getDouble(String qualifier, String key,
069: double defaultValue, IScopeContext[] contexts) {
070: return 0;
071: }
072:
073: public float getFloat(String qualifier, String key,
074: float defaultValue, IScopeContext[] contexts) {
075: return 0;
076: }
077:
078: public int getInt(String qualifier, String key, int defaultValue,
079: IScopeContext[] contexts) {
080: return 0;
081: }
082:
083: public long getLong(String qualifier, String key,
084: long defaultValue, IScopeContext[] contexts) {
085: return 0;
086: }
087:
088: public String getString(String qualifier, String key,
089: String defaultValue, IScopeContext[] contexts) {
090: return null;
091: }
092:
093: public IEclipsePreferences getRootNode() {
094: return null;
095: }
096:
097: public IStatus exportPreferences(IEclipsePreferences node,
098: OutputStream output, String[] excludesList)
099: throws CoreException {
100: return null;
101: }
102:
103: public IStatus importPreferences(InputStream input)
104: throws CoreException {
105: return null;
106: }
107:
108: public IStatus applyPreferences(IExportedPreferences preferences)
109: throws CoreException {
110: return null;
111: }
112:
113: public IExportedPreferences readPreferences(InputStream input)
114: throws CoreException {
115: return null;
116: }
117:
118: public String[] getDefaultLookupOrder(String qualifier, String key) {
119: return null;
120: }
121:
122: public String[] getLookupOrder(String qualifier, String key) {
123: return null;
124: }
125:
126: public void setDefaultLookupOrder(String qualifier, String key,
127: String[] order) {
128: }
129:
130: public void exportPreferences(IEclipsePreferences node,
131: IPreferenceFilter[] filters, OutputStream output)
132: throws CoreException {
133: }
134:
135: public IPreferenceFilter[] matches(IEclipsePreferences node,
136: IPreferenceFilter[] filters) throws CoreException {
137: return null;
138: }
139:
140: public void applyPreferences(IEclipsePreferences node,
141: IPreferenceFilter[] filters) throws CoreException {
142: }
143:
144: /**
145: * See who is plugged into the extension point.
146: */
147: private void initializeScopes() {
148: IExtensionPoint point = Platform.getExtensionRegistry()
149: .getExtensionPoint(StyleEditor.ID);
150: if (point == null)
151: return;
152: IExtension[] extensions = point.getExtensions();
153: for (int i = 0; i < extensions.length; i++) {
154: IConfigurationElement[] elements = extensions[i]
155: .getConfigurationElements();
156: for (int j = 0; j < elements.length; j++)
157: if (ELEMENT_SCOPE.equalsIgnoreCase(elements[j]
158: .getName()))
159: scopeAdded(elements[j]);
160: }
161: Platform.getExtensionRegistry().addRegistryChangeListener(this ,
162: StyleEditor.ID);
163: }
164:
165: /*
166: * Abstracted into a separate method to prepare for dynamic awareness.
167: */
168: static void scopeAdded(IConfigurationElement element) {
169: String key = element.getAttribute(ATTRIBUTE_NAME);
170: if (key == null) {
171: // String message = NLS.bind(Messages.preferences_missingScopeAttribute, element.getDeclaringExtension().getUniqueIdentifier());
172: // log(createStatusWarning(message, null));
173: return;
174: }
175: scopeRegistry.put(key, element);
176: root.addChild(key, null);
177: }
178:
179: public void registryChanged(IRegistryChangeEvent event) {
180: }
181:
182: protected IEclipsePreferences createNode(String name) {
183: IScope scope = null;
184: Object value = scopeRegistry.get(name);
185: if (value instanceof IConfigurationElement) {
186: try {
187: scope = (IScope) ((IConfigurationElement) value)
188: .createExecutableExtension(ATTRIBUTE_CLASS);
189: scopeRegistry.put(name, scope);
190: } catch (ClassCastException e) {
191: // log(createStatusError(Messages.preferences_classCastScope, e));
192: return new SLDPreferences(root, name);
193: } catch (CoreException e) {
194: log(e.getStatus());
195: return new SLDPreferences(root, name);
196: }
197: } else
198: scope = (IScope) value;
199: return scope.create(root, name);
200: }
201:
202: /*
203: * Return the instance.
204: */
205: public static SLDPreferencesService getDefault() {
206: if (instance == null)
207: instance = new SLDPreferencesService();
208: return instance;
209: }
210:
211: static void log(IStatus status) {
212: //InternalPlatform.getDefault().log(status);
213: }
214:
215: }
|