1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.eventcore.gui; |
---|
16 | |
---|
17 | import java.util.LinkedList; |
---|
18 | import java.util.List; |
---|
19 | |
---|
20 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
21 | import de.ugoe.cs.autoquest.eventcore.IEventTarget; |
---|
22 | |
---|
23 | /** |
---|
24 | * <p> |
---|
25 | * This class condenses mouse clicks, i.e. it reduces a sequence of mouse button down, mouse button |
---|
26 | * up and possibly a subsequent mouse click with the same button on the same event target and the |
---|
27 | * same coordinates to a single mouse click with that button on that target at the coordinates. |
---|
28 | * The mouse button down and mouse button up events are discarded. For this, it iterates the |
---|
29 | * provided sequence and identifies any match of the named event sequence pattern. This match is |
---|
30 | * condensed to the mouse click event. |
---|
31 | * </p> |
---|
32 | * |
---|
33 | * @version 1.0 |
---|
34 | * @author Patrick Harms |
---|
35 | */ |
---|
36 | public class MouseClickCondenser { |
---|
37 | |
---|
38 | /** |
---|
39 | * <p> |
---|
40 | * This method performs the work described in the description of the class. A new list is |
---|
41 | * instantiated and returned. This list is filled with the events provided by the sequence being |
---|
42 | * the parameter of the method except for mouse button down and mouse button up events which are |
---|
43 | * followed by a mouse click event with the same button on the same target. |
---|
44 | * </p> |
---|
45 | * |
---|
46 | * @param sequence |
---|
47 | * the event sequence to condense the mouse clicks in |
---|
48 | * |
---|
49 | * @return the resulting sequence, in which mouse clicks are condensed to single mouse click |
---|
50 | * events |
---|
51 | */ |
---|
52 | public List<Event> condenseMouseClicks(List<Event> sequence) { |
---|
53 | List<Event> resultingSequence = new LinkedList<Event>(); |
---|
54 | |
---|
55 | int index = 0; |
---|
56 | boolean mouseClickHandled; |
---|
57 | while (index < sequence.size()) |
---|
58 | { |
---|
59 | mouseClickHandled = false; |
---|
60 | if ((index + 1) < sequence.size()) { |
---|
61 | Event mbDown = sequence.get(index); |
---|
62 | Event mbUp = sequence.get(index + 1); |
---|
63 | |
---|
64 | if (((index + 2) < sequence.size()) && |
---|
65 | mouseClickSequenceFound(mbDown, mbUp, sequence.get(index + 2))) |
---|
66 | { |
---|
67 | // skip the mouse button down and mouse button up event and add the mouse click |
---|
68 | index += 2; |
---|
69 | resultingSequence.add(sequence.get(index)); |
---|
70 | index++; |
---|
71 | mouseClickHandled = true; |
---|
72 | } |
---|
73 | else if (mouseClickSequenceFound(mbDown, mbUp)) { |
---|
74 | // replace the mouse button down and mouse button up event with a generated |
---|
75 | // mouse click |
---|
76 | index += 2; |
---|
77 | resultingSequence.add(createClick(mbDown, mbUp)); |
---|
78 | mouseClickHandled = true; |
---|
79 | } |
---|
80 | else if (mouseDragAndDropSequenceFound(mbDown, mbUp)) { |
---|
81 | // replace the mouse button down and mouse button up event with a generated |
---|
82 | // mouse drag and drop |
---|
83 | index += 2; |
---|
84 | resultingSequence.add(createDragAndDrop(mbDown, mbUp)); |
---|
85 | mouseClickHandled = true; |
---|
86 | } |
---|
87 | else if (mouseDoubleClickSequenceFound(mbDown, mbUp)) { |
---|
88 | // replace the two mouse click events with a generated mouse double click |
---|
89 | index += 2; |
---|
90 | resultingSequence.add(createDoubleClick(mbDown, mbUp)); |
---|
91 | mouseClickHandled = true; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | if (!mouseClickHandled) { |
---|
96 | resultingSequence.add(sequence.get(index)); |
---|
97 | index++; |
---|
98 | } |
---|
99 | |
---|
100 | if (resultingSequence.size() > 1) { |
---|
101 | // check for double clicks |
---|
102 | int resultingSequenceIndex = resultingSequence.size() - 1; |
---|
103 | Event click1 = resultingSequence.get(resultingSequenceIndex - 1); |
---|
104 | Event click2 = resultingSequence.get(resultingSequenceIndex); |
---|
105 | if (mouseDoubleClickSequenceFound(click1, click2)) { |
---|
106 | resultingSequence.remove(resultingSequenceIndex); |
---|
107 | resultingSequence.remove(resultingSequenceIndex - 1); |
---|
108 | resultingSequence.add(createDoubleClick(click1, click2)); |
---|
109 | } |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | return resultingSequence; |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * |
---|
118 | */ |
---|
119 | private boolean mouseClickSequenceFound(Event mouseButtonDown, |
---|
120 | Event mouseButtonUp, |
---|
121 | Event mouseClick) |
---|
122 | { |
---|
123 | if (!mouseClickSequenceFound(mouseButtonDown, mouseButtonUp)) { |
---|
124 | return false; |
---|
125 | } |
---|
126 | |
---|
127 | // check the third node for validity |
---|
128 | if (!(mouseClick.getType() instanceof MouseClick)) { |
---|
129 | return false; |
---|
130 | } |
---|
131 | |
---|
132 | if (!targetsEqual(mouseButtonDown, mouseClick)) { |
---|
133 | return false; |
---|
134 | } |
---|
135 | |
---|
136 | if (!buttonsEqual(mouseButtonDown, mouseClick)) { |
---|
137 | return false; |
---|
138 | } |
---|
139 | |
---|
140 | if (!coordinatesEqual(mouseButtonDown, mouseClick)) { |
---|
141 | return false; |
---|
142 | } |
---|
143 | |
---|
144 | return true; |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * |
---|
149 | */ |
---|
150 | private boolean mouseClickSequenceFound(Event mouseButtonDown, |
---|
151 | Event mouseButtonUp) |
---|
152 | { |
---|
153 | // check the first in a row of three for validity |
---|
154 | if (!(mouseButtonDown.getType() instanceof MouseButtonDown)) { |
---|
155 | return false; |
---|
156 | } |
---|
157 | |
---|
158 | // check the second node for validity |
---|
159 | if (!(mouseButtonUp.getType() instanceof MouseButtonUp)) { |
---|
160 | return false; |
---|
161 | } |
---|
162 | |
---|
163 | if (!targetsEqual(mouseButtonDown, mouseButtonUp)) { |
---|
164 | return false; |
---|
165 | } |
---|
166 | |
---|
167 | if (!buttonsEqual(mouseButtonDown, mouseButtonUp)) { |
---|
168 | return false; |
---|
169 | } |
---|
170 | |
---|
171 | if (!coordinatesEqual(mouseButtonDown, mouseButtonUp)) { |
---|
172 | return false; |
---|
173 | } |
---|
174 | |
---|
175 | return true; |
---|
176 | } |
---|
177 | |
---|
178 | /** |
---|
179 | * |
---|
180 | */ |
---|
181 | private boolean mouseDragAndDropSequenceFound(Event mouseButtonDown, |
---|
182 | Event mouseButtonUp) |
---|
183 | { |
---|
184 | // check the first in a row of three for validity |
---|
185 | if (!(mouseButtonDown.getType() instanceof MouseButtonDown)) { |
---|
186 | return false; |
---|
187 | } |
---|
188 | |
---|
189 | // check the second node for validity |
---|
190 | if (!(mouseButtonUp.getType() instanceof MouseButtonUp)) { |
---|
191 | return false; |
---|
192 | } |
---|
193 | |
---|
194 | if (!targetsEqual(mouseButtonDown, mouseButtonUp)) { |
---|
195 | return false; |
---|
196 | } |
---|
197 | |
---|
198 | MouseButtonInteraction.Button button = |
---|
199 | ((MouseButtonDown) mouseButtonDown.getType()).getButton(); |
---|
200 | |
---|
201 | if (MouseButtonInteraction.Button.LEFT != button) { |
---|
202 | return false; |
---|
203 | } |
---|
204 | |
---|
205 | if (!buttonsEqual(mouseButtonDown, mouseButtonUp)) { |
---|
206 | return false; |
---|
207 | } |
---|
208 | |
---|
209 | if (coordinatesEqual(mouseButtonDown, mouseButtonUp)) { |
---|
210 | return false; |
---|
211 | } |
---|
212 | |
---|
213 | return true; |
---|
214 | } |
---|
215 | |
---|
216 | /** |
---|
217 | * |
---|
218 | */ |
---|
219 | private boolean mouseDoubleClickSequenceFound(Event click1, |
---|
220 | Event click2) |
---|
221 | { |
---|
222 | // check the first in a row of three for validity |
---|
223 | if (!(click1.getType() instanceof MouseClick)) { |
---|
224 | return false; |
---|
225 | } |
---|
226 | |
---|
227 | if (((MouseClick) click1.getType()).getButton() != MouseButtonInteraction.Button.LEFT) { |
---|
228 | return false; |
---|
229 | } |
---|
230 | |
---|
231 | // check the second node for validity |
---|
232 | if (!(click2.getType() instanceof MouseClick)) { |
---|
233 | return false; |
---|
234 | } |
---|
235 | |
---|
236 | // use 500 milliseconds as timestamp difference as this is more or less similar to default |
---|
237 | // values in Microsoft Windows |
---|
238 | if (!timestampDifferenceSmallerThan(click1, click2, 500)) { |
---|
239 | return false; |
---|
240 | } |
---|
241 | |
---|
242 | if (!targetsEqual(click1, click2)) { |
---|
243 | return false; |
---|
244 | } |
---|
245 | |
---|
246 | if (!buttonsEqual(click1, click2)) { |
---|
247 | return false; |
---|
248 | } |
---|
249 | |
---|
250 | if (!coordinatesEqual(click1, click2)) { |
---|
251 | return false; |
---|
252 | } |
---|
253 | |
---|
254 | return true; |
---|
255 | } |
---|
256 | |
---|
257 | /** |
---|
258 | * |
---|
259 | */ |
---|
260 | private Event createClick(Event mouseButtonDown, Event mouseButtonUp) { |
---|
261 | MouseButtonInteraction.Button button = |
---|
262 | ((MouseButtonDown) mouseButtonDown.getType()).getButton(); |
---|
263 | |
---|
264 | int x = ((MouseButtonDown) mouseButtonDown.getType()).getX(); |
---|
265 | int y = ((MouseButtonDown) mouseButtonDown.getType()).getY(); |
---|
266 | |
---|
267 | Event click = new Event(new MouseClick(button, x, y), mouseButtonDown.getTarget()); |
---|
268 | click.setTimestamp(mouseButtonDown.getTimestamp()); |
---|
269 | return click; |
---|
270 | } |
---|
271 | |
---|
272 | /** |
---|
273 | * |
---|
274 | */ |
---|
275 | private Event createDoubleClick(Event click1, Event click2) { |
---|
276 | MouseButtonInteraction.Button button = ((MouseClick) click1.getType()).getButton(); |
---|
277 | |
---|
278 | int x = ((MouseClick) click1.getType()).getX(); |
---|
279 | int y = ((MouseClick) click1.getType()).getY(); |
---|
280 | |
---|
281 | Event doubleClick = new Event(new MouseDoubleClick(button, x, y), click1.getTarget()); |
---|
282 | doubleClick.setTimestamp(click1.getTimestamp()); |
---|
283 | return doubleClick; |
---|
284 | } |
---|
285 | |
---|
286 | /** |
---|
287 | * |
---|
288 | */ |
---|
289 | private Event createDragAndDrop(Event mouseButtonDown, Event mouseButtonUp) { |
---|
290 | int xStart = ((MouseButtonDown) mouseButtonDown.getType()).getX(); |
---|
291 | int yStart = ((MouseButtonDown) mouseButtonDown.getType()).getY(); |
---|
292 | int xEnd = ((MouseButtonUp) mouseButtonUp.getType()).getX(); |
---|
293 | int yEnd = ((MouseButtonUp) mouseButtonUp.getType()).getY(); |
---|
294 | |
---|
295 | Event dragAndDrop = new Event |
---|
296 | (new MouseDragAndDrop(xStart, yStart, xEnd, yEnd), mouseButtonDown.getTarget()); |
---|
297 | |
---|
298 | dragAndDrop.setTimestamp(mouseButtonDown.getTimestamp()); |
---|
299 | return dragAndDrop; |
---|
300 | } |
---|
301 | |
---|
302 | /** |
---|
303 | * |
---|
304 | */ |
---|
305 | private boolean targetsEqual(Event event1, Event event2) { |
---|
306 | IEventTarget target1 = event1.getTarget(); |
---|
307 | IEventTarget target2 = event2.getTarget(); |
---|
308 | |
---|
309 | return target1 == null ? target2 == null : target1.equals(target2); |
---|
310 | } |
---|
311 | |
---|
312 | /** |
---|
313 | * |
---|
314 | */ |
---|
315 | private boolean buttonsEqual(Event event1, Event event2) { |
---|
316 | MouseButtonInteraction.Button button1 = |
---|
317 | (event1.getType() instanceof MouseButtonInteraction) ? |
---|
318 | ((MouseButtonInteraction) event1.getType()).getButton() : null; |
---|
319 | |
---|
320 | MouseButtonInteraction.Button button2 = |
---|
321 | (event2.getType() instanceof MouseButtonInteraction) ? |
---|
322 | ((MouseButtonInteraction) event2.getType()).getButton() : null; |
---|
323 | |
---|
324 | return button1 == null ? button2 == null : button1.equals(button2); |
---|
325 | } |
---|
326 | |
---|
327 | /** |
---|
328 | * |
---|
329 | */ |
---|
330 | private boolean timestampDifferenceSmallerThan(Event event1, Event event2, long difference) { |
---|
331 | long timestamp1 = event1.getTimestamp(); |
---|
332 | |
---|
333 | if (timestamp1 < 0) { |
---|
334 | return false; |
---|
335 | } |
---|
336 | |
---|
337 | long timestamp2 = event2.getTimestamp(); |
---|
338 | |
---|
339 | if (timestamp2 < 0) { |
---|
340 | return false; |
---|
341 | } |
---|
342 | |
---|
343 | return (Math.abs((timestamp2 - timestamp1))) < difference; |
---|
344 | } |
---|
345 | |
---|
346 | /** |
---|
347 | * |
---|
348 | */ |
---|
349 | private boolean coordinatesEqual(Event event1, Event event2) { |
---|
350 | int x1 = |
---|
351 | (event1.getType() instanceof MouseButtonInteraction) ? |
---|
352 | ((MouseButtonInteraction) event1.getType()).getX() : -1; |
---|
353 | |
---|
354 | int x2 = |
---|
355 | (event2.getType() instanceof MouseButtonInteraction) ? |
---|
356 | ((MouseButtonInteraction) event2.getType()).getX() : -1; |
---|
357 | |
---|
358 | // allow a deviation of one pixel to identify it as click anyway |
---|
359 | if ((x1 == -1) || (x2 == -1) || (x2 < (x1 - 1)) || ((x1 + 1) < x2)) { |
---|
360 | return false; |
---|
361 | } |
---|
362 | |
---|
363 | int y1 = |
---|
364 | (event1.getType() instanceof MouseButtonInteraction) ? |
---|
365 | ((MouseButtonInteraction) event1.getType()).getY() : -1; |
---|
366 | |
---|
367 | int y2 = |
---|
368 | (event2.getType() instanceof MouseButtonInteraction) ? |
---|
369 | ((MouseButtonInteraction) event2.getType()).getY() : -1; |
---|
370 | |
---|
371 | return (y1 != -1) && (y2 != -1) && ((y1 - 1) < y2) && (y2 < (y1 + 1)); |
---|
372 | } |
---|
373 | |
---|
374 | } |
---|