-
Notifications
You must be signed in to change notification settings - Fork 9
/
ArrowLineData.java
372 lines (321 loc) · 9.9 KB
/
ArrowLineData.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* Classname : ArrowLineData
*
* Version information : 1
*
* Date
*
* Description : Contains methods for drawing
* lines for the
* associatios
*/
/******************************
* Copyright (c) 2003-2022 Kevin Lano
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
* *****************************/
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.Vector;
public class ArrowLineData extends VisualData
{ // coordinates of start and end points of the line
int xstart, xend;
int ystart, yend;
Transition transition;
Flow flow; /* One of these is null usually */
//coordinates for the arrow head
double arrow1x, arrow1y;
double arrow2x, arrow2y;
// coordinates for roles and cards
double card1x, card1y;
double card2x, card2y;
double role1x, role1y;
double role2x, role2y;
double length; //length of the line
// The line type -- solid or dashed
int linetype;
// Solid line stroke
final static BasicStroke stroke =
new BasicStroke(2.0f);
// For producing dashed lines
final static float dash1[] = {10.0f};
final static BasicStroke dashed =
new BasicStroke(1.0f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
10.0f, dash1, 0.0f);
public static final int NONESELECTED = 0;
public static final int MIDSELECTED = 1;
public static final int STARTSELECTED = 2;
public static final int ENDSELECTED = 3;
int selected = NONESELECTED;
public ArrowLineData(int xs, int ys, int xe,
int ye, int linecount, int type)
{ super(new String("t" + linecount),Color.black);
xstart = xs;
ystart = ys;
xend = xe;
yend = ye;
linetype = type;
// This places the name in the middle of the line
namex = (xstart + xend)/2;
namey = (ystart + yend)/2;
setArrows();
}
public void setStartSelected()
{ selected = STARTSELECTED; }
public void setMidSelected()
{ selected = MIDSELECTED; }
public void setEndSelected()
{ selected = ENDSELECTED; }
public Object clone()
{ String cnt = label.substring(1,label.length()-1);
int count = 0;
try { count = Integer.parseInt(cnt); }
catch (Exception e)
{ count = 0; }
LineData ld =
new LineData(xstart, ystart, xend,
yend, count, linetype);
ld.setTransition(transition);
ld.setFlow(flow);
ld.setModelElement(modelElement);
return ld;
}
public void setTransition(Transition t)
{ transition = t; }
public void setFlow(Flow fl)
{ flow = fl; }
// returns x position of first point of the line
int getx()
{ return xstart; }
// returns y position of the first point of line
int gety()
{ return ystart; }
int LineLength()
{ return (int) Math.sqrt((xend - xstart)*
(xend - xstart) +
(yend - ystart)*(yend - ystart));
}
/* Identifies the components that it is under */
boolean isUnder(int x, int y)
{ boolean res = false;
int midx = (xstart + xend)/2;
int midy = (ystart + yend)/2;
res = ((int) Math.sqrt((x - midx)*(x - midx) +
(y - midy)*(y - midy)) < 10);
if (res) { selected = MIDSELECTED; }
return res;
}
boolean isUnderStart(int x, int y)
{ boolean res =
((int) Math.sqrt((x - xstart)*(x - xstart) +
(y - ystart)*(y - ystart)) < 10);
if (res) { selected = STARTSELECTED; }
return res;
}
boolean isUnderEnd(int x, int y)
{ boolean res =
((int) Math.sqrt((x - xend)*(x - xend) +
(y - yend)*(y - yend)) < 10);
if (res) { selected = ENDSELECTED; }
return res;
}
boolean isNearEnd(int x, int y)
{ boolean res =
((int) Math.sqrt((x - xend)*(x - xend) +
(y - yend)*(y - yend)) < 30);
// if (res) { selected = ENDSELECTED; }
return res;
}
void SetName(String s, int x, int y)
{ label = s;
namex = x;
namey = y;
}
boolean nearlyEqual(int a, int b)
{ return ((a <= b+5 && b <= a) ||
(b <= a+5 && a <= b));
}
void setArrows()
{ // The vector from start to end of line
double xvect = xend - xstart;
double yvect = yend - ystart;
// The length of the line drawn
length = Math.sqrt((xvect*xvect)+ (yvect*yvect));
double xdir = xvect/length ;
double ydir = yvect/length;
arrow1x = xend - (5*xdir) - (5*ydir);
arrow1y = yend - (5*ydir) + (5*xdir);
arrow2x = xend - (5*xdir) + (5*ydir);
arrow2y = yend - (5*ydir) - (5*xdir);
role2x = xend - (10*xdir) - (8*ydir);
role2y = yend - (8*ydir) + (10*xdir);
card2x = xend - (10*xdir) + (8*ydir);
card2y = yend - (8*ydir) - (10*xdir);
}
// Base the roles and cards on these positions as well?
void drawData(Graphics2D g)
{ if (LineLength() > 5)
{ g.setColor(Color.black);
if (linetype == 1)
{ g.setStroke(dashed); }
g.drawLine(xstart,ystart,xend,yend);
g.setStroke(stroke);
g.draw(new Line2D.Double(arrow1x,arrow1y,
(double) xend,(double) yend));
g.draw(new Line2D.Double(arrow2x,arrow2y,
(double) xend,(double) yend));
if (modelElement != null)
{ Association ast = (Association) modelElement;
String role1 = ast.getRole1();
String role2 = ast.getRole2();
String c1 =
ModelElement.convertCard(ast.getCard1());
String c2 =
ModelElement.convertCard(ast.getCard2());
if (role1 != null)
{ g.drawString(role1,xstart+5,ystart-5); }
if (role2 != null)
{ g.drawString(role2,(int) role2x,(int) role2y); }
g.drawString(c1,xstart+5,ystart+10);
g.drawString(c2,(int) card2x,(int) card2y);
}
}
}
void drawData(Graphics g)
{ if (LineLength() > 5)
{ g.setColor(Color.black);
// if (linetype == 1)
// { g.setStroke(dashed); }
g.drawLine(xstart,ystart,xend,yend);
// g.setStroke(stroke);
g.drawLine((int) arrow1x,(int) arrow1y,xend,yend);
g.drawLine((int) arrow2x,(int) arrow2y,xend,yend);
if (modelElement != null)
{ Association ast = (Association) modelElement;
String role1 = ast.getRole1();
String role2 = ast.getRole2();
String c1 =
ModelElement.convertCard(ast.getCard1());
String c2 =
ModelElement.convertCard(ast.getCard2());
if (role1 != null)
{ g.drawString(role1,xstart+5,ystart-5); }
if (role2 != null)
{ g.drawString(role2,(int) role2x,(int) role2y); }
g.drawString(c1,xstart+5,ystart+8);
g.drawString(c2,(int) card2x,(int) card2y);
}
}
}
public void changePosition(int oldx, int oldy, int x, int y)
{ if (selected == STARTSELECTED)
{ xstart = x;
ystart = y;
}
else if (selected == ENDSELECTED)
{ xend = x;
yend = y;
}
else if (selected == MIDSELECTED)
{ int xmid = (xstart + xend)/2;
int ymid = (ystart + yend)/2;
xstart = xstart + x - xmid;
ystart = ystart + y - ymid;
xend = xend + x - xmid;
yend = yend + y - ymid;
}
namex = (xstart + xend)/2;
namey = (ystart + yend)/2;
setArrows();
}
public void shrink(int factor)
{ super.shrink(factor);
xstart = xstart/factor;
ystart = ystart/factor;
xend = xend/factor;
yend = yend/factor;
setArrows();
}
public void moveUp(int amount)
{ super.moveUp(amount);
ystart -= amount;
yend -= amount;
setArrows();
}
public void moveDown(int amount)
{ super.moveDown(amount);
ystart += amount;
yend += amount;
setArrows();
}
public void moveLeft(int amount)
{ super.moveLeft(amount);
xstart -= amount;
xend -= amount;
setArrows();
}
public void moveRight(int amount)
{ super.moveRight(amount);
xstart += amount;
xend += amount;
setArrows();
}
public boolean isInput()
{ return linetype == UCDArea.DASHED; }
public void setInput(boolean ii)
{ if (ii)
{ linetype = UCDArea.DASHED; }
else
{ linetype = UCDArea.SOLID; }
}
public static LineData
drawBetween(RectForm rd1, RectForm rd2,
int i)
{ int x1 = rd1.getx();
int y1 = rd1.gety();
int x2 = rd2.getx();
int y2 = rd2.gety();
int deltas = 10;
int deltae = 5;
int startx, starty, endx, endy;
if (y1 < y2)
{ startx = x1 + deltae;
starty = y1 + rd1.height - deltas;
endx = x2 + deltae;
endy = y2 + deltae;
}
else if (y2 < y1)
{ startx = x1 + rd1.width - deltas;
starty = y1 + deltae;
endx = x2 + rd2.width - deltas;
endy = y2 + rd2.height - deltas;
}
else if (x1 < x2)
{ startx = x1 + rd1.width - deltas;
starty = y1 + deltae;
endx = x2 + deltae;
endy = y2 + deltae;
}
else if (x2 < x1)
{ startx = x1 + deltae;
starty = y1 + rd1.height - deltas;
endx = x2 + rd2.width - deltas;
endy = y2 + rd2.height - deltas;
}
else
{ startx = x1 + deltae;
starty = y1 + deltae;
endx = x2 + rd2.width - deltas;
endy = y2 + rd2.height - deltas;
}
return new LineData(startx, starty,
endx, endy, i,
UCDArea.DASHED);
}
}