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