source: trunk/autoquest-core-events/src/main/java/de/ugoe/cs/autoquest/eventcore/gui/MouseClickCondenser.java @ 967

Last change on this file since 967 was 958, checked in by pharms, 12 years ago
  • made event final, so that it can't be derived of
  • added support for adding further event parameters
File size: 6.4 KB
Line 
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
15package de.ugoe.cs.autoquest.eventcore.gui;
16
17import java.util.LinkedList;
18import java.util.List;
19
20import de.ugoe.cs.autoquest.eventcore.Event;
21import 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 * TODO correctly identify drag and drop
33 *
34 * @version 1.0
35 * @author Patrick Harms
36 */
37public class MouseClickCondenser {
38
39    /**
40     * <p>
41     * This method performs the work described in the description of the class. A new list is
42     * instantiated and returned. This list is filled with the events provided by the sequence being
43     * the parameter of the method except for mouse button down and mouse button up events which are
44     * followed by a mouse click event with the same button on the same target.
45     * </p>
46     *
47     * @param sequence
48     *            the event sequence to condense the mouse clicks in
49     *
50     * @return the resulting sequence, in which mouse clicks are condensed to single mouse click
51     *         events
52     */
53    public List<Event> condenseMouseClicks(List<Event> sequence) {
54        List<Event> resultingSequence = new LinkedList<Event>();
55
56        int index = 0;
57        boolean mouseClickHandled;
58        while (index < sequence.size())
59        {
60            mouseClickHandled = false;
61            if ((index + 1) < sequence.size()) {
62                Event mbDown = sequence.get(index);
63                Event mbUp = sequence.get(index + 1);
64               
65                if (((index + 2) < sequence.size()) &&
66                    mouseClickSequenceFound(mbDown, mbUp, sequence.get(index + 2)))
67                {
68                    // skip the mouse button down and mouse button up event and add the mouse click
69                    index += 2;
70                    resultingSequence.add(sequence.get(index));
71                    index++;
72                    mouseClickHandled = true;
73                }
74                else if (mouseClickSequenceFound(mbDown, mbUp)) {
75                    // skip the mouse button down and mouse button up event and add a mouse click
76                    index += 2;
77                    resultingSequence.add(createClick(mbDown, mbUp));
78                    mouseClickHandled = true;
79                }
80            }
81
82            if (!mouseClickHandled) {
83                resultingSequence.add(sequence.get(index));
84                index++;
85            }
86        }
87
88        return resultingSequence;
89    }
90
91    /**
92     *
93     */
94    private boolean mouseClickSequenceFound(Event mouseButtonDown,
95                                            Event mouseButtonUp,
96                                            Event mouseClick)
97    {
98        if (!mouseClickSequenceFound(mouseButtonDown, mouseButtonUp)) {
99            return false;
100        }
101       
102        // check the third node for validity
103        if (!(mouseClick.getType() instanceof MouseClick)) {
104            return false;
105        }
106
107        IEventTarget eventTarget = mouseButtonDown.getTarget();
108
109        if (!eventTarget.equals(mouseClick.getTarget())) {
110            return false;
111        }
112
113        MouseButtonInteraction.Button button =
114            ((MouseButtonDown) mouseButtonDown.getType()).getButton();
115
116        if (!button.equals(((MouseClick) mouseClick.getType()).getButton())) {
117            return false;
118        }
119
120        int x = ((MouseButtonDown) mouseButtonDown.getType()).getX();
121
122        if (x != ((MouseClick) mouseClick.getType()).getX()) {
123            return false;
124        }
125       
126        int y = ((MouseButtonDown) mouseButtonDown.getType()).getY();
127
128        if (y != ((MouseClick) mouseClick.getType()).getY()) {
129            return false;
130        }
131       
132        return true;
133    }
134
135    /**
136     *
137     */
138    private boolean mouseClickSequenceFound(Event mouseButtonDown,
139                                            Event mouseButtonUp)
140    {
141        // check the first in a row of three for validity
142        if (!(mouseButtonDown.getType() instanceof MouseButtonDown)) {
143            return false;
144        }
145
146        // check the second node for validity
147        if (!(mouseButtonUp.getType() instanceof MouseButtonUp)) {
148            return false;
149        }
150
151        IEventTarget eventTarget = mouseButtonDown.getTarget();
152
153        if (!eventTarget.equals(mouseButtonUp.getTarget())) {
154            return false;
155        }
156
157        MouseButtonInteraction.Button button =
158            ((MouseButtonDown) mouseButtonDown.getType()).getButton();
159
160        if (!button.equals(((MouseButtonUp) mouseButtonUp.getType()).getButton())) {
161            return false;
162        }
163
164        int x = ((MouseButtonDown) mouseButtonDown.getType()).getX();
165
166        if (x != ((MouseButtonUp) mouseButtonUp.getType()).getX()) {
167            return false;
168        }
169       
170        int y = ((MouseButtonDown) mouseButtonDown.getType()).getY();
171
172        if (y != ((MouseButtonUp) mouseButtonUp.getType()).getY()) {
173            return false;
174        }
175       
176        return true;
177    }
178
179    /**
180     *
181     */
182    private Event createClick(Event mouseButtonDown, Event mouseButtonUp) {
183        MouseButtonInteraction.Button button =
184            ((MouseButtonDown) mouseButtonDown.getType()).getButton();
185       
186        int x = ((MouseButtonDown) mouseButtonDown.getType()).getX();
187        int y = ((MouseButtonDown) mouseButtonDown.getType()).getY();
188
189        return new Event(new MouseClick(button, x, y), mouseButtonDown.getTarget());
190    }
191
192}
Note: See TracBrowser for help on using the repository browser.