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