[1671] | 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. |
---|
| 14 | |
---|
[1673] | 15 | package de.ugoe.cs.autoquest.plugin.jfc.commands; |
---|
[1671] | 16 | |
---|
| 17 | import java.io.BufferedWriter; |
---|
[1673] | 18 | import java.io.File; |
---|
| 19 | import java.io.FileOutputStream; |
---|
[1671] | 20 | import java.io.IOException; |
---|
[1673] | 21 | import java.io.OutputStreamWriter; |
---|
[1825] | 22 | import java.nio.charset.Charset; |
---|
| 23 | import java.nio.file.Files; |
---|
| 24 | import java.nio.file.Paths; |
---|
[1671] | 25 | import java.util.ArrayList; |
---|
[1715] | 26 | import java.util.Calendar; |
---|
[1671] | 27 | import java.util.Collection; |
---|
[1704] | 28 | import java.util.HashMap; |
---|
[1671] | 29 | import java.util.Iterator; |
---|
| 30 | import java.util.List; |
---|
[1825] | 31 | import java.util.Stack; |
---|
[1678] | 32 | import java.util.UUID; |
---|
[1673] | 33 | import java.util.logging.Level; |
---|
[1671] | 34 | |
---|
| 35 | import de.ugoe.cs.autoquest.CommandHelpers; |
---|
| 36 | import de.ugoe.cs.autoquest.SequenceInstanceOf; |
---|
[1673] | 37 | import de.ugoe.cs.util.console.Command; |
---|
[1671] | 38 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
[1809] | 39 | import de.ugoe.cs.autoquest.eventcore.IEventTarget; |
---|
[1685] | 40 | import de.ugoe.cs.autoquest.eventcore.gui.*; |
---|
[1825] | 41 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel; |
---|
| 42 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; |
---|
[1691] | 43 | import de.ugoe.cs.autoquest.keyboardmaps.VirtualKey; |
---|
[1673] | 44 | import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCGUIElement; |
---|
[1825] | 45 | import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCMenu; |
---|
| 46 | import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCMenuButton; |
---|
[1671] | 47 | import de.ugoe.cs.util.console.Console; |
---|
| 48 | import de.ugoe.cs.util.console.GlobalDataContainer; |
---|
| 49 | |
---|
[1688] | 50 | // helper class for the tree like structure part within a Jacareto file |
---|
| 51 | class StructureNode { |
---|
[1713] | 52 | public static int nextRef = 0; |
---|
[1689] | 53 | |
---|
[1688] | 54 | public String content; |
---|
| 55 | public ArrayList<StructureNode> children; |
---|
| 56 | |
---|
[1689] | 57 | public StructureNode(String type) { |
---|
[1714] | 58 | setContent(type); |
---|
[1689] | 59 | children = new ArrayList<StructureNode>(); |
---|
[1688] | 60 | } |
---|
| 61 | |
---|
[1689] | 62 | public StructureNode() { |
---|
| 63 | content = "<Recordable ref=\"" + (nextRef++) + "\" />"; |
---|
| 64 | children = new ArrayList<StructureNode>(); |
---|
| 65 | } |
---|
| 66 | |
---|
[1714] | 67 | public void setContent(String type) { |
---|
| 68 | content = "<StructureElement class=\"jacareto.struct." + type + "\">"; |
---|
| 69 | } |
---|
| 70 | |
---|
[1689] | 71 | public StructureNode add(String type) { |
---|
| 72 | StructureNode node = new StructureNode(type); |
---|
[1688] | 73 | children.add(node); |
---|
| 74 | return node; |
---|
| 75 | } |
---|
| 76 | |
---|
[1689] | 77 | public void addRecordable() { |
---|
| 78 | children.add(new StructureNode()); |
---|
| 79 | } |
---|
| 80 | |
---|
[1688] | 81 | @Override |
---|
| 82 | public String toString() { |
---|
| 83 | String separator = System.getProperty("line.separator"); |
---|
| 84 | String result = content + separator; |
---|
| 85 | |
---|
| 86 | for (StructureNode child : children) { |
---|
| 87 | result += child.toString(); |
---|
| 88 | } |
---|
| 89 | |
---|
[1689] | 90 | if (content.endsWith("/>")) { |
---|
[1688] | 91 | return result; |
---|
| 92 | } |
---|
| 93 | return result + "</StructureElement>" + separator; |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
[1671] | 97 | /** |
---|
| 98 | * <p> |
---|
| 99 | * Command to create a Jacareto xml replay file from stored sessions. |
---|
| 100 | * </p> |
---|
| 101 | * |
---|
| 102 | * @author Daniel May |
---|
| 103 | * @version 1.0 |
---|
| 104 | */ |
---|
[1673] | 105 | public class CMDgenerateJacaretoReplay implements Command { |
---|
[1719] | 106 | private static final int EVENT_DURATION = 150; |
---|
[1808] | 107 | private static final int DOUBLE_CLICK_DURATION = 50; |
---|
| 108 | private static final int STARTUP_DELAY = 10000; |
---|
[1719] | 109 | |
---|
[1686] | 110 | private JFCGUIElement currentFocus; |
---|
[1688] | 111 | private StructureNode structure; |
---|
[1704] | 112 | |
---|
[1701] | 113 | private StructureNode lastKeySequenceEvent; |
---|
| 114 | private StructureNode lastKeyTypedEvent; |
---|
[1704] | 115 | private int currentKeyModifiers; |
---|
| 116 | |
---|
| 117 | private HashMap<VirtualKey, Integer> modifiers; |
---|
[1712] | 118 | |
---|
[1688] | 119 | private StructureNode lastMouseClickEvent; |
---|
| 120 | private StructureNode lastFocusChangeEvent; |
---|
| 121 | private StructureNode lastItemActionEvent; |
---|
[1809] | 122 | private IEventTarget lastMouseDownTarget; |
---|
[1825] | 123 | private HashMap<String, JFCGUIElement> menuElements; |
---|
| 124 | private List<String> menuList; |
---|
[1688] | 125 | |
---|
[1671] | 126 | /* |
---|
| 127 | * (non-Javadoc) |
---|
| 128 | * |
---|
| 129 | * @see de.ugoe.cs.util.console.Command#help() |
---|
| 130 | */ |
---|
| 131 | @Override |
---|
| 132 | public String help() { |
---|
[1825] | 133 | return "generateJacaretoReplay <filename> <sequences> <class> <basepath> <classpathext> {<menufile>}"; |
---|
[1671] | 134 | } |
---|
| 135 | |
---|
| 136 | /* |
---|
| 137 | * (non-Javadoc) |
---|
| 138 | * |
---|
| 139 | * @see de.ugoe.cs.util.console.Command#run(java.util.List) |
---|
| 140 | */ |
---|
| 141 | @SuppressWarnings("unchecked") |
---|
| 142 | @Override |
---|
| 143 | public void run(List<Object> parameters) { |
---|
| 144 | String filename; |
---|
| 145 | String sequencesName; |
---|
[1712] | 146 | String classpath; |
---|
| 147 | String basepath; |
---|
| 148 | String classpathext; |
---|
[1671] | 149 | try { |
---|
| 150 | filename = (String) parameters.get(0); |
---|
| 151 | sequencesName = (String) parameters.get(1); |
---|
[1712] | 152 | classpath = (String) parameters.get(2); |
---|
| 153 | basepath = (String) parameters.get(3); |
---|
| 154 | classpathext = (String) parameters.get(4); |
---|
[1671] | 155 | } |
---|
| 156 | catch (Exception e) { |
---|
| 157 | throw new IllegalArgumentException(); |
---|
| 158 | } |
---|
| 159 | |
---|
[1712] | 160 | if (parameters.size() > 5) { |
---|
[1825] | 161 | try { |
---|
| 162 | menuList = |
---|
| 163 | Files.readAllLines(Paths.get((String) parameters.get(5)), |
---|
| 164 | Charset.defaultCharset()); |
---|
| 165 | } |
---|
| 166 | catch (IOException e) { |
---|
| 167 | Console.printerrln("Unable to open menu file"); |
---|
| 168 | Console.logException(e); |
---|
| 169 | } |
---|
[1712] | 170 | } |
---|
| 171 | |
---|
[1671] | 172 | Collection<List<Event>> sequences = null; |
---|
| 173 | Object dataObject = GlobalDataContainer.getInstance().getData(sequencesName); |
---|
| 174 | if (dataObject == null) { |
---|
| 175 | CommandHelpers.objectNotFoundMessage(sequencesName); |
---|
| 176 | return; |
---|
| 177 | } |
---|
| 178 | if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) { |
---|
| 179 | CommandHelpers.objectNotType(sequencesName, "Collection<List<Event<?>>>"); |
---|
| 180 | return; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | sequences = (Collection<List<Event>>) dataObject; |
---|
| 184 | |
---|
[1825] | 185 | menuElements = new HashMap<>(); |
---|
| 186 | |
---|
[1704] | 187 | // map which maps VirtualKeys back to awt key modifier codes |
---|
| 188 | modifiers = new HashMap<>(); |
---|
| 189 | modifiers.put(VirtualKey.SHIFT, 1); |
---|
| 190 | modifiers.put(VirtualKey.CONTROL, 2); |
---|
| 191 | modifiers.put(VirtualKey.ALT, 8); |
---|
| 192 | modifiers.put(VirtualKey.ALT_GRAPH, 32); |
---|
| 193 | currentKeyModifiers = 0; |
---|
[1712] | 194 | |
---|
[1713] | 195 | StructureNode.nextRef = 0; |
---|
| 196 | |
---|
[1825] | 197 | writeJacaretoXML(sequences, filename, classpath, basepath, classpathext); |
---|
[1671] | 198 | } |
---|
| 199 | |
---|
| 200 | private void writeLine(BufferedWriter writer, String line) throws IOException { |
---|
| 201 | writer.write(line); |
---|
| 202 | writer.newLine(); |
---|
| 203 | } |
---|
| 204 | |
---|
[1712] | 205 | private void writeJacaretoHead(BufferedWriter writer, |
---|
| 206 | String classname, |
---|
| 207 | String basepath, |
---|
| 208 | String classpathext) throws IOException |
---|
| 209 | { |
---|
[1715] | 210 | Calendar now = Calendar.getInstance(); |
---|
| 211 | |
---|
[1671] | 212 | writeLine(writer, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"); |
---|
| 213 | writeLine(writer, "<JacaretoStructure>"); |
---|
| 214 | writeLine(writer, "<Record>"); |
---|
| 215 | |
---|
[1715] | 216 | //@formatter:off |
---|
[1716] | 217 | writeLine(writer, "<Calendar " |
---|
| 218 | + "procTime=\"0\" " |
---|
[1715] | 219 | + "duration=\"0\" " |
---|
| 220 | + "year=\"" + now.get(Calendar.YEAR) + "\" " |
---|
[1716] | 221 | + "month=\"" + (now.get(Calendar.MONTH) + 1) + "\" " |
---|
[1715] | 222 | + "date=\"" + now.get(Calendar.DAY_OF_MONTH) + "\" " |
---|
| 223 | + "hour=\"" + now.get(Calendar.HOUR_OF_DAY) + "\" " |
---|
| 224 | + "min=\"" + now.get(Calendar.MINUTE) + "\" " |
---|
| 225 | + "sec=\"" + now.get(Calendar.SECOND) + "\" " |
---|
| 226 | + "uuid=\"" + UUID.randomUUID() + "\" />" |
---|
| 227 | ); |
---|
[1671] | 228 | writeLine(writer, |
---|
| 229 | "<SystemInfo procTime=\"0\" duration=\"0\" screenWidth=\"2646\" screenHeight=\"1024\" javaVersion=\"1.7.0_65\" lookAndFeel=\"javax.swing.plaf.metal.MetalLookAndFeel\" uuid=\"720f430f-52cf-4d8b-9fbe-58434f766efe\" />"); |
---|
| 230 | writeLine(writer, |
---|
| 231 | "<KeyboardState procTime=\"0\" duration=\"0\" isNumLockOn=\"false\" isScrollLockOn=\"false\" isCapsLockOn=\"false\" applyIsNumLockOn=\"true\" applyIsScrollLockOn=\"true\" applyIsCapsLockOn=\"true\" uuid=\"28146f79-9fc7-49f9-b4a8-5866a7625683\" />"); |
---|
[1678] | 232 | writeLine(writer, "<ComponentMode numberPopupMenues=\"true\" />"); |
---|
[1712] | 233 | writeLine(writer, "<ApplicationStarter " |
---|
[1808] | 234 | + "procTime=\"" + STARTUP_DELAY + "\" " |
---|
| 235 | + "duration=\"" + STARTUP_DELAY + "\" " |
---|
[1712] | 236 | + "name=\"Autoquest Replay\" " |
---|
| 237 | + "class=\"" + classname + "\" " |
---|
[1825] | 238 | + "initclass=\"\" " |
---|
[1712] | 239 | + "basepath=\"" + basepath + "\" " |
---|
| 240 | + "classpathext=\"" + classpathext + "\" " |
---|
| 241 | + "detectDuration=\"false\" " |
---|
| 242 | + "captureparams=\"\" " |
---|
| 243 | + "replayparams=\"\" " |
---|
[1715] | 244 | + "uuid=\"" + UUID.randomUUID() + "\" />" |
---|
[1712] | 245 | ); |
---|
| 246 | //@formatter:on |
---|
[1671] | 247 | } |
---|
| 248 | |
---|
[1688] | 249 | private void writeJacaretoEvents(BufferedWriter writer, Collection<List<Event>> sequences) |
---|
[1671] | 250 | throws IOException |
---|
| 251 | { |
---|
[1689] | 252 | structure = new StructureNode("RootElement"); |
---|
[1671] | 253 | // reference the elements that we included in the header |
---|
[1689] | 254 | structure.addRecordable(); // Calendar |
---|
| 255 | structure.addRecordable(); // SystemInfo |
---|
| 256 | structure.addRecordable(); // KeyboardState |
---|
| 257 | structure.addRecordable(); // ComponentMode |
---|
| 258 | structure.addRecordable(); // ApplicationStarter |
---|
[1671] | 259 | |
---|
| 260 | for (List<Event> sequence : sequences) { |
---|
| 261 | for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) { |
---|
| 262 | Event event = eventIter.next(); |
---|
| 263 | |
---|
[1685] | 264 | if (event.getType() instanceof MouseButtonDown) { |
---|
[1808] | 265 | commitFocusEvent(); |
---|
[1701] | 266 | lastKeySequenceEvent = null; |
---|
| 267 | |
---|
[1689] | 268 | lastMouseClickEvent = new StructureNode("MouseClick"); |
---|
[1809] | 269 | lastMouseDownTarget = event.getTarget(); |
---|
[1825] | 270 | writeMouseClickEvent(writer, event, (JFCGUIElement) event.getTarget(), |
---|
| 271 | EVENT_DURATION, 501); |
---|
[1684] | 272 | } |
---|
[1685] | 273 | else if (event.getType() instanceof MouseButtonUp) { |
---|
[1701] | 274 | lastKeySequenceEvent = null; |
---|
| 275 | |
---|
[1825] | 276 | writeMouseClickEvent(writer, event, (JFCGUIElement) event.getTarget(), |
---|
| 277 | EVENT_DURATION, 502); |
---|
[1685] | 278 | } |
---|
[1808] | 279 | else if (event.getType() instanceof MouseDoubleClick) { |
---|
| 280 | StructureNode multiClick = structure.add("MultipleMouseClick"); |
---|
| 281 | |
---|
| 282 | // first click |
---|
| 283 | lastMouseClickEvent = multiClick.add("MouseClick"); |
---|
[1825] | 284 | writeMouseClickEvent(writer, event, (JFCGUIElement) event.getTarget(), |
---|
| 285 | DOUBLE_CLICK_DURATION, 501); |
---|
| 286 | writeMouseClickEvent(writer, event, (JFCGUIElement) event.getTarget(), |
---|
| 287 | DOUBLE_CLICK_DURATION, 502); |
---|
| 288 | writeMouseClickEvent(writer, event, (JFCGUIElement) event.getTarget(), |
---|
| 289 | DOUBLE_CLICK_DURATION, 500); |
---|
[1808] | 290 | // second click |
---|
| 291 | lastMouseClickEvent = multiClick.add("MouseClick"); |
---|
[1825] | 292 | writeMouseClickEvent(writer, event, (JFCGUIElement) event.getTarget(), |
---|
| 293 | DOUBLE_CLICK_DURATION, 501); |
---|
| 294 | writeMouseClickEvent(writer, event, (JFCGUIElement) event.getTarget(), |
---|
| 295 | DOUBLE_CLICK_DURATION, 502); |
---|
| 296 | writeMouseClickEvent(writer, event, (JFCGUIElement) event.getTarget(), |
---|
| 297 | DOUBLE_CLICK_DURATION, 500); |
---|
[1808] | 298 | |
---|
| 299 | lastMouseClickEvent = null; |
---|
| 300 | } |
---|
[1685] | 301 | else if (event.getType() instanceof MouseClick) { |
---|
[1825] | 302 | // if a menu file was provided, use the improved event |
---|
| 303 | // generation |
---|
[1834] | 304 | // TODO: check how much of this is still necessary with |
---|
| 305 | // the new menu index system |
---|
[1825] | 306 | if (menuList != null) { |
---|
| 307 | if (event.getTarget() instanceof JFCMenuButton) { |
---|
| 308 | if (menuElements.isEmpty()) { |
---|
| 309 | // parse the menu structure |
---|
| 310 | GUIModel model = ((IGUIElement) event.getTarget()).getGUIModel(); |
---|
| 311 | getMenuElements(model.getRootElements(), model); |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | Stack<JFCGUIElement> hierarchy = |
---|
| 315 | findMenuItemHierarchy((JFCGUIElement) event.getTarget()); |
---|
| 316 | |
---|
| 317 | while (!hierarchy.empty()) { |
---|
| 318 | generateFullClick(writer, event, hierarchy.pop()); |
---|
| 319 | } |
---|
| 320 | continue; |
---|
| 321 | } |
---|
| 322 | } |
---|
| 323 | |
---|
[1809] | 324 | lastKeySequenceEvent = null; |
---|
| 325 | |
---|
[1808] | 326 | if (lastMouseClickEvent != null) { |
---|
[1809] | 327 | if (lastMouseDownTarget == event.getTarget()) { |
---|
| 328 | // this is the standard case: |
---|
| 329 | // mouseDown, mouseUp and mouseClick sequence |
---|
| 330 | // was triggered on this target |
---|
[1701] | 331 | |
---|
[1825] | 332 | writeMouseClickEvent(writer, event, (JFCGUIElement) event.getTarget(), |
---|
| 333 | EVENT_DURATION, 500); |
---|
[1809] | 334 | writeItemActionEvent(writer, event); |
---|
| 335 | |
---|
| 336 | if (lastFocusChangeEvent == null) { |
---|
| 337 | // write structure sequentially |
---|
| 338 | structure.children.add(lastMouseClickEvent); |
---|
| 339 | structure.children.add(lastItemActionEvent); |
---|
| 340 | } |
---|
| 341 | else { |
---|
| 342 | // with nested structure |
---|
| 343 | structure.children.add(lastItemActionEvent); |
---|
| 344 | lastItemActionEvent.children.add(0, lastFocusChangeEvent); |
---|
| 345 | lastFocusChangeEvent.children.add(0, lastMouseClickEvent); |
---|
| 346 | |
---|
| 347 | lastFocusChangeEvent = null; |
---|
| 348 | lastMouseClickEvent = null; |
---|
| 349 | } |
---|
[1808] | 350 | } |
---|
| 351 | else { |
---|
[1809] | 352 | // target of mouseDown and mouseClick are different |
---|
| 353 | // -> this is, for example, a click on a menu item |
---|
| 354 | // within a condensed sequence |
---|
| 355 | commitFocusEvent(); |
---|
[1688] | 356 | |
---|
[1809] | 357 | // finish the last click on the old target |
---|
[1825] | 358 | writeMouseClickEvent(writer, event, (JFCGUIElement) event.getTarget(), |
---|
| 359 | EVENT_DURATION, 500); |
---|
[1809] | 360 | structure.children.add(lastMouseClickEvent); |
---|
| 361 | |
---|
| 362 | // and generate a new one |
---|
[1825] | 363 | generateFullClick(writer, event, (JFCGUIElement) event.getTarget()); |
---|
[1808] | 364 | } |
---|
[1688] | 365 | } |
---|
[1809] | 366 | else { |
---|
| 367 | // a target was clicked repeatedly: |
---|
| 368 | // the condensed sequence contains no mouseDowns or |
---|
| 369 | // mouseUps anymore |
---|
| 370 | // -> just generate another full click |
---|
[1825] | 371 | generateFullClick(writer, event, (JFCGUIElement) event.getTarget()); |
---|
[1809] | 372 | } |
---|
[1685] | 373 | } |
---|
[1686] | 374 | else if (event.getType() instanceof KeyboardFocusChange) { |
---|
[1701] | 375 | lastKeySequenceEvent = null; |
---|
| 376 | |
---|
[1688] | 377 | writeFocusChangeEvent(writer, event); |
---|
[1686] | 378 | } |
---|
[1691] | 379 | else if (event.getType() instanceof KeyPressed) { |
---|
[1808] | 380 | commitFocusEvent(); |
---|
[1714] | 381 | |
---|
[1701] | 382 | if (lastKeySequenceEvent == null) { |
---|
| 383 | lastKeySequenceEvent = structure.add("KeySequence"); |
---|
| 384 | } |
---|
| 385 | lastKeyTypedEvent = lastKeySequenceEvent.add("KeyTyped"); |
---|
| 386 | writeKeyEvent(writer, event, 401); |
---|
[1691] | 387 | } |
---|
| 388 | else if (event.getType() instanceof KeyReleased) { |
---|
[1808] | 389 | commitFocusEvent(); |
---|
[1714] | 390 | |
---|
[1701] | 391 | writeKeyEvent(writer, event, 402); |
---|
[1691] | 392 | } |
---|
[1671] | 393 | } |
---|
| 394 | } |
---|
| 395 | } |
---|
| 396 | |
---|
[1825] | 397 | private void getMenuElements(List<IGUIElement> elements, GUIModel model) { |
---|
| 398 | for (IGUIElement child : elements) { |
---|
| 399 | if (child instanceof JFCMenuButton || child instanceof JFCMenu) { |
---|
| 400 | menuElements.put(((JFCGUIElement) child).getName().replaceAll("^\"|\"$", ""), |
---|
| 401 | (JFCGUIElement) child); |
---|
| 402 | } |
---|
| 403 | getMenuElements(model.getChildren(child), model); |
---|
| 404 | } |
---|
| 405 | } |
---|
| 406 | |
---|
| 407 | private Stack<JFCGUIElement> findMenuItemHierarchy(JFCGUIElement item) { |
---|
| 408 | Stack<JFCGUIElement> elements = new Stack<>(); |
---|
| 409 | |
---|
| 410 | // find line that contains this menu item name |
---|
| 411 | int lineOfItem = -1; |
---|
| 412 | for (int i = 0; i < menuList.size(); i++) { |
---|
| 413 | String name = "\"" + menuList.get(i).trim().toLowerCase() + "\""; |
---|
| 414 | if (name.equals(item.getName().trim().toLowerCase())) { |
---|
| 415 | lineOfItem = i; |
---|
| 416 | } |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | // now go backwards until the toplevel menu is found |
---|
| 420 | int oldIndent = Integer.MAX_VALUE; |
---|
| 421 | for (int j = lineOfItem; j >= 0; j--) { |
---|
| 422 | String stripped = menuList.get(j).replaceFirst("^ *", ""); |
---|
| 423 | int indent = menuList.get(j).length() - stripped.length(); |
---|
| 424 | |
---|
| 425 | if (indent < oldIndent) { |
---|
| 426 | // this is a parent submenu |
---|
[1834] | 427 | elements.push(menuElements.get(stripped)); |
---|
[1825] | 428 | oldIndent = indent; |
---|
| 429 | } |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | return elements; |
---|
| 433 | } |
---|
| 434 | |
---|
[1808] | 435 | private void commitFocusEvent() { |
---|
| 436 | if (lastFocusChangeEvent != null) { |
---|
| 437 | structure.children.add(lastFocusChangeEvent); |
---|
| 438 | lastFocusChangeEvent = null; |
---|
| 439 | } |
---|
| 440 | } |
---|
| 441 | |
---|
[1825] | 442 | private void generateFullClick(BufferedWriter writer, Event event, JFCGUIElement target) |
---|
| 443 | throws IOException |
---|
| 444 | { |
---|
[1809] | 445 | lastMouseClickEvent = new StructureNode("MouseClick"); |
---|
| 446 | lastMouseDownTarget = event.getTarget(); |
---|
| 447 | |
---|
[1825] | 448 | writeMouseClickEvent(writer, event, target, EVENT_DURATION, 501); |
---|
| 449 | writeMouseClickEvent(writer, event, target, EVENT_DURATION, 502); |
---|
| 450 | writeMouseClickEvent(writer, event, target, EVENT_DURATION, 500); |
---|
[1809] | 451 | writeItemActionEvent(writer, event); |
---|
| 452 | |
---|
| 453 | structure.children.add(lastMouseClickEvent); |
---|
| 454 | structure.children.add(lastItemActionEvent); |
---|
| 455 | |
---|
| 456 | lastMouseDownTarget = null; |
---|
[1714] | 457 | } |
---|
| 458 | |
---|
[1688] | 459 | private void writeJacaretoTail(BufferedWriter writer) throws IOException { |
---|
[1671] | 460 | writeLine(writer, "</Record>"); |
---|
| 461 | |
---|
| 462 | // write the recording's structure |
---|
| 463 | writeLine(writer, "<Structure>"); |
---|
[1688] | 464 | writer.write(structure.toString()); |
---|
[1678] | 465 | // close root element |
---|
[1671] | 466 | writeLine(writer, "</Structure>"); |
---|
| 467 | } |
---|
| 468 | |
---|
[1712] | 469 | private void writeJacaretoXML(Collection<List<Event>> sequences, |
---|
| 470 | String filename, |
---|
| 471 | String classpath, |
---|
| 472 | String basepath, |
---|
| 473 | String classpathext) |
---|
| 474 | { |
---|
[1671] | 475 | BufferedWriter writer = new BufferedWriter(openReplayFile(filename + ".xml")); |
---|
| 476 | |
---|
| 477 | try { |
---|
[1825] | 478 | writeJacaretoHead(writer, classpath, basepath, classpathext); |
---|
[1688] | 479 | writeJacaretoEvents(writer, sequences); |
---|
| 480 | writeJacaretoTail(writer); |
---|
[1678] | 481 | writeLine(writer, "</JacaretoStructure>"); |
---|
[1671] | 482 | |
---|
| 483 | writer.flush(); |
---|
| 484 | writer.close(); |
---|
| 485 | } |
---|
| 486 | catch (IOException e) { |
---|
| 487 | Console.printerrln("Unable to write Jacareto replay file " + filename); |
---|
| 488 | } |
---|
| 489 | } |
---|
| 490 | |
---|
[1673] | 491 | /** |
---|
| 492 | * <p> |
---|
| 493 | * Helper function that opens the replay file for writing. |
---|
| 494 | * </p> |
---|
| 495 | * |
---|
| 496 | * @param filename |
---|
| 497 | * name and path of the replay file |
---|
| 498 | * @param encoding |
---|
| 499 | * file encoding, empty string for platform default |
---|
| 500 | * @return {@link OutputStreamWriter} that writes to the replay file |
---|
| 501 | */ |
---|
| 502 | private OutputStreamWriter openReplayFile(String filename) { |
---|
| 503 | File file = new File(filename); |
---|
| 504 | boolean fileCreated; |
---|
| 505 | try { |
---|
| 506 | fileCreated = file.createNewFile(); |
---|
| 507 | if (!fileCreated) { |
---|
| 508 | Console.traceln(Level.INFO, "Created logfile " + filename); |
---|
| 509 | } |
---|
| 510 | else { |
---|
| 511 | Console.traceln(Level.INFO, "Overwrote existing logfile " + filename); |
---|
| 512 | } |
---|
| 513 | } |
---|
| 514 | catch (IOException e) { |
---|
| 515 | Console.printerrln("Unable to create file " + filename); |
---|
| 516 | Console.logException(e); |
---|
| 517 | } |
---|
| 518 | OutputStreamWriter writer = null; |
---|
| 519 | try { |
---|
| 520 | writer = new OutputStreamWriter(new FileOutputStream(file)); |
---|
| 521 | } |
---|
| 522 | catch (IOException e) { |
---|
| 523 | Console.printerrln("Unable to open file for writing (read-only file):" + filename); |
---|
| 524 | Console.logException(e); |
---|
| 525 | } |
---|
| 526 | return writer; |
---|
| 527 | } |
---|
[1683] | 528 | |
---|
[1688] | 529 | private void writeItemActionEvent(BufferedWriter writer, Event event) throws IOException { |
---|
[1685] | 530 | JFCGUIElement target = (JFCGUIElement) event.getTarget(); |
---|
| 531 | MouseButtonInteraction info = (MouseButtonInteraction) event.getType(); |
---|
| 532 | |
---|
[1684] | 533 | //@formatter:off |
---|
| 534 | writeLine(writer, |
---|
| 535 | "<ItemEvent " |
---|
| 536 | + "procTime=\"0\" " |
---|
| 537 | + "duration=\"0\" " |
---|
| 538 | + "source=\"" + target.getJacaretoHierarchy() + "\" " |
---|
| 539 | + "class=\"" + target.getSpecification().getType() + "\" " |
---|
| 540 | + "uuid=\"" + UUID.randomUUID() + "\" " |
---|
| 541 | + "ID=\"701\" " |
---|
| 542 | + "item=\"\" " |
---|
| 543 | + "stateChange=\"1\" />" |
---|
| 544 | ); |
---|
| 545 | writeLine(writer, |
---|
| 546 | "<ActionEvent " |
---|
| 547 | + "procTime=\"0\" " |
---|
| 548 | + "duration=\"0\" " |
---|
| 549 | + "source=\"" + target.getJacaretoHierarchy() + "\" " |
---|
| 550 | + "class=\"" + target.getSpecification().getType() + "\" " |
---|
| 551 | + "uuid=\"" + UUID.randomUUID() + "\" " |
---|
| 552 | + "ID=\"1001\" " |
---|
| 553 | + "command=" + target.getName() + " " |
---|
[1685] | 554 | + "modifiers=\"" + getButtonModifier(info) + "\" />" |
---|
[1684] | 555 | ); |
---|
| 556 | //@formatter:on |
---|
[1689] | 557 | lastItemActionEvent = new StructureNode("ItemStateChange"); |
---|
| 558 | lastItemActionEvent.addRecordable(); |
---|
| 559 | lastItemActionEvent.addRecordable(); |
---|
[1684] | 560 | } |
---|
| 561 | |
---|
[1688] | 562 | private void writeFocusChangeEvent(BufferedWriter writer, Event event) throws IOException { |
---|
[1686] | 563 | KeyboardFocusChange info = (KeyboardFocusChange) event.getType(); |
---|
| 564 | JFCGUIElement target = (JFCGUIElement) event.getTarget(); |
---|
| 565 | |
---|
| 566 | if (currentFocus != null) { |
---|
[1689] | 567 | lastFocusChangeEvent = new StructureNode("FocusChange"); |
---|
[1688] | 568 | |
---|
[1686] | 569 | // focus lost on old target |
---|
[1688] | 570 | writeFocusEvent(writer, info, currentFocus, 1005); |
---|
[1686] | 571 | // focus gained on new target |
---|
[1688] | 572 | writeFocusEvent(writer, info, target, 1004); |
---|
[1686] | 573 | } |
---|
| 574 | else { |
---|
| 575 | // TODO: it seems like Jacareto wants a window activation before |
---|
| 576 | // the first focus event but that is not the case in autoquest, |
---|
| 577 | // skip for now |
---|
| 578 | } |
---|
| 579 | |
---|
| 580 | currentFocus = target; |
---|
| 581 | } |
---|
| 582 | |
---|
| 583 | private void writeFocusEvent(BufferedWriter writer, |
---|
| 584 | KeyboardFocusChange info, |
---|
| 585 | JFCGUIElement target, |
---|
| 586 | int jacId) throws IOException |
---|
| 587 | { |
---|
| 588 | //@formatter:off |
---|
| 589 | writeLine(writer, |
---|
| 590 | "<FocusEvent " |
---|
| 591 | + "procTime=\"0\" " |
---|
| 592 | + "duration=\"0\" " |
---|
| 593 | + "source=\"" + target.getJacaretoHierarchy() + "\" " |
---|
| 594 | + "class=\"" + target.getSpecification().getType() + "\" " |
---|
| 595 | + "uuid=\"" + UUID.randomUUID() + "\" " |
---|
| 596 | + "ID=\"" + jacId + "\" " |
---|
| 597 | + "component=\"null\" " |
---|
| 598 | + "root=\"" + target.getJacaretoRoot() + "\" " |
---|
| 599 | + "xPos=\"0\" " |
---|
| 600 | + "yPos=\"0\" " |
---|
| 601 | + "width=\"0\" " |
---|
| 602 | + "height=\"0\" " |
---|
| 603 | + "isTemporary=\"false\" />" |
---|
| 604 | ); |
---|
| 605 | //@formatter:on |
---|
[1689] | 606 | lastFocusChangeEvent.addRecordable(); |
---|
[1686] | 607 | } |
---|
| 608 | |
---|
[1825] | 609 | private void writeMouseClickEvent(BufferedWriter writer, |
---|
| 610 | Event event, |
---|
| 611 | JFCGUIElement target, |
---|
| 612 | int duration, |
---|
| 613 | int jacId) throws IOException |
---|
[1683] | 614 | { |
---|
[1685] | 615 | MouseButtonInteraction info = (MouseButtonInteraction) event.getType(); |
---|
| 616 | int clickCount = event.getType() instanceof MouseDoubleClick ? 2 : 1; |
---|
| 617 | |
---|
[1683] | 618 | //@formatter:off |
---|
| 619 | writeLine(writer, |
---|
| 620 | "<MouseEvent " |
---|
| 621 | + "procTime=\"0\" " |
---|
[1808] | 622 | + "duration=\"" + duration + "\" " |
---|
[1683] | 623 | + "source=\"" + target.getJacaretoHierarchy() + "\" " |
---|
| 624 | + "class=\"" + target.getSpecification().getType() + "\" " |
---|
| 625 | + "uuid=\"" + UUID.randomUUID() + "\" " |
---|
| 626 | + "ID=\"" + jacId + "\" " |
---|
| 627 | + "component=\"null\" " |
---|
| 628 | + "root=\"" + target.getJacaretoRoot() + "\" " |
---|
| 629 | + "xPos=\"0\" " |
---|
| 630 | + "yPos=\"0\" " |
---|
| 631 | + "width=\"0\" " |
---|
| 632 | + "height=\"0\" " |
---|
[1685] | 633 | + "when=\"" + event.getTimestamp() + "\" " |
---|
[1683] | 634 | + "isConsumed=\"false\">" |
---|
| 635 | ); |
---|
| 636 | writeLine(writer, |
---|
| 637 | "<MouseInfo " |
---|
[1686] | 638 | + "xPosition=\"" + info.getX() + "\" " |
---|
| 639 | + "yPosition=\"" + info.getY() + "\" " |
---|
[1683] | 640 | + "rootX=\"0\" " |
---|
| 641 | + "rootY=\"0\" " |
---|
[1685] | 642 | + "clickCount=\"" + clickCount + "\" " |
---|
| 643 | + "modifiers=\"" + getButtonModifier(info) + "\" " |
---|
[1683] | 644 | + "isPopupTrigger=\"false\" />" |
---|
| 645 | ); |
---|
| 646 | writeLine(writer, "</MouseEvent>"); |
---|
| 647 | //@formatter:on |
---|
| 648 | |
---|
[1689] | 649 | lastMouseClickEvent.addRecordable(); |
---|
[1683] | 650 | } |
---|
[1685] | 651 | |
---|
| 652 | private int getButtonModifier(MouseButtonInteraction info) { |
---|
| 653 | switch (info.getButton()) |
---|
| 654 | { |
---|
| 655 | case LEFT: |
---|
| 656 | return 16; |
---|
| 657 | case MIDDLE: |
---|
| 658 | return 8; |
---|
| 659 | case RIGHT: |
---|
| 660 | return 4; |
---|
| 661 | default: |
---|
| 662 | // TODO: handle unknown mouse button |
---|
| 663 | return -1; |
---|
| 664 | } |
---|
| 665 | } |
---|
[1691] | 666 | |
---|
| 667 | private void writeKeyEvent(BufferedWriter writer, Event event, int jacId) throws IOException { |
---|
| 668 | KeyInteraction info = (KeyInteraction) event.getType(); |
---|
| 669 | JFCGUIElement target = (JFCGUIElement) event.getTarget(); |
---|
[1704] | 670 | int keyCode = info.getKey().getVirtualKeyCode(); |
---|
[1691] | 671 | |
---|
[1704] | 672 | applyKeyModifier(info.getKey(), jacId == 401); |
---|
[1712] | 673 | |
---|
[1691] | 674 | //@formatter:off |
---|
| 675 | writeLine(writer, |
---|
| 676 | "<KeyEvent " |
---|
| 677 | + "procTime=\"0\" " |
---|
[1719] | 678 | + "duration=\"" + EVENT_DURATION + "\" " |
---|
[1691] | 679 | + "source=\"" + target.getJacaretoHierarchy() + "\" " |
---|
| 680 | + "class=\"" + target.getSpecification().getType() + "\" " |
---|
| 681 | + "uuid=\"" + UUID.randomUUID() + "\" " |
---|
| 682 | + "ID=\"" + jacId + "\" " |
---|
| 683 | + "component=\"null\" " |
---|
| 684 | + "root=\"" + target.getJacaretoRoot() + "\" " |
---|
| 685 | + "xPos=\"0\" " |
---|
| 686 | + "yPos=\"0\" " |
---|
| 687 | + "width=\"0\" " |
---|
| 688 | + "height=\"0\" " |
---|
| 689 | + "when=\"" + event.getTimestamp() + "\" " |
---|
| 690 | + "isConsumed=\"false\">" |
---|
| 691 | ); |
---|
| 692 | writeLine(writer, |
---|
| 693 | "<KeyInfo " |
---|
[1704] | 694 | + "keyCode=\"" + keyCode + "\" " |
---|
| 695 | + "keyChar=\"" + getKeyChar(keyCode) + "\" " |
---|
| 696 | + "modifiers=\"" + currentKeyModifiers + "\" />" |
---|
[1691] | 697 | ); |
---|
| 698 | |
---|
| 699 | writeLine(writer, "</KeyEvent>"); |
---|
| 700 | |
---|
[1701] | 701 | lastKeyTypedEvent.addRecordable(); |
---|
[1691] | 702 | } |
---|
| 703 | |
---|
[1701] | 704 | private String getKeyChar (int keyCode) { |
---|
| 705 | if (keyCode >= 32 && keyCode < 127) { |
---|
| 706 | return String.valueOf((char)keyCode); |
---|
| 707 | } |
---|
[1712] | 708 | return "_NO_LEGAL_XML_CHAR"; |
---|
[1691] | 709 | } |
---|
[1704] | 710 | |
---|
| 711 | private void applyKeyModifier (VirtualKey key, boolean set) { |
---|
| 712 | Integer modifier = modifiers.get(key); |
---|
| 713 | if (modifier != null) { |
---|
| 714 | if (set) { |
---|
| 715 | currentKeyModifiers |= modifier; |
---|
| 716 | } |
---|
| 717 | else { |
---|
| 718 | currentKeyModifiers &= ~modifier; |
---|
| 719 | } |
---|
| 720 | } |
---|
| 721 | } |
---|
[1671] | 722 | } |
---|