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

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
File size: 3.2 KB
Line 
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
15package de.ugoe.cs.autoquest.eventcore.gui;
16
17import java.util.LinkedList;
18import java.util.List;
19
20import de.ugoe.cs.autoquest.eventcore.Event;
21import 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 */
34public 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            if (event.getTarget() instanceof IGUIElement) {
55                if (event.getType() instanceof KeyboardFocusChange) {
56                    currentKeyboardFocusGUIElement = (IGUIElement) event.getTarget();
57                    event = null;
58                }
59                else if (event.getType() instanceof KeyInteraction) {
60                    if (currentKeyboardFocusGUIElement == null) {
61                        currentKeyboardFocusGUIElement = (IGUIElement) event.getTarget();
62                    }
63                   
64                    if (!currentKeyboardFocusGUIElement.equals(event.getTarget())) {
65                        event = new Event(event.getType(), currentKeyboardFocusGUIElement);
66                    }
67                }
68            }
69           
70            if (event != null) {
71                resultingSequence.add(event);
72            }
73        }
74       
75        return resultingSequence;
76    }
77}
Note: See TracBrowser for help on using the repository browser.