]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/preferences/PreferencePropertyTester.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / preferences / PreferencePropertyTester.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.ui.workbench.preferences;
13
14 import org.eclipse.core.expressions.PropertyTester;
15 import org.eclipse.core.runtime.preferences.DefaultScope;
16 import org.eclipse.core.runtime.preferences.InstanceScope;
17 import org.osgi.service.prefs.Preferences;
18 import org.simantics.utils.ObjectUtils;
19
20 /**
21  * A JFace property tester extension for the Eclipse command framework for
22  * testing Eclipse preference values according to the specified arguments.
23  * 
24  * <p>
25  * The tester first tries to find the specified property from the instance scope
26  * (see {@link InstanceScope}) and if no value is found, it falls back to using
27  * the default scope (see {@link DefaultScope})
28  * 
29  * <p>
30  * The tester expects to receive:
31  * <ul>
32  * <li><b>args</b>: two strings separated by comma, args[0] = bundle id, args[1] =
33  * preference to test</li>
34  * <li><b>expected value</b>: the value that the preference is expected to have to
35  * evaluate to <code>true</code></li>
36  * </ul>
37  * 
38  * @author Tuukka Lehtonen
39  * 
40  * @see InstanceScope
41  * @see DefaultScope
42  */
43 public class PreferencePropertyTester extends PropertyTester {
44
45     @Override
46     public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
47         //System.out.println("TEST: " + receiver + ", " + property + ", " + Arrays.toString(args) + ", " + expectedValue);
48
49         if (args.length == 2) {
50             String bundle = (String) args[0];
51             String preference = (String) args[1];
52             String expected = null;
53             if (expectedValue instanceof Boolean)
54                 expected = ((Boolean) expectedValue).toString();
55             else
56                 expected = (String) expectedValue;
57
58             Preferences prefs = InstanceScope.INSTANCE.getNode(bundle);
59             String value = prefs.get(preference, null);
60             if (value == null) {
61                 // Get the default value only if there is no instance value.
62                 prefs = DefaultScope.INSTANCE.getNode(bundle);
63                 value = prefs.get(preference, null);
64             }
65             //System.out.println("TESTING VALUE: " + value);
66             return ObjectUtils.objectEquals(value, expected);
67         }
68
69         return false;
70     }
71
72 }