1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.ui.workbench.preferences;
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;
21 * A JFace property tester extension for the Eclipse command framework for
22 * testing Eclipse preference values according to the specified arguments.
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})
30 * The tester expects to receive:
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>
38 * @author Tuukka Lehtonen
43 public class PreferencePropertyTester extends PropertyTester {
46 public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
47 //System.out.println("TEST: " + receiver + ", " + property + ", " + Arrays.toString(args) + ", " + expectedValue);
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();
56 expected = (String) expectedValue;
58 Preferences prefs = InstanceScope.INSTANCE.getNode(bundle);
59 String value = prefs.get(preference, 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);
65 //System.out.println("TESTING VALUE: " + value);
66 return ObjectUtils.objectEquals(value, expected);