[927] | 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 | |
---|
[922] | 15 | package de.ugoe.cs.autoquest.eventcore.gui; |
---|
[708] | 16 | |
---|
| 17 | import java.util.LinkedList; |
---|
| 18 | import java.util.List; |
---|
[2252] | 19 | import java.util.Map; |
---|
[708] | 20 | |
---|
[922] | 21 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
| 22 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; |
---|
[708] | 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 |
---|
[719] | 29 | * provided sequence. Events changing the keyboard focus are discarded herewith. |
---|
[708] | 30 | * </p> |
---|
| 31 | * |
---|
| 32 | * @version $Revision: $ $Date: 29.08.2012$ |
---|
| 33 | * @author 2012, last modified by $Author: pharms$ |
---|
| 34 | */ |
---|
| 35 | public 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) { |
---|
[1432] | 55 | Event nextEvent = event; |
---|
[708] | 56 | if (event.getTarget() instanceof IGUIElement) { |
---|
| 57 | if (event.getType() instanceof KeyboardFocusChange) { |
---|
| 58 | currentKeyboardFocusGUIElement = (IGUIElement) event.getTarget(); |
---|
[1432] | 59 | nextEvent = null; |
---|
[708] | 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())) { |
---|
[1432] | 67 | nextEvent = new Event(event.getType(), currentKeyboardFocusGUIElement); |
---|
| 68 | nextEvent.setTimestamp(event.getTimestamp()); |
---|
| 69 | |
---|
[2252] | 70 | for (Map.Entry<String, String> parameter : event.getParameters().entrySet()) { |
---|
| 71 | nextEvent.setParameter(parameter.getKey(), parameter.getValue()); |
---|
| 72 | } |
---|
[708] | 73 | } |
---|
| 74 | } |
---|
| 75 | } |
---|
| 76 | |
---|
[1432] | 77 | if (nextEvent != null) { |
---|
| 78 | resultingSequence.add(nextEvent); |
---|
[708] | 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | return resultingSequence; |
---|
| 83 | } |
---|
| 84 | } |
---|