]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/ConnectionLineStyle.java
Add connected components to visualisation from profiles
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / styles / ConnectionLineStyle.java
1 package org.simantics.district.network.ui.styles;
2
3 import java.awt.geom.Point2D;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.List;
7
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;
30
31 @Deprecated
32 public class ConnectionLineStyle extends StyleBase<List<Point2D>> {
33         
34         @Override
35         public List<Point2D> calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem)
36                         throws DatabaseException {
37                 return doCalculateConnectedComponentPoints(graph, groupItem);
38         }
39
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);
44         
45         if (!graph.isInstanceOf(vertex, DN.Vertex))
46             return Collections.emptyList();
47
48         double[] coords = graph.getRelatedValue(vertex, DiagramResource.getInstance(graph).HasLocation);
49
50         Resource component = DistrictNetworkUtil.getMappedComponentCached(graph, vertex);
51         if (component == null)
52             return Collections.emptyList();
53
54         Resource componentType = graph.getPossibleType(component, STR.Component);
55         if (componentType == null)
56             return Collections.emptyList();
57
58         Function1<Resource, List<Resource>> fun = getConnectedComponentsFunctionCached(graph, componentType);
59         if (fun == null)
60             return Collections.emptyList();
61
62         List<Resource> components = Simantics.applySCLRead(graph, fun, component);
63
64         if (components == null || components.isEmpty())
65             return Collections.emptyList();
66
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]));
76             } else {
77                 result.add(null);
78             }
79         }
80
81         return result;
82     }
83
84         @Override
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);
88                         return;
89                 }
90                 
91                 ConnectionLineNode node = ProfileVariables.claimChild(parent, "*", ConnectionLineNode.NODE_NAME, ConnectionLineNode.class, observer);
92                 if (node == null)
93                         return;
94                 
95                 node.setPoints(result);
96                 node.setZIndex(0);
97                 node.setStrokeWidth(2.f);
98         }
99
100         @Override
101         protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode parent) {
102                 ProfileVariables.denyChild(parent, "*", ConnectionLineNode.NODE_NAME);
103         }
104         
105         private static Function1<Resource, List<Resource>> getConnectedComponentsFunctionCached(ReadGraph graph, Resource componentType)
106                         throws DatabaseException {
107                 return graph.syncRequest(new ConnectedComponentsFunctionRequest(componentType), TransientCacheListener.instance());
108         }
109         
110         private static final class ConnectedComponentsFunctionRequest
111                         extends ResourceRead<Function1<Resource, List<Resource>>> {
112                 public ConnectedComponentsFunctionRequest(Resource resource) {
113                         super(resource);
114                 }
115
116                 @SuppressWarnings("unchecked")
117                 @Override
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))
121                                 return null;
122                         
123                         String uri = graph.getURI(actionsModule);
124                         SCLContext sclContext = SCLContext.getCurrent();
125                         Object oldGraph = sclContext.get("graph");
126                         try {
127                                 sclContext.put("graph", graph);
128                                 return (Function1<Resource, List<Resource>>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, "getConnectedComponents");
129                         } catch (ValueNotFound e1) {
130                                 return null;
131                         } finally {
132                                 sclContext.put("graph", oldGraph);
133                         }
134                 }
135         }
136 }