source: trunk/autoquest-plugin-html/src/main/java/de/ugoe/cs/autoquest/plugin/html/eventcore/HTMLEventTypeFactory.java @ 1054

Last change on this file since 1054 was 1054, checked in by pharms, 11 years ago
  • added comments
  • Property svn:mime-type set to text/plain
File size: 3.9 KB
Line 
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
15package de.ugoe.cs.autoquest.plugin.html.eventcore;
16
17import java.util.Map;
18import java.util.logging.Level;
19
20import de.ugoe.cs.autoquest.eventcore.IEventType;
21import de.ugoe.cs.autoquest.eventcore.gui.IInteraction;
22import de.ugoe.cs.autoquest.eventcore.gui.KeyboardFocusChange;
23import de.ugoe.cs.autoquest.eventcore.gui.MouseButtonInteraction;
24import de.ugoe.cs.autoquest.eventcore.gui.MouseClick;
25import de.ugoe.cs.autoquest.eventcore.gui.Scroll;
26import de.ugoe.cs.util.console.Console;
27
28/**
29 * <p>
30 * TODO comment
31 * </p>
32 *
33 * @author Patrick Harms
34 */
35public 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}
Note: See TracBrowser for help on using the repository browser.