source: trunk/autoquest-ui-core/src/main/java/de/ugoe/cs/autoquest/commands/sequences/CMDsortKeyInteractions.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: 4.3 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;
20import java.util.logging.Level;
21
22import de.ugoe.cs.autoquest.CommandHelpers;
23import de.ugoe.cs.autoquest.SequenceInstanceOf;
24import de.ugoe.cs.autoquest.eventcore.Event;
25import de.ugoe.cs.autoquest.eventcore.gui.KeyInteractionCorrector;
26import de.ugoe.cs.autoquest.eventcore.gui.KeyInteractionCorrector.CleanupMode;
27import de.ugoe.cs.util.console.Command;
28import de.ugoe.cs.util.console.Console;
29import de.ugoe.cs.util.console.GlobalDataContainer;
30
31/**
32 * <p>
33 * Command to sort the key interactions in a sequence of events. An example, the sequence to write
34 * the upper case D
35 * <ul>
36 * <li>press shift key</li>
37 * <li>press D key</li>
38 * <li>release shift key</li>
39 * <li>release D key</li>
40 * </ul>
41 *
42 * is transformed to the sequence
43 *
44 * <ul>
45 * <li>press shift key</li>
46 * <li>press D key</li>
47 * <li>release D key</li>
48 * <li>release shift key</li>
49 * </ul>
50 *
51 * in which the first pressed key (shift in this case) is always released last. The same is done for
52 * the alt and the ctrl keys.
53 *
54 * </p>
55 *
56 * @author Patrick Harms
57 * @version 1.0
58 */
59public class CMDsortKeyInteractions implements Command {
60
61    /*
62     * (non-Javadoc)
63     *
64     * @see de.ugoe.cs.util.console.Command#help()
65     */
66    @Override
67    public String help() {
68        return "sortKeyInteractions <sequences> {<new sequences>}";
69    }
70
71    /*
72     * (non-Javadoc)
73     *
74     * @see de.ugoe.cs.util.console.Command#run(java.util.List)
75     */
76    @SuppressWarnings("unchecked")
77    @Override
78    public void run(List<Object> parameters) {
79        String sequencesName;
80        String newSequencesName;
81        String modeString = "ADDITION";
82        try {
83            sequencesName = (String) parameters.get(0);
84            if (parameters.size() > 1) {
85                newSequencesName = (String) parameters.get(1);
86            }
87            else {
88                newSequencesName = sequencesName;
89            }
90            if (parameters.size() > 2) {
91                modeString = (String) parameters.get(2);
92            }
93        }
94        catch (Exception e) {
95            throw new IllegalArgumentException("must provide a sequences name");
96        }
97
98        Collection<List<Event>> sequences = null;
99        Object dataObject = GlobalDataContainer.getInstance().getData(sequencesName);
100        if (dataObject == null) {
101            CommandHelpers.objectNotFoundMessage(sequencesName);
102            return;
103        }
104        if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) {
105            CommandHelpers.objectNotType(sequencesName, "Collection<List<Event<?>>>");
106            return;
107        }
108
109        CleanupMode mode = null;
110        try {
111            mode = CleanupMode.valueOf(modeString);
112        }
113        catch (IllegalArgumentException e) {
114            Console.printerrln("Invalid mode. Only REMOVAL and ADDITION are allowed values!");
115            return;
116        }
117
118        sequences = (Collection<List<Event>>) dataObject;
119
120        Collection<List<Event>> newSequences = new LinkedList<List<Event>>();
121
122        int i = 1;
123        for (List<Event> sequence : sequences) {
124            Console.traceln(Level.INFO, "Processing sequence " + i++);
125            newSequences.add(new KeyInteractionCorrector(mode).sortKeyInteractions(sequence));
126        }
127
128        if (GlobalDataContainer.getInstance().addData(newSequencesName, newSequences)) {
129            CommandHelpers.dataOverwritten(newSequencesName);
130        }
131
132    }
133
134}
Note: See TracBrowser for help on using the repository browser.