]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram.connection/src/org/simantics/diagram/connection/rendering/BasicConnectionStyle.java
514eeb92ae3430ce9bd2357b695a8cf0517d70ee
[simantics/platform.git] / bundles / org.simantics.diagram.connection / src / org / simantics / diagram / connection / rendering / BasicConnectionStyle.java
1 /*******************************************************************************\r
2  * Copyright (c) 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.diagram.connection.rendering;\r
13 \r
14 import java.awt.Color;\r
15 import java.awt.Graphics2D;\r
16 import java.awt.Stroke;\r
17 import java.awt.geom.Ellipse2D;\r
18 import java.awt.geom.Line2D;\r
19 import java.awt.geom.Path2D;\r
20 import java.io.Serializable;\r
21 \r
22 /**\r
23  * @author Tuukka Lehtonen\r
24  */\r
25 public class BasicConnectionStyle implements ConnectionStyle, Serializable {\r
26 \r
27     private static final long serialVersionUID = -5799681720482456895L;\r
28 \r
29     // line thickness in millimeters.\r
30     final Color                     lineColor;\r
31     final Color                     branchPointColor;\r
32     final double                    branchPointRadius;\r
33     final Stroke                    lineStroke;\r
34     final Stroke                    routeLineStroke;\r
35     final double                    degenerateLineLength; \r
36 \r
37     transient Line2D          line             = new Line2D.Double();\r
38     transient Ellipse2D       ellipse          = new Ellipse2D.Double();\r
39 \r
40     public BasicConnectionStyle(Color lineColor, Color branchPointColor, double branchPointRadius, Stroke lineStroke, Stroke routeLineStroke, double degenerateLineLength) {\r
41         this.lineColor = lineColor;\r
42         this.branchPointColor = branchPointColor;\r
43         this.branchPointRadius = branchPointRadius;\r
44         this.lineStroke = lineStroke;\r
45         this.routeLineStroke = routeLineStroke;\r
46         this.degenerateLineLength = degenerateLineLength;\r
47     }\r
48 \r
49     public Color getLineColor() {\r
50         return lineColor;\r
51     }\r
52     \r
53     public Color getBranchPointColor() {\r
54         return branchPointColor;\r
55     }\r
56     \r
57     public double getBranchPointRadius() {\r
58         return branchPointRadius;\r
59     }\r
60     \r
61     public Stroke getLineStroke() {\r
62         return lineStroke;\r
63     }\r
64     \r
65     public Stroke getRouteLineStroke() {\r
66         return routeLineStroke;\r
67     }\r
68 \r
69     @Override\r
70     public void drawLine(Graphics2D g, double x1, double y1, double x2,\r
71             double y2, boolean isTransient) {\r
72         if (lineColor != null)\r
73             g.setColor(lineColor);\r
74         if(isTransient) {\r
75             g.setStroke(lineStroke);\r
76             line.setLine(x1, y1, x2, y2);\r
77             g.draw(line);\r
78         } else {\r
79             g.setStroke(routeLineStroke);\r
80             line.setLine(x1, y1, x2, y2);\r
81             g.draw(line);\r
82         }\r
83     }\r
84 \r
85     @Override\r
86     public void drawPath(Graphics2D g, Path2D path, boolean isTransient) {\r
87         if (lineColor != null)\r
88             g.setColor(lineColor);\r
89         if (lineStroke != null)\r
90             g.setStroke(lineStroke);\r
91         g.draw(path);\r
92     }\r
93 \r
94     @Override\r
95     public void drawBranchPoint(Graphics2D g, double x, double y) {\r
96         g.setColor(branchPointColor);\r
97         double r = branchPointRadius;\r
98         double d = 2*r;\r
99         ellipse.setFrame(x-r, y-r, d, d);\r
100         g.fill(ellipse);\r
101     }\r
102 \r
103     @Override\r
104     public void drawDegeneratedLine(Graphics2D g, double x, double y,\r
105             boolean isHorizontal, boolean isTransient) {\r
106         double d = getDegeneratedLineLength()*0.5;\r
107         if(isHorizontal) {\r
108             line.setLine(x-d, y, x+d, y);\r
109             g.draw(line);\r
110         } else {\r
111             line.setLine(x, y-d, x, y+d);\r
112             g.draw(line);\r
113         }\r
114     }\r
115 \r
116     @Override\r
117     public double getDegeneratedLineLength() {\r
118         return degenerateLineLength;\r
119     }\r
120 \r
121     @Override\r
122     public int hashCode() {\r
123         final int prime = 31;\r
124         int result = 1;\r
125         result = prime * result + ((branchPointColor == null) ? 0 : branchPointColor.hashCode());\r
126         long temp;\r
127         temp = Double.doubleToLongBits(branchPointRadius);\r
128         result = prime * result + (int) (temp ^ (temp >>> 32));\r
129         temp = Double.doubleToLongBits(degenerateLineLength);\r
130         result = prime * result + (int) (temp ^ (temp >>> 32));\r
131         result = prime * result + ((lineColor == null) ? 0 : lineColor.hashCode());\r
132         result = prime * result + ((lineStroke == null) ? 0 : lineStroke.hashCode());\r
133         result = prime * result + ((routeLineStroke == null) ? 0 : routeLineStroke.hashCode());\r
134         return result;\r
135     }\r
136 \r
137     @Override\r
138     public boolean equals(Object obj) {\r
139         if (this == obj)\r
140             return true;\r
141         if (obj == null)\r
142             return false;\r
143         if (getClass() != obj.getClass())\r
144             return false;\r
145         BasicConnectionStyle other = (BasicConnectionStyle) obj;\r
146         if (branchPointColor == null) {\r
147             if (other.branchPointColor != null)\r
148                 return false;\r
149         } else if (!branchPointColor.equals(other.branchPointColor))\r
150             return false;\r
151         if (Double.doubleToLongBits(branchPointRadius) != Double.doubleToLongBits(other.branchPointRadius))\r
152             return false;\r
153         if (Double.doubleToLongBits(degenerateLineLength) != Double.doubleToLongBits(other.degenerateLineLength))\r
154             return false;\r
155         if (lineColor == null) {\r
156             if (other.lineColor != null)\r
157                 return false;\r
158         } else if (!lineColor.equals(other.lineColor))\r
159             return false;\r
160         if (lineStroke == null) {\r
161             if (other.lineStroke != null)\r
162                 return false;\r
163         } else if (!lineStroke.equals(other.lineStroke))\r
164             return false;\r
165         if (routeLineStroke == null) {\r
166             if (other.routeLineStroke != null)\r
167                 return false;\r
168         } else if (!routeLineStroke.equals(other.routeLineStroke))\r
169             return false;\r
170         return true;\r
171     }\r
172 \r
173 }\r