Skip to content

Package: PreferenceHelper

PreferenceHelper

nameinstructionbranchcomplexitylinemethod
getPreference(String, String)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
setPreference(String, String)
M: 38 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2008-2011 Chair for Applied Software Engineering,
3: * Technische Universitaet Muenchen.
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: ******************************************************************************/
13: package org.eclipse.emf.ecp.internal.ui;
14:
15: import org.eclipse.core.runtime.Platform;
16: import org.eclipse.core.runtime.preferences.ConfigurationScope;
17: import org.osgi.service.prefs.BackingStoreException;
18:
19: /**
20: * The preference helper aids storing {key, value} pairs.
21: *
22: * @author pfeifferc
23: *
24: */
25: public final class PreferenceHelper {
26:
27:         private PreferenceHelper() {
28:                 // nothing to do here
29:         }
30:
31:         // TODO: ChainSaw namespace?
32:         private static final String PREFERENCE_NODE = "ecp"; //$NON-NLS-1$
33:
34:         /**
35:          * Get a preference value for a specific key.
36:          *
37:          * @param key the
38:          * @param defaultValue the
39:          * @return the value if it exists, otherwise the defaultValue
40:          */
41:         public static String getPreference(String key, String defaultValue) {
42:                 final String value = Platform.getPreferencesService().getRootNode().node(ConfigurationScope.SCOPE)
43:                         .node(PREFERENCE_NODE).get(key, defaultValue);
44:                 return value;
45:         }
46:
47:         /**
48:          * Set the preference value for a specific key. Key and value must not equal null.
49:          *
50:          * @param key the
51:          * @param value the
52:          */
53:         public static void setPreference(String key, String value) {
54:•                if (key != null && value != null) {
55:                         Platform.getPreferencesService().getRootNode().node(ConfigurationScope.SCOPE).node(PREFERENCE_NODE)
56:                                 .put(key, value);
57:                         try {
58:                                 Platform.getPreferencesService().getRootNode().node(ConfigurationScope.SCOPE).node(PREFERENCE_NODE)
59:                                         .flush();
60:                         } catch (final BackingStoreException e) {
61:                                 Activator.log(
62:                                         "Could not persist the preference change: {" + key + ", " + value + "}", e); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
63:                         }
64:                 }
65:         }
66:
67: }