1 package org.simantics.district.network.ui.nodes;
3 import java.awt.BasicStroke;
5 import java.awt.Graphics2D;
6 import java.awt.geom.Line2D;
7 import java.awt.geom.Point2D;
8 import java.awt.geom.Rectangle2D;
9 import java.util.ArrayList;
10 import java.util.Collections;
11 import java.util.List;
13 import org.simantics.Simantics;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
17 import org.simantics.db.common.request.ResourceRead;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.layer0.util.Layer0Utils;
20 import org.simantics.diagram.profile.StyleBase;
21 import org.simantics.diagram.stubs.DiagramResource;
22 import org.simantics.district.network.DistrictNetworkUtil;
23 import org.simantics.district.network.ontology.DistrictNetworkResource;
24 import org.simantics.layer0.Layer0;
25 import org.simantics.modeling.ModelingResources;
26 import org.simantics.scenegraph.INode;
27 import org.simantics.scenegraph.g2d.G2DNode;
28 import org.simantics.scenegraph.profile.EvaluationContext;
29 import org.simantics.scenegraph.profile.common.ProfileVariables;
30 import org.simantics.scenegraph.utils.GeometryUtils;
31 import org.simantics.scl.compiler.top.ValueNotFound;
32 import org.simantics.scl.osgi.SCLOsgi;
33 import org.simantics.scl.runtime.SCLContext;
34 import org.simantics.scl.runtime.function.Function1;
35 import org.simantics.structural.stubs.StructuralResource2;
37 public class ConnectionLineStyle extends StyleBase<List<Point2D>> {
39 public static class ConnectionLineNode extends G2DNode {
40 private static final BasicStroke STROKE = new BasicStroke(1.f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.f, new float[] {4.f, 2.f}, 0.f);
41 private static final Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.ORANGE, Color.CYAN, Color.PINK };
43 private float strokeWidth;
44 private Line2D[] lines;
46 public ConnectionLineNode() {
50 private static final long serialVersionUID = 1L;
53 public Rectangle2D getBoundsInLocal() {
58 public Rectangle2D getBoundsInLocal(boolean b) {
63 public Rectangle2D getBounds() {
67 public void setStrokeWidth(float w) {
71 public void setPoints(List<Point2D> result) {
72 Point2D p0 = DistrictNetworkNodeUtils.calculatePoint2D(result.get(0), null);
73 lines = new Line2D[result.size() - 1];
74 for (int i = 1; i < result.size(); i++)
76 Point2D p = result.get(i);
77 lines[i-1] = p != null ? new Line2D.Double(p0, DistrictNetworkNodeUtils.calculatePoint2D(p, null)) : null;
82 public void render(Graphics2D g2d) {
83 if (lines == null || lines.length == 0)
86 // Keep fixed line width on screen
87 float scaleRecip = (float) GeometryUtils.getScale(g2d.getTransform());
88 g2d.setStroke(GeometryUtils.scaleStroke(STROKE, strokeWidth / scaleRecip));
90 for (int i = 0; i < lines.length; i++) {
91 if (lines[i] != null) {
92 g2d.setColor(colors[i % colors.length]);
100 public List<Point2D> calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem)
101 throws DatabaseException {
102 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
103 ModelingResources MOD = ModelingResources.getInstance(graph);
104 StructuralResource2 STR = StructuralResource2.getInstance(graph);
106 Resource vertex = groupItem;
107 if (!graph.isInstanceOf(vertex, DN.Vertex))
108 return Collections.emptyList();
110 double[] coords = graph.getRelatedValue(vertex, DiagramResource.getInstance(graph).HasLocation);
112 Resource component = DistrictNetworkUtil.getMappedComponentCached(graph, vertex);
113 if (component == null)
114 return Collections.emptyList();
116 Resource componentType = graph.getPossibleType(component, STR.Component);
117 if (componentType == null)
118 return Collections.emptyList();
120 Function1<Resource, List<Resource>> fun = getConnectedComponentsFunctionCached(graph, componentType);
122 return Collections.emptyList();
124 List<Resource> components = Simantics.applySCLRead(graph, fun, component);
126 if (components == null || components.isEmpty())
127 return Collections.emptyList();
129 List<Point2D> result = new ArrayList<>(components.size() + 1);
130 result.add(new Point2D.Double(coords[0], coords[1]));
131 for (Resource comp : components) {
132 Resource e = comp != null ? graph.getPossibleObject(comp, MOD.ComponentToElement) : null;
133 Resource mappingElement = e != null ? graph.getPossibleObject(e, DN.MappedFromElement) : null;
134 if (mappingElement != null) {
135 double[] coords2 = graph.getRelatedValue(mappingElement, DiagramResource.getInstance(graph).HasLocation);
136 result.add(new Point2D.Double(coords2[0], coords2[1]));
147 public void applyStyleForNode(EvaluationContext observer, INode parent, List<Point2D> result) {
148 if (result == null || result.size() < 2) {
149 ProfileVariables.denyChild(parent, "*", "districtNetworkConnection");
153 ConnectionLineNode node = ProfileVariables.claimChild(parent, "*", "districtNetworkConnection", ConnectionLineNode.class, observer);
157 node.setPoints(result);
159 node.setStrokeWidth(2.f);
163 protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode parent) {
164 ProfileVariables.denyChild(parent, "*", "districtNetworkConnection");
167 private static Function1<Resource, List<Resource>> getConnectedComponentsFunctionCached(ReadGraph graph, Resource componentType)
168 throws DatabaseException {
169 return graph.syncRequest(new ConnectedComponentsFunctionRequest(componentType), TransientCacheListener.instance());
172 private static final class ConnectedComponentsFunctionRequest
173 extends ResourceRead<Function1<Resource, List<Resource>>> {
174 public ConnectedComponentsFunctionRequest(Resource resource) {
178 @SuppressWarnings("unchecked")
180 public Function1<Resource, List<Resource>> perform(ReadGraph graph) throws DatabaseException {
181 Resource actionsModule = Layer0Utils.getPossibleChild(graph, resource, "Actions");
182 if (actionsModule == null || !graph.isInstanceOf(actionsModule, Layer0.getInstance(graph).SCLModule))
185 String uri = graph.getURI(actionsModule);
186 SCLContext sclContext = SCLContext.getCurrent();
187 Object oldGraph = sclContext.get("graph");
189 sclContext.put("graph", graph);
190 return (Function1<Resource, List<Resource>>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, "getConnectedComponents");
191 } catch (ValueNotFound e1) {
194 sclContext.put("graph", oldGraph);