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