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