From: Reino Ruusu Date: Thu, 7 Mar 2019 19:13:33 +0000 (+0200) Subject: Connection lines for control inputs. X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F62%2F2762%2F2;p=simantics%2Fdistrict.git Connection lines for control inputs. gitlab #37 Change-Id: I9d784032ccae42e50bfa13fbdb6600601a63ab5d --- diff --git a/org.simantics.district.network.ontology/graph/DistrictNetworkProfiles.pgraph b/org.simantics.district.network.ontology/graph/DistrictNetworkProfiles.pgraph index a5c5cee5..54700829 100644 --- a/org.simantics.district.network.ontology/graph/DistrictNetworkProfiles.pgraph +++ b/org.simantics.district.network.ontology/graph/DistrictNetworkProfiles.pgraph @@ -24,6 +24,7 @@ DN.VertexSizeStyle : DIA.Style DN.EdgeThicknessStyle : DIA.Style DN.HideStyle : DIA.Style DN.VertexSymbolStyle : DIA.Style +DN.ConnectionLineStyle : DIA.Style // Function for dynamic selection of symbols for a vertex // The input of the function is a DN.Vertex diff --git a/org.simantics.district.network.ontology/src/org/simantics/district/network/ontology/DistrictNetworkResource.java b/org.simantics.district.network.ontology/src/org/simantics/district/network/ontology/DistrictNetworkResource.java index c2b15437..178dbc28 100644 --- a/org.simantics.district.network.ontology/src/org/simantics/district/network/ontology/DistrictNetworkResource.java +++ b/org.simantics.district.network.ontology/src/org/simantics/district/network/ontology/DistrictNetworkResource.java @@ -18,6 +18,7 @@ public class DistrictNetworkResource { public final Resource BrowseContext; public final Resource Composite; public final Resource Connection; + public final Resource ConnectionLineStyle; public final Resource Diagram; public final Resource Diagram_MappedDiagram; public final Resource Diagram_MappedFromDiagram; @@ -296,6 +297,7 @@ public class DistrictNetworkResource { public static final String BrowseContext = "http://www.simantics.org/DistrictNetwork-1.0/BrowseContext"; public static final String Composite = "http://www.simantics.org/DistrictNetwork-1.0/Composite"; public static final String Connection = "http://www.simantics.org/DistrictNetwork-1.0/Connection"; + public static final String ConnectionLineStyle = "http://www.simantics.org/DistrictNetwork-1.0/ConnectionLineStyle"; public static final String Diagram = "http://www.simantics.org/DistrictNetwork-1.0/Diagram"; public static final String Diagram_MappedDiagram = "http://www.simantics.org/DistrictNetwork-1.0/Diagram/MappedDiagram"; public static final String Diagram_MappedFromDiagram = "http://www.simantics.org/DistrictNetwork-1.0/Diagram/MappedFromDiagram"; @@ -584,6 +586,7 @@ public class DistrictNetworkResource { BrowseContext = getResourceOrNull(graph, URIs.BrowseContext); Composite = getResourceOrNull(graph, URIs.Composite); Connection = getResourceOrNull(graph, URIs.Connection); + ConnectionLineStyle = getResourceOrNull(graph, URIs.ConnectionLineStyle); Diagram = getResourceOrNull(graph, URIs.Diagram); Diagram_MappedDiagram = getResourceOrNull(graph, URIs.Diagram_MappedDiagram); Diagram_MappedFromDiagram = getResourceOrNull(graph, URIs.Diagram_MappedFromDiagram); diff --git a/org.simantics.district.network.ui/META-INF/MANIFEST.MF b/org.simantics.district.network.ui/META-INF/MANIFEST.MF index 6638242d..d2c3962a 100644 --- a/org.simantics.district.network.ui/META-INF/MANIFEST.MF +++ b/org.simantics.district.network.ui/META-INF/MANIFEST.MF @@ -31,7 +31,8 @@ Require-Bundle: org.eclipse.e4.ui.model.workbench;bundle-version="1.1.100.v20150 org.eclipse.e4.core.contexts, org.eclipse.jface, org.simantics.scl.osgi, - org.simantics.district.route + org.simantics.district.route, + org.simantics.scenegraph.profile Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Import-Package: javax.annotation;version="1.0.0";resolution:=optional, javax.inject;version="1.0.0" diff --git a/org.simantics.district.network.ui/adapters.xml b/org.simantics.district.network.ui/adapters.xml index eda331ba..590cb0c6 100644 --- a/org.simantics.district.network.ui/adapters.xml +++ b/org.simantics.district.network.ui/adapters.xml @@ -19,6 +19,12 @@ + + + + + > { + + public static class ConnectionLineNode extends G2DNode { + 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); + private static final Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.ORANGE, Color.CYAN, Color.PINK }; + + private float strokeWidth; + private Line2D[] lines; + + public ConnectionLineNode() { + super(); + } + + private static final long serialVersionUID = 1L; + + @Override + public Rectangle2D getBoundsInLocal() { + return null; + } + + @Override + public Rectangle2D getBoundsInLocal(boolean b) { + return null; + } + + @Override + public Rectangle2D getBounds() { + return null; + } + + public void setStrokeWidth(float w) { + strokeWidth = w; + } + + public void setPoints(List result) { + Point2D p0 = DistrictNetworkNodeUtils.calculatePoint2D(result.get(0), null); + lines = new Line2D[result.size() - 1]; + for (int i = 1; i < result.size(); i++) + { + Point2D p = result.get(i); + lines[i-1] = p != null ? new Line2D.Double(p0, DistrictNetworkNodeUtils.calculatePoint2D(p, null)) : null; + } + } + + @Override + public void render(Graphics2D g2d) { + if (lines == null || lines.length == 0) + return; + + // Keep fixed line width on screen + float scaleRecip = (float) GeometryUtils.getScale(g2d.getTransform()); + g2d.setStroke(GeometryUtils.scaleStroke(STROKE, strokeWidth / scaleRecip)); + + for (int i = 0; i < lines.length; i++) { + if (lines[i] != null) { + g2d.setColor(colors[i % colors.length]); + g2d.draw(lines[i]); + } + } + } + } + + @Override + public List calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) + throws DatabaseException { + return graph.syncRequest(new ElementConnectionRequest(groupItem), TransientCacheListener.instance()); + } + + @Override + public void applyStyleForNode(EvaluationContext observer, INode parent, List result) { + if (result == null || result.size() < 2) { + ProfileVariables.denyChild(parent, "*", "districtNetworkConnection"); + return; + } + + ConnectionLineNode node = ProfileVariables.claimChild(parent, "*", "districtNetworkConnection", ConnectionLineNode.class, observer); + if (node == null) + return; + + node.setPoints(result); + node.setZIndex(0); + node.setStrokeWidth(2.f); + } + + @Override + protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode parent) { + ProfileVariables.denyChild(parent, "*", "districtNetworkConnection"); + } + + private static final class ElementConnectionRequest extends ResourceRead> { + private ElementConnectionRequest(Resource resource) { + super(resource); + } + + @Override + public List perform(ReadGraph graph) throws DatabaseException { + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + ModelingResources MOD = ModelingResources.getInstance(graph); + StructuralResource2 STR = StructuralResource2.getInstance(graph); + + Resource vertex = resource; + if (!graph.isInstanceOf(vertex, DN.Vertex)) + return Collections.emptyList(); + + double[] coords = graph.getRelatedValue(vertex, DiagramResource.getInstance(graph).HasLocation); + + Resource element = graph.getPossibleObject(vertex, DN.MappedComponent); + if (element == null) + return Collections.emptyList(); + Resource component = graph.getPossibleObject(element, MOD.ElementToComponent); + if (component == null) + return Collections.emptyList(); + Resource componentType = graph.getPossibleType(component, STR.Component); + if (componentType == null) + return Collections.emptyList(); + + Resource actionsModule = Layer0Utils.getPossibleChild(graph, componentType, "Actions"); + if (actionsModule == null || !graph.isInstanceOf(actionsModule, Layer0.getInstance(graph).SCLModule)) + return Collections.emptyList(); + + List components; + try { + components = Simantics.applySCL(graph.getURI(actionsModule), "getConnectedComponents", graph, component); + } + catch (DatabaseException e) { + return Collections.emptyList(); + } + + if (components == null || components.isEmpty()) + return Collections.emptyList(); + + List result = new ArrayList<>(components.size() + 1); + result.add(new Point2D.Double(coords[0], coords[1])); + for (Resource comp : components) { + Resource e = comp != null ? graph.getPossibleObject(comp, MOD.ComponentToElement) : null; + Resource mappingElement = e != null ? graph.getPossibleObject(e, DN.MappedFromElement) : null; + if (mappingElement != null) { + double[] coords2 = graph.getRelatedValue(mappingElement, DiagramResource.getInstance(graph).HasLocation); + result.add(new Point2D.Double(coords2[0], coords2[1])); + } + else { + result.add(null); + } + } + + return result; + } + } +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java index 6fb075eb..221ed714 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java @@ -109,8 +109,10 @@ public class DistrictNetworkVertexNode extends G2DParentNode implements ISelecti // Render SVG symbol for (INode nn : getNodes()) { G2DNode g2dNode = (G2DNode)nn; - symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform); - g2dNode.setTransform(symbolTransform); + if (nn instanceof SVGNode) { + symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform); + g2dNode.setTransform(symbolTransform); + } g2dNode.render(g2d); }