source: trunk/quest-core-events/src/main/java/de/ugoe/cs/quest/eventcore/gui/KeyInteractionTargetCorrector.java @ 708

Last change on this file since 708 was 708, checked in by pharms, 12 years ago
  • moved target correction of key events regarding the current keyboard focus to a dedicated command
File size: 2.8 KB
Line 
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
6package de.ugoe.cs.quest.eventcore.gui;
7
8import java.util.LinkedList;
9import java.util.List;
10
11import de.ugoe.cs.quest.eventcore.Event;
12import 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 sequence.
20 * </p>
21 *
22 * @version $Revision: $ $Date: 29.08.2012$
23 * @author 2012, last modified by $Author: pharms$
24 */
25public 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        Event resultingEvent;
44       
45        for (Event event : sequence) {
46            resultingEvent = null;
47           
48            if (event.getTarget() instanceof IGUIElement) {
49                if (event.getType() instanceof KeyboardFocusChange) {
50                    currentKeyboardFocusGUIElement = (IGUIElement) event.getTarget();
51                }
52                else if (event.getType() instanceof KeyInteraction) {
53                    if (currentKeyboardFocusGUIElement == null) {
54                        currentKeyboardFocusGUIElement = (IGUIElement) event.getTarget();
55                    }
56                   
57                    if (!currentKeyboardFocusGUIElement.equals(event.getTarget())) {
58                        resultingEvent = new Event(event.getType(), currentKeyboardFocusGUIElement);
59                    }
60                }
61            }
62           
63            if (resultingEvent != null) {
64                resultingSequence.add(resultingEvent);
65            }
66            else {
67                resultingSequence.add(event);
68            }
69        }
70       
71        return resultingSequence;
72    }
73}
Note: See TracBrowser for help on using the repository browser.