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 gnu.trove.set.hash.THashSet;
16 import java.awt.BasicStroke;
17 import java.awt.Color;
18 import java.awt.Graphics2D;
19 import java.awt.geom.Path2D;
20 import java.io.Serializable;
21 import java.util.List;
23 import org.simantics.diagram.connection.RouteGraph;
24 import org.simantics.diagram.connection.RouteLine;
25 import org.simantics.diagram.connection.RoutePoint;
26 import org.simantics.diagram.connection.RouteTerminal;
27 import org.simantics.diagram.connection.rendering.arrows.ILineEndStyle;
30 public class StyledRouteGraphRenderer implements IRouteGraphRenderer, Serializable {
32 private static final BasicStroke GUIDE_LINE_STROKE = new BasicStroke(0.1f);
33 private static final Color GUIDE_LINE_COLOR = new Color(255,255,255);
35 private static final long serialVersionUID = 1564960933064029020L;
37 protected final ConnectionStyle style;
39 private static class Cache {
40 Path2D path = new Path2D.Double();
41 THashSet<RoutePoint> branchPoints = new THashSet<>();
44 // Caches to avoid creating new objects all the time during rendering
45 protected static ThreadLocal<Cache> caches = ThreadLocal.withInitial(() -> new Cache());
47 public StyledRouteGraphRenderer(ConnectionStyle style) {
49 throw new NullPointerException("null style");
53 public ConnectionStyle getStyle() {
58 public void render(Graphics2D g, RouteGraph rg) {
59 Cache cache = caches.get();
60 Path2D path = cache.path;
61 THashSet<RoutePoint> branchPoints = cache.branchPoints;
65 style.drawPath(g, path, false);
68 for(RouteLine line : rg.getLines()) {
69 renderLine(g, line, false);
70 collectBranchPoints(line, branchPoints);
72 for(RouteLine line : rg.getTransientLines()) {
73 renderLine(g, line, true);
74 collectBranchPoints(line, branchPoints);
77 /*for(RouteTerminal terminal : rg.getTerminals())
79 for(RoutePoint point : branchPoints) {
80 style.drawBranchPoint(g, point.getX(), point.getY());
84 private static void collectBranchPoints(RouteLine line, THashSet<RoutePoint> branchPoints) {
85 List<RoutePoint> points = line.getPoints();
86 for(int i=1;i<points.size()-1;++i) {
87 RoutePoint point = points.get(i);
88 branchPoints.add(point);
92 private void renderLine(Graphics2D g, RouteLine line, boolean isTransient) {
93 if(line.getPoints().size() == 0) {
94 System.err.println("Route line does not contain any points (data = " + line.getData() + ").");
97 RoutePoint p1 = line.getBegin();
98 RoutePoint p2 = line.getEnd();
99 double x1 = p1.getX();
100 double y1 = p1.getY();
101 double x2 = p2.getX();
102 double y2 = p2.getY();
103 boolean isHorizontal = line.isHorizontal();
104 // double length = isHorizontal ? x2-x1 : y2-y1; // original length before removing the endpoints
106 if(p1 instanceof RouteTerminal) {
107 RouteTerminal terminal = (RouteTerminal)p1;
108 ILineEndStyle style = terminal.getRenderStyle();
109 style.render(g, x1, y1, 2);
110 x1 += style.getLineEndLength(0);
112 if(p2 instanceof RouteTerminal) {
113 RouteTerminal terminal = (RouteTerminal)p2;
114 ILineEndStyle style = terminal.getRenderStyle();
115 style.render(g, x2, y2, 0);
116 x2 -= style.getLineEndLength(2);
120 if(p1 instanceof RouteTerminal) {
121 RouteTerminal terminal = (RouteTerminal)p1;
122 ILineEndStyle style = terminal.getRenderStyle();
123 style.render(g, x1, y1, 3);
124 y1 += style.getLineEndLength(1);
126 if(p2 instanceof RouteTerminal) {
127 RouteTerminal terminal = (RouteTerminal)p2;
128 ILineEndStyle style = terminal.getRenderStyle();
129 style.render(g, x2, y2, 1);
130 y2 -= style.getLineEndLength(3);
133 // if(length <= style.getDegeneratedLineLength())
134 // style.drawDegeneratedLine(g, 0.5*(x1+x2), 0.5*(y1+y2), isHorizontal, isTransient);
136 // style.drawLine(g, x1, y1, x2, y2, isTransient);
140 public void renderGuides(Graphics2D g, RouteGraph rg) {
141 Path2D path = new Path2D.Double();
142 for(RouteLine line : rg.getLines()) {
143 RoutePoint p1 = line.getBegin();
144 RoutePoint p2 = line.getEnd();
145 double dx = p2.getX() - p1.getX();
146 double dy = p2.getY() - p1.getY();
147 double adx = Math.abs(dx);
148 double ady = Math.abs(dy);
151 dx = 0.5*dx - Math.signum(dx);
154 path.moveTo(p1.getX()+dx, p1.getY());
155 path.lineTo(p2.getX()-dx, p2.getY());
159 dy = 0.5*dy - Math.signum(dy);
162 path.moveTo(p1.getX(), p1.getY()+dy);
163 path.lineTo(p2.getX(), p2.getY()-dy);
166 g.setStroke(GUIDE_LINE_STROKE);
167 g.setColor(GUIDE_LINE_COLOR);
172 public int hashCode() {
173 return style.hashCode();
177 public boolean equals(Object obj) {
182 if (getClass() != obj.getClass())
184 StyledRouteGraphRenderer other = (StyledRouteGraphRenderer) obj;
185 return style.equals(other.style);