1 package org.simantics.browsing.ui.model.tests;
\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
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
16 * @author Tuukka Lehtonen
\r
18 public class InstanceOfTest implements Test {
\r
25 public InstanceOfTest(String bundle, String className) {
\r
26 this.bundleName = bundle;
\r
27 this.className = className;
\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
36 private Class<?> resolveClass() {
\r
41 Bundle b = Platform.getBundle(bundleName);
\r
43 System.err.println(getClass() + " could not resolve class " + className + ", bundle " + bundleName + " not found in platform");
\r
48 this.clazz = b.loadClass(className);
\r
50 } catch (ClassNotFoundException e) {
\r
51 System.err.println(getClass() + " could not resolve class " + className + ", from bundle " + bundleName);
\r
58 public boolean isCompatible(Class<?> contentType) {
\r
59 Class<?> clazz = resolveClass();
\r
60 return clazz != null && clazz.equals(contentType);
\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