]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/preferences/PreferencePropertyTester.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / preferences / PreferencePropertyTester.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.ui.workbench.preferences;\r
13 \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
19 \r
20 /**\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
23  * \r
24  * <p>\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
28  * \r
29  * <p>\r
30  * The tester expects to receive:\r
31  * <ul>\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
36  * </ul>\r
37  * \r
38  * @author Tuukka Lehtonen\r
39  * \r
40  * @see InstanceScope\r
41  * @see DefaultScope\r
42  */\r
43 public class PreferencePropertyTester extends PropertyTester {\r
44 \r
45     @Override\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
48 \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
55             else\r
56                 expected = (String) expectedValue;\r
57 \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
64             }\r
65             //System.out.println("TESTING VALUE: " + value);\r
66             return ObjectUtils.objectEquals(value, expected);\r
67         }\r
68 \r
69         return false;\r
70     }\r
71 \r
72 }\r