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