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