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

Last change on this file since 2252 was 2252, checked in by pharms, 7 years ago
  • solved some findbugs issues
File size: 3.5 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;
19import java.util.Map;
20
21import de.ugoe.cs.autoquest.eventcore.Event;
22import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
23
24/**
25 * <p>
26 * This class iterates the provided sequence and sets the target of all key interaction events
27 * to the GUI element having the current keyboard focus. The current keyboard focus is determined
28 * either by keyboard focus events or by using the target of the first key interaction in the
29 * provided sequence. Events changing the keyboard focus are discarded herewith.
30 * </p>
31 *
32 * @version $Revision: $ $Date: 29.08.2012$
33 * @author 2012, last modified by $Author: pharms$
34 */
35public class KeyInteractionTargetCorrector {
36   
37    /**
38     * <p>
39     * This method performs the work described in the description of the class. A new list is
40     * instantiated and returned. This list is filled with the events provided by the sequence
41     * being the parameter of the method except for key interaction events. Those are replaced
42     * by a new event with the identical event type but the corrected event target.
43     * </p>
44     *
45     * @param sequence the event sequence to correct the key interactions targets in
46     *
47     * @return the resulting sequence, in which key interactions have the correct target, i.e.
48     *         the GUI element having the keyboard focus
49     */
50    public List<Event> correctKeyInteractionTargets(List<Event> sequence) {
51        List<Event> resultingSequence = new LinkedList<Event>();
52        IGUIElement currentKeyboardFocusGUIElement = null;
53       
54        for (Event event : sequence) {
55            Event nextEvent = event;
56            if (event.getTarget() instanceof IGUIElement) {
57                if (event.getType() instanceof KeyboardFocusChange) {
58                    currentKeyboardFocusGUIElement = (IGUIElement) event.getTarget();
59                    nextEvent = null;
60                }
61                else if (event.getType() instanceof KeyInteraction) {
62                    if (currentKeyboardFocusGUIElement == null) {
63                        currentKeyboardFocusGUIElement = (IGUIElement) event.getTarget();
64                    }
65                   
66                    if (!currentKeyboardFocusGUIElement.equals(event.getTarget())) {
67                        nextEvent = new Event(event.getType(), currentKeyboardFocusGUIElement);
68                        nextEvent.setTimestamp(event.getTimestamp());
69                       
70                        for (Map.Entry<String, String> parameter : event.getParameters().entrySet()) {
71                            nextEvent.setParameter(parameter.getKey(), parameter.getValue());
72                        }
73                    }
74                }
75            }
76           
77            if (nextEvent != null) {
78                resultingSequence.add(nextEvent);
79            }
80        }
81       
82        return resultingSequence;
83    }
84}
Note: See TracBrowser for help on using the repository browser.