1 package org.catacomb.graph.gui;
2
3
4
5 import java.awt.Color;
6 import java.awt.Graphics2D;
7
8
9 final class BoxSelectionHandler extends MouseHandler {
10
11 private boolean vtl;
12 private boolean vtr;
13 private boolean vbl;
14 private boolean vbr;
15
16
17 private int x0;
18 private int y0;
19 private int x1;
20 private int y1;
21
22 private int[] xyxy;
23
24 final static int NORMAL = 0;
25 final static int SIMPLE = 1;
26
27 int actionMode;
28
29
30
31 public BoxSelectionHandler() {
32 clear();
33 actionMode = NORMAL;
34 xyxy = new int[4];
35 setRepaintStatus(MouseHandler.NONE);
36 }
37
38
39
40 public void activate() {
41 actionMode = NORMAL;
42 super.activate();
43 }
44
45
46
47 public void simpleActivate() {
48 activate();
49 actionMode = SIMPLE;
50 }
51
52
53
54
55 public void clear() {
56 vtl = false;
57 vtr = false;
58 vbl = false;
59 vbr = false;
60 }
61
62
63 public void init(Mouse m) {
64 clear();
65 x0 = m.getX();
66 y0 = m.getY();
67
68 if (actionMode == SIMPLE) {
69 setClaimIn();
70 }
71 }
72
73
74 public void advance(Mouse m) {
75 if (isUndecided()) {
76 checkActivate(m.getX(), m.getY());
77 }
78 setRepaintStatus(MouseHandler.BUFFERED);
79 }
80
81
82
83
84
85
86 void echoPaint(Graphics2D g) {
87
88 g.setColor(Color.red);
89 g.drawLine(x0, y0, x1, y0);
90 g.drawLine(x0, y0, x0, y1);
91 g.drawLine(x1, y1, x1, y0);
92 g.drawLine(x1, y1, x0, y1);
93 }
94
95
96
97
98
99 public void applyOnDown(Mouse m) {
100
101 }
102
103
104 public void applyOnDrag(Mouse m) {
105 readPosition(m);
106
107 setRepaintStatus(MouseHandler.BUFFERED);
108 }
109
110
111 public void applyOnRelease(Mouse m) {
112 readPosition(m);
113 int xa = (x0 < x1 ? x0 : x1);
114 int ya = (y0 < y1 ? y0 : y1);
115 int xb = (x0 < x1 ? x1 : x0);
116 int yb = (y0 < y1 ? y1 : y0);
117
118 m.boxSelected(xa, ya, xb, yb);
119 }
120
121
122
123 void readPosition(Mouse m) {
124 x1 = m.getX();
125 y1 = m.getY();
126 }
127
128
129
130
131 public int[] getXYXY() {
132 xyxy[0] = x0;
133 xyxy[1] = y0;
134 xyxy[2] = x1;
135 xyxy[3] = y1;
136 return xyxy;
137 }
138
139
140
141
142 private void checkActivate(int x, int y) {
143 int dx = x - x0;
144 int dy = y - y0;
145
146 int thresh = 8;
147 int pt = thresh;
148 int mt = -1 * thresh;
149
150 vtl = (vtl || (dx < mt && dy < mt));
151 vtr = (vtr || (dx > pt && dy < mt));
152 vbl = (vbl || (dx < mt && dy > pt));
153 vbr = (vbr || (dx > pt && dy > pt));
154
155
156 if ((vtl && dx > 0 && dy > 0) ||
157 (vtr && dx < 0 && dy > 0) ||
158 (vbl && dx > 0 && dy < 0) ||
159 (vbr && dx < 0 && dy < 0)) {
160
161 setClaimIn();
162
163 x1 = x;
164 y1 = y;
165 }
166 }
167
168
169
170
171 }