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

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