1 package org.simantics.sysdyn.ui.elements.connections;
\r
3 import java.awt.Color;
\r
4 import java.awt.Graphics2D;
\r
5 import java.awt.Stroke;
\r
6 import java.awt.geom.Path2D;
\r
8 import org.simantics.databoard.Bindings;
\r
9 import org.simantics.db.ReadGraph;
\r
10 import org.simantics.db.Resource;
\r
11 import org.simantics.db.exception.DatabaseException;
\r
12 import org.simantics.db.request.Read;
\r
13 import org.simantics.diagram.connection.rendering.BasicConnectionStyle;
\r
14 import org.simantics.sysdyn.SysdynResource;
\r
15 import org.simantics.sysdyn.ui.elements.LoopNode;
\r
16 import org.simantics.ui.SimanticsUI;
\r
18 public class FlowConnectionStyle extends BasicConnectionStyle {
\r
20 private static final long serialVersionUID = 2777194644079591357L;
\r
25 private Resource resource;
\r
27 // Is the default color overridden by the loop color
\r
28 private boolean loopColorOverride = false;
\r
30 public static final float DEFAULT_LINE_WIDTH = 1.0f;
\r
32 public FlowConnectionStyle(Color lineColor, Stroke lineStroke, Resource resource) {
\r
33 super(lineColor, Color.BLACK, 0.5, lineStroke, lineStroke, 0.8);
\r
34 this.lineColor = lineColor;
\r
35 this.lineStroke = lineStroke;
\r
36 this.resource = resource;
\r
40 public void drawBranchPoint(Graphics2D g, double x, double y) {
\r
44 public void drawLine(Graphics2D g, double x1, double y1, double x2, double y2, boolean isTransient) {
\r
48 public void drawPath(Graphics2D g, Path2D path, boolean isTransient) {
\r
49 if (lineColor != null) // Highlight the flow if loop where the flow belongs to is selected.
\r
50 g.setColor(loopColorOverride ? LoopNode.HIGHLIGHT_COLOR : lineColor);
\r
51 if (lineStroke != null)
\r
52 g.setStroke(lineStroke);
\r
54 // Fetch the width of the flow
\r
55 Float width = DEFAULT_LINE_WIDTH;
\r
57 Float connectionWidth = SimanticsUI.getSession().syncRequest(new Read<Float>() {
\r
60 public Float perform(ReadGraph graph)
\r
61 throws DatabaseException {
\r
62 SysdynResource sr = SysdynResource.getInstance(graph);
\r
63 if (resource == null)
\r
65 return graph.getPossibleRelatedValue(resource, sr.FlowConnection_width, Bindings.FLOAT);
\r
68 if (connectionWidth != null)
\r
69 width = connectionWidth;
\r
70 } catch (DatabaseException e) {
\r
71 e.printStackTrace();
\r
74 Path2D p1 = Flows.createOffsetPath(path, width/2);
\r
75 Path2D p2 = Flows.createOffsetPath(path, -width/2);
\r
76 p1.append(p2, false);
\r
81 public void drawDegeneratedLine(Graphics2D g, double x, double y, boolean isHorizontal, boolean isTransient) {
\r
85 public double getDegeneratedLineLength() {
\r
90 * Set if the flow color should be overwritten with loop color
\r
91 * @param loopColorOverride
\r
93 public void setLoopColorOverride(boolean loopColorOverride) {
\r
94 this.loopColorOverride = loopColorOverride;
\r