1 package org.catacomb.graph.gui;
2
3
4
5 import org.catacomb.interlish.interact.ClickListener;
6 import org.catacomb.report.E;
7 import org.catacomb.util.MouseUtil;
8
9 import java.awt.Graphics2D;
10 import java.awt.event.MouseEvent;
11 import java.awt.event.MouseListener;
12 import java.awt.event.MouseMotionListener;
13
14
15
16 public final class Mouse implements MouseListener, MouseMotionListener {
17
18 public final static int LEFT = 1;
19 public final static int MIDDLE = 2;
20 public final static int RIGHT = 3;
21 private int button;
22
23 private int canvasWidth;
24 private int canvasHeight;
25
26 private int xDown;
27 private int yDown;
28
29 private int xCurrent;
30 private int yCurrent;
31
32 private boolean down;
33
34
35
36
37
38 private int nHandler;
39 private MouseHandler[] handlers;
40
41
42 private MouseHandler activeHandler;
43 private MouseHandler motionHandler;
44
45
46 private WorldCanvas canvas;
47
48
49 private ClickListener clickListener;
50
51
52 public Mouse(WorldCanvas c, boolean interactive) {
53 super();
54 canvas = c;
55
56 if (interactive) {
57 canvas.addMouseListener(this);
58 canvas.addMouseMotionListener(this);
59 }
60
61 handlers = new MouseHandler[10];
62 }
63
64
65 public void setClickListener(ClickListener cl) {
66 clickListener = cl;
67 }
68
69
70 public void detach() {
71 canvas.removeMouseListener(this);
72 canvas.removeMouseMotionListener(this);
73 }
74
75
76 public void addHandler(MouseHandler h) {
77 if (nHandler >= handlers.length) {
78 E.error("Mouse handler array too small");
79 } else {
80 handlers[nHandler++] = h;
81 }
82 }
83
84
85 public void prependHandler(MouseHandler h) {
86 if (nHandler >= handlers.length) {
87 E.error("Mouse handler array too small");
88 } else {
89 for (int i = nHandler; i > 0; i--) {
90 handlers[i] = handlers[i - 1];
91 }
92 handlers[0] = h;
93 nHandler += 1;
94 }
95 }
96
97
98
99 private void requestRepaint() {
100 canvas.repaint();
101 }
102
103
104 boolean leftButton() {
105 return (button == LEFT);
106 }
107
108
109 boolean middleButton() {
110 return (button == MIDDLE);
111 }
112
113
114 boolean rightButton() {
115 return (button == RIGHT);
116 }
117
118
119 public void updateCanvasDimensions() {
120 canvasWidth = canvas.getWidth();
121 canvasHeight = canvas.getHeight();
122 }
123
124
125 int getCanvasWidth() {
126 return canvasWidth;
127 }
128
129
130 int getCanvasHeight() {
131 return canvasHeight;
132 }
133
134
135
136 public void mouseEntered(MouseEvent e) {
137
138 }
139
140
141 public void mouseExited(MouseEvent e) {
142
143 }
144
145
146 public void mouseClicked(MouseEvent e) {
147 readPosition(e);
148
149 if (clickListener != null) {
150 clickListener.pointClicked(e.getX(), e.getY(), MouseUtil.getButton(e));
151 }
152 }
153
154
155
156 public void mouseMoved(MouseEvent e) {
157 if (down) {
158
159 E.shortWarning("mouse moved when down?? " + e);
160 down = false;
161 return;
162 }
163
164 readPosition(e);
165
166
167 for (int i = 0; i < nHandler; i++) {
168 MouseHandler mh = handlers[i];
169 if (mh.isActive() && mh.motionAware()) {
170 if (mh.motionChange(this)) {
171
172 motionHandler = mh;
173
174
175
176
177 canvas.repaint();
178
179 }
180 }
181 }
182 }
183
184
185
186 public void mousePressed(MouseEvent e) {
187 down = true;
188
189 motionHandler = null;
190
191 readButton(e);
192 readPosition(e);
193 readPressPosition(e);
194
195
196
197
198
199 activeHandler = null;
200
201 for (int i = 0; i < nHandler; i++) {
202 MouseHandler mh = handlers[i];
203
204 if (mh.isActive()) {
205
206 mh.setClaimUndecided();
207
208 mh.init(this);
209 if (mh.isIn()) {
210 activeHandler = mh;
211 break;
212 }
213 }
214 }
215
216 if (activeHandler != null) {
217 activeHandler.applyOnDown(this);
218 }
219
220 for (MouseHandler mh : handlers) {
221 if (mh == activeHandler) {
222
223 } else if (mh != null) {
224 mh.missedPress(this);
225 }
226 }
227
228 }
229
230
231
232 public void mouseDragged(MouseEvent e) {
233 if (!down) {
234 return;
235 }
236 readPosition(e);
237
238 if (activeHandler == null) {
239 for (int i = 0; i < nHandler; i++) {
240 MouseHandler mh = handlers[i];
241
242 if (mh.isActive()) {
243 if (mh.isOut()) {
244
245
246 } else {
247 mh.advance(this);
248 if (mh.isIn()) {
249 activeHandler = mh;
250 break;
251 }
252 }
253 }
254 }
255 }
256
257 if (activeHandler != null) {
258 activeHandler.applyOnDrag(this);
259
260 if (activeHandler.getRepaintStatus() == MouseHandler.FULL) {
261 requestRepaint();
262
263 } else if (activeHandler.getRepaintStatus() == MouseHandler.BUFFERED) {
264
265 requestRepaint();
266
267 } else {
268
269 }
270 }
271 }
272
273
274
275 public void mouseReleased(MouseEvent e) {
276 if (!down) {
277 return;
278 }
279
280 readPosition(e);
281
282 if (activeHandler == null) {
283 for (int i = 0; i < nHandler; i++) {
284 MouseHandler mh = handlers[i];
285 if (mh.isActive()) {
286 if (mh.isOut()) {
287
288 } else {
289 mh.release(this);
290 if (mh.isIn()) {
291 activeHandler = mh;
292 break;
293 }
294 }
295 }
296 }
297 }
298
299 if (activeHandler != null) {
300 activeHandler.applyOnRelease(this);
301 }
302
303 activeHandler = null;
304 down = false;
305 requestRepaint();
306
307 canvas.fixRanges();
308
309 updateCanvasDimensions();
310 }
311
312
313
314 private void readPosition(MouseEvent e) {
315 xCurrent = e.getX();
316 yCurrent = e.getY();
317 }
318
319
320 private void readPressPosition(MouseEvent e) {
321 xDown = e.getX();
322 yDown = e.getY();
323 }
324
325
326
327 private void readButton(MouseEvent e) {
328 button = MouseUtil.getButton(e);
329 }
330
331
332
333 public int getButton() {
334 return button;
335 }
336
337
338 public boolean isDown() {
339 return down;
340 }
341
342
343 int getX() {
344 return xCurrent;
345 }
346
347
348 int getY() {
349 return yCurrent;
350 }
351
352
353 int getXDown() {
354 return xDown;
355 }
356
357
358 int getYDown() {
359 return yDown;
360 }
361
362
363
364 void echoPaint(Graphics2D g) {
365 if (activeHandler != null) {
366
367 activeHandler.echoPaint(g);
368
369 activeHandler.setRepaintStatus(MouseHandler.NONE);
370
371 } else if (motionHandler != null) {
372 motionHandler.echoPaint(g);
373
374
375
376 }
377
378 }
379
380
381
382
383 void boxSelected(int x0, int y0, int x1, int y1) {
384 canvas.boxSelected(x0, y0, x1, y1);
385 }
386
387
388 void initializeZoom(int xc, int yc) {
389 canvas.initializeZoom(xc, yc);
390 }
391
392 void dragZoom(double fx, double fy, int xc, int yc) {
393 canvas.dragZoom(fx, fy, xc, yc);
394 }
395
396 void zoom(double fac, int xc, int yc) {
397 canvas.zoom(fac, xc, yc);
398 }
399
400
401 void zoom(double xfac, double yfac, int xc, int yc) {
402 canvas.zoom(xfac, yfac, xc, yc);
403 }
404
405
406
407 void trialPan(int xfrom, int yfrom, int xto, int yto) {
408 canvas.trialPan(xfrom, yfrom, xto, yto);
409 }
410
411
412 void permanentPan(int xfrom, int yfrom, int xto, int yto) {
413 canvas.permanentPan(xfrom, yfrom, xto, yto);
414 }
415
416
417 public void dragRollRotate(int pdx, int pdy) {
418 canvas.dragRollRotate(pdx, pdy);
419 }
420
421 public void dragZRotate(int pdx, int pdy) {
422 canvas.dragZRotate(pdx, pdy);
423 }
424
425 public void initializeRotation(int ix, int iy) {
426 canvas.initializeRotation(ix, iy);
427 }
428
429 public void initializeRotation(double x, double y, double z) {
430 canvas.initializeRotation(x, y, z);
431 }
432
433 public void restoreAA() {
434 canvas.restoreAA();
435 }
436
437
438 }