[1047] | 1 | |
---|
| 2 | package de.ugoe.cs.autoquest.plugin.html.eventcore; |
---|
| 3 | |
---|
| 4 | import java.util.Map; |
---|
| 5 | import java.util.logging.Level; |
---|
| 6 | |
---|
| 7 | import de.ugoe.cs.autoquest.eventcore.IEventType; |
---|
| 8 | import de.ugoe.cs.autoquest.eventcore.gui.IInteraction; |
---|
| 9 | import de.ugoe.cs.autoquest.eventcore.gui.KeyboardFocusChange; |
---|
| 10 | import de.ugoe.cs.autoquest.eventcore.gui.MouseButtonInteraction; |
---|
| 11 | import de.ugoe.cs.autoquest.eventcore.gui.MouseClick; |
---|
| 12 | import de.ugoe.cs.autoquest.eventcore.gui.Scroll; |
---|
| 13 | import de.ugoe.cs.util.console.Console; |
---|
| 14 | |
---|
| 15 | public class HTMLEventTypeFactory { |
---|
| 16 | private static HTMLEventTypeFactory instance = new HTMLEventTypeFactory(); |
---|
| 17 | |
---|
| 18 | private HTMLEventTypeFactory() {} |
---|
| 19 | |
---|
| 20 | public static HTMLEventTypeFactory getInstance() { |
---|
| 21 | return instance; |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | public IEventType getEventType(String eventName, Map<String, String> eventParameters) { |
---|
| 25 | IInteraction result = null; |
---|
| 26 | |
---|
| 27 | if ("onscroll".equals(eventName)) { |
---|
| 28 | int[] coordinates = getCoordinateParameter(eventName, eventParameters); |
---|
| 29 | result = new Scroll(coordinates[0], coordinates[1]); |
---|
| 30 | } |
---|
| 31 | else if ("onclick".equals(eventName)) { |
---|
| 32 | int[] coordinates = getCoordinateParameter(eventName, eventParameters); |
---|
| 33 | result = |
---|
| 34 | new MouseClick(MouseButtonInteraction.Button.LEFT, coordinates[0], coordinates[1]); |
---|
| 35 | } |
---|
| 36 | else if ("onchange".equals(eventName)) { |
---|
| 37 | // TODO: Implement "onchange" event handling |
---|
| 38 | Console.traceln(Level.FINE, "Unhandled event of type \"" + eventName + "\""); |
---|
| 39 | } |
---|
| 40 | else if ("onfocus".equals(eventName)) { |
---|
| 41 | result = new KeyboardFocusChange(); |
---|
| 42 | } |
---|
| 43 | else if ("onunload".equals(eventName) || "onbeforeunload".equals(eventName) || |
---|
| 44 | "onpagehide".equals(eventName) || "onpageshow".equals(eventName)) |
---|
| 45 | { |
---|
| 46 | Console.traceln(Level.FINE, "Ignored event name \"" + eventName + "\""); |
---|
| 47 | } |
---|
| 48 | else { |
---|
| 49 | throw new IllegalArgumentException("unknown event name: \"" + eventName + "\""); |
---|
| 50 | } |
---|
| 51 | return result; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | /** |
---|
| 55 | * <p> |
---|
| 56 | * TODO: comment |
---|
| 57 | * </p> |
---|
| 58 | * |
---|
| 59 | * @param eventName |
---|
| 60 | * @param eventParameters |
---|
| 61 | * @return |
---|
| 62 | */ |
---|
| 63 | private int[] getCoordinateParameter(String eventName, Map<String, String> eventParameters) { |
---|
| 64 | String xCoord = eventParameters.get("X"); |
---|
| 65 | if (xCoord == null) { |
---|
| 66 | throw new IllegalArgumentException("eventParameters do not contain X coordinate."); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | String yCoord = eventParameters.get("Y"); |
---|
| 70 | if (yCoord == null) { |
---|
| 71 | throw new IllegalArgumentException("eventParameters do not contain Y coordinate."); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | try { |
---|
| 75 | return new int[] |
---|
| 76 | { Integer.parseInt(xCoord), Integer.parseInt(yCoord) }; |
---|
| 77 | } |
---|
| 78 | catch (NumberFormatException e) { |
---|
| 79 | throw new IllegalArgumentException("the coordinates provided" + " of an " + eventName + |
---|
| 80 | " event are no numbers"); |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | } |
---|