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 | // skip the mouse button down and mouse button up event and add a mouse click |
---|
75 | index += 2; |
---|
76 | resultingSequence.add(createClick(mbDown, mbUp)); |
---|
77 | mouseClickHandled = true; |
---|
78 | } |
---|
79 | else if (mouseDragAndDropSequenceFound(mbDown, mbUp)) { |
---|
80 | // skip the mouse button down and mouse button up event and add a mouse click |
---|
81 | index += 2; |
---|
82 | resultingSequence.add(createDragAndDrop(mbDown, mbUp)); |
---|
83 | mouseClickHandled = true; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | if (!mouseClickHandled) { |
---|
88 | resultingSequence.add(sequence.get(index)); |
---|
89 | index++; |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | return resultingSequence; |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * |
---|
98 | */ |
---|
99 | private boolean mouseClickSequenceFound(Event mouseButtonDown, |
---|
100 | Event mouseButtonUp, |
---|
101 | Event mouseClick) |
---|
102 | { |
---|
103 | if (!mouseClickSequenceFound(mouseButtonDown, mouseButtonUp)) { |
---|
104 | return false; |
---|
105 | } |
---|
106 | |
---|
107 | // check the third node for validity |
---|
108 | if (!(mouseClick.getType() instanceof MouseClick)) { |
---|
109 | return false; |
---|
110 | } |
---|
111 | |
---|
112 | if (!targetsEqual(mouseButtonDown, mouseClick)) { |
---|
113 | return false; |
---|
114 | } |
---|
115 | |
---|
116 | if (!buttonsEqual(mouseButtonDown, mouseClick)) { |
---|
117 | return false; |
---|
118 | } |
---|
119 | |
---|
120 | if (!coordinatesEqual(mouseButtonDown, mouseClick)) { |
---|
121 | return false; |
---|
122 | } |
---|
123 | |
---|
124 | return true; |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * |
---|
129 | */ |
---|
130 | private boolean mouseClickSequenceFound(Event mouseButtonDown, |
---|
131 | Event mouseButtonUp) |
---|
132 | { |
---|
133 | // check the first in a row of three for validity |
---|
134 | if (!(mouseButtonDown.getType() instanceof MouseButtonDown)) { |
---|
135 | return false; |
---|
136 | } |
---|
137 | |
---|
138 | // check the second node for validity |
---|
139 | if (!(mouseButtonUp.getType() instanceof MouseButtonUp)) { |
---|
140 | return false; |
---|
141 | } |
---|
142 | |
---|
143 | if (!targetsEqual(mouseButtonDown, mouseButtonUp)) { |
---|
144 | return false; |
---|
145 | } |
---|
146 | |
---|
147 | if (!buttonsEqual(mouseButtonDown, mouseButtonUp)) { |
---|
148 | return false; |
---|
149 | } |
---|
150 | |
---|
151 | if (!coordinatesEqual(mouseButtonDown, mouseButtonUp)) { |
---|
152 | return false; |
---|
153 | } |
---|
154 | |
---|
155 | return true; |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * |
---|
160 | */ |
---|
161 | private boolean mouseDragAndDropSequenceFound(Event mouseButtonDown, |
---|
162 | Event mouseButtonUp) |
---|
163 | { |
---|
164 | // check the first in a row of three for validity |
---|
165 | if (!(mouseButtonDown.getType() instanceof MouseButtonDown)) { |
---|
166 | return false; |
---|
167 | } |
---|
168 | |
---|
169 | // check the second node for validity |
---|
170 | if (!(mouseButtonUp.getType() instanceof MouseButtonUp)) { |
---|
171 | return false; |
---|
172 | } |
---|
173 | |
---|
174 | if (!targetsEqual(mouseButtonDown, mouseButtonUp)) { |
---|
175 | return false; |
---|
176 | } |
---|
177 | |
---|
178 | MouseButtonInteraction.Button button = |
---|
179 | ((MouseButtonDown) mouseButtonDown.getType()).getButton(); |
---|
180 | |
---|
181 | if (MouseButtonInteraction.Button.LEFT != button) { |
---|
182 | return false; |
---|
183 | } |
---|
184 | |
---|
185 | if (!buttonsEqual(mouseButtonDown, mouseButtonUp)) { |
---|
186 | return false; |
---|
187 | } |
---|
188 | |
---|
189 | if (coordinatesEqual(mouseButtonDown, mouseButtonUp)) { |
---|
190 | return false; |
---|
191 | } |
---|
192 | |
---|
193 | return true; |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | * |
---|
198 | */ |
---|
199 | private Event createClick(Event mouseButtonDown, Event mouseButtonUp) { |
---|
200 | MouseButtonInteraction.Button button = |
---|
201 | ((MouseButtonDown) mouseButtonDown.getType()).getButton(); |
---|
202 | |
---|
203 | int x = ((MouseButtonDown) mouseButtonDown.getType()).getX(); |
---|
204 | int y = ((MouseButtonDown) mouseButtonDown.getType()).getY(); |
---|
205 | |
---|
206 | return new Event(new MouseClick(button, x, y), mouseButtonDown.getTarget()); |
---|
207 | } |
---|
208 | |
---|
209 | /** |
---|
210 | * |
---|
211 | */ |
---|
212 | private Event createDragAndDrop(Event mouseButtonDown, Event mouseButtonUp) { |
---|
213 | int xStart = ((MouseButtonDown) mouseButtonDown.getType()).getX(); |
---|
214 | int yStart = ((MouseButtonDown) mouseButtonDown.getType()).getY(); |
---|
215 | int xEnd = ((MouseButtonUp) mouseButtonUp.getType()).getX(); |
---|
216 | int yEnd = ((MouseButtonUp) mouseButtonUp.getType()).getY(); |
---|
217 | |
---|
218 | return new Event |
---|
219 | (new MouseDragAndDrop(xStart, yStart, xEnd, yEnd), mouseButtonDown.getTarget()); |
---|
220 | } |
---|
221 | |
---|
222 | /** |
---|
223 | * |
---|
224 | */ |
---|
225 | private boolean targetsEqual(Event event1, Event event2) { |
---|
226 | IEventTarget target1 = event1.getTarget(); |
---|
227 | IEventTarget target2 = event2.getTarget(); |
---|
228 | |
---|
229 | return target1 == null ? target2 == null : target1.equals(target2); |
---|
230 | } |
---|
231 | |
---|
232 | /** |
---|
233 | * |
---|
234 | */ |
---|
235 | private boolean buttonsEqual(Event event1, Event event2) { |
---|
236 | MouseButtonInteraction.Button button1 = |
---|
237 | (event1.getType() instanceof MouseButtonInteraction) ? |
---|
238 | ((MouseButtonInteraction) event1.getType()).getButton() : null; |
---|
239 | |
---|
240 | MouseButtonInteraction.Button button2 = |
---|
241 | (event2.getType() instanceof MouseButtonInteraction) ? |
---|
242 | ((MouseButtonInteraction) event2.getType()).getButton() : null; |
---|
243 | |
---|
244 | return button1 == null ? button2 == null : button1.equals(button2); |
---|
245 | } |
---|
246 | |
---|
247 | /** |
---|
248 | * |
---|
249 | */ |
---|
250 | private boolean coordinatesEqual(Event event1, Event event2) { |
---|
251 | int x1 = |
---|
252 | (event1.getType() instanceof MouseButtonInteraction) ? |
---|
253 | ((MouseButtonInteraction) event1.getType()).getX() : -1; |
---|
254 | |
---|
255 | int x2 = |
---|
256 | (event2.getType() instanceof MouseButtonInteraction) ? |
---|
257 | ((MouseButtonInteraction) event2.getType()).getX() : -1; |
---|
258 | |
---|
259 | if ((x1 == -1) || (x1 != x2)) { |
---|
260 | return false; |
---|
261 | } |
---|
262 | |
---|
263 | int y1 = |
---|
264 | (event1.getType() instanceof MouseButtonInteraction) ? |
---|
265 | ((MouseButtonInteraction) event1.getType()).getY() : -1; |
---|
266 | |
---|
267 | int y2 = |
---|
268 | (event2.getType() instanceof MouseButtonInteraction) ? |
---|
269 | ((MouseButtonInteraction) event2.getType()).getY() : -1; |
---|
270 | |
---|
271 | return (y1 != -1) && (y1 == y2); |
---|
272 | } |
---|
273 | |
---|
274 | } |
---|