]> gerrit.simantics Code Review - simantics/platform.git/blob
b084e5875714938026400e5a8b6c938659e3ca71
[simantics/platform.git] /
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 gnu.trove.set.hash.THashSet;
15
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;
22
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;
28
29
30 public class StyledRouteGraphRenderer implements IRouteGraphRenderer, Serializable {
31
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);
34     
35         private static final long serialVersionUID = 1564960933064029020L;
36
37         protected final ConnectionStyle style;
38
39         // Caches to avoid creating new objects all the time during rendering
40         protected transient Path2D path;
41         protected transient THashSet<RoutePoint> branchPoints;
42
43         public StyledRouteGraphRenderer(ConnectionStyle style) {
44                 if (style == null)
45                         throw new NullPointerException("null style");
46                 this.style = style;
47         }
48
49         public ConnectionStyle getStyle() {
50                 return style;
51         }
52
53         @Override
54         public void render(Graphics2D g, RouteGraph rg) {
55                 if (path == null)
56                         path = new Path2D.Double();
57                 path.reset();
58                 rg.getPath2D(path);
59                 style.drawPath(g, path, false);
60
61                 if (branchPoints == null)
62                         branchPoints = new THashSet<RoutePoint>();
63                 branchPoints.clear();
64                 for(RouteLine line : rg.getLines()) {
65                     renderLine(g, line, false);
66                     collectBranchPoints(line, branchPoints);
67                 }
68                 for(RouteLine line : rg.getTransientLines()) {
69                         renderLine(g, line, true);
70                         collectBranchPoints(line, branchPoints);
71                 }
72
73                 /*for(RouteTerminal terminal : rg.getTerminals())
74                         terminal.render(g);*/
75                 for(RoutePoint point : branchPoints) {
76                         style.drawBranchPoint(g, point.getX(), point.getY());
77                 }
78         }
79         
80         private static void collectBranchPoints(RouteLine line, THashSet<RoutePoint> branchPoints) {
81                 List<RoutePoint> points = line.getPoints();
82                 for(int i=1;i<points.size()-1;++i) {
83                         RoutePoint point = points.get(i);
84                         branchPoints.add(point);
85                 }
86         }
87         
88         private void renderLine(Graphics2D g, RouteLine line, boolean isTransient) {
89             if(line.getPoints().size() == 0) {
90                 System.err.println("Route line does not contain any points (data = " + line.getData() + ").");
91                 return;
92             }
93                 RoutePoint p1 = line.getBegin();
94                 RoutePoint p2 = line.getEnd();
95                 double x1 = p1.getX();
96                 double y1 = p1.getY();
97                 double x2 = p2.getX();
98                 double y2 = p2.getY();
99                 boolean isHorizontal = line.isHorizontal();
100 //              double length = isHorizontal ? x2-x1 : y2-y1; // original length before removing the endpoints
101                 if(isHorizontal) {
102                         if(p1 instanceof RouteTerminal) {
103                                 RouteTerminal terminal = (RouteTerminal)p1;
104                                 ILineEndStyle style = terminal.getRenderStyle();
105                                 style.render(g, x1, y1, 2);
106                                 x1 += style.getLineEndLength(0);
107                         }
108                         if(p2 instanceof RouteTerminal) {
109                                 RouteTerminal terminal = (RouteTerminal)p2;
110                                 ILineEndStyle style = terminal.getRenderStyle();
111                                 style.render(g, x2, y2, 0);
112                                 x2 -= style.getLineEndLength(2);
113                         }
114                 }
115                 else {
116                         if(p1 instanceof RouteTerminal) {
117                                 RouteTerminal terminal = (RouteTerminal)p1;
118                                 ILineEndStyle style = terminal.getRenderStyle();
119                                 style.render(g, x1, y1, 3);
120                                 y1 += style.getLineEndLength(1);
121                         }
122                         if(p2 instanceof RouteTerminal) {
123                                 RouteTerminal terminal = (RouteTerminal)p2;
124                                 ILineEndStyle style = terminal.getRenderStyle();
125                                 style.render(g, x2, y2, 1);
126                                 y2 -= style.getLineEndLength(3);
127                         }
128                 }
129 //              if(length <= style.getDegeneratedLineLength())
130 //                      style.drawDegeneratedLine(g, 0.5*(x1+x2), 0.5*(y1+y2), isHorizontal, isTransient);
131 //              else
132 //                      style.drawLine(g, x1, y1, x2, y2, isTransient);
133         }
134         
135         @Override
136         public void renderGuides(Graphics2D g, RouteGraph rg) {
137             Path2D path = new Path2D.Double();
138         for(RouteLine line : rg.getLines()) {
139             RoutePoint p1 = line.getBegin();
140             RoutePoint p2 = line.getEnd();
141             double dx = p2.getX() - p1.getX();
142             double dy = p2.getY() - p1.getY();
143             double adx = Math.abs(dx);
144             double ady = Math.abs(dy);
145             if(adx > ady) {
146                 if(adx > 4)
147                     dx = 0.5*dx - Math.signum(dx);
148                 else
149                     dx *= 0.25;
150                 path.moveTo(p1.getX()+dx, p1.getY());
151                 path.lineTo(p2.getX()-dx, p2.getY());
152             }
153             else {
154                 if(ady > 4)
155                     dy = 0.5*dy - Math.signum(dy);
156                 else
157                     dy *= 0.25;
158                 path.moveTo(p1.getX(), p1.getY()+dy);
159                 path.lineTo(p2.getX(), p2.getY()-dy);
160             }
161         }
162         g.setStroke(GUIDE_LINE_STROKE);
163         g.setColor(GUIDE_LINE_COLOR);
164         g.draw(path);
165         }
166
167         @Override
168         public int hashCode() {
169                 return style.hashCode();
170         }
171
172         @Override
173         public boolean equals(Object obj) {
174                 if (this == obj)
175                         return true;
176                 if (obj == null)
177                         return false;
178                 if (getClass() != obj.getClass())
179                         return false;
180                 StyledRouteGraphRenderer other = (StyledRouteGraphRenderer) obj;
181                 return style.equals(other.style);
182         }
183
184 }