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;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
14 * A visual contribution rule test that checks whether the input content is an
15 * instance of a Java class. The class is specified by identifying the source
16 * bundle and the class name.
18 * @author Tuukka Lehtonen
20 public class InstanceOfTest implements Test {
22 private static final Logger LOGGER = LoggerFactory.getLogger(InstanceOfTest.class);
28 public InstanceOfTest(String bundle, String className) {
29 this.bundleName = bundle;
30 this.className = className;
33 public InstanceOfTest(ReadGraph graph, Resource test) throws DatabaseException {
34 ViewpointResource VP = ViewpointResource.getInstance(graph);
35 this.bundleName = graph.getRelatedValue(test, VP.InstanceOfTest_bundleName, Bindings.STRING);
36 this.className = graph.getRelatedValue(test, VP.InstanceOfTest_className, Bindings.STRING);
39 private Class<?> resolveClass() {
44 Bundle b = Platform.getBundle(bundleName);
46 LOGGER.error(getClass() + " could not resolve class " + className + ", bundle " + bundleName + " not found in platform");
51 this.clazz = b.loadClass(className);
53 } catch (ClassNotFoundException e) {
54 LOGGER.error(getClass() + " could not resolve class " + className + ", from bundle " + bundleName, e);
61 public boolean isCompatible(Class<?> contentType) {
62 Class<?> clazz = resolveClass();
63 return clazz != null && clazz.equals(contentType);
67 public boolean test(ReadGraph graph, Object content)
68 throws DatabaseException {
69 Class<?> clazz = resolveClass();
70 return clazz != null && clazz.isInstance(content);