/
SessionEditorEdge.java
324 lines (257 loc) · 11.1 KB
/
SessionEditorEdge.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
///////////////////////////////////////////////////////////////////////////////
// For information as to what this class does, see the Javadoc, below. //
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, //
// 2007, 2008, 2009, 2010, 2014, 2015, 2022 by Peter Spirtes, Richard //
// Scheines, Joseph Ramsey, and Clark Glymour. //
// //
// This program 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 2 of the License, or //
// (at your option) any later version. //
// //
// This program is 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 program; if not, write to the Free Software //
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
///////////////////////////////////////////////////////////////////////////////
package edu.cmu.tetradapp.app;
import edu.cmu.tetradapp.workbench.DisplayEdge;
import edu.cmu.tetradapp.workbench.PointPair;
import java.awt.*;
/**
* Presents an edge in the Tetrad SessionWorkbench.
*
* @author josephramsey
*/
final class SessionEditorEdge extends DisplayEdge {
/* Colors */
private static final Color DIE_BACKGROUND = Color.red;
private static final Color DIE_DOT = Color.black;
/* Modes */
public static final int UNRANDOMIZED = 0;
private static final int RANDOMIZED = 1;
/* States */
private final Color curr_color = SessionEditorEdge.DIE_BACKGROUND;
private int sessionEdgeMode;
/**
* Constructs a new SessionEditorEdge connecting two components, 'node1' and
* 'node2'. The anchor component will be node1.
*
* @param node1 the 'from' component.
* @param node2 the 'to' component.
* @param sessionEdgeMode the sessionEdgeMode of the edge, either
* UNRANDOMIZED or RANDOMIZED.
*/
public SessionEditorEdge(SessionEditorNode node1, SessionEditorNode node2,
int sessionEdgeMode) {
super(node1, node2, DisplayEdge.SESSION);
if ((sessionEdgeMode >= 0) && (sessionEdgeMode <= 1)) {
this.sessionEdgeMode = sessionEdgeMode;
} else {
throw new IllegalArgumentException();
}
}
/**
* Constructs a new unanchored session edge. The end of the edge at 'node1'
* is anchored, but the other end tracks a mouse point. The mouse point
* should be updated by the parent component using repeated calls to
* 'updateTrackPoint'; this process is finished by finally anchoring the
* second end of the of edge using 'anchorSecondEnd'. Once this is done,
* the edge is considered anchored and will not be able to track a mouse
* point any longer.
*
* @param node1 the 'from' component.
* @param mouseTrackPoint the initial value of the mouse track point.
* @see #updateTrackPoint
*/
public SessionEditorEdge(SessionEditorNode node1, Point mouseTrackPoint) {
super(node1, mouseTrackPoint, DisplayEdge.SESSION);
}
/**
* Constructs a new unanchored session edge. The end of the edge a 'node1'
* is anchored, but the other end tracks a mouse point. The mouse point
* should be updated by the parent component using repeated calls to
* 'updateTrackPoint'; this process is finished by finally anchoring the
* second end of the of edge using 'anchorSecondEnd'. Once this is done,
* the edge is considered anchored and will not be able to track a mouse
* point any longer.
*
* @param node1 the 'from' component.
* @param mouseTrackPoint the initial value of the mouse track point.
* @param mode ??
* @see #updateTrackPoint
*/
public SessionEditorEdge(SessionEditorNode node1, Point mouseTrackPoint,
int mode) {
super(node1, mouseTrackPoint, DisplayEdge.SESSION);
this.sessionEdgeMode = mode;
}
/**
* Calculates the sleeve of the die.
*
* @param dice the four points defining the die.
* @return the sleeve
*/
private static Polygon calcDiceSleeve(Point[] dice) {
int[] xpoint = new int[4];
int[] ypoint = new int[4];
xpoint[0] = dice[0].x;
xpoint[1] = dice[1].x;
xpoint[2] = dice[2].x;
xpoint[3] = dice[3].x;
ypoint[0] = dice[0].y;
ypoint[1] = dice[1].y;
ypoint[2] = dice[2].y;
ypoint[3] = dice[3].y;
return new Polygon(xpoint, ypoint, 4);
}
private void drawDice(Graphics g, Color c) {
Polygon dice = getDiceSleeve();
Circle[] dicedot = getDiceDot();
g.setColor(c);
g.fillPolygon(dice);
g.setColor(SessionEditorEdge.DIE_DOT);
g.drawPolygon(dice);
int height = dicedot[0].radius * 2;
for (Circle aDicedot : dicedot) {
g.fillOval(aDicedot.center.x, aDicedot.center.y, height,
height);
}
}
/**
* Calculates the four corners of the die
*
* @return this array of points.
*/
private Point[] getDiceArea() {
int[] xpoint = new int[4];
int[] ypoint = new int[4];
PointPair pp = getConnectedPoints();
Point midPoint = new Point((pp.getFrom().x + pp.getTo().x) / 2,
(pp.getFrom().y + pp.getTo().y) / 2);
double d = DisplayEdge.distance(pp.getFrom(), pp.getTo());
if (d < 1) {
d = 1;
}
double sin = (pp.getFrom().y - pp.getTo().y) / d;
double cos = (pp.getFrom().x - pp.getTo().x) / d;
xpoint[0] = (int) (midPoint.x - 10 * cos);
xpoint[1] = (int) (midPoint.x - 10 * sin);
xpoint[2] = (int) (midPoint.x + 10 * cos);
xpoint[3] = (int) (midPoint.x + 10 * sin);
ypoint[0] = (int) (midPoint.y + 10 * sin);
ypoint[1] = (int) (midPoint.y - 10 * cos);
ypoint[2] = (int) (midPoint.y - 10 * sin);
ypoint[3] = (int) (midPoint.y + 10 * cos);
Point[] dice = new Point[4];
dice[0] = new Point(xpoint[0], ypoint[0]);
dice[1] = new Point(xpoint[1], ypoint[1]);
dice[2] = new Point(xpoint[2], ypoint[2]);
dice[3] = new Point(xpoint[3], ypoint[3]);
return dice;
}
private Circle[] getDiceDot() {
PointPair pp = getConnectedPoints();
Point midPoint = new Point((pp.getFrom().x + pp.getTo().x) / 2,
(pp.getFrom().y + pp.getTo().y) / 2);
Point[] dice = getDiceArea();
Circle[] dot = new Circle[5];
dot[0] = new Circle(new Point(midPoint.x - 1, midPoint.y - 1), 2);
dot[1] = new Circle(new Point((dice[0].x + midPoint.x) / 2 - 1,
(dice[0].y + midPoint.y) / 2 - 1), 2);
dot[2] = new Circle(new Point((dice[1].x + midPoint.x) / 2 - 1,
(dice[1].y + midPoint.y) / 2 - 1), 2);
dot[3] = new Circle(new Point((dice[2].x + midPoint.x) / 2 - 1,
(dice[2].y + midPoint.y) / 2 - 1), 2);
dot[4] = new Circle(new Point((dice[3].x + midPoint.x) / 2 - 1,
(dice[3].y + midPoint.y) / 2 - 1), 2);
return dot;
}
private Polygon getDiceSleeve() {
return SessionEditorEdge.calcDiceSleeve(getDiceArea());
}
/**
* This method paints the component.
*
* @param g the graphics context.
*/
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
// NOTE: For this component, the resetBounds() methods should ALWAYS
// be called before repaint().
Stroke s;
boolean thick = true;
float width = thick ? 2.5f : 1.1f;
Stroke solid = new BasicStroke(width);
g2d.setStroke(solid);
PointPair pp;
switch (getMode()) {
case DisplayEdge.HALF_ANCHORED:
g2d.setColor(getLineColor());
pp = calculateEdge(getNode1(), getRelativeMouseTrackPoint());
if (pp != null) {
pp.getFrom().translate(-getLocation().x, -getLocation().y);
pp.getTo().translate(-getLocation().x, -getLocation().y);
setClickRegion(null);
g2d.drawLine(pp.getFrom().x, pp.getFrom().y, pp.getTo().x,
pp.getTo().y);
drawEndpoints(pp, g2d);
firePropertyChange("newPointPair", null, pp);
}
break;
case DisplayEdge.ANCHORED_UNSELECTED:
g2d.setColor(getLineColor());
pp = calculateEdge(getNode1(), getNode2());
if (pp != null) {
pp.getFrom().translate(-getLocation().x, -getLocation().y);
pp.getTo().translate(-getLocation().x, -getLocation().y);
setClickRegion(null);
g2d.drawLine(pp.getFrom().x, pp.getFrom().y, pp.getTo().x,
pp.getTo().y);
drawEndpoints(pp, g2d);
firePropertyChange("newPointPair", null, pp);
}
break;
case DisplayEdge.ANCHORED_SELECTED:
g2d.setColor(getSelectedColor());
pp = calculateEdge(getNode1(), getNode2());
if (pp != null) {
pp.getFrom().translate(-getLocation().x, -getLocation().y);
pp.getTo().translate(-getLocation().x, -getLocation().y);
setClickRegion(null);
g2d.drawLine(pp.getFrom().x, pp.getFrom().y, pp.getTo().x,
pp.getTo().y);
drawEndpoints(pp, g2d);
firePropertyChange("newPointPair", null, pp);
}
break;
default:
throw new IllegalStateException();
}
setConnectedPoints(pp);
if (this.sessionEdgeMode == SessionEditorEdge.RANDOMIZED) {
drawDice(g2d, this.curr_color);
}
}
/**
* Holds the radius and diameter of a circle.
*
* @author Pucktada
*/
private static final class Circle {
public final int radius;
public final Point center;
/**
* @param c the center of the circle.
* @param r the radius of the circle.
*/
public Circle(Point c, int r) {
this.radius = r;
this.center = c;
}
}
}