[439] | 1 | package de.ugoe.cs.quest.tasktrees.temporalrelation; |
---|
| 2 | |
---|
| 3 | import java.util.ArrayList; |
---|
| 4 | import java.util.List; |
---|
| 5 | import java.util.Locale; |
---|
| 6 | |
---|
[557] | 7 | import de.ugoe.cs.quest.eventcore.IEventTarget; |
---|
| 8 | import de.ugoe.cs.quest.eventcore.gui.KeyInteraction; |
---|
| 9 | import de.ugoe.cs.quest.eventcore.gui.KeyPressed; |
---|
| 10 | import de.ugoe.cs.quest.eventcore.gui.KeyReleased; |
---|
| 11 | import de.ugoe.cs.quest.eventcore.guimodel.ITextArea; |
---|
| 12 | import de.ugoe.cs.quest.eventcore.guimodel.ITextField; |
---|
| 13 | import de.ugoe.cs.quest.tasktrees.treeifc.IEventTask; |
---|
| 14 | import de.ugoe.cs.quest.tasktrees.treeifc.ISequence; |
---|
| 15 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeBuilder; |
---|
| 16 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode; |
---|
| 17 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNodeFactory; |
---|
| 18 | import de.ugoe.cs.quest.tasktrees.treeifc.ITextInputEventTask; |
---|
[439] | 19 | import de.ugoe.cs.tasktree.keyboardmaps.KeyboardMap; |
---|
| 20 | import de.ugoe.cs.tasktree.keyboardmaps.KeyboardMapFactory; |
---|
| 21 | import de.ugoe.cs.tasktree.keyboardmaps.VirtualKey; |
---|
| 22 | |
---|
| 23 | /** |
---|
| 24 | * TODO comment |
---|
| 25 | * |
---|
| 26 | * @version $Revision: $ $Date: 18.03.2012$ |
---|
| 27 | * @author 2012, last modified by $Author: patrick$ |
---|
| 28 | */ |
---|
[557] | 29 | public class DefaultTextInputReductionRule implements TemporalRelationshipRule { |
---|
| 30 | |
---|
| 31 | /** */ |
---|
| 32 | private KeyboardMap keyboardMap = KeyboardMapFactory.createKeyboardMap(Locale.GERMAN); |
---|
[439] | 33 | |
---|
[557] | 34 | /* |
---|
| 35 | * (non-Javadoc) |
---|
| 36 | * |
---|
| 37 | * @see TemporalRelationshipRule#apply(TaskTreeNode, TaskTreeBuilder, TaskTreeNodeFactory) |
---|
| 38 | */ |
---|
| 39 | @Override |
---|
| 40 | public RuleApplicationResult apply(ITaskTreeNode parent, |
---|
| 41 | ITaskTreeBuilder builder, |
---|
| 42 | ITaskTreeNodeFactory nodeFactory, |
---|
| 43 | boolean finalize) |
---|
[439] | 44 | { |
---|
[557] | 45 | if ((!(parent instanceof ISequence)) || (parent instanceof ITextInputEventTask)) { |
---|
| 46 | return null; |
---|
[439] | 47 | } |
---|
[557] | 48 | |
---|
| 49 | RuleApplicationResult result = new RuleApplicationResult(); |
---|
| 50 | int textEntryStartIndex = -1; |
---|
| 51 | IEventTarget currentEventTarget = null; |
---|
| 52 | |
---|
| 53 | int index = 0; |
---|
| 54 | ITaskTreeNode task = null; |
---|
| 55 | while (index < parent.getChildren().size()) { |
---|
| 56 | task = parent.getChildren().get(index); |
---|
| 57 | if (isKeyInteraction(task) && |
---|
| 58 | isDataInputEventTarget(((IEventTask) task).getEventTarget())) |
---|
| 59 | { |
---|
| 60 | if (textEntryStartIndex < 0) { |
---|
| 61 | textEntryStartIndex = index; |
---|
| 62 | currentEventTarget = ((IEventTask) task).getEventTarget(); |
---|
| 63 | } |
---|
| 64 | else if (!currentEventTarget.equals(((IEventTask) task).getEventTarget())) { |
---|
| 65 | handleTextEntrySequence(parent, textEntryStartIndex, index - 1, |
---|
| 66 | currentEventTarget, builder, nodeFactory, result); |
---|
| 67 | return result; |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | else if (textEntryStartIndex >= 0) { |
---|
| 71 | handleTextEntrySequence(parent, textEntryStartIndex, index - 1, currentEventTarget, |
---|
| 72 | builder, nodeFactory, result); |
---|
| 73 | return result; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | index++; |
---|
[439] | 77 | } |
---|
[557] | 78 | |
---|
| 79 | if (textEntryStartIndex >= 0) { |
---|
| 80 | if (finalize) { |
---|
| 81 | handleTextEntrySequence(parent, textEntryStartIndex, |
---|
| 82 | parent.getChildren().size() - 1, currentEventTarget, |
---|
| 83 | builder, nodeFactory, result); |
---|
| 84 | } |
---|
| 85 | else { |
---|
| 86 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE); |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
[439] | 90 | return result; |
---|
| 91 | } |
---|
[557] | 92 | |
---|
| 93 | /** |
---|
| 94 | * TODO: comment |
---|
| 95 | * |
---|
| 96 | * @param taskTreeNode |
---|
| 97 | * @return |
---|
| 98 | */ |
---|
| 99 | private boolean isKeyInteraction(ITaskTreeNode taskTreeNode) { |
---|
| 100 | if ((taskTreeNode instanceof IEventTask)) { |
---|
| 101 | return (((IEventTask) taskTreeNode).getEventType() instanceof KeyInteraction); |
---|
| 102 | } |
---|
| 103 | else { |
---|
| 104 | return false; |
---|
| 105 | } |
---|
[439] | 106 | } |
---|
| 107 | |
---|
[557] | 108 | /** |
---|
| 109 | * TODO: comment |
---|
| 110 | * |
---|
| 111 | * @param textEntryStartIndex |
---|
| 112 | * @param i |
---|
| 113 | * @param result |
---|
| 114 | * @return |
---|
| 115 | */ |
---|
| 116 | private void handleTextEntrySequence(ITaskTreeNode parent, |
---|
| 117 | int startIndex, |
---|
| 118 | int endIndex, |
---|
| 119 | IEventTarget eventTarget, |
---|
| 120 | ITaskTreeBuilder builder, |
---|
| 121 | ITaskTreeNodeFactory nodeFactory, |
---|
| 122 | RuleApplicationResult result) |
---|
[439] | 123 | { |
---|
[557] | 124 | ITextInputEventTask textInput = nodeFactory.createNewTextInputEventTask(eventTarget); |
---|
| 125 | |
---|
| 126 | for (int i = startIndex; i <= endIndex; i++) { |
---|
| 127 | builder.addChild(textInput, parent.getChildren().get(startIndex)); |
---|
| 128 | builder.removeChild((ISequence) parent, startIndex); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | builder.addChild((ISequence) parent, startIndex, textInput); |
---|
| 132 | |
---|
| 133 | StringBuffer enteredText = new StringBuffer(); |
---|
| 134 | determineEnteredText(textInput, new ArrayList<VirtualKey>(), enteredText); |
---|
| 135 | textInput.setEnteredText(enteredText.toString()); |
---|
| 136 | |
---|
| 137 | result.addNewlyCreatedParentNode(textInput); |
---|
| 138 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED); |
---|
[439] | 139 | } |
---|
[557] | 140 | |
---|
| 141 | /** |
---|
| 142 | * TODO: comment |
---|
| 143 | * |
---|
| 144 | * @param eventTarget |
---|
| 145 | * @return |
---|
| 146 | */ |
---|
| 147 | private boolean isDataInputEventTarget(IEventTarget eventTarget) { |
---|
| 148 | return ((eventTarget instanceof ITextField) || (eventTarget instanceof ITextArea)); |
---|
[439] | 149 | } |
---|
| 150 | |
---|
[557] | 151 | /** |
---|
| 152 | * TODO: comment |
---|
| 153 | * |
---|
| 154 | * @param sequence |
---|
| 155 | * @param enteredText |
---|
| 156 | */ |
---|
| 157 | private void determineEnteredText(ITaskTreeNode node, |
---|
| 158 | List<VirtualKey> pressedKeys, |
---|
| 159 | StringBuffer enteredText) |
---|
[439] | 160 | { |
---|
[557] | 161 | if ((node instanceof ISequence) || (node instanceof ITextInputEventTask)) { |
---|
| 162 | for (ITaskTreeNode child : node.getChildren()) { |
---|
| 163 | if (child instanceof IEventTask) { |
---|
| 164 | if (((IEventTask) child).getEventType() instanceof KeyPressed) { |
---|
| 165 | VirtualKey key = |
---|
| 166 | ((KeyPressed) ((IEventTask) child).getEventType()).getKey(); |
---|
| 167 | |
---|
| 168 | pressedKeys.add(key); |
---|
| 169 | |
---|
| 170 | if (key == VirtualKey.BACK_SPACE) { |
---|
| 171 | if (enteredText.length() > 0) { |
---|
| 172 | enteredText.deleteCharAt(enteredText.length() - 1); |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | else if (key == VirtualKey.ENTER) { |
---|
| 176 | // text fields only contain one line of code. Therefore the return is |
---|
| 177 | // ignored. |
---|
| 178 | if (!(((IEventTask) child).getEventTarget() instanceof ITextField)) { |
---|
| 179 | enteredText.append(getCharacter(key, pressedKeys)); |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | else { |
---|
| 183 | char theChar = getCharacter(key, pressedKeys); |
---|
| 184 | if (theChar != Character.UNASSIGNED) { |
---|
| 185 | enteredText.append(theChar); |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | else if (((IEventTask) child).getEventType() instanceof KeyReleased) { |
---|
| 190 | pressedKeys.remove |
---|
| 191 | (((KeyReleased) ((IEventTask) child).getEventType()).getKey()); |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | else { |
---|
| 195 | determineEnteredText(child, pressedKeys, enteredText); |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | } |
---|
[439] | 199 | } |
---|
| 200 | |
---|
[557] | 201 | /** |
---|
| 202 | * TODO: comment |
---|
| 203 | * |
---|
| 204 | * @param key |
---|
| 205 | * @param pressedKeys |
---|
| 206 | * @return |
---|
| 207 | */ |
---|
| 208 | private char getCharacter(VirtualKey key, List<VirtualKey> pressedKeys) { |
---|
| 209 | boolean numlock = false; |
---|
| 210 | boolean shift = false; |
---|
| 211 | boolean altgr = false; |
---|
[439] | 212 | |
---|
[557] | 213 | for (VirtualKey pressedKey : pressedKeys) { |
---|
| 214 | if (pressedKey.isShiftKey()) { |
---|
| 215 | shift = !shift; |
---|
[439] | 216 | } |
---|
[557] | 217 | else if (pressedKey == VirtualKey.ALT_GRAPH) { |
---|
| 218 | altgr = !altgr; |
---|
[439] | 219 | } |
---|
[557] | 220 | else if (pressedKey == VirtualKey.NUM_LOCK) { |
---|
| 221 | numlock = !numlock; |
---|
[439] | 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|
[557] | 225 | return keyboardMap.getCharacterFor(key, numlock, shift, altgr, false); |
---|
[439] | 226 | } |
---|
| 227 | |
---|
| 228 | } |
---|