[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
|
---|
[684] | 35 | * <ul>
|
---|
[765] | 36 | * <li>press shift key</li>
|
---|
| 37 | * <li>press D key</li>
|
---|
| 38 | * <li>release shift key</li>
|
---|
| 39 | * <li>release D key</li>
|
---|
[684] | 40 | * </ul>
|
---|
| 41 | *
|
---|
| 42 | * is transformed to the sequence
|
---|
| 43 | *
|
---|
| 44 | * <ul>
|
---|
[765] | 45 | * <li>press shift key</li>
|
---|
| 46 | * <li>press D key</li>
|
---|
| 47 | * <li>release D key</li>
|
---|
| 48 | * <li>release shift key</li>
|
---|
[684] | 49 | * </ul>
|
---|
| 50 | *
|
---|
[765] | 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.
|
---|
[684] | 53 | *
|
---|
| 54 | * </p>
|
---|
| 55 | *
|
---|
| 56 | * @author Patrick Harms
|
---|
| 57 | * @version 1.0
|
---|
| 58 | */
|
---|
| 59 | public 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() {
|
---|
[803] | 68 | return "sortKeyInteractions <sequences> {<new sequences>}";
|
---|
[684] | 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;
|
---|
[765] | 81 | String modeString = "ADDITION";
|
---|
[684] | 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 | }
|
---|
[765] | 90 | if (parameters.size() > 2) {
|
---|
| 91 | modeString = (String) parameters.get(2);
|
---|
| 92 | }
|
---|
[684] | 93 | }
|
---|
| 94 | catch (Exception e) {
|
---|
[766] | 95 | throw new IllegalArgumentException("must provide a sequences name");
|
---|
[684] | 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 |
|
---|
[765] | 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 |
|
---|
[684] | 118 | sequences = (Collection<List<Event>>) dataObject;
|
---|
| 119 |
|
---|
| 120 | Collection<List<Event>> newSequences = new LinkedList<List<Event>>();
|
---|
[765] | 121 |
|
---|
| 122 | int i = 1;
|
---|
[684] | 123 | for (List<Event> sequence : sequences) {
|
---|
[765] | 124 | Console.traceln(Level.INFO, "Processing sequence " + i++);
|
---|
[852] | 125 | newSequences.add(new KeyInteractionCorrector(mode).sortKeyInteractions(sequence));
|
---|
[684] | 126 | }
|
---|
| 127 |
|
---|
| 128 | if (GlobalDataContainer.getInstance().addData(newSequencesName, newSequences)) {
|
---|
[750] | 129 | CommandHelpers.dataOverwritten(newSequencesName);
|
---|
[684] | 130 | }
|
---|
[765] | 131 |
|
---|
[684] | 132 | }
|
---|
| 133 |
|
---|
| 134 | }
|
---|