/
FtileSwitchWithManyLinks.java
332 lines (283 loc) · 11.3 KB
/
FtileSwitchWithManyLinks.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2020, Arnaud Roques
*
* Project Info: http://plantuml.com
*
* If you like this project or if you find it useful, you can support us at:
*
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
*
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
*
* Original Author: Arnaud Roques
*
*
*/
package net.sourceforge.plantuml.activitydiagram3.ftile.vcompact.cond;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import net.sourceforge.plantuml.Direction;
import net.sourceforge.plantuml.activitydiagram3.Branch;
import net.sourceforge.plantuml.activitydiagram3.ftile.AbstractConnection;
import net.sourceforge.plantuml.activitydiagram3.ftile.Arrows;
import net.sourceforge.plantuml.activitydiagram3.ftile.Connection;
import net.sourceforge.plantuml.activitydiagram3.ftile.Ftile;
import net.sourceforge.plantuml.activitydiagram3.ftile.FtileGeometry;
import net.sourceforge.plantuml.activitydiagram3.ftile.FtileUtils;
import net.sourceforge.plantuml.activitydiagram3.ftile.Snake;
import net.sourceforge.plantuml.activitydiagram3.ftile.Swimlane;
import net.sourceforge.plantuml.graphic.Rainbow;
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.graphic.VerticalAlignment;
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.UPolygon;
import net.sourceforge.plantuml.ugraphic.UTranslate;
public class FtileSwitchWithManyLinks extends FtileSwitchWithDiamonds {
private final Rainbow arrowColor;
private final double margin = 10;
public FtileSwitchWithManyLinks(List<Ftile> tiles, List<Branch> branches, Swimlane in, Ftile diamond1,
Ftile diamond2, StringBounder stringBounder, Rainbow arrowColor) {
super(tiles, branches, in, diamond1, diamond2, stringBounder);
this.arrowColor = arrowColor;
}
class ConnectionHorizontalThenVertical extends AbstractConnection {
private final Branch branch;
public ConnectionHorizontalThenVertical(Ftile tile, Branch branch) {
super(diamond1, tile);
this.branch = branch;
}
public void drawU(UGraphic ug) {
final StringBounder stringBounder = ug.getStringBounder();
final Point2D p1 = getP1(stringBounder);
final Point2D p2 = getP2(stringBounder);
final double x1 = p1.getX();
final double y1 = p1.getY();
final double x2 = p2.getX();
final double y2 = p2.getY();
final Snake snake = Snake.create(null, arrowColor, Arrows.asToDown()).withLabel(getLabelPositive(branch),
arrowHorizontalAlignment());
snake.addPoint(x1, y1);
snake.addPoint(x2, y1);
snake.addPoint(x2, y2);
ug.draw(snake);
}
private Point2D getP1(StringBounder stringBounder) {
final FtileGeometry dimDiamond1 = diamond1.calculateDimension(stringBounder);
final Point2D pt;
if (getFtile2() == tiles.get(0)) {
pt = dimDiamond1.getPointD();
} else if (getFtile2() == tiles.get(tiles.size() - 1)) {
pt = dimDiamond1.getPointB();
} else {
throw new IllegalStateException();
}
return getTranslateDiamond1(stringBounder).getTranslated(pt);
}
private Point2D getP2(final StringBounder stringBounder) {
return getTranslateOf(getFtile2(), stringBounder)
.getTranslated(getFtile2().calculateDimension(stringBounder).getPointIn());
}
}
class ConnectionVerticalThenHorizontal extends AbstractConnection {
public ConnectionVerticalThenHorizontal(Ftile tile) {
super(tile, diamond2);
}
public void drawU(UGraphic ug) {
final StringBounder stringBounder = ug.getStringBounder();
final FtileGeometry geo = getFtile1().calculateDimension(stringBounder);
if (geo.hasPointOut() == false) {
return;
}
final Point2D p1 = getP1(stringBounder);
final double x1 = p1.getX();
final double y1 = p1.getY();
final FtileGeometry dimDiamond2 = diamond2.calculateDimension(stringBounder);
final Point2D ptA = getTranslateDiamond2(stringBounder).getTranslated(dimDiamond2.getPointA());
final Point2D ptB = getTranslateDiamond2(stringBounder).getTranslated(dimDiamond2.getPointB());
final Point2D ptD = getTranslateDiamond2(stringBounder).getTranslated(dimDiamond2.getPointD());
final Point2D p2;
final UPolygon arrow;
final Direction direction;
if (x1 < ptD.getX()) {
p2 = ptD;
arrow = Arrows.asToRight();
direction = Direction.RIGHT;
} else if (x1 > ptB.getX()) {
p2 = ptB;
arrow = Arrows.asToLeft();
direction = Direction.LEFT;
} else {
p2 = ptA;
arrow = Arrows.asToDown();
direction = Direction.DOWN;
}
final double x2 = p2.getX();
final double y2 = p2.getY();
final Snake snake = Snake.create(arrowColor, arrow);
snake.addPoint(x1, y1);
if (direction == Direction.LEFT && x2 > x1 - 10) {
snake.addPoint(x1, y2 - 8);
snake.addPoint(x1 + 12, y2 - 8);
snake.addPoint(x1 + 12, y2);
} else {
snake.addPoint(x1, y2);
}
snake.addPoint(x2, y2);
ug.draw(snake);
}
private Point2D getP1(StringBounder stringBounder) {
return getTranslateOf(getFtile1(), stringBounder)
.getTranslated(getFtile1().calculateDimension(stringBounder).getPointOut());
}
}
class ConnectionVerticalTop extends AbstractConnection {
private final Branch branch;
public ConnectionVerticalTop(Ftile tile, Branch branch) {
super(diamond1, tile);
this.branch = branch;
}
public void drawU(UGraphic ug) {
final StringBounder stringBounder = ug.getStringBounder();
final FtileGeometry dimDiamond1 = diamond1.calculateDimension(stringBounder);
final UTranslate translateDiamond1 = getTranslateDiamond1(stringBounder);
final Point2D p1b = translateDiamond1.getTranslated(dimDiamond1.getPointB());
final Point2D p1c = translateDiamond1.getTranslated(dimDiamond1.getPointC());
final Point2D p1d = translateDiamond1.getTranslated(dimDiamond1.getPointD());
final Point2D p2 = getP2(stringBounder);
final double x2 = p2.getX();
final double y2 = p2.getY();
final Snake snake = Snake.create(null, arrowColor, Arrows.asToDown()).withLabel(getLabelPositive(branch),
VerticalAlignment.BOTTOM);
if (x2 < p1d.getX() - margin || x2 > p1b.getX() + margin) {
snake.addPoint(x2, p1d.getY());
snake.addPoint(x2, y2);
} else {
final double x1 = p1c.getX();
final double y1 = p1c.getY();
final double ym = (y1 * 2 + y2) / 3;
snake.addPoint(x1, y1);
snake.addPoint(x1, ym);
snake.addPoint(x2, ym);
snake.addPoint(x2, y2);
}
ug.draw(snake);
}
private Point2D getP2(final StringBounder stringBounder) {
return getTranslateOf(getFtile2(), stringBounder)
.getTranslated(getFtile2().calculateDimension(stringBounder).getPointIn());
}
}
class ConnectionVerticalBottom extends AbstractConnection {
public ConnectionVerticalBottom(Ftile tile) {
super(tile, diamond2);
}
public void drawU(UGraphic ug) {
final StringBounder stringBounder = ug.getStringBounder();
final Point2D p1 = getP1(stringBounder);
final FtileGeometry dimDiamond2 = diamond2.calculateDimension(stringBounder);
final UTranslate translateDiamond2 = getTranslateDiamond2(stringBounder);
final Point2D p2a = translateDiamond2.getTranslated(dimDiamond2.getPointA());
final Point2D p2b = translateDiamond2.getTranslated(dimDiamond2.getPointB());
final Point2D p2d = translateDiamond2.getTranslated(dimDiamond2.getPointD());
final FtileGeometry dimDiamond1 = diamond1.calculateDimension(stringBounder);
final UTranslate translateDiamond1 = getTranslateDiamond1(stringBounder);
final Point2D p1b = translateDiamond1.getTranslated(dimDiamond1.getPointB());
final Point2D p1c = translateDiamond1.getTranslated(dimDiamond1.getPointC());
final Point2D p1d = translateDiamond1.getTranslated(dimDiamond1.getPointD());
final double x1 = p1.getX();
final double y1 = p1.getY();
final double x2 = p2a.getX();
final double y2 = p2a.getY();
final double ym = (y1 + y2) / 2;
final Snake snake = Snake.create(null, arrowColor, Arrows.asToDown());
if (x1 < p1d.getX() - margin || x1 > p1b.getX() + margin) {
snake.addPoint(x1, y1);
snake.addPoint(x1, p2d.getY());
} else {
snake.addPoint(x1, y1);
snake.addPoint(x1, ym);
snake.addPoint(x2, ym);
snake.addPoint(x2, y2);
}
ug.draw(snake);
}
private Point2D getP1(StringBounder stringBounder) {
return getTranslateOf(getFtile1(), stringBounder)
.getTranslated(getFtile1().calculateDimension(stringBounder).getPointOut());
}
}
public double getYdelta1a(StringBounder stringBounder) {
double max = 10;
for (Branch branch : branches) {
max = Math.max(max, getLabelPositive(branch).calculateDimension(stringBounder).getHeight());
}
return max + 10;
}
public Ftile addLinks(StringBounder stringBounder) {
final List<Connection> conns = new ArrayList<>();
addIngoingArrows(conns);
addOutgoingArrows(stringBounder, conns);
return FtileUtils.addConnection(this, conns);
}
private void addIngoingArrows(final List<Connection> conns) {
conns.add(new ConnectionHorizontalThenVertical(tiles.get(0), branches.get(0)));
conns.add(new ConnectionHorizontalThenVertical(tiles.get(tiles.size() - 1), branches.get(tiles.size() - 1)));
for (int i = 1; i < tiles.size() - 1; i++) {
final Ftile tile = tiles.get(i);
conns.add(new ConnectionVerticalTop(tile, branches.get(i)));
}
}
private void addOutgoingArrows(StringBounder stringBounder, final List<Connection> conns) {
final int firstOutgoingArrow = getFirstOutgoingArrow(stringBounder);
final int lastOutgoingArrow = getLastOutgoingArrow(stringBounder);
if (firstOutgoingArrow < tiles.size())
conns.add(new ConnectionVerticalThenHorizontal(tiles.get(firstOutgoingArrow)));
if (lastOutgoingArrow > 0)
conns.add(new ConnectionVerticalThenHorizontal(tiles.get(lastOutgoingArrow)));
for (int i = firstOutgoingArrow + 1; i < lastOutgoingArrow; i++) {
final Ftile tile = tiles.get(i);
if (tile.calculateDimension(stringBounder).hasPointOut()) {
conns.add(new ConnectionVerticalBottom(tile));
}
}
}
private int getFirstOutgoingArrow(StringBounder stringBounder) {
for (int i = 0; i < tiles.size() - 1; i++) {
final Ftile tile = tiles.get(i);
if (tile.calculateDimension(stringBounder).hasPointOut()) {
return i;
}
}
return tiles.size();
}
private int getLastOutgoingArrow(StringBounder stringBounder) {
for (int i = tiles.size() - 1; i >= 0; i--) {
final Ftile tile = tiles.get(i);
if (tile.calculateDimension(stringBounder).hasPointOut()) {
return i;
}
}
return -1;
}
}