1 /*******************************************************************************
2 * Copyright (c) 2011 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.connection.rendering;
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;
23 * @author Tuukka Lehtonen
25 public class BasicConnectionStyle implements ConnectionStyle, Serializable {
27 private static final long serialVersionUID = -5799681720482456895L;
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;
37 transient Line2D line = new Line2D.Double();
38 transient Ellipse2D ellipse = new Ellipse2D.Double();
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;
49 public Color getLineColor() {
53 public Color getBranchPointColor() {
54 return branchPointColor;
57 public double getBranchPointRadius() {
58 return branchPointRadius;
61 public Stroke getLineStroke() {
65 public Stroke getRouteLineStroke() {
66 return routeLineStroke;
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);
75 g.setStroke(lineStroke);
76 line.setLine(x1, y1, x2, y2);
79 g.setStroke(routeLineStroke);
80 line.setLine(x1, y1, x2, y2);
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);
95 public void drawBranchPoint(Graphics2D g, double x, double y) {
96 g.setColor(branchPointColor);
97 double r = branchPointRadius;
99 ellipse.setFrame(x-r, y-r, d, d);
104 public void drawDegeneratedLine(Graphics2D g, double x, double y,
105 boolean isHorizontal, boolean isTransient) {
106 double d = getDegeneratedLineLength()*0.5;
108 line.setLine(x-d, y, x+d, y);
111 line.setLine(x, y-d, x, y+d);
117 public double getDegeneratedLineLength() {
118 return degenerateLineLength;
122 public int hashCode() {
123 final int prime = 31;
125 result = prime * result + ((branchPointColor == null) ? 0 : branchPointColor.hashCode());
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());
138 public boolean equals(Object obj) {
143 if (getClass() != obj.getClass())
145 BasicConnectionStyle other = (BasicConnectionStyle) obj;
146 if (branchPointColor == null) {
147 if (other.branchPointColor != null)
149 } else if (!branchPointColor.equals(other.branchPointColor))
151 if (Double.doubleToLongBits(branchPointRadius) != Double.doubleToLongBits(other.branchPointRadius))
153 if (Double.doubleToLongBits(degenerateLineLength) != Double.doubleToLongBits(other.degenerateLineLength))
155 if (lineColor == null) {
156 if (other.lineColor != null)
158 } else if (!lineColor.equals(other.lineColor))
160 if (lineStroke == null) {
161 if (other.lineStroke != null)
163 } else if (!lineStroke.equals(other.lineStroke))
165 if (routeLineStroke == null) {
166 if (other.routeLineStroke != null)
168 } else if (!routeLineStroke.equals(other.routeLineStroke))