]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/tests/InstanceOfTest.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / tests / InstanceOfTest.java
1 package org.simantics.browsing.ui.model.tests;\r
2 \r
3 import org.eclipse.core.runtime.Platform;\r
4 import org.osgi.framework.Bundle;\r
5 import org.simantics.databoard.Bindings;\r
6 import org.simantics.db.ReadGraph;\r
7 import org.simantics.db.Resource;\r
8 import org.simantics.db.exception.DatabaseException;\r
9 import org.simantics.viewpoint.ontology.ViewpointResource;\r
10 \r
11 /**\r
12  * A visual contribution rule test that checks whether the input content is an\r
13  * instance of a Java class. The class is specified by identifying the source\r
14  * bundle and the class name.\r
15  * \r
16  * @author Tuukka Lehtonen\r
17  */\r
18 public class InstanceOfTest implements Test {\r
19 \r
20     String   bundleName;\r
21     String   className;\r
22     Class<?> clazz;\r
23     boolean  failed;\r
24 \r
25     public InstanceOfTest(String bundle, String className) {\r
26         this.bundleName = bundle;\r
27         this.className = className;\r
28     }\r
29 \r
30     public InstanceOfTest(ReadGraph graph, Resource test) throws DatabaseException {\r
31         ViewpointResource VP = ViewpointResource.getInstance(graph);\r
32         this.bundleName = graph.getRelatedValue(test, VP.InstanceOfTest_bundleName, Bindings.STRING);\r
33         this.className = graph.getRelatedValue(test, VP.InstanceOfTest_className, Bindings.STRING);\r
34     }\r
35 \r
36     private Class<?> resolveClass() {\r
37         if (failed)\r
38             return null;\r
39         if (clazz != null)\r
40             return clazz;\r
41         Bundle b = Platform.getBundle(bundleName);\r
42         if (b == null) {\r
43             System.err.println(getClass() + " could not resolve class " + className + ", bundle " + bundleName + " not found in platform");\r
44             failed = true;\r
45             return null;\r
46         }\r
47         try {\r
48             this.clazz = b.loadClass(className);\r
49             return clazz;\r
50         } catch (ClassNotFoundException e) {\r
51             System.err.println(getClass() + " could not resolve class " + className + ", from bundle " + bundleName);\r
52             failed = true;\r
53             return null;\r
54         }\r
55     }\r
56 \r
57     @Override\r
58     public boolean isCompatible(Class<?> contentType) {\r
59         Class<?> clazz = resolveClass();\r
60         return clazz != null && clazz.equals(contentType);\r
61     }\r
62 \r
63     @Override\r
64     public boolean test(ReadGraph graph, Object content)\r
65             throws DatabaseException {\r
66         Class<?> clazz = resolveClass();\r
67         return clazz != null && clazz.isInstance(content);\r
68     }\r
69 \r
70 }\r