[1054] | 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. |
---|
[1047] | 14 | |
---|
| 15 | package de.ugoe.cs.autoquest.plugin.html.eventcore; |
---|
| 16 | |
---|
| 17 | import java.util.Map; |
---|
| 18 | import java.util.logging.Level; |
---|
| 19 | |
---|
| 20 | import de.ugoe.cs.autoquest.eventcore.IEventType; |
---|
| 21 | import de.ugoe.cs.autoquest.eventcore.gui.IInteraction; |
---|
[1250] | 22 | import de.ugoe.cs.autoquest.eventcore.gui.KeyPressed; |
---|
[1047] | 23 | import de.ugoe.cs.autoquest.eventcore.gui.KeyboardFocusChange; |
---|
| 24 | import de.ugoe.cs.autoquest.eventcore.gui.MouseButtonInteraction; |
---|
| 25 | import de.ugoe.cs.autoquest.eventcore.gui.MouseClick; |
---|
[1069] | 26 | import de.ugoe.cs.autoquest.eventcore.gui.MouseDoubleClick; |
---|
[1047] | 27 | import de.ugoe.cs.autoquest.eventcore.gui.Scroll; |
---|
[1059] | 28 | import de.ugoe.cs.autoquest.eventcore.gui.TextInput; |
---|
[1223] | 29 | import de.ugoe.cs.autoquest.eventcore.gui.ValueSelection; |
---|
| 30 | import de.ugoe.cs.autoquest.eventcore.guimodel.ICheckBox; |
---|
[1247] | 31 | import de.ugoe.cs.autoquest.eventcore.guimodel.IComboBox; |
---|
[1059] | 32 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; |
---|
[1223] | 33 | import de.ugoe.cs.autoquest.eventcore.guimodel.IRadioButton; |
---|
[1059] | 34 | import de.ugoe.cs.autoquest.eventcore.guimodel.ITextArea; |
---|
| 35 | import de.ugoe.cs.autoquest.eventcore.guimodel.ITextField; |
---|
[1250] | 36 | import de.ugoe.cs.autoquest.keyboardmaps.VirtualKey; |
---|
[1047] | 37 | import de.ugoe.cs.util.console.Console; |
---|
| 38 | |
---|
[1054] | 39 | /** |
---|
| 40 | * <p> |
---|
[1276] | 41 | * convenience class to instantiate the correct event type based on an event name, event parameters, |
---|
| 42 | * and the target GUI element. |
---|
[1054] | 43 | * </p> |
---|
| 44 | * |
---|
| 45 | * @author Patrick Harms |
---|
[1059] | 46 | * @author Fabian Glaser |
---|
[1054] | 47 | */ |
---|
[1047] | 48 | public class HTMLEventTypeFactory { |
---|
[1054] | 49 | |
---|
[1276] | 50 | /** |
---|
| 51 | * <p> |
---|
| 52 | * single instance |
---|
| 53 | * </p> |
---|
| 54 | */ |
---|
[1047] | 55 | private static HTMLEventTypeFactory instance = new HTMLEventTypeFactory(); |
---|
| 56 | |
---|
[1054] | 57 | /** |
---|
| 58 | * <p> |
---|
[1276] | 59 | * private constructor to implement correct singleton |
---|
[1054] | 60 | * </p> |
---|
| 61 | */ |
---|
[1047] | 62 | private HTMLEventTypeFactory() {} |
---|
| 63 | |
---|
[1054] | 64 | /** |
---|
| 65 | * <p> |
---|
[1276] | 66 | * singleton retrieval |
---|
[1054] | 67 | * </p> |
---|
| 68 | */ |
---|
[1047] | 69 | public static HTMLEventTypeFactory getInstance() { |
---|
| 70 | return instance; |
---|
| 71 | } |
---|
| 72 | |
---|
[1054] | 73 | /** |
---|
| 74 | * <p> |
---|
[1276] | 75 | * returns the event type for the given event name, the parameters as well as the target GUI |
---|
| 76 | * element |
---|
[1054] | 77 | * </p> |
---|
| 78 | * |
---|
[1276] | 79 | * @param eventName the name of the event, e.g., onscroll |
---|
| 80 | * @param eventParameters the parameters of the event, e.g., scroll coordinates |
---|
| 81 | * @param guiElement the target GUI element of the event (required for some event type |
---|
| 82 | * differentiation |
---|
| 83 | * |
---|
| 84 | * @return the event type matching the proviced parameters |
---|
| 85 | * |
---|
| 86 | * @throws IllegalArgumentException if the provided parameters in their combination do not |
---|
| 87 | * correctly specify an event type |
---|
[1054] | 88 | */ |
---|
[1276] | 89 | public IEventType getEventType(String eventName, |
---|
| 90 | Map<String, String> eventParameters, |
---|
| 91 | IGUIElement guiElement) |
---|
| 92 | { |
---|
[1047] | 93 | IInteraction result = null; |
---|
| 94 | |
---|
| 95 | if ("onscroll".equals(eventName)) { |
---|
[1069] | 96 | int[] coordinates = |
---|
| 97 | getCoordinateParameter("scrollX", "scrollY", eventName, eventParameters); |
---|
| 98 | |
---|
[1047] | 99 | result = new Scroll(coordinates[0], coordinates[1]); |
---|
| 100 | } |
---|
| 101 | else if ("onclick".equals(eventName)) { |
---|
[1069] | 102 | int[] coordinates = getCoordinateParameter("X", "Y", eventName, eventParameters); |
---|
[1047] | 103 | result = |
---|
| 104 | new MouseClick(MouseButtonInteraction.Button.LEFT, coordinates[0], coordinates[1]); |
---|
| 105 | } |
---|
[1069] | 106 | else if ("ondblclick".equals(eventName)) { |
---|
| 107 | int[] coordinates = getCoordinateParameter("X", "Y", eventName, eventParameters); |
---|
| 108 | result = new MouseDoubleClick |
---|
| 109 | (MouseButtonInteraction.Button.LEFT, coordinates[0], coordinates[1]); |
---|
| 110 | } |
---|
[1047] | 111 | else if ("onchange".equals(eventName)) { |
---|
[1059] | 112 | String value = eventParameters.get("selectedValue"); |
---|
| 113 | |
---|
| 114 | if ((guiElement instanceof ITextArea) || (guiElement instanceof ITextField)) { |
---|
| 115 | result = new TextInput(value, null); |
---|
| 116 | } |
---|
[1247] | 117 | else if ((guiElement instanceof IRadioButton) || (guiElement instanceof ICheckBox) || |
---|
| 118 | (guiElement instanceof IComboBox)) |
---|
| 119 | { |
---|
[1223] | 120 | result = new ValueSelection<String>(value); |
---|
| 121 | } |
---|
[1059] | 122 | else { |
---|
| 123 | throw new IllegalArgumentException("can not handle onchange events on GUI " + |
---|
| 124 | "elements of type " + guiElement.getClass()); |
---|
| 125 | } |
---|
[1047] | 126 | } |
---|
[1250] | 127 | else if ("onkeydown".equals(eventName)) { |
---|
| 128 | try { |
---|
| 129 | int key = Integer.parseInt(eventParameters.get("key")); |
---|
| 130 | result = new KeyPressed(VirtualKey.valueOf(key)); |
---|
| 131 | } |
---|
| 132 | catch (NumberFormatException e) { |
---|
| 133 | throw new IllegalArgumentException("the key id provided by an " + eventName + |
---|
| 134 | " is no correct integer"); |
---|
| 135 | } |
---|
| 136 | } |
---|
[1047] | 137 | else if ("onfocus".equals(eventName)) { |
---|
| 138 | result = new KeyboardFocusChange(); |
---|
| 139 | } |
---|
[1435] | 140 | else if ("onload".equals(eventName) || |
---|
| 141 | "onunload".equals(eventName) || "onbeforeunload".equals(eventName) || |
---|
[1194] | 142 | "onpagehide".equals(eventName) || "onpageshow".equals(eventName) || |
---|
[1223] | 143 | "onsubmit".equals(eventName) || "onselect".equals(eventName) || |
---|
[1435] | 144 | "onreset".equals(eventName) || "onabort".equals(eventName)) |
---|
[1047] | 145 | { |
---|
| 146 | Console.traceln(Level.FINE, "Ignored event name \"" + eventName + "\""); |
---|
| 147 | } |
---|
| 148 | else { |
---|
| 149 | throw new IllegalArgumentException("unknown event name: \"" + eventName + "\""); |
---|
| 150 | } |
---|
| 151 | return result; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | * <p> |
---|
[1276] | 156 | * convenience method to retrieve coordinates from the event parameters. |
---|
[1047] | 157 | * </p> |
---|
| 158 | */ |
---|
[1069] | 159 | private int[] getCoordinateParameter(String xParamName, |
---|
| 160 | String yParamName, |
---|
| 161 | String eventName, |
---|
| 162 | Map<String, String> eventParameters) |
---|
| 163 | { |
---|
| 164 | String xCoord = eventParameters.get(xParamName); |
---|
[1047] | 165 | if (xCoord == null) { |
---|
[1354] | 166 | Console.traceln(Level.WARNING, |
---|
| 167 | "eventParameters do not contain " + xParamName + " coordinate."); |
---|
[1239] | 168 | xCoord = "0"; |
---|
[1047] | 169 | } |
---|
| 170 | |
---|
[1069] | 171 | String yCoord = eventParameters.get(yParamName); |
---|
[1047] | 172 | if (yCoord == null) { |
---|
[1354] | 173 | Console.traceln(Level.WARNING, |
---|
| 174 | "eventParameters do not contain " + yParamName + " coordinate."); |
---|
[1239] | 175 | yCoord = "0"; |
---|
[1047] | 176 | } |
---|
| 177 | |
---|
| 178 | try { |
---|
| 179 | return new int[] |
---|
| 180 | { Integer.parseInt(xCoord), Integer.parseInt(yCoord) }; |
---|
| 181 | } |
---|
| 182 | catch (NumberFormatException e) { |
---|
[1069] | 183 | throw new IllegalArgumentException("the coordinates provided by an " + eventName + |
---|
[1354] | 184 | " event are no numbers"); |
---|
[1047] | 185 | } |
---|
| 186 | } |
---|
[1059] | 187 | |
---|
| 188 | |
---|
| 189 | |
---|
[1047] | 190 | } |
---|