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