1 | |
---|
2 | package de.ugoe.cs.autoquest.eventcore.gui; |
---|
3 | |
---|
4 | import java.util.LinkedList; |
---|
5 | import java.util.List; |
---|
6 | |
---|
7 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
8 | import de.ugoe.cs.autoquest.eventcore.IEventTarget; |
---|
9 | |
---|
10 | /** |
---|
11 | * <p> |
---|
12 | * This class condenses mouse clicks, i.e. it reduces a sequence of mouse button down, mouse button |
---|
13 | * up and mouse click with the same button on the same event target to a single mouse click with |
---|
14 | * that button on that target. The mouse button down and mouse button up events are discarded. For |
---|
15 | * this, it iterates the provided sequence and identifies any match of the named event sequence |
---|
16 | * pattern. This match is condensed to the mouse click event. |
---|
17 | * </p> |
---|
18 | * |
---|
19 | * @version 1.0 |
---|
20 | * @author Patrick Harms |
---|
21 | */ |
---|
22 | public class MouseClickCondenser { |
---|
23 | |
---|
24 | /** |
---|
25 | * <p> |
---|
26 | * This method performs the work described in the description of the class. A new list is |
---|
27 | * instantiated and returned. This list is filled with the events provided by the sequence being |
---|
28 | * the parameter of the method except for mouse button down and mouse button up events which are |
---|
29 | * followed by a mouse click event with the same button on the same target. |
---|
30 | * </p> |
---|
31 | * |
---|
32 | * @param sequence |
---|
33 | * the event sequence to condense the mouse clicks in |
---|
34 | * |
---|
35 | * @return the resulting sequence, in which mouse clicks are condensed to single mouse click |
---|
36 | * events |
---|
37 | */ |
---|
38 | public List<Event> condenseMouseClicks(List<Event> sequence) { |
---|
39 | List<Event> resultingSequence = new LinkedList<Event>(); |
---|
40 | |
---|
41 | int index = 0; |
---|
42 | while (index < sequence.size()) // -2 because we don't need to go to the end |
---|
43 | { |
---|
44 | if ((index + 2) < sequence.size()) { |
---|
45 | Event mouseButtonDown = sequence.get(index); |
---|
46 | Event mouseButtonUp = sequence.get(index + 1); |
---|
47 | Event mouseClick = sequence.get(index + 2); |
---|
48 | if (mouseClickSequenceFound(mouseButtonDown, mouseButtonUp, mouseClick)) { |
---|
49 | // skip the mouse button down and mouse button up event |
---|
50 | index += 2; |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | resultingSequence.add(sequence.get(index)); |
---|
55 | index++; |
---|
56 | } |
---|
57 | |
---|
58 | return resultingSequence; |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * |
---|
63 | */ |
---|
64 | private boolean mouseClickSequenceFound(Event mouseButtonDown, |
---|
65 | Event mouseButtonUp, |
---|
66 | Event mouseClick) |
---|
67 | { |
---|
68 | // check the first in a row of three for validity |
---|
69 | if (!(mouseButtonDown.getType() instanceof MouseButtonDown)) { |
---|
70 | return false; |
---|
71 | } |
---|
72 | |
---|
73 | // check the second node for validity |
---|
74 | if (!(mouseButtonUp.getType() instanceof MouseButtonUp)) { |
---|
75 | return false; |
---|
76 | } |
---|
77 | |
---|
78 | IEventTarget eventTarget = mouseButtonDown.getTarget(); |
---|
79 | |
---|
80 | if (!eventTarget.equals(mouseButtonUp.getTarget())) { |
---|
81 | return false; |
---|
82 | } |
---|
83 | |
---|
84 | MouseButtonInteraction.Button button = |
---|
85 | ((MouseButtonDown) mouseButtonDown.getType()).getButton(); |
---|
86 | |
---|
87 | if (!button.equals(((MouseButtonUp) mouseButtonUp.getType()).getButton())) { |
---|
88 | return false; |
---|
89 | } |
---|
90 | |
---|
91 | // check the third node for validity |
---|
92 | if (!(mouseClick.getType() instanceof MouseClick)) { |
---|
93 | return false; |
---|
94 | } |
---|
95 | |
---|
96 | if (!eventTarget.equals(mouseClick.getTarget())) { |
---|
97 | return false; |
---|
98 | } |
---|
99 | |
---|
100 | if (!button.equals(((MouseClick) mouseClick.getType()).getButton())) { |
---|
101 | return false; |
---|
102 | } |
---|
103 | |
---|
104 | return true; |
---|
105 | } |
---|
106 | |
---|
107 | } |
---|