+package org.simantics.browsing.ui.model.tests;\r
+\r
+import org.eclipse.core.runtime.Platform;\r
+import org.osgi.framework.Bundle;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.viewpoint.ontology.ViewpointResource;\r
+\r
+/**\r
+ * A visual contribution rule test that checks whether the input content is an\r
+ * instance of a Java class. The class is specified by identifying the source\r
+ * bundle and the class name.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class InstanceOfTest implements Test {\r
+\r
+ String bundleName;\r
+ String className;\r
+ Class<?> clazz;\r
+ boolean failed;\r
+\r
+ public InstanceOfTest(String bundle, String className) {\r
+ this.bundleName = bundle;\r
+ this.className = className;\r
+ }\r
+\r
+ public InstanceOfTest(ReadGraph graph, Resource test) throws DatabaseException {\r
+ ViewpointResource VP = ViewpointResource.getInstance(graph);\r
+ this.bundleName = graph.getRelatedValue(test, VP.InstanceOfTest_bundleName, Bindings.STRING);\r
+ this.className = graph.getRelatedValue(test, VP.InstanceOfTest_className, Bindings.STRING);\r
+ }\r
+\r
+ private Class<?> resolveClass() {\r
+ if (failed)\r
+ return null;\r
+ if (clazz != null)\r
+ return clazz;\r
+ Bundle b = Platform.getBundle(bundleName);\r
+ if (b == null) {\r
+ System.err.println(getClass() + " could not resolve class " + className + ", bundle " + bundleName + " not found in platform");\r
+ failed = true;\r
+ return null;\r
+ }\r
+ try {\r
+ this.clazz = b.loadClass(className);\r
+ return clazz;\r
+ } catch (ClassNotFoundException e) {\r
+ System.err.println(getClass() + " could not resolve class " + className + ", from bundle " + bundleName);\r
+ failed = true;\r
+ return null;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public boolean isCompatible(Class<?> contentType) {\r
+ Class<?> clazz = resolveClass();\r
+ return clazz != null && clazz.equals(contentType);\r
+ }\r
+\r
+ @Override\r
+ public boolean test(ReadGraph graph, Object content)\r
+ throws DatabaseException {\r
+ Class<?> clazz = resolveClass();\r
+ return clazz != null && clazz.isInstance(content);\r
+ }\r
+\r
+}\r