]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/tests/InstanceOfTest.java
08888911319a36b37240f600ef4e66575049f5f8
[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;
2
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;
12
13 /**
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.
17  * 
18  * @author Tuukka Lehtonen
19  */
20 public class InstanceOfTest implements Test {
21
22     private static final Logger LOGGER = LoggerFactory.getLogger(InstanceOfTest.class);
23     String   bundleName;
24     String   className;
25     Class<?> clazz;
26     boolean  failed;
27
28     public InstanceOfTest(String bundle, String className) {
29         this.bundleName = bundle;
30         this.className = className;
31     }
32
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);
37     }
38
39     private Class<?> resolveClass() {
40         if (failed)
41             return null;
42         if (clazz != null)
43             return clazz;
44         Bundle b = Platform.getBundle(bundleName);
45         if (b == null) {
46             LOGGER.error(getClass() + " could not resolve class " + className + ", bundle " + bundleName + " not found in platform");
47             failed = true;
48             return null;
49         }
50         try {
51             this.clazz = b.loadClass(className);
52             return clazz;
53         } catch (ClassNotFoundException e) {
54             LOGGER.error(getClass() + " could not resolve class " + className + ", from bundle " + bundleName, e);
55             failed = true;
56             return null;
57         }
58     }
59
60     @Override
61     public boolean isCompatible(Class<?> contentType) {
62         Class<?> clazz = resolveClass();
63         return clazz != null && clazz.equals(contentType);
64     }
65
66     @Override
67     public boolean test(ReadGraph graph, Object content)
68             throws DatabaseException {
69         Class<?> clazz = resolveClass();
70         return clazz != null && clazz.isInstance(content);
71     }
72
73 }