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

Last change on this file since 1316 was 1316, checked in by pharms, 11 years ago
  • added command to correct tab key navigation events if they occur before the text input event which they submit and which they should, therefore, follow
File size: 6.0 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                result.add(sequence.get(index + 1));
122                result.add(sequence.get(index));
123                index += 2;
124            }
125            else {
126                result.add(sequence.get(index++));
127            }
128        }
129       
130        return result;
131    }
132
133    /**
134     * <p>
135     * convenience method that checks if at the given position in the sequence is a tab key event
136     * followed by a text input event. If this is the case, both events have the wrong order and
137     * this method returns true. Otherwise it returns false.
138     * </p>
139     *
140     * @param sequence the sequence to check for the occurrence of a correctable tab key navigation
141     * @param index    the index in the sequence to check
142     *
143     * @return as described
144     */
145    private boolean mustCorrectTabKeyNavigationOrder(List<Event> sequence, int index) {
146        boolean result = false;
147       
148        Event event1 = sequence.get(index);
149       
150        if ((index < (sequence.size() - 1)) &&
151            ((event1.getType() instanceof KeyPressed) || (event1.getType() instanceof KeyTyped)))
152        {
153            VirtualKey key = ((KeyInteraction) event1.getType()).getKey();
154            if (key == VirtualKey.TAB) {
155                IEventTarget target1 = event1.getTarget();
156                Event event2 = sequence.get(index + 1);
157               
158                if (((target1 instanceof ITextArea) || (target1 instanceof ITextField)) &&
159                    (target1.equals(event2.getTarget())) &&
160                    (event2.getType() instanceof TextInput))
161                {
162                    result = true;
163                }
164            }
165        }
166       
167        return result;
168    }
169
170}
Note: See TracBrowser for help on using the repository browser.