]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/viewpoint/ChildrenInActiveExperiment.java
Prevent unnecessary read transaction for synch master typical handle
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / viewpoint / ChildrenInActiveExperiment.java
1 package org.simantics.modeling.ui.viewpoint;
2
3 import java.util.Collection;
4 import java.util.Collections;
5
6 import org.simantics.browsing.ui.model.children.ChildRule;
7 import org.simantics.databoard.Bindings;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.variable.Variable;
12 import org.simantics.layer0.Layer0;
13 import org.simantics.scl.runtime.function.Function;
14 import org.simantics.simulation.ontology.SimulationResource;
15 import org.simantics.structural.stubs.StructuralResource2;
16
17 public class ChildrenInActiveExperiment implements ChildRule {
18
19     Layer0 L0;
20     SimulationResource SIMU;
21     StructuralResource2 STR;
22     
23     public ChildrenInActiveExperiment(ReadGraph graph) {
24         this.L0 = Layer0.getInstance(graph);
25         this.SIMU = SimulationResource.getInstance(graph);
26         this.STR = StructuralResource2.getInstance(graph);
27     }
28
29     private Variable getVariable(ReadGraph graph, Resource r) throws DatabaseException {
30         Resource model = graph.getPossibleObject(r, SIMU.IsConfigurationOf);
31         if(model != null) {
32             return (Variable)graph.adapt(SIMU.ActiveExperiment, Function.class)
33                 .apply(graph, model);
34         }
35         String name = graph.getPossibleRelatedValue(r, L0.HasName, Bindings.STRING);
36         Resource parent = graph.getPossibleObject(r, L0.PartOf);
37         if(name == null || parent == null)
38             return null;
39         Variable parentVariable = getVariable(graph, parent);
40         if(parentVariable == null)
41             return null;
42         return parentVariable.getPossibleChild(graph, name);
43     }
44
45     @Override
46     public boolean isCompatible(Class<?> contentType) {
47         return contentType.equals(Resource.class);
48     }
49
50     @Override
51     public Collection<?> getChildren(ReadGraph graph, Object parent_)
52             throws DatabaseException {
53         Resource parent = (Resource)parent_;
54         if(graph.isInstanceOf(parent, STR.Composite))
55             return Collections.emptyList();
56         Variable variable = getVariable(graph, parent);
57         if(variable == null)
58             return Collections.emptyList();
59         Collection<Variable> children = variable.getChildren(graph);
60         return VariableChildren.filterByClassification(graph, children, StructuralResource2.URIs.Component);
61     }
62
63     @Override
64     public Collection<?> getParents(ReadGraph graph, Object child)
65             throws DatabaseException {
66         return Collections.emptyList();
67     }
68
69 }