001: package net.refractions.udig.style.sld.editor;
002:
003: import org.eclipse.core.runtime.IPath;
004: import org.eclipse.core.runtime.Platform;
005: import org.eclipse.core.runtime.Plugin;
006: import org.eclipse.core.runtime.preferences.IEclipsePreferences;
007: import org.osgi.service.prefs.BackingStoreException;
008: import org.osgi.service.prefs.Preferences;
009:
010: public class SLDRootPreferences extends SLDPreferences {
011:
012: /**
013: * Default constructor.
014: */
015: public SLDRootPreferences() {
016: super (null, ""); //$NON-NLS-1$
017: }
018:
019: /*
020: * @see org.osgi.service.prefs.Preferences#flush()
021: */
022: public void flush() throws BackingStoreException {
023: // flush all children
024: BackingStoreException exception = null;
025: String[] names = childrenNames();
026: for (int i = 0; i < names.length; i++) {
027: try {
028: node(names[i]).flush();
029: } catch (BackingStoreException e) {
030: // store the first exception we get and still try and flush
031: // the rest of the children.
032: if (e != null)
033: exception = e;
034: }
035: }
036: if (exception != null)
037: throw exception;
038: }
039:
040: /*
041: * @see EclipsePreferences#getChild(String, Plugin)
042: */
043: protected synchronized IEclipsePreferences getChild(String key,
044: Plugin context) {
045: Object value = null;
046: IEclipsePreferences child = null;
047: if (children != null)
048: value = children.get(key);
049: if (value != null) {
050: if (value instanceof IEclipsePreferences)
051: return (IEclipsePreferences) value;
052: //lazy initialization
053: child = ((SLDPreferencesService) Platform
054: .getPreferencesService()).createNode(key);
055: addChild(key, child);
056: }
057: return child;
058: }
059:
060: /*
061: * @see EclipsePreferences#getChildren()
062: */
063: protected synchronized IEclipsePreferences[] getChildren()
064: throws BackingStoreException {
065: //must perform lazy initialization of child nodes
066: String[] childNames = childrenNames();
067: IEclipsePreferences[] childNodes = new IEclipsePreferences[childNames.length];
068: for (int i = 0; i < childNames.length; i++)
069: childNodes[i] = getChild(childNames[i], null);
070: return childNodes;
071: }
072:
073: /*
074: * @see Preferences#node(String)
075: */
076: public Preferences node(String path) {
077: if (path.length() == 0
078: || (path.length() == 1 && path.charAt(0) == IPath.SEPARATOR))
079: return this ;
080: int startIndex = path.charAt(0) == IPath.SEPARATOR ? 1 : 0;
081: int endIndex = path.indexOf(IPath.SEPARATOR, startIndex + 1);
082: String scope = path.substring(startIndex, endIndex == -1 ? path
083: .length() : endIndex);
084: IEclipsePreferences child = getChild(scope, null);
085: if (child == null) {
086: child = new SLDPreferences(this , scope);
087: addChild(scope, child);
088: }
089: return child
090: .node(endIndex == -1 ? "" : path.substring(endIndex + 1)); //$NON-NLS-1$
091: }
092:
093: /*
094: * @see org.osgi.service.prefs.Preferences#sync()
095: */
096: public void sync() throws BackingStoreException {
097: // sync all children
098: BackingStoreException exception = null;
099: String[] names = childrenNames();
100: for (int i = 0; i < names.length; i++) {
101: try {
102: node(names[i]).sync();
103: } catch (BackingStoreException e) {
104: // store the first exception we get and still try and sync
105: // the rest of the children.
106: if (e != null)
107: exception = e;
108: }
109: }
110: if (exception != null)
111: throw exception;
112: }
113: }
|