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