1
2 package org.catacomb.graph.arbor;
3
4
5 import org.catacomb.be.Position;
6 import org.catacomb.datalish.Box;
7 import org.catacomb.graph.gui.*;
8 import org.catacomb.report.E;
9
10 import java.awt.Color;
11
12
13 public class SegmentGraphVE implements BuildPaintInstructor, PickListener {
14
15
16 SegmentGraph graph;
17
18 double pressX;
19 double pressY;
20 double pressRadius;
21
22
23 boolean selectable;
24 boolean modifiable;
25 boolean antialias;
26
27
28 int selectAction;
29 public final static int VIEW = 0;
30 public final static int EDIT = 1;
31 public final static int MOVE = 2;
32 public final static int DELETE = 3;
33 public final static int PATH = 4;
34 public final static int TREE = 5;
35 public final static int TRACE = 6;
36
37 final static String[] actionNames = {"view", "edit", "move", "delete",
38 "selectPath", "selectTree", "trace"
39 };
40
41
42 SegmentGraphPoint sgp1;
43 SegmentGraphPoint sgp2;
44
45
46 public SegmentGraphVE() {
47 antialias = true;
48 selectAction = 0;
49 }
50
51
52 public void setSegmentGraph(SegmentGraph sg) {
53 graph = sg;
54 }
55
56
57
58 public void setAntialias(boolean b) {
59 antialias = b;
60 }
61
62
63
64 public void setSelectAction(String sin) {
65 String s = sin;
66 if (s == null) {
67 s = "view";
68 }
69 int iact = -1;
70 for (int i = 0; i < actionNames.length; i++) {
71 if (actionNames[i].equals(s)) {
72 iact = i;
73 }
74 }
75
76 if (iact >= 0) {
77 setSelectAction(iact);
78 } else {
79 E.error("dont know action " + s);
80 }
81 }
82
83
84 public void setSelectAction(int act) {
85 selectAction = act;
86 clearSelect();
87
88 }
89
90
91 public void clearSelect() {
92 sgp1 = null;
93 sgp2 = null;
94 }
95
96
97
98 public boolean antialias() {
99 return antialias;
100 }
101
102
103
104
105
106 public void instruct(Painter painter, Builder builder) {
107 if (graph == null) {
108 return;
109 }
110
111
112
113
114 if (selectAction == EDIT) {
115 for (SegmentGraphPoint sgp : graph.getPoints()) {
116 paintProto(sgp, painter, builder);
117 }
118 }
119
120
121 for (SegmentGraphPoint sgp : graph.getPoints()) {
122 paintReal(sgp, painter, builder);
123 }
124
125 }
126
127
128
129
130 public void paintProto(SegmentGraphPoint sgp,
131 Painter painter, Builder builder) {
132 SegmentGraphPoint sgpe = sgp.getExtensionProto();
133
134 painter.setColor(Color.gray);
135 painter.drawLine(sgp.getPosition(), sgpe.getPosition());
136
137 builder.addPoint(sgpe);
138
139
140 SegmentGraphPoint[] psgps = sgp.getProtoNeighbors();
141 SegmentGraphPoint[] rsgps = sgp.getNeighbors();
142
143 int index = sgp.getIndex();
144 for (int i = 0; i < psgps.length; i++) {
145 if (index < rsgps[i].getIndex()) {
146 builder.addPoint(psgps[i]);
147 }
148 }
149
150 }
151
152
153
154
155
156
157 public void paintReal(SegmentGraphPoint sgp,
158 Painter painter, Builder builder) {
159
160 if (selectAction != VIEW) {
161 builder.addPoint(sgp);
162 }
163
164 SegmentGraphPoint[] nbrs = sgp.getNeighbors();
165 int index = sgp.getIndex();
166
167
168 if (sgp.isHighlighted()) {
169 painter.setColor(Color.orange);
170 painter.fillCircle(sgp.getPosition(), sgp.getRadius());
171
172 } else {
173 painter.setColor(Color.white);
174 painter.drawCircle(sgp.getPosition(), sgp.getRadius());
175 }
176
177
178
179 for (int i = 0; i < nbrs.length; i++) {
180 if (nbrs[i].getIndex() > index) {
181 if (sgp.isHighlighted() && nbrs[i].isHighlighted()) {
182 painter.setColor(Color.orange);
183 } else {
184 painter.setColor(Color.white);
185 }
186 drawSegment(painter, sgp, nbrs[i]);
187 }
188 }
189 }
190
191
192 public void drawSegment(Painter painter,
193 SegmentGraphPoint sgpa, SegmentGraphPoint sgpb) {
194 Position p1 = sgpa.getPosition();
195 double x1 = p1.getX();
196 double y1 = p1.getY();
197 double r1 = sgpa.getRadius();
198
199 Position p2 = sgpb.getPosition();
200 double x2 = p2.getX();
201 double y2 = p2.getY();
202 double r2 = sgpb.getRadius();
203
204
205 painter.drawCarrotSides(x1, y1, r1, x2, y2, r2);
206 }
207
208
209
210 public void backgroundPressed(int i, int x, int y) {
211
212 }
213
214
215 public void pickPressed(Pickable pbl, int button, int ix, int iy) {
216
217 if (pbl instanceof SegmentGraphPoint) {
218 SegmentGraphPoint sgp = (SegmentGraphPoint)pbl;
219
220
221
222 if (button == Mouse.LEFT) {
223 if (sgp.isReal()) {
224
225 tryHighlight(sgp);
226
227 } else {
228 graph.addRealize(sgp);
229 }
230 } else if (button == Mouse.RIGHT) {
231 if (sgp.isReal()) {
232 pressRadius = sgp.getRadius();
233 pressX = sgp.getPosition().getX();
234 pressY = sgp.getPosition().getY();
235 }
236 }
237 }
238 }
239
240
241
242
243 public void pickDragged(Pickable pbl, Position pos, int button, int ix, int iy) {
244
245 if (selectAction == EDIT || selectAction == MOVE) {
246 if (pbl instanceof SegmentGraphPoint) {
247 SegmentGraphPoint sgp = (SegmentGraphPoint)pbl;
248
249 if (button == Mouse.LEFT) {
250 sgp.move(pos);
251
252 } else if (button == Mouse.RIGHT) {
253 double f = 0.1 * (pos.getY() - pressY) / pressRadius;
254 sgp.setRadius(pressRadius * Math.exp(f));
255
256 }
257 }
258 }
259
260 }
261
262
263
264 public void pickReleased(Pickable pbl, int button) {
265
266
267 }
268
269
270 private void tryHighlight(SegmentGraphPoint sgp) {
271 sgp1 = sgp2;
272 sgp2 = sgp;
273
274 if (selectAction == TRACE) {
275 graph.highlightTrace(sgp);
276
277
278 } else if (sgp1 != null) {
279
280 if (selectAction == PATH) {
281 graph.highlightPath(sgp1, sgp2);
282 clearSelect();
283
284 } else if (selectAction == TREE) {
285 graph.highlightTree(sgp1, sgp2);
286 clearSelect();
287 }
288
289 } else {
290 graph.clearHighlight();
291 sgp2.highlight();
292 }
293 }
294
295 public void pickEnteredTrash(Pickable pbl) {
296 }
297
298 public void pickLeftTrash(Pickable pbl) {
299 }
300
301 public void pickTrashed(Pickable pbl) {
302 }
303
304
305 public void trashPressed() {
306
307
308 }
309
310
311 public void pickHovered(Pickable hoverItem) {
312
313
314 }
315
316
317 public Box getLimitBox() {
318
319 return null;
320 }
321
322
323 public Box getLimitBox(Painter p) {
324
325 return null;
326 }
327
328
329
330 }