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