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. |
---|
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.KeyPressed; |
---|
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; |
---|
26 | import de.ugoe.cs.autoquest.eventcore.gui.MouseDoubleClick; |
---|
27 | import de.ugoe.cs.autoquest.eventcore.gui.Scroll; |
---|
28 | import de.ugoe.cs.autoquest.eventcore.gui.TextInput; |
---|
29 | import de.ugoe.cs.autoquest.eventcore.gui.ValueSelection; |
---|
30 | import de.ugoe.cs.autoquest.eventcore.guimodel.ICheckBox; |
---|
31 | import de.ugoe.cs.autoquest.eventcore.guimodel.IComboBox; |
---|
32 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; |
---|
33 | import de.ugoe.cs.autoquest.eventcore.guimodel.IRadioButton; |
---|
34 | import de.ugoe.cs.autoquest.eventcore.guimodel.ITextArea; |
---|
35 | import de.ugoe.cs.autoquest.eventcore.guimodel.ITextField; |
---|
36 | import de.ugoe.cs.autoquest.keyboardmaps.VirtualKey; |
---|
37 | import de.ugoe.cs.util.console.Console; |
---|
38 | |
---|
39 | /** |
---|
40 | * <p> |
---|
41 | * convenience class to instantiate the correct event type based on an event name, event parameters, |
---|
42 | * and the target GUI element. |
---|
43 | * </p> |
---|
44 | * |
---|
45 | * @author Patrick Harms |
---|
46 | * @author Fabian Glaser |
---|
47 | */ |
---|
48 | public class HTMLEventTypeFactory { |
---|
49 | |
---|
50 | /** |
---|
51 | * <p> |
---|
52 | * single instance |
---|
53 | * </p> |
---|
54 | */ |
---|
55 | private static HTMLEventTypeFactory instance = new HTMLEventTypeFactory(); |
---|
56 | |
---|
57 | /** |
---|
58 | * <p> |
---|
59 | * private constructor to implement correct singleton |
---|
60 | * </p> |
---|
61 | */ |
---|
62 | private HTMLEventTypeFactory() {} |
---|
63 | |
---|
64 | /** |
---|
65 | * <p> |
---|
66 | * singleton retrieval |
---|
67 | * </p> |
---|
68 | */ |
---|
69 | public static HTMLEventTypeFactory getInstance() { |
---|
70 | return instance; |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * <p> |
---|
75 | * returns the event type for the given event name, the parameters as well as the target GUI |
---|
76 | * element |
---|
77 | * </p> |
---|
78 | * |
---|
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 |
---|
88 | */ |
---|
89 | public IEventType getEventType(String eventName, |
---|
90 | Map<String, String> eventParameters, |
---|
91 | IGUIElement guiElement) |
---|
92 | { |
---|
93 | IInteraction result = null; |
---|
94 | |
---|
95 | if ("onscroll".equals(eventName)) { |
---|
96 | int[] coordinates = |
---|
97 | getCoordinateParameter("scrollX", "scrollY", eventName, eventParameters); |
---|
98 | |
---|
99 | result = new Scroll(coordinates[0], coordinates[1]); |
---|
100 | } |
---|
101 | else if ("onclick".equals(eventName)) { |
---|
102 | int[] coordinates = getCoordinateParameter("X", "Y", eventName, eventParameters); |
---|
103 | result = |
---|
104 | new MouseClick(MouseButtonInteraction.Button.LEFT, coordinates[0], coordinates[1]); |
---|
105 | } |
---|
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 | } |
---|
111 | else if ("onchange".equals(eventName)) { |
---|
112 | String value = eventParameters.get("selectedValue"); |
---|
113 | |
---|
114 | if ((guiElement instanceof ITextArea) || (guiElement instanceof ITextField)) { |
---|
115 | result = new TextInput(value, null); |
---|
116 | } |
---|
117 | else if ((guiElement instanceof IRadioButton) || (guiElement instanceof ICheckBox) || |
---|
118 | (guiElement instanceof IComboBox)) |
---|
119 | { |
---|
120 | result = new ValueSelection<String>(value); |
---|
121 | } |
---|
122 | else { |
---|
123 | throw new IllegalArgumentException("can not handle onchange events on GUI " + |
---|
124 | "elements of type " + guiElement.getClass()); |
---|
125 | } |
---|
126 | } |
---|
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 | } |
---|
137 | else if ("onfocus".equals(eventName)) { |
---|
138 | result = new KeyboardFocusChange(); |
---|
139 | } |
---|
140 | else if ("onunload".equals(eventName) || "onbeforeunload".equals(eventName) || |
---|
141 | "onpagehide".equals(eventName) || "onpageshow".equals(eventName) || |
---|
142 | "onsubmit".equals(eventName) || "onselect".equals(eventName) || |
---|
143 | "onreset".equals(eventName)) |
---|
144 | { |
---|
145 | Console.traceln(Level.FINE, "Ignored event name \"" + eventName + "\""); |
---|
146 | } |
---|
147 | else { |
---|
148 | throw new IllegalArgumentException("unknown event name: \"" + eventName + "\""); |
---|
149 | } |
---|
150 | return result; |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * <p> |
---|
155 | * convenience method to retrieve coordinates from the event parameters. |
---|
156 | * </p> |
---|
157 | */ |
---|
158 | private int[] getCoordinateParameter(String xParamName, |
---|
159 | String yParamName, |
---|
160 | String eventName, |
---|
161 | Map<String, String> eventParameters) |
---|
162 | { |
---|
163 | String xCoord = eventParameters.get(xParamName); |
---|
164 | if (xCoord == null) { |
---|
165 | Console.printerrln("eventParameters do not contain " + xParamName + " coordinate."); |
---|
166 | xCoord = "0"; |
---|
167 | } |
---|
168 | |
---|
169 | String yCoord = eventParameters.get(yParamName); |
---|
170 | if (yCoord == null) { |
---|
171 | Console.printerrln("eventParameters do not contain " + yParamName + " coordinate."); |
---|
172 | yCoord = "0"; |
---|
173 | } |
---|
174 | |
---|
175 | try { |
---|
176 | return new int[] |
---|
177 | { Integer.parseInt(xCoord), Integer.parseInt(yCoord) }; |
---|
178 | } |
---|
179 | catch (NumberFormatException e) { |
---|
180 | throw new IllegalArgumentException("the coordinates provided by an " + eventName + |
---|
181 | " event are no numbers"); |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | } |
---|