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