Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 44ccc7e97713d4762e9726d74816f61fb77035d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * Copyright (c) 2013, 2014 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.releng.preferences.util;

import org.eclipse.emf.cdo.releng.preferences.PreferenceNode;
import org.eclipse.emf.cdo.releng.preferences.PreferencesFactory;
import org.eclipse.emf.cdo.releng.preferences.Property;

import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.NodeChangeEvent;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;

import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author Eike Stepper
 */
public final class PreferencesUtil
{
  /**
   * A resource load option to load an instance using {@link #getRootPreferenceNode(boolean) PreferencesUtil.getRootPreferenceNode(true)}.
   * The resource must be {@link Resource#unload() unloaded}, to avoid dangling listeners.
   */
  public static final String OPTION_SYNCHRONIZED_PREFERENCES = "SYNCHRONIZED_PREFERENCES";

  private static final IEclipsePreferences ROOT = Platform.getPreferencesService().getRootNode();

  public static final URI ROOT_PREFERENCE_NODE_URI = URI.createURI("preference:/");

  private static class PreferencesAdapter extends AdapterImpl implements IEclipsePreferences.INodeChangeListener,
      IEclipsePreferences.IPreferenceChangeListener
  {
    protected IEclipsePreferences preferences;

    public PreferencesAdapter(IEclipsePreferences preferences)
    {
      this.preferences = preferences;

      preferences.addNodeChangeListener(this);
      preferences.addPreferenceChangeListener(this);
    }

    @Override
    public boolean isAdapterForType(Object type)
    {
      return type == PreferencesAdapter.class;
    }

    public void preferenceChange(PreferenceChangeEvent event)
    {
      PreferenceNode preferenceNode = (PreferenceNode)target;
      String name = event.getKey();
      Object value = event.getNewValue();
      EList<Property> properties = preferenceNode.getProperties();
      for (int i = 0, size = properties.size(); i < size; ++i)
      {
        Property property = properties.get(i);
        int comparison = property.getName().compareTo(name);
        if (comparison == 0)
        {
          if (value == null)
          {
            properties.remove(i);
          }
          else
          {
            property.setValue(value.toString());
          }
          return;
        }
        else if (comparison > 0)
        {
          if (value != null)
          {
            property = PreferencesFactory.eINSTANCE.createProperty();
            property.setName(name);
            property.setValue(value.toString());
            properties.add(i, property);
          }
          return;
        }
      }
      Property property = PreferencesFactory.eINSTANCE.createProperty();
      property.setName(name);
      property.setValue(value.toString());
      properties.add(property);
    }

    public void added(NodeChangeEvent event)
    {
      PreferenceNode preferenceNode = (PreferenceNode)target;
      Preferences childNode = event.getChild();
      PreferenceNode childPreferenceNode = PreferencesFactory.eINSTANCE.createPreferenceNode();
      String name = childNode.name();
      childPreferenceNode.setName(name);
      traverse(childPreferenceNode, childNode, true);
      EList<PreferenceNode> children = preferenceNode.getChildren();
      for (int i = 0, size = children.size(); i < size; ++i)
      {
        PreferenceNode otherChildPreferenceNode = children.get(i);
        if (otherChildPreferenceNode.getName().compareTo(name) >= 0)
        {
          children.add(i, childPreferenceNode);
          return;
        }
      }
      children.add(childPreferenceNode);
    }

    public void removed(NodeChangeEvent event)
    {
      PreferenceNode preferenceNode = (PreferenceNode)target;
      Preferences childNode = event.getChild();
      String name = childNode.name();
      EList<PreferenceNode> children = preferenceNode.getChildren();
      for (int i = 0, size = children.size(); i < size; ++i)
      {
        PreferenceNode childPreferenceNode = children.get(i);
        if (childPreferenceNode.getName().equals(name))
        {
          children.remove(i);
          return;
        }
      }
    }

    @Override
    public void unsetTarget(Notifier oldTarget)
    {
      super.unsetTarget(oldTarget);

      preferences.removeNodeChangeListener(this);
      preferences.removePreferenceChangeListener(this);
    }
  }

  public static PreferenceNode getRootPreferenceNode()
  {
    return getRootPreferenceNode(false);
  }

  public static PreferenceNode getRootPreferenceNode(boolean isSynchronized)
  {
    ResourceSet resourceSet = new ResourceSetImpl();
    Resource resource = resourceSet.createResource(ROOT_PREFERENCE_NODE_URI.appendSegment("*.preferences"));
    PreferenceNode root = PreferencesFactory.eINSTANCE.createPreferenceNode();
    traverse(root, ROOT, isSynchronized);

    int index = 0;
    for (String name : new String[] { "bundle_defaults", "default", "configuration", "instance", "project" })
    {
      PreferenceNode node = root.getNode(name);
      if (node != null)
      {
        root.getChildren().move(index++, node);
      }
    }

    resource.getContents().add(root);

    return root;
  }

  private static void traverse(PreferenceNode preferenceNode, Preferences node, boolean isSynchronized)
  {
    try
    {
      if (isSynchronized && node instanceof IEclipsePreferences)
      {
        preferenceNode.eAdapters().add(new PreferencesAdapter((IEclipsePreferences)node));
      }

      preferenceNode.setName(node.name());

      EList<PreferenceNode> children = preferenceNode.getChildren();
      String[] childrenNames = node.childrenNames();
      Arrays.sort(childrenNames);
      for (String name : childrenNames)
      {
        Preferences childNode = node.node(name);
        PreferenceNode childPreferenceNode = PreferencesFactory.eINSTANCE.createPreferenceNode();
        traverse(childPreferenceNode, childNode, isSynchronized);
        children.add(childPreferenceNode);
      }
      EList<Property> properties = preferenceNode.getProperties();
      String[] keys = node.keys();
      Arrays.sort(keys);
      for (String name : keys)
      {
        Property property = PreferencesFactory.eINSTANCE.createProperty();
        property.setName(name);
        property.setValue(node.get(name, null));
        properties.add(property);
      }
    }
    catch (BackingStoreException ex)
    {
      // Ignore
    }
  }

  public static Preferences getPreferences(PreferenceNode preferenceNode, boolean demandCreate)
      throws BackingStoreException
  {
    if (preferenceNode == null)
    {
      return ROOT;
    }

    Preferences parentPreferences = getPreferences(preferenceNode.getParent(), demandCreate);
    if (parentPreferences != null)
    {
      String name = preferenceNode.getName();
      if (demandCreate || parentPreferences.nodeExists(name))
      {
        return parentPreferences.node(name);
      }
    }
    return null;
  }

  public static IPath getLocation(Preferences preferences)
  {
    if (preferences == null)
    {
      return null;
    }

    try
    {
      Method getLocationMethod = preferences.getClass().getDeclaredMethod("getLocation");
      getLocationMethod.setAccessible(true);
      IPath location = (IPath)getLocationMethod.invoke(preferences);
      return location;
    }
    catch (Exception ex)
    {
      // Ignore
    }

    return null;
  }

  public static class PreferenceProperty
  {
    private Preferences node;

    private String property;

    public PreferenceProperty(String preferencePropertyPath)
    {
      node = Platform.getPreferencesService().getRootNode();

      String[] segments = preferencePropertyPath.split("/");
      StringBuilder property = null;
      boolean startProperty = false;
      for (int i = 0; i < segments.length - 1; i++)
      {
        String segment = segments[i];
        if (property != null)
        {
          if (startProperty)
          {
            property.append('/');
          }
          else
          {
            startProperty = true;
          }

          property.append(segment);
        }
        else if (i != 0 && segment.length() == 0)
        {
          property = new StringBuilder();
        }
        else
        {
          node = node.node(segment);
        }
      }

      if (property == null)
      {
        this.property = segments[segments.length - 1];
      }
      else
      {
        this.property = property.append('/').append(segments[segments.length - 1]).toString();
      }
    }

    public Preferences getNode()
    {
      return node;
    }

    public String getProperty()
    {
      return property;
    }

    public void set(String value)
    {
      if (value == null)
      {
        remove();
      }
      node.put(property, value);
    }

    public String get(String defaultValue)
    {
      return node.get(property, defaultValue);
    }

    public void remove()
    {
      node.remove(property);
    }

    public void setInt(int value)
    {
      node.putInt(property, value);
    }

    public int getInt(int defaultValue)
    {
      return node.getInt(property, defaultValue);
    }

    public void setLong(long value)
    {
      node.putLong(property, value);
    }

    public long getLong(long defaultValue)
    {
      return node.getLong(property, defaultValue);
    }

    public void setBoolean(boolean value)
    {
      node.putBoolean(property, value);
    }

    public boolean getBoolean(boolean defaultValue)
    {
      return node.getBoolean(property, defaultValue);
    }

    public void setFloat(float value)
    {
      node.putFloat(property, value);
    }

    public float getFloat(float defaultValue)
    {
      return node.getFloat(property, defaultValue);
    }

    public void setDouble(double value)
    {
      node.putDouble(property, value);
    }

    public double getDouble(double defaultValue)
    {
      return node.getDouble(property, defaultValue);
    }

    public void setByteArray(byte[] value)
    {
      if (value == null)
      {
        remove();
      }
      else
      {
        node.putByteArray(property, value);
      }
    }

    public byte[] getByteArray(byte[] defaultValue)
    {
      return node.getByteArray(property, defaultValue);
    }
  }
}

Back to the top