1 package org.simantics.browsing.ui.model.tests;
3 import org.eclipse.core.runtime.Platform;
4 import org.osgi.framework.Bundle;
5 import org.simantics.databoard.Bindings;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.viewpoint.ontology.ViewpointResource;
12 * A visual contribution rule test that checks whether the input content is an
13 * instance of a Java class. The class is specified by identifying the source
14 * bundle and the class name.
16 * @author Tuukka Lehtonen
18 public class InstanceOfTest implements Test {
25 public InstanceOfTest(String bundle, String className) {
26 this.bundleName = bundle;
27 this.className = className;
30 public InstanceOfTest(ReadGraph graph, Resource test) throws DatabaseException {
31 ViewpointResource VP = ViewpointResource.getInstance(graph);
32 this.bundleName = graph.getRelatedValue(test, VP.InstanceOfTest_bundleName, Bindings.STRING);
33 this.className = graph.getRelatedValue(test, VP.InstanceOfTest_className, Bindings.STRING);
36 private Class<?> resolveClass() {
41 Bundle b = Platform.getBundle(bundleName);
43 System.err.println(getClass() + " could not resolve class " + className + ", bundle " + bundleName + " not found in platform");
48 this.clazz = b.loadClass(className);
50 } catch (ClassNotFoundException e) {
51 System.err.println(getClass() + " could not resolve class " + className + ", from bundle " + bundleName);
58 public boolean isCompatible(Class<?> contentType) {
59 Class<?> clazz = resolveClass();
60 return clazz != null && clazz.equals(contentType);
64 public boolean test(ReadGraph graph, Object content)
65 throws DatabaseException {
66 Class<?> clazz = resolveClass();
67 return clazz != null && clazz.isInstance(content);