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

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
File size: 4.1 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 mouse click with the same button on the same event target to a single mouse click with
27 * that button on that target. The mouse button down and mouse button up events are discarded. For
28 * this, it iterates the provided sequence and identifies any match of the named event sequence
29 * pattern. This match is condensed to the mouse click event.
30 * </p>
31 *
32 * @version 1.0
33 * @author Patrick Harms
34 */
35public class MouseClickCondenser {
36
37    /**
38     * <p>
39     * This method performs the work described in the description of the class. A new list is
40     * instantiated and returned. This list is filled with the events provided by the sequence being
41     * the parameter of the method except for mouse button down and mouse button up events which are
42     * followed by a mouse click event with the same button on the same target.
43     * </p>
44     *
45     * @param sequence
46     *            the event sequence to condense the mouse clicks in
47     *
48     * @return the resulting sequence, in which mouse clicks are condensed to single mouse click
49     *         events
50     */
51    public List<Event> condenseMouseClicks(List<Event> sequence) {
52        List<Event> resultingSequence = new LinkedList<Event>();
53
54        int index = 0;
55        while (index < sequence.size()) // -2 because we don't need to go to the end
56        {
57            if ((index + 2) < sequence.size()) {
58                Event mouseButtonDown = sequence.get(index);
59                Event mouseButtonUp = sequence.get(index + 1);
60                Event mouseClick = sequence.get(index + 2);
61                if (mouseClickSequenceFound(mouseButtonDown, mouseButtonUp, mouseClick)) {
62                    // skip the mouse button down and mouse button up event
63                    index += 2;
64                }
65            }
66
67            resultingSequence.add(sequence.get(index));
68            index++;
69        }
70
71        return resultingSequence;
72    }
73
74    /**
75     *
76     */
77    private boolean mouseClickSequenceFound(Event mouseButtonDown,
78                                            Event mouseButtonUp,
79                                            Event mouseClick)
80    {
81        // check the first in a row of three for validity
82        if (!(mouseButtonDown.getType() instanceof MouseButtonDown)) {
83            return false;
84        }
85
86        // check the second node for validity
87        if (!(mouseButtonUp.getType() instanceof MouseButtonUp)) {
88            return false;
89        }
90
91        IEventTarget eventTarget = mouseButtonDown.getTarget();
92
93        if (!eventTarget.equals(mouseButtonUp.getTarget())) {
94            return false;
95        }
96
97        MouseButtonInteraction.Button button =
98            ((MouseButtonDown) mouseButtonDown.getType()).getButton();
99
100        if (!button.equals(((MouseButtonUp) mouseButtonUp.getType()).getButton())) {
101            return false;
102        }
103
104        // check the third node for validity
105        if (!(mouseClick.getType() instanceof MouseClick)) {
106            return false;
107        }
108
109        if (!eventTarget.equals(mouseClick.getTarget())) {
110            return false;
111        }
112
113        if (!button.equals(((MouseClick) mouseClick.getType()).getButton())) {
114            return false;
115        }
116
117        return true;
118    }
119
120}
Note: See TracBrowser for help on using the repository browser.