1 package org.simantics.district.network.ui.styles;
3 import java.awt.geom.Point2D;
4 import java.util.ArrayList;
5 import java.util.Collections;
8 import org.simantics.Simantics;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
12 import org.simantics.db.common.request.ResourceRead;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.util.Layer0Utils;
15 import org.simantics.diagram.profile.StyleBase;
16 import org.simantics.diagram.stubs.DiagramResource;
17 import org.simantics.district.network.DistrictNetworkUtil;
18 import org.simantics.district.network.ontology.DistrictNetworkResource;
19 import org.simantics.district.network.ui.nodes.ConnectionLineNode;
20 import org.simantics.layer0.Layer0;
21 import org.simantics.modeling.ModelingResources;
22 import org.simantics.scenegraph.INode;
23 import org.simantics.scenegraph.profile.EvaluationContext;
24 import org.simantics.scenegraph.profile.common.ProfileVariables;
25 import org.simantics.scl.compiler.top.ValueNotFound;
26 import org.simantics.scl.osgi.SCLOsgi;
27 import org.simantics.scl.runtime.SCLContext;
28 import org.simantics.scl.runtime.function.Function1;
29 import org.simantics.structural.stubs.StructuralResource2;
32 public class ConnectionLineStyle extends StyleBase<List<Point2D>> {
35 public List<Point2D> calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem)
36 throws DatabaseException {
37 return doCalculateConnectedComponentPoints(graph, groupItem);
40 public static List<Point2D> doCalculateConnectedComponentPoints(ReadGraph graph, Resource vertex) throws DatabaseException {
41 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
42 ModelingResources MOD = ModelingResources.getInstance(graph);
43 StructuralResource2 STR = StructuralResource2.getInstance(graph);
45 if (!graph.isInstanceOf(vertex, DN.Vertex))
46 return Collections.emptyList();
48 double[] coords = graph.getRelatedValue(vertex, DiagramResource.getInstance(graph).HasLocation);
50 Resource component = DistrictNetworkUtil.getMappedComponentCached(graph, vertex);
51 if (component == null)
52 return Collections.emptyList();
54 Resource componentType = graph.getPossibleType(component, STR.Component);
55 if (componentType == null)
56 return Collections.emptyList();
58 Function1<Resource, List<Resource>> fun = getConnectedComponentsFunctionCached(graph, componentType);
60 return Collections.emptyList();
62 List<Resource> components = Simantics.applySCLRead(graph, fun, component);
64 if (components == null || components.isEmpty())
65 return Collections.emptyList();
67 List<Point2D> result = new ArrayList<>(components.size() + 1);
68 result.add(new Point2D.Double(coords[0], coords[1]));
69 for (Resource comp : components) {
70 Resource e = comp != null ? graph.getPossibleObject(comp, MOD.ComponentToElement) : null;
71 Resource mappingElement = e != null ? graph.getPossibleObject(e, DN.MappedFromElement) : null;
72 if (mappingElement != null) {
73 double[] coords2 = graph.getRelatedValue(mappingElement,
74 DiagramResource.getInstance(graph).HasLocation);
75 result.add(new Point2D.Double(coords2[0], coords2[1]));
85 public void applyStyleForNode(EvaluationContext observer, INode parent, List<Point2D> result) {
86 if (result == null || result.size() < 2) {
87 ProfileVariables.denyChild(parent, "*", ConnectionLineNode.NODE_NAME);
91 ConnectionLineNode node = ProfileVariables.claimChild(parent, "*", ConnectionLineNode.NODE_NAME, ConnectionLineNode.class, observer);
95 node.setPoints(result);
97 node.setStrokeWidth(2.f);
101 protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode parent) {
102 ProfileVariables.denyChild(parent, "*", ConnectionLineNode.NODE_NAME);
105 private static Function1<Resource, List<Resource>> getConnectedComponentsFunctionCached(ReadGraph graph, Resource componentType)
106 throws DatabaseException {
107 return graph.syncRequest(new ConnectedComponentsFunctionRequest(componentType), TransientCacheListener.instance());
110 private static final class ConnectedComponentsFunctionRequest
111 extends ResourceRead<Function1<Resource, List<Resource>>> {
112 public ConnectedComponentsFunctionRequest(Resource resource) {
116 @SuppressWarnings("unchecked")
118 public Function1<Resource, List<Resource>> perform(ReadGraph graph) throws DatabaseException {
119 Resource actionsModule = Layer0Utils.getPossibleChild(graph, resource, "Actions");
120 if (actionsModule == null || !graph.isInstanceOf(actionsModule, Layer0.getInstance(graph).SCLModule))
123 String uri = graph.getURI(actionsModule);
124 SCLContext sclContext = SCLContext.getCurrent();
125 Object oldGraph = sclContext.get("graph");
127 sclContext.put("graph", graph);
128 return (Function1<Resource, List<Resource>>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, "getConnectedComponents");
129 } catch (ValueNotFound e1) {
132 sclContext.put("graph", oldGraph);