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.ontology.DistrictNetworkResource;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.modeling.ModelingResources;
25 import org.simantics.scenegraph.INode;
26 import org.simantics.scenegraph.g2d.G2DNode;
27 import org.simantics.scenegraph.profile.EvaluationContext;
28 import org.simantics.scenegraph.profile.common.ProfileVariables;
29 import org.simantics.scenegraph.utils.GeometryUtils;
30 import org.simantics.structural.stubs.StructuralResource2;
32 public class ConnectionLineStyle extends StyleBase<List<Point2D>> {
34 public static class ConnectionLineNode extends G2DNode {
35 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);
36 private static final Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.ORANGE, Color.CYAN, Color.PINK };
38 private float strokeWidth;
39 private Line2D[] lines;
41 public ConnectionLineNode() {
45 private static final long serialVersionUID = 1L;
48 public Rectangle2D getBoundsInLocal() {
53 public Rectangle2D getBoundsInLocal(boolean b) {
58 public Rectangle2D getBounds() {
62 public void setStrokeWidth(float w) {
66 public void setPoints(List<Point2D> result) {
67 Point2D p0 = DistrictNetworkNodeUtils.calculatePoint2D(result.get(0), null);
68 lines = new Line2D[result.size() - 1];
69 for (int i = 1; i < result.size(); i++)
71 Point2D p = result.get(i);
72 lines[i-1] = p != null ? new Line2D.Double(p0, DistrictNetworkNodeUtils.calculatePoint2D(p, null)) : null;
77 public void render(Graphics2D g2d) {
78 if (lines == null || lines.length == 0)
81 // Keep fixed line width on screen
82 float scaleRecip = (float) GeometryUtils.getScale(g2d.getTransform());
83 g2d.setStroke(GeometryUtils.scaleStroke(STROKE, strokeWidth / scaleRecip));
85 for (int i = 0; i < lines.length; i++) {
86 if (lines[i] != null) {
87 g2d.setColor(colors[i % colors.length]);
95 public List<Point2D> calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem)
96 throws DatabaseException {
97 return graph.syncRequest(new ElementConnectionRequest(groupItem), TransientCacheListener.instance());
101 public void applyStyleForNode(EvaluationContext observer, INode parent, List<Point2D> result) {
102 if (result == null || result.size() < 2) {
103 ProfileVariables.denyChild(parent, "*", "districtNetworkConnection");
107 ConnectionLineNode node = ProfileVariables.claimChild(parent, "*", "districtNetworkConnection", ConnectionLineNode.class, observer);
111 node.setPoints(result);
113 node.setStrokeWidth(2.f);
117 protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode parent) {
118 ProfileVariables.denyChild(parent, "*", "districtNetworkConnection");
121 private static final class ElementConnectionRequest extends ResourceRead<List<Point2D>> {
122 private ElementConnectionRequest(Resource resource) {
127 public List<Point2D> perform(ReadGraph graph) throws DatabaseException {
128 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
129 ModelingResources MOD = ModelingResources.getInstance(graph);
130 StructuralResource2 STR = StructuralResource2.getInstance(graph);
132 Resource vertex = resource;
133 if (!graph.isInstanceOf(vertex, DN.Vertex))
134 return Collections.emptyList();
136 double[] coords = graph.getRelatedValue(vertex, DiagramResource.getInstance(graph).HasLocation);
138 Resource element = graph.getPossibleObject(vertex, DN.MappedComponent);
140 return Collections.emptyList();
141 Resource component = graph.getPossibleObject(element, MOD.ElementToComponent);
142 if (component == null)
143 return Collections.emptyList();
144 Resource componentType = graph.getPossibleType(component, STR.Component);
145 if (componentType == null)
146 return Collections.emptyList();
148 Resource actionsModule = Layer0Utils.getPossibleChild(graph, componentType, "Actions");
149 if (actionsModule == null || !graph.isInstanceOf(actionsModule, Layer0.getInstance(graph).SCLModule))
150 return Collections.emptyList();
152 List<Resource> components;
154 components = Simantics.applySCL(graph.getURI(actionsModule), "getConnectedComponents", graph, component);
156 catch (DatabaseException e) {
157 return Collections.emptyList();
160 if (components == null || components.isEmpty())
161 return Collections.emptyList();
163 List<Point2D> result = new ArrayList<>(components.size() + 1);
164 result.add(new Point2D.Double(coords[0], coords[1]));
165 for (Resource comp : components) {
166 Resource e = comp != null ? graph.getPossibleObject(comp, MOD.ComponentToElement) : null;
167 Resource mappingElement = e != null ? graph.getPossibleObject(e, DN.MappedFromElement) : null;
168 if (mappingElement != null) {
169 double[] coords2 = graph.getRelatedValue(mappingElement, DiagramResource.getInstance(graph).HasLocation);
170 result.add(new Point2D.Double(coords2[0], coords2[1]));