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