]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
a7476de6f285372bb9ba73a84ff29da369e72504
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.elements.connections;\r
2 \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
7 \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
17 \r
18 public class FlowConnectionStyle  extends BasicConnectionStyle {\r
19 \r
20     private static final long serialVersionUID = 2777194644079591357L;\r
21 \r
22     Color                     lineColor;\r
23     Stroke                    lineStroke;\r
24 \r
25         private Resource resource;\r
26 \r
27         // Is the default color overridden by the loop color\r
28         private boolean loopColorOverride = false;\r
29     \r
30     public static final float DEFAULT_LINE_WIDTH = 1.0f;\r
31 \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
37     }\r
38     \r
39     @Override\r
40     public void drawBranchPoint(Graphics2D g, double x, double y) {\r
41     }\r
42 \r
43     @Override\r
44     public void drawLine(Graphics2D g, double x1, double y1, double x2, double y2, boolean isTransient) {\r
45     }\r
46 \r
47     @Override\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
53 \r
54         // Fetch the width of the flow\r
55         Float width = DEFAULT_LINE_WIDTH;\r
56         try {\r
57                         Float connectionWidth = SimanticsUI.getSession().syncRequest(new Read<Float>() {\r
58 \r
59                                 @Override\r
60                                 public Float perform(ReadGraph graph)\r
61                                                 throws DatabaseException {\r
62                                         SysdynResource sr = SysdynResource.getInstance(graph);\r
63                                         if (resource == null)\r
64                                                 return null;\r
65                                         return graph.getPossibleRelatedValue(resource, sr.FlowConnection_width, Bindings.FLOAT);\r
66                                 }\r
67                         });\r
68                         if (connectionWidth != null)\r
69                                 width = connectionWidth; \r
70                 } catch (DatabaseException e) {\r
71                         e.printStackTrace();\r
72                 }\r
73 \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
77         g.draw(p1);\r
78     }\r
79 \r
80     @Override\r
81     public void drawDegeneratedLine(Graphics2D g, double x, double y, boolean isHorizontal, boolean isTransient) {\r
82     }\r
83 \r
84     @Override\r
85     public double getDegeneratedLineLength() {\r
86         return 0;\r
87     }\r
88 \r
89     /**\r
90      * Set if the flow color should be overwritten with loop color\r
91      * @param loopColorOverride \r
92      */\r
93         public void setLoopColorOverride(boolean loopColorOverride) {\r
94                 this.loopColorOverride  = loopColorOverride;\r
95         }\r
96     \r
97 }\r