1 package org.simantics.district.network.ui.styles;
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.district.network.ui.nodes.DistrictNetworkNodeUtils;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.modeling.ModelingResources;
27 import org.simantics.scenegraph.INode;
28 import org.simantics.scenegraph.g2d.G2DNode;
29 import org.simantics.scenegraph.profile.EvaluationContext;
30 import org.simantics.scenegraph.profile.common.ProfileVariables;
31 import org.simantics.scenegraph.utils.GeometryUtils;
32 import org.simantics.scl.compiler.top.ValueNotFound;
33 import org.simantics.scl.osgi.SCLOsgi;
34 import org.simantics.scl.runtime.SCLContext;
35 import org.simantics.scl.runtime.function.Function1;
36 import org.simantics.structural.stubs.StructuralResource2;
38 public class ConnectionLineStyle extends StyleBase<List<Point2D>> {
40 public static class ConnectionLineNode extends G2DNode {
41 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);
42 private static final Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.ORANGE, Color.CYAN, Color.PINK };
44 private float strokeWidth;
45 private Line2D[] lines;
47 public ConnectionLineNode() {
51 private static final long serialVersionUID = 1L;
54 public Rectangle2D getBoundsInLocal() {
59 public Rectangle2D getBoundsInLocal(boolean b) {
64 public Rectangle2D getBounds() {
68 public void setStrokeWidth(float w) {
72 public void setPoints(List<Point2D> result) {
73 Point2D p0 = DistrictNetworkNodeUtils.calculatePoint2D(result.get(0), null);
74 lines = new Line2D[result.size() - 1];
75 for (int i = 1; i < result.size(); i++)
77 Point2D p = result.get(i);
78 lines[i-1] = p != null ? new Line2D.Double(p0, DistrictNetworkNodeUtils.calculatePoint2D(p, null)) : null;
83 public void render(Graphics2D g2d) {
84 if (lines == null || lines.length == 0)
87 // Keep fixed line width on screen
88 float scaleRecip = (float) GeometryUtils.getScale(g2d.getTransform());
89 g2d.setStroke(GeometryUtils.scaleStroke(STROKE, strokeWidth / scaleRecip));
91 for (int i = 0; i < lines.length; i++) {
92 if (lines[i] != null) {
93 g2d.setColor(colors[i % colors.length]);
101 public List<Point2D> calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem)
102 throws DatabaseException {
103 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
104 ModelingResources MOD = ModelingResources.getInstance(graph);
105 StructuralResource2 STR = StructuralResource2.getInstance(graph);
107 Resource vertex = groupItem;
108 if (!graph.isInstanceOf(vertex, DN.Vertex))
109 return Collections.emptyList();
111 double[] coords = graph.getRelatedValue(vertex, DiagramResource.getInstance(graph).HasLocation);
113 Resource component = DistrictNetworkUtil.getMappedComponentCached(graph, vertex);
114 if (component == null)
115 return Collections.emptyList();
117 Resource componentType = graph.getPossibleType(component, STR.Component);
118 if (componentType == null)
119 return Collections.emptyList();
121 Function1<Resource, List<Resource>> fun = getConnectedComponentsFunctionCached(graph, componentType);
123 return Collections.emptyList();
125 List<Resource> components = Simantics.applySCLRead(graph, fun, component);
127 if (components == null || components.isEmpty())
128 return Collections.emptyList();
130 List<Point2D> result = new ArrayList<>(components.size() + 1);
131 result.add(new Point2D.Double(coords[0], coords[1]));
132 for (Resource comp : components) {
133 Resource e = comp != null ? graph.getPossibleObject(comp, MOD.ComponentToElement) : null;
134 Resource mappingElement = e != null ? graph.getPossibleObject(e, DN.MappedFromElement) : null;
135 if (mappingElement != null) {
136 double[] coords2 = graph.getRelatedValue(mappingElement, DiagramResource.getInstance(graph).HasLocation);
137 result.add(new Point2D.Double(coords2[0], coords2[1]));
148 public void applyStyleForNode(EvaluationContext observer, INode parent, List<Point2D> result) {
149 if (result == null || result.size() < 2) {
150 ProfileVariables.denyChild(parent, "*", "districtNetworkConnection");
154 ConnectionLineNode node = ProfileVariables.claimChild(parent, "*", "districtNetworkConnection", ConnectionLineNode.class, observer);
158 node.setPoints(result);
160 node.setStrokeWidth(2.f);
164 protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode parent) {
165 ProfileVariables.denyChild(parent, "*", "districtNetworkConnection");
168 private static Function1<Resource, List<Resource>> getConnectedComponentsFunctionCached(ReadGraph graph, Resource componentType)
169 throws DatabaseException {
170 return graph.syncRequest(new ConnectedComponentsFunctionRequest(componentType), TransientCacheListener.instance());
173 private static final class ConnectedComponentsFunctionRequest
174 extends ResourceRead<Function1<Resource, List<Resource>>> {
175 public ConnectedComponentsFunctionRequest(Resource resource) {
179 @SuppressWarnings("unchecked")
181 public Function1<Resource, List<Resource>> perform(ReadGraph graph) throws DatabaseException {
182 Resource actionsModule = Layer0Utils.getPossibleChild(graph, resource, "Actions");
183 if (actionsModule == null || !graph.isInstanceOf(actionsModule, Layer0.getInstance(graph).SCLModule))
186 String uri = graph.getURI(actionsModule);
187 SCLContext sclContext = SCLContext.getCurrent();
188 Object oldGraph = sclContext.get("graph");
190 sclContext.put("graph", graph);
191 return (Function1<Resource, List<Resource>>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, "getConnectedComponents");
192 } catch (ValueNotFound e1) {
195 sclContext.put("graph", oldGraph);