[1049] | 1 |
|
---|
[963] | 2 | package de.ugoe.cs.autoquest.plugin.jfc;
|
---|
| 3 |
|
---|
| 4 | import java.awt.event.MouseEvent;
|
---|
| 5 | import java.io.File;
|
---|
| 6 | import java.io.FileInputStream;
|
---|
| 7 | import java.io.FileNotFoundException;
|
---|
| 8 | import java.io.IOException;
|
---|
| 9 | import java.io.InputStreamReader;
|
---|
| 10 | import java.io.UnsupportedEncodingException;
|
---|
| 11 | import java.util.ArrayList;
|
---|
| 12 | import java.util.Collection;
|
---|
| 13 | import java.util.HashMap;
|
---|
| 14 | import java.util.LinkedList;
|
---|
| 15 | import java.util.List;
|
---|
| 16 | import java.util.Map;
|
---|
| 17 | import java.util.logging.Level;
|
---|
| 18 |
|
---|
| 19 | import javax.xml.parsers.ParserConfigurationException;
|
---|
| 20 | import javax.xml.parsers.SAXParser;
|
---|
| 21 | import javax.xml.parsers.SAXParserFactory;
|
---|
| 22 |
|
---|
| 23 | import org.xml.sax.Attributes;
|
---|
| 24 | import org.xml.sax.InputSource;
|
---|
| 25 | import org.xml.sax.SAXException;
|
---|
| 26 | import org.xml.sax.SAXParseException;
|
---|
| 27 | import org.xml.sax.helpers.DefaultHandler;
|
---|
| 28 |
|
---|
| 29 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
| 30 | import de.ugoe.cs.autoquest.eventcore.gui.IInteraction;
|
---|
| 31 | import de.ugoe.cs.autoquest.eventcore.gui.KeyPressed;
|
---|
| 32 | import de.ugoe.cs.autoquest.eventcore.gui.KeyReleased;
|
---|
| 33 | import de.ugoe.cs.autoquest.eventcore.gui.KeyboardFocusChange;
|
---|
| 34 | import de.ugoe.cs.autoquest.eventcore.gui.MouseButtonDown;
|
---|
| 35 | import de.ugoe.cs.autoquest.eventcore.gui.MouseButtonInteraction;
|
---|
| 36 | import de.ugoe.cs.autoquest.eventcore.gui.MouseButtonUp;
|
---|
| 37 | import de.ugoe.cs.autoquest.eventcore.gui.MouseClick;
|
---|
[1003] | 38 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIElementTree;
|
---|
[963] | 39 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel;
|
---|
[1084] | 40 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModelException;
|
---|
[963] | 41 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
|
---|
| 42 | import de.ugoe.cs.autoquest.keyboardmaps.VirtualKey;
|
---|
| 43 | import de.ugoe.cs.autoquest.plugin.jfc.eventcore.JFCEventId;
|
---|
| 44 | import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCGUIElement;
|
---|
| 45 | import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCGUIElementSpec;
|
---|
| 46 | import de.ugoe.cs.util.console.Console;
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * <p>
|
---|
| 50 | * This class provides functionality to parse XML log files generated by the JFCMonitor of
|
---|
| 51 | * autoquest. The result of parsing a file is a collection of event sequences.
|
---|
| 52 | * </p>
|
---|
| 53 | *
|
---|
| 54 | * @author Fabian Glaser
|
---|
| 55 | * @author Steffen Herbold
|
---|
| 56 | * @version 1.0
|
---|
| 57 | */
|
---|
| 58 | public class JFCSimplifiedLogParser extends DefaultHandler {
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | * <p>
|
---|
[1049] | 62 | * Map that holds events that had no registered target GUI element during parsing. Keys are the
|
---|
| 63 | * IDs of the unregistered targets.
|
---|
| 64 | * </p>
|
---|
| 65 | */
|
---|
| 66 | private Map<Long, List<Event>> eventsWithoutTargets;
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
| 69 | * <p>
|
---|
[963] | 70 | * Collection of event sequences that is contained in the log file, which is parsed.
|
---|
| 71 | * </p>
|
---|
| 72 | */
|
---|
| 73 | private Collection<List<Event>> sequences;
|
---|
| 74 |
|
---|
| 75 | /**
|
---|
| 76 | * <p>
|
---|
| 77 | * Internal handle to the id of the event that is currently being parsed.
|
---|
| 78 | * </p>
|
---|
| 79 | */
|
---|
| 80 | private JFCEventId currentEventId;
|
---|
[1027] | 81 |
|
---|
[963] | 82 | /**
|
---|
| 83 | *
|
---|
| 84 | * <p>
|
---|
[1009] | 85 | * Internal handle to the hashcode of the GUI element, that is currently parsed.
|
---|
[963] | 86 | * </p>
|
---|
| 87 | */
|
---|
[1009] | 88 | private Long currentGUIElementHash;
|
---|
[1027] | 89 |
|
---|
[963] | 90 | /**
|
---|
| 91 | *
|
---|
| 92 | * <p>
|
---|
[1049] | 93 | * Internal handle to the hashcode of the parent of the GUI element, that is currently parsed.
|
---|
[963] | 94 | * </p>
|
---|
| 95 | */
|
---|
[1004] | 96 | private Long currentParentHash;
|
---|
[1027] | 97 |
|
---|
[963] | 98 | /**
|
---|
| 99 | * <p>
|
---|
[1001] | 100 | * Internal handle to the source of the event that is currently being parsed.
|
---|
[963] | 101 | * </p>
|
---|
| 102 | */
|
---|
[1004] | 103 | private Long currentEventSource;
|
---|
[1027] | 104 |
|
---|
[1001] | 105 | /**
|
---|
| 106 | * <p>
|
---|
| 107 | * Internal handle to the timestamp of the event that is currently being parsed.
|
---|
| 108 | */
|
---|
| 109 | private Long currentEventTimestamp = -1l;
|
---|
[1027] | 110 |
|
---|
[963] | 111 | /**
|
---|
| 112 | * <p>
|
---|
| 113 | * Internal handle to the parameters of the event currently being parsed.
|
---|
| 114 | * </p>
|
---|
| 115 | */
|
---|
| 116 | private Map<String, String> currentEventParameters;
|
---|
| 117 |
|
---|
| 118 | /**
|
---|
| 119 | * <p>
|
---|
| 120 | * Internal handle to the event sequence that is currently being parsed.
|
---|
| 121 | * </p>
|
---|
| 122 | */
|
---|
| 123 | private List<Event> currentSequence;
|
---|
[1027] | 124 |
|
---|
[963] | 125 | /**
|
---|
| 126 | * <p>
|
---|
[1009] | 127 | * internal handle to the specification currently parsed for a GUI element
|
---|
[963] | 128 | * </p>
|
---|
| 129 | */
|
---|
| 130 | private JFCGUIElementSpec currentGuiElementSpec;
|
---|
| 131 |
|
---|
| 132 | /**
|
---|
| 133 | * <p>
|
---|
[1009] | 134 | * internal handle to the parsed GUI structure, stored in a GUIElementTree
|
---|
[963] | 135 | * </p>
|
---|
| 136 | */
|
---|
[1027] | 137 | private GUIElementTree<Long> currentGUIElementTree;
|
---|
[963] | 138 |
|
---|
| 139 | /**
|
---|
| 140 | * <p>
|
---|
[1049] | 141 | * internal handle to the class ancestors
|
---|
[963] | 142 | * </p>
|
---|
| 143 | */
|
---|
[990] | 144 | private List<String> currentTypeHierarchy;
|
---|
[963] | 145 |
|
---|
| 146 | /**
|
---|
| 147 | * <p>
|
---|
| 148 | * this is used to check, if for every pressed key, there is a release of it
|
---|
| 149 | * </p>
|
---|
| 150 | */
|
---|
| 151 | private List<VirtualKey> mPressedKeys = new ArrayList<VirtualKey>();
|
---|
[1027] | 152 |
|
---|
[963] | 153 | /**
|
---|
| 154 | * <p>
|
---|
| 155 | * a specification for event ids to be omitted by the parser
|
---|
| 156 | * </p>
|
---|
| 157 | */
|
---|
| 158 | private Collection<JFCEventId> eventFilter;
|
---|
| 159 |
|
---|
| 160 | /**
|
---|
| 161 | * <p>
|
---|
| 162 | * Constructor. Creates a new JFCLogParser with a default event filter. This ignores focus
|
---|
| 163 | * events, mouse pressed, and mouse released events.
|
---|
| 164 | * </p>
|
---|
| 165 | */
|
---|
| 166 | public JFCSimplifiedLogParser() {
|
---|
| 167 | sequences = new LinkedList<List<Event>>();
|
---|
| 168 | currentSequence = null;
|
---|
[1049] | 169 | eventsWithoutTargets = new HashMap<Long, List<Event>>();
|
---|
| 170 | // setupDefaultEventFilter();
|
---|
[963] | 171 | }
|
---|
| 172 |
|
---|
| 173 | /**
|
---|
| 174 | * <p>
|
---|
| 175 | * Constructor. Creates a new JFCLogParser with a specific event filter. The events in the
|
---|
| 176 | * provided collection are ignored by the parser. As events, the constants of the different
|
---|
| 177 | * event classes must be used. E.g. creating a collection and putting
|
---|
| 178 | * <code>MouseEvent.MOUSE_PRESSED</code> will cause the parser to ignore all mouse pressed
|
---|
| 179 | * events. If the provided collection is null, no event is ignored.
|
---|
| 180 | * </p>
|
---|
| 181 | *
|
---|
| 182 | * @param ignoredEvents
|
---|
| 183 | * the events to be ignored by the parser, can be null
|
---|
| 184 | */
|
---|
| 185 | public JFCSimplifiedLogParser(Collection<JFCEventId> ignoredEvents) {
|
---|
| 186 | sequences = new LinkedList<List<Event>>();
|
---|
| 187 | currentSequence = null;
|
---|
[1049] | 188 | eventsWithoutTargets = new HashMap<Long, List<Event>>();
|
---|
[963] | 189 | eventFilter = ignoredEvents;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | /**
|
---|
| 193 | * <p>
|
---|
| 194 | * Parses a log file written by the JFCMonitor and creates a collection of event sequences.
|
---|
| 195 | * </p>
|
---|
| 196 | *
|
---|
| 197 | * @param filename
|
---|
| 198 | * name and path of the log file
|
---|
| 199 | */
|
---|
| 200 | public void parseFile(String filename) {
|
---|
| 201 | if (filename == null) {
|
---|
| 202 | throw new IllegalArgumentException("filename must not be null");
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | parseFile(new File(filename));
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | /**
|
---|
| 209 | * <p>
|
---|
| 210 | * Parses a log file written by the JFCMonitor and creates a collection of event sequences.
|
---|
| 211 | * </p>
|
---|
| 212 | *
|
---|
| 213 | * @param file
|
---|
| 214 | * name and path of the log file
|
---|
| 215 | */
|
---|
| 216 | public void parseFile(File file) {
|
---|
| 217 | if (file == null) {
|
---|
| 218 | throw new IllegalArgumentException("file must not be null");
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | SAXParserFactory spf = SAXParserFactory.newInstance();
|
---|
| 222 | spf.setValidating(true);
|
---|
| 223 |
|
---|
| 224 | SAXParser saxParser = null;
|
---|
| 225 | InputSource inputSource = null;
|
---|
| 226 | try {
|
---|
| 227 | saxParser = spf.newSAXParser();
|
---|
| 228 | inputSource =
|
---|
[1049] | 229 | new InputSource(new InputStreamReader(new FileInputStream(file), "UTF-8"));
|
---|
[963] | 230 | }
|
---|
| 231 | catch (UnsupportedEncodingException e) {
|
---|
| 232 | Console.printerr("Error parsing file + " + file.getName());
|
---|
| 233 | Console.logException(e);
|
---|
| 234 | return;
|
---|
| 235 | }
|
---|
| 236 | catch (ParserConfigurationException e) {
|
---|
| 237 | Console.printerr("Error parsing file + " + file.getName());
|
---|
| 238 | Console.logException(e);
|
---|
| 239 | return;
|
---|
| 240 | }
|
---|
| 241 | catch (SAXException e) {
|
---|
| 242 | Console.printerr("Error parsing file + " + file.getName());
|
---|
| 243 | Console.logException(e);
|
---|
| 244 | return;
|
---|
| 245 | }
|
---|
| 246 | catch (FileNotFoundException e) {
|
---|
| 247 | Console.printerr("Error parsing file + " + file.getName());
|
---|
| 248 | Console.logException(e);
|
---|
| 249 | return;
|
---|
| 250 | }
|
---|
| 251 | if (inputSource != null) {
|
---|
| 252 | inputSource.setSystemId("file://" + file.getAbsolutePath());
|
---|
| 253 | try {
|
---|
| 254 | if (saxParser == null) {
|
---|
| 255 | throw new RuntimeException("SAXParser creation failed");
|
---|
| 256 | }
|
---|
| 257 | saxParser.parse(inputSource, this);
|
---|
| 258 | }
|
---|
| 259 | catch (SAXParseException e) {
|
---|
| 260 | Console.printerrln("Failure parsing file in line " + e.getLineNumber() +
|
---|
[1049] | 261 | ", column " + e.getColumnNumber() + ".");
|
---|
[963] | 262 | Console.logException(e);
|
---|
| 263 | return;
|
---|
| 264 | }
|
---|
| 265 | catch (SAXException e) {
|
---|
| 266 | Console.printerr("Error parsing file + " + file.getName());
|
---|
| 267 | Console.logException(e);
|
---|
| 268 | return;
|
---|
| 269 | }
|
---|
| 270 | catch (IOException e) {
|
---|
| 271 | Console.printerr("Error parsing file + " + file.getName());
|
---|
| 272 | Console.logException(e);
|
---|
| 273 | return;
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
[1049] | 276 | if (!eventsWithoutTargets.isEmpty()) {
|
---|
[1058] | 277 | Console.printerr("Some events reference GUI elements that are not part of logfile. " +
|
---|
| 278 | "These events have been parsed without target.");
|
---|
[1049] | 279 | }
|
---|
[963] | 280 | }
|
---|
| 281 |
|
---|
| 282 | /**
|
---|
| 283 | * <p>
|
---|
| 284 | * Returns the collection of event sequences that is obtained from parsing log files.
|
---|
| 285 | * </p>
|
---|
| 286 | *
|
---|
| 287 | * @return collection of event sequences
|
---|
| 288 | */
|
---|
| 289 | public Collection<List<Event>> getSequences() {
|
---|
| 290 | return sequences;
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | /**
|
---|
| 294 | * <p>
|
---|
[1009] | 295 | * Returns the GUI model that is obtained from parsing log files.
|
---|
[963] | 296 | * </p>
|
---|
| 297 | *
|
---|
| 298 | * @return GUIModel
|
---|
| 299 | */
|
---|
| 300 | public GUIModel getGuiModel() {
|
---|
[1003] | 301 | return currentGUIElementTree.getGUIModel();
|
---|
[963] | 302 | }
|
---|
| 303 |
|
---|
| 304 | /*
|
---|
| 305 | * (non-Javadoc)
|
---|
| 306 | *
|
---|
| 307 | * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String,
|
---|
| 308 | * java.lang.String, org.xml.sax.Attributes)
|
---|
| 309 | */
|
---|
| 310 | public void startElement(String uri, String localName, String qName, Attributes atts)
|
---|
[1049] | 311 | throws SAXException
|
---|
| 312 | {
|
---|
[963] | 313 | if (qName.equals("sessions")) {
|
---|
| 314 | currentSequence = new LinkedList<Event>();
|
---|
[1003] | 315 | if (currentGUIElementTree == null)
|
---|
[1027] | 316 | currentGUIElementTree = new GUIElementTree<Long>();
|
---|
[963] | 317 | }
|
---|
| 318 | if (qName.equals("newsession")) {
|
---|
| 319 | Console.traceln(Level.FINE, "start of session");
|
---|
| 320 | if (currentSequence != null && !currentSequence.isEmpty()) {
|
---|
| 321 | // create a copy of the list just to have a correctly typed one.
|
---|
| 322 | sequences.add(currentSequence.subList(0, currentSequence.size() - 1));
|
---|
| 323 | }
|
---|
| 324 | currentSequence = new LinkedList<Event>();
|
---|
| 325 | }
|
---|
| 326 | else if (qName.equals("component")) {
|
---|
[1027] | 327 | currentGUIElementHash = Long.parseLong(atts.getValue("hash"), 16);
|
---|
| 328 | currentGuiElementSpec = new JFCGUIElementSpec();
|
---|
| 329 | currentGuiElementSpec.setElementHash((int) currentGUIElementHash.longValue());
|
---|
[963] | 330 | }
|
---|
| 331 | else if (qName.equals("event")) {
|
---|
| 332 | JFCEventId eventId = JFCEventId.parseEventId(atts.getValue("id"));
|
---|
| 333 | if ((eventFilter == null) || (!eventFilter.contains(eventId))) {
|
---|
| 334 | currentEventId = eventId;
|
---|
| 335 | currentEventParameters = new HashMap<String, String>();
|
---|
| 336 | }
|
---|
| 337 | }
|
---|
| 338 | else if (qName.equals("componentNameChange")) {
|
---|
[1027] | 339 | long sourceHash = Long.parseLong(atts.getValue("hash"), 16);
|
---|
| 340 | String newName = atts.getValue("newName");
|
---|
| 341 | // int titleSource = Integer.parseInt(atts.getValue("titleSource"));
|
---|
| 342 | JFCGUIElement sourceElement = (JFCGUIElement) currentGUIElementTree.find(sourceHash);
|
---|
| 343 | JFCGUIElementSpec sourceSpec = (JFCGUIElementSpec) sourceElement.getSpecification();
|
---|
| 344 | sourceSpec.setName(newName);
|
---|
[963] | 345 | }
|
---|
| 346 | else if (qName.equals("param")) {
|
---|
[1049] | 347 | if (currentEventId != null) {
|
---|
| 348 | if ("source".equals(atts.getValue("name"))) {
|
---|
[1027] | 349 | currentEventSource = Long.parseLong(atts.getValue("value"), 16);
|
---|
| 350 | }
|
---|
[1049] | 351 | if ("timestamp".equals(atts.getValue("name"))) {
|
---|
[1027] | 352 | currentEventTimestamp = Long.parseLong(atts.getValue("value"));
|
---|
| 353 | }
|
---|
[963] | 354 | currentEventParameters.put(atts.getValue("name"), atts.getValue("value"));
|
---|
[1049] | 355 | }
|
---|
| 356 | else if (currentGUIElementHash != null) {
|
---|
[1027] | 357 | if ("title".equals(atts.getValue("name"))) {
|
---|
| 358 | currentGuiElementSpec.setName(atts.getValue("value"));
|
---|
| 359 | }
|
---|
| 360 | else if ("class".equals(atts.getValue("name"))) {
|
---|
| 361 | currentGuiElementSpec.setType(atts.getValue("value"));
|
---|
| 362 | }
|
---|
| 363 | else if ("icon".equals(atts.getValue("name"))) {
|
---|
| 364 | currentGuiElementSpec.setIcon(atts.getValue("value"));
|
---|
| 365 | }
|
---|
| 366 | else if ("index".equals(atts.getValue("name"))) {
|
---|
| 367 | currentGuiElementSpec.setIndex(Integer.parseInt(atts.getValue("value")));
|
---|
| 368 | }
|
---|
| 369 | else if ("parent".equals(atts.getValue("name"))) {
|
---|
| 370 | currentParentHash = Long.parseLong(atts.getValue("value"), 16);
|
---|
| 371 | }
|
---|
[963] | 372 | }
|
---|
| 373 | }
|
---|
[1049] | 374 | else if (qName.equals("ancestor")) {
|
---|
[1027] | 375 | currentTypeHierarchy.add(atts.getValue("name"));
|
---|
[963] | 376 | }
|
---|
[1049] | 377 | else if (qName.equals("ancestors")) {
|
---|
[1027] | 378 | currentTypeHierarchy = new LinkedList<String>();
|
---|
[963] | 379 | }
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | /*
|
---|
| 383 | * (non-Javadoc)
|
---|
| 384 | *
|
---|
| 385 | * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String,
|
---|
| 386 | * java.lang.String)
|
---|
| 387 | */
|
---|
| 388 | @Override
|
---|
| 389 | public void endElement(String uri, String localName, String qName) throws SAXException {
|
---|
| 390 | if (qName.equals("sessions")) {
|
---|
| 391 | if (currentSequence != null && !currentSequence.isEmpty()) {
|
---|
| 392 | sequences.add(currentSequence);
|
---|
| 393 | }
|
---|
| 394 | currentSequence = null;
|
---|
| 395 | }
|
---|
[1049] | 396 | else if (qName.equals("ancestors")) {
|
---|
[1027] | 397 | currentGuiElementSpec.setTypeHierarchy(currentTypeHierarchy);
|
---|
[984] | 398 | }
|
---|
[1009] | 399 | else if (qName.equals("component") && currentGUIElementHash != null) {
|
---|
[1084] | 400 | try {
|
---|
| 401 | currentGUIElementTree.add(currentGUIElementHash, currentParentHash,
|
---|
| 402 | currentGuiElementSpec);
|
---|
| 403 | }
|
---|
| 404 | catch (GUIModelException e) {
|
---|
| 405 | throw new SAXException("could not handle GUI element with hash " +
|
---|
| 406 | currentGUIElementHash + ": " + e.getMessage(), e);
|
---|
| 407 | }
|
---|
[1049] | 408 | List<Event> unhandledEvents = eventsWithoutTargets.get(currentGUIElementHash);
|
---|
| 409 | if (unhandledEvents != null) {
|
---|
| 410 | JFCGUIElement guiElement =
|
---|
| 411 | (JFCGUIElement) currentGUIElementTree.find(currentGUIElementHash);
|
---|
| 412 | for (Event event : unhandledEvents) {
|
---|
| 413 | event.setTarget(guiElement);
|
---|
[1051] | 414 | guiElement.markUsed();
|
---|
[1049] | 415 | }
|
---|
| 416 | eventsWithoutTargets.remove(currentGUIElementHash);
|
---|
| 417 | }
|
---|
[1027] | 418 | currentGUIElementHash = null;
|
---|
| 419 | currentParentHash = null;
|
---|
| 420 | currentTypeHierarchy = null;
|
---|
[963] | 421 | }
|
---|
| 422 | else if (currentEventId != null) {
|
---|
| 423 | if (qName.equals("event")) {
|
---|
[1027] | 424 |
|
---|
[963] | 425 | IGUIElement currentGUIElement;
|
---|
[1003] | 426 | currentGUIElement = currentGUIElementTree.find(currentEventSource);
|
---|
[1058] | 427 | Event event;
|
---|
[1049] | 428 | // in some rare cases the target GUI element of the event is not
|
---|
| 429 | // known yet
|
---|
| 430 | if (currentGUIElement == null) {
|
---|
[1058] | 431 | event = new Event(instantiateInteraction(currentEventId, currentEventParameters));
|
---|
[1049] | 432 | List<Event> eventList = eventsWithoutTargets.get(currentEventSource);
|
---|
| 433 | if (eventList == null) {
|
---|
| 434 | eventList = new ArrayList<Event>();
|
---|
| 435 | eventsWithoutTargets.put(currentEventSource, eventList);
|
---|
| 436 | }
|
---|
| 437 | eventList.add(event);
|
---|
| 438 | }
|
---|
| 439 | else {
|
---|
[1058] | 440 | event = new Event(instantiateInteraction(currentEventId, currentEventParameters),
|
---|
[1049] | 441 | currentGUIElement);
|
---|
| 442 | JFCGUIElement currentEventTarget = (JFCGUIElement) event.getTarget();
|
---|
[1051] | 443 | currentEventTarget.markUsed();
|
---|
[1049] | 444 | }
|
---|
[1058] | 445 | event.setTimestamp(currentEventTimestamp);
|
---|
| 446 | currentSequence.add(event);
|
---|
[1049] | 447 |
|
---|
[963] | 448 | currentEventParameters = null;
|
---|
| 449 | currentEventId = null;
|
---|
[1001] | 450 | currentEventTimestamp = -1l;
|
---|
[963] | 451 | }
|
---|
| 452 | }
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | /**
|
---|
| 456 | * <p>
|
---|
| 457 | * depending on the event id and the event parameters, this method instantiates the concrete
|
---|
| 458 | * interaction, that took place, i.e. the event type
|
---|
| 459 | * </p>
|
---|
[1049] | 460 | *
|
---|
[963] | 461 | * @param eventId
|
---|
| 462 | * the id of the event
|
---|
| 463 | * @param eventParameters
|
---|
| 464 | * the parameters provided for the event
|
---|
[1049] | 465 | *
|
---|
[963] | 466 | * @return as described
|
---|
| 467 | *
|
---|
[1049] | 468 | * @throws SAXException
|
---|
| 469 | * thrown if the provided event id is unknown
|
---|
[963] | 470 | */
|
---|
[1049] | 471 | private IInteraction instantiateInteraction(JFCEventId eventId,
|
---|
[963] | 472 | Map<String, String> eventParameters)
|
---|
[1049] | 473 | throws SAXException
|
---|
| 474 | {
|
---|
[963] | 475 | switch (eventId)
|
---|
| 476 | {
|
---|
| 477 | case FOCUS_GAINED:
|
---|
| 478 | return handleNewFocus(eventId, eventParameters);
|
---|
| 479 |
|
---|
| 480 | case KEY_PRESSED:
|
---|
| 481 | case KEY_RELEASED:
|
---|
| 482 | case KEY_TYPED:
|
---|
| 483 | return handleKeyAction(eventId, eventParameters);
|
---|
| 484 |
|
---|
| 485 | case MOUSE_CLICKED:
|
---|
| 486 | case MOUSE_PRESSED:
|
---|
| 487 | case MOUSE_RELEASED:
|
---|
| 488 | case MOUSE_MOVED:
|
---|
| 489 | case MOUSE_ENTERED:
|
---|
| 490 | case MOUSE_EXITED:
|
---|
| 491 | case MOUSE_DRAGGED:
|
---|
| 492 | case MOUSE_WHEEL:
|
---|
| 493 | return handleMouseAction(eventId, eventParameters);
|
---|
| 494 |
|
---|
| 495 | default:
|
---|
| 496 | throw new SAXException("unhandled event id " + eventId);
|
---|
| 497 | }
|
---|
[1049] | 498 | }
|
---|
[963] | 499 |
|
---|
| 500 | /**
|
---|
| 501 | * <p>
|
---|
| 502 | * handles a mouse interaction. The method determines based on the event id and the parameters
|
---|
| 503 | * which mouse button is pressed, released or clicked.
|
---|
| 504 | * </p>
|
---|
[1049] | 505 | *
|
---|
[963] | 506 | * @param eventId
|
---|
| 507 | * the id of the event
|
---|
| 508 | * @param eventParameters
|
---|
| 509 | * the parameters provided for the event
|
---|
[1049] | 510 | *
|
---|
[963] | 511 | * @return as described
|
---|
| 512 | *
|
---|
[1049] | 513 | * @throws SAXException
|
---|
| 514 | * thrown if the provided event id or button index is unknown
|
---|
[963] | 515 | */
|
---|
| 516 | private IInteraction handleMouseAction(JFCEventId eventId, Map<String, String> eventParameters)
|
---|
[1049] | 517 | throws SAXException
|
---|
| 518 | {
|
---|
[963] | 519 | MouseButtonInteraction.Button button = null;
|
---|
| 520 |
|
---|
[1049] | 521 | if (eventParameters.get("Button") != null) {
|
---|
[963] | 522 | int buttonId = Integer.parseInt(eventParameters.get("Button"));
|
---|
[1049] | 523 | if (buttonId == MouseEvent.BUTTON1) {
|
---|
[963] | 524 | button = MouseButtonInteraction.Button.LEFT;
|
---|
| 525 | }
|
---|
[1049] | 526 | else if (buttonId == MouseEvent.BUTTON2) {
|
---|
[963] | 527 | button = MouseButtonInteraction.Button.MIDDLE;
|
---|
| 528 | }
|
---|
[1049] | 529 | else if (buttonId == MouseEvent.BUTTON3) {
|
---|
[963] | 530 | button = MouseButtonInteraction.Button.RIGHT;
|
---|
| 531 | }
|
---|
[1049] | 532 | else {
|
---|
[963] | 533 | throw new SAXException("unknown mouse button index " + buttonId);
|
---|
| 534 | }
|
---|
| 535 | }
|
---|
| 536 |
|
---|
[1049] | 537 | if (JFCEventId.MOUSE_CLICKED == eventId) {
|
---|
[963] | 538 | int x = Integer.parseInt(eventParameters.get("X"));
|
---|
| 539 | int y = Integer.parseInt(eventParameters.get("Y"));
|
---|
| 540 | return new MouseClick(button, x, y);
|
---|
| 541 | }
|
---|
[1049] | 542 | else if (JFCEventId.MOUSE_PRESSED == eventId) {
|
---|
[963] | 543 | int x = Integer.parseInt(eventParameters.get("X"));
|
---|
| 544 | int y = Integer.parseInt(eventParameters.get("Y"));
|
---|
| 545 | return new MouseButtonDown(button, x, y);
|
---|
| 546 | }
|
---|
[1049] | 547 | else if (JFCEventId.MOUSE_RELEASED == eventId) {
|
---|
[963] | 548 | int x = Integer.parseInt(eventParameters.get("X"));
|
---|
| 549 | int y = Integer.parseInt(eventParameters.get("Y"));
|
---|
| 550 | return new MouseButtonUp(button, x, y);
|
---|
| 551 | }
|
---|
[1049] | 552 | else {
|
---|
[963] | 553 | throw new SAXException("unknown event id " + eventId);
|
---|
| 554 | }
|
---|
[1049] | 555 | }
|
---|
[963] | 556 |
|
---|
| 557 | /**
|
---|
| 558 | * <p>
|
---|
| 559 | * handles a keyboard interaction. The method determines based on the event id and the
|
---|
[1049] | 560 | * parameters which key on the keyboard is pressed or released. It further checks, if for every
|
---|
| 561 | * released key there is also a pressed event
|
---|
[963] | 562 | * </p>
|
---|
[1049] | 563 | *
|
---|
[963] | 564 | * @param eventId
|
---|
| 565 | * the id of the event
|
---|
| 566 | * @param eventParameters
|
---|
| 567 | * the parameters provided for the event
|
---|
[1049] | 568 | *
|
---|
[963] | 569 | * @return as described
|
---|
| 570 | *
|
---|
[1049] | 571 | * @throws SAXException
|
---|
| 572 | * thrown if the provided event id is unknown or if there is a key release without a
|
---|
| 573 | * preceding press of the same key
|
---|
[963] | 574 | */
|
---|
| 575 | private IInteraction handleKeyAction(JFCEventId eventId, Map<String, String> eventParameters)
|
---|
[1049] | 576 | throws SAXException
|
---|
| 577 | {
|
---|
[963] | 578 | // TODO handle shortcuts
|
---|
[1049] | 579 | if (JFCEventId.KEY_PRESSED == eventId) {
|
---|
[963] | 580 | VirtualKey key = VirtualKey.parseVirtualKey(eventParameters.get("KeyCode"));
|
---|
| 581 | mPressedKeys.add(key);
|
---|
| 582 |
|
---|
| 583 | return new KeyPressed(key);
|
---|
| 584 | }
|
---|
[1049] | 585 | else if (JFCEventId.KEY_RELEASED == eventId) {
|
---|
[963] | 586 | VirtualKey key = VirtualKey.parseVirtualKey(eventParameters.get("KeyCode"));
|
---|
[1049] | 587 | if (mPressedKeys.contains(key)) {
|
---|
[963] | 588 | mPressedKeys.remove(key);
|
---|
| 589 | }
|
---|
[1049] | 590 | else {
|
---|
| 591 | Console.traceln(Level.SEVERE,
|
---|
| 592 | "log file has an error, as it contains a key up event on key " +
|
---|
| 593 | key + " for which there is no preceeding key down event");
|
---|
[963] | 594 | }
|
---|
| 595 |
|
---|
| 596 | return new KeyReleased(key);
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | throw new SAXException("unknown event id " + eventId);
|
---|
[1049] | 600 | }
|
---|
[963] | 601 |
|
---|
| 602 | /**
|
---|
| 603 | * <p>
|
---|
| 604 | * handles explicit keyboard focus changes.
|
---|
| 605 | * </p>
|
---|
[1049] | 606 | *
|
---|
[963] | 607 | * @param eventId
|
---|
| 608 | * the id of the event
|
---|
| 609 | * @param eventParameters
|
---|
| 610 | * the parameters provided for the event
|
---|
[1049] | 611 | *
|
---|
[963] | 612 | * @return as described
|
---|
| 613 | *
|
---|
[1049] | 614 | * @throws SAXException
|
---|
| 615 | * thrown if the provided event id is unknown
|
---|
[963] | 616 | */
|
---|
| 617 | private IInteraction handleNewFocus(JFCEventId eventId, Map<String, String> eventParameters)
|
---|
[1049] | 618 | throws SAXException
|
---|
| 619 | {
|
---|
| 620 | if (JFCEventId.FOCUS_GAINED == eventId) {
|
---|
[963] | 621 | return new KeyboardFocusChange();
|
---|
| 622 | }
|
---|
[1049] | 623 | else {
|
---|
[963] | 624 | throw new SAXException("unknown event id " + eventId);
|
---|
| 625 | }
|
---|
[1049] | 626 | }
|
---|
[963] | 627 |
|
---|
| 628 | /**
|
---|
| 629 | * <p>
|
---|
| 630 | * creates a default event filter that ignores focus changes, mouse pressed and mouse released
|
---|
| 631 | * events.
|
---|
| 632 | * </p>
|
---|
| 633 | */
|
---|
[1049] | 634 | /*
|
---|
| 635 | * private void setupDefaultEventFilter() { eventFilter = new HashSet<JFCEventId>();
|
---|
| 636 | * eventFilter.add(JFCEventId.MOUSE_PRESSED); eventFilter.add(JFCEventId.MOUSE_RELEASED);
|
---|
| 637 | * eventFilter.add(JFCEventId.FOCUS_GAINED); }
|
---|
| 638 | */
|
---|
[963] | 639 | }
|
---|