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.guimodel.IGUIElement; |
---|
22 | |
---|
23 | /** |
---|
24 | * <p> |
---|
25 | * This class iterates the provided sequence and sets the target of all key interaction events |
---|
26 | * to the GUI element having the current keyboard focus. The current keyboard focus is determined |
---|
27 | * either by keyboard focus events or by using the target of the first key interaction in the |
---|
28 | * provided sequence. Events changing the keyboard focus are discarded herewith. |
---|
29 | * </p> |
---|
30 | * |
---|
31 | * @version $Revision: $ $Date: 29.08.2012$ |
---|
32 | * @author 2012, last modified by $Author: pharms$ |
---|
33 | */ |
---|
34 | public class KeyInteractionTargetCorrector { |
---|
35 | |
---|
36 | /** |
---|
37 | * <p> |
---|
38 | * This method performs the work described in the description of the class. A new list is |
---|
39 | * instantiated and returned. This list is filled with the events provided by the sequence |
---|
40 | * being the parameter of the method except for key interaction events. Those are replaced |
---|
41 | * by a new event with the identical event type but the corrected event target. |
---|
42 | * </p> |
---|
43 | * |
---|
44 | * @param sequence the event sequence to correct the key interactions targets in |
---|
45 | * |
---|
46 | * @return the resulting sequence, in which key interactions have the correct target, i.e. |
---|
47 | * the GUI element having the keyboard focus |
---|
48 | */ |
---|
49 | public List<Event> correctKeyInteractionTargets(List<Event> sequence) { |
---|
50 | List<Event> resultingSequence = new LinkedList<Event>(); |
---|
51 | IGUIElement currentKeyboardFocusGUIElement = null; |
---|
52 | |
---|
53 | for (Event event : sequence) { |
---|
54 | Event nextEvent = event; |
---|
55 | if (event.getTarget() instanceof IGUIElement) { |
---|
56 | if (event.getType() instanceof KeyboardFocusChange) { |
---|
57 | currentKeyboardFocusGUIElement = (IGUIElement) event.getTarget(); |
---|
58 | nextEvent = null; |
---|
59 | } |
---|
60 | else if (event.getType() instanceof KeyInteraction) { |
---|
61 | if (currentKeyboardFocusGUIElement == null) { |
---|
62 | currentKeyboardFocusGUIElement = (IGUIElement) event.getTarget(); |
---|
63 | } |
---|
64 | |
---|
65 | if (!currentKeyboardFocusGUIElement.equals(event.getTarget())) { |
---|
66 | nextEvent = new Event(event.getType(), currentKeyboardFocusGUIElement); |
---|
67 | nextEvent.setTimestamp(event.getTimestamp()); |
---|
68 | |
---|
69 | // TODO copy event parameters |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | if (nextEvent != null) { |
---|
75 | resultingSequence.add(nextEvent); |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | return resultingSequence; |
---|
80 | } |
---|
81 | } |
---|