source: trunk/autoquest-ui-core/src/main/java/de/ugoe/cs/autoquest/commands/sequences/CMDcorrectTabKeyNavigationOrder.java @ 1432

Last change on this file since 1432 was 1432, checked in by pharms, 10 years ago
  • corrected time stamp handling
File size: 6.4 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.commands.sequences;
16
17import java.util.Collection;
18import java.util.LinkedList;
19import java.util.List;
20
21import de.ugoe.cs.autoquest.CommandHelpers;
22import de.ugoe.cs.autoquest.SequenceInstanceOf;
23import de.ugoe.cs.autoquest.eventcore.Event;
24import de.ugoe.cs.autoquest.eventcore.IEventTarget;
25import de.ugoe.cs.autoquest.eventcore.gui.KeyInteraction;
26import de.ugoe.cs.autoquest.eventcore.gui.KeyPressed;
27import de.ugoe.cs.autoquest.eventcore.gui.KeyTyped;
28import de.ugoe.cs.autoquest.eventcore.gui.TextInput;
29import de.ugoe.cs.autoquest.eventcore.guimodel.ITextArea;
30import de.ugoe.cs.autoquest.eventcore.guimodel.ITextField;
31import de.ugoe.cs.autoquest.keyboardmaps.VirtualKey;
32import de.ugoe.cs.util.console.Command;
33import de.ugoe.cs.util.console.GlobalDataContainer;
34
35/**
36 * <p>
37 * This command iterates the provided sequences and corrects the order of events in case of tab
38 * key navigation. This is required, as from time to time the event of pressing the tab key for
39 * navigation in formulars comes before the text input event in a text input field out of which
40 * the tab key navigates.
41 * </p>
42 *
43 * @author Patrick Harms
44 * @version 1.0
45 */
46public class CMDcorrectTabKeyNavigationOrder implements Command {
47
48    /*
49     * (non-Javadoc)
50     *
51     * @see de.ugoe.cs.util.console.Command#help()
52     */
53    @Override
54    public String help() {
55        return "correctTabKeyNavigationOrder <sequences> {<new sequences>}";
56    }
57
58    /*
59     * (non-Javadoc)
60     *
61     * @see de.ugoe.cs.util.console.Command#run(java.util.List)
62     */
63    @SuppressWarnings("unchecked")
64    @Override
65    public void run(List<Object> parameters) {
66        String sequencesName;
67        String newSequencesName;
68        try {
69            sequencesName = (String) parameters.get(0);
70            if (parameters.size() > 1) {
71                newSequencesName = (String) parameters.get(1);
72            }
73            else {
74                newSequencesName = sequencesName;
75            }
76        }
77        catch (Exception e) {
78            throw new IllegalArgumentException("must provide a sequences name");
79        }
80
81        Collection<List<Event>> sequences = null;
82        Object dataObject = GlobalDataContainer.getInstance().getData(sequencesName);
83        if (dataObject == null) {
84            CommandHelpers.objectNotFoundMessage(sequencesName);
85            return;
86        }
87        if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) {
88            CommandHelpers.objectNotType(sequencesName, "Collection<List<Event<?>>>");
89            return;
90        }
91
92        sequences = (Collection<List<Event>>) dataObject;
93
94        Collection<List<Event>> newSequences = new LinkedList<List<Event>>();
95       
96        for (List<Event> sequence : sequences) {
97            newSequences.add(correctTabKeyNavigationOrder(sequence));
98        }
99
100        if (GlobalDataContainer.getInstance().addData(newSequencesName, newSequences)) {
101            CommandHelpers.dataOverwritten(newSequencesName);
102        }
103       
104    }
105
106    /**
107     * <p>
108     * convenience method that corrects the order of tab key navigation events, if required
109     * </p>
110     *
111     * @param sequence the sequence, in which the tab key navigation events shall be corrected.
112     *
113     * @return a new sequence with a corrected order of tab key navigation events
114     */
115    private List<Event> correctTabKeyNavigationOrder(List<Event> sequence) {
116        List<Event> result = new LinkedList<Event>();
117       
118        int index = 0;
119        while (index < sequence.size()) {
120            if (mustCorrectTabKeyNavigationOrder(sequence, index)) {
121                Event event1 = sequence.get(index);
122                Event event2 = sequence.get(index + 1);
123               
124                // switch timestamps
125                long timestamp1 = event1.getTimestamp();
126                event1.setTimestamp(event2.getTimestamp());
127                event2.setTimestamp(timestamp1);
128               
129                // change order of events
130                result.add(event2);
131                result.add(event1);
132               
133                index += 2;
134            }
135            else {
136                result.add(sequence.get(index++));
137            }
138        }
139       
140        return result;
141    }
142
143    /**
144     * <p>
145     * convenience method that checks if at the given position in the sequence is a tab key event
146     * followed by a text input event. If this is the case, both events have the wrong order and
147     * this method returns true. Otherwise it returns false.
148     * </p>
149     *
150     * @param sequence the sequence to check for the occurrence of a correctable tab key navigation
151     * @param index    the index in the sequence to check
152     *
153     * @return as described
154     */
155    private boolean mustCorrectTabKeyNavigationOrder(List<Event> sequence, int index) {
156        boolean result = false;
157       
158        Event event1 = sequence.get(index);
159       
160        if ((index < (sequence.size() - 1)) &&
161            ((event1.getType() instanceof KeyPressed) || (event1.getType() instanceof KeyTyped)))
162        {
163            VirtualKey key = ((KeyInteraction) event1.getType()).getKey();
164            if (key == VirtualKey.TAB) {
165                IEventTarget target1 = event1.getTarget();
166                Event event2 = sequence.get(index + 1);
167               
168                if (((target1 instanceof ITextArea) || (target1 instanceof ITextField)) &&
169                    (target1.equals(event2.getTarget())) &&
170                    (event2.getType() instanceof TextInput))
171                {
172                    result = true;
173                }
174            }
175        }
176       
177        return result;
178    }
179
180}
Note: See TracBrowser for help on using the repository browser.