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

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