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