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

Last change on this file since 1876 was 1824, checked in by pharms, 10 years ago
  • corrected handling of key press events
  • Property svn:mime-type set to text/plain
File size: 7.0 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.util.console.Console;
37
38/**
39 * <p>
40 * convenience class to instantiate the correct event type based on an event name, event parameters,
41 * and the target GUI element.
42 * </p>
43 *
44 * @author Patrick Harms
45 * @author Fabian Glaser
46 */
47public class HTMLEventTypeFactory {
48   
49    /**
50     * <p>
51     * single instance
52     * </p>
53     */
54    private static HTMLEventTypeFactory instance = new HTMLEventTypeFactory();
55
56    /**
57     * <p>
58     * private constructor to implement correct singleton
59     * </p>
60     */
61    private HTMLEventTypeFactory() {}
62
63    /**
64     * <p>
65     * singleton retrieval
66     * </p>
67     */
68    public static HTMLEventTypeFactory getInstance() {
69        return instance;
70    }
71
72    /**
73     * <p>
74     * returns the event type for the given event name, the parameters as well as the target GUI
75     * element
76     * </p>
77     *
78     * @param eventName       the name of the event, e.g., onscroll
79     * @param eventParameters the parameters of the event, e.g., scroll coordinates
80     * @param guiElement      the target GUI element of the event (required for some event type
81     *                        differentiation
82     *                       
83     * @return the event type matching the proviced parameters
84     *
85     * @throws IllegalArgumentException if the provided parameters in their combination do not
86     *                                  correctly specify an event type
87     */
88    public IEventType getEventType(String              eventName,
89                                   Map<String, String> eventParameters,
90                                   IGUIElement         guiElement)
91    {
92        IInteraction result = null;
93
94        if ("onscroll".equals(eventName)) {
95            int[] coordinates =
96                getCoordinateParameter("scrollX", "scrollY", eventName, eventParameters);
97           
98            result = new Scroll(coordinates[0], coordinates[1]);
99        }
100        else if ("onclick".equals(eventName)) {
101            int[] coordinates = getCoordinateParameter("X", "Y", eventName, eventParameters);
102            result =
103                new MouseClick(MouseButtonInteraction.Button.LEFT, coordinates[0], coordinates[1]);
104        }
105        else if ("ondblclick".equals(eventName)) {
106            int[] coordinates = getCoordinateParameter("X", "Y", eventName, eventParameters);
107            result = new MouseDoubleClick
108                (MouseButtonInteraction.Button.LEFT, coordinates[0], coordinates[1]);
109        }
110        else if ("onchange".equals(eventName)) {
111            String value = eventParameters.get("selectedValue");
112           
113            if ((guiElement instanceof ITextArea) || (guiElement instanceof ITextField)) {
114                result = new TextInput(value, null);
115            }
116            else if ((guiElement instanceof IRadioButton) || (guiElement instanceof ICheckBox) ||
117                     (guiElement instanceof IComboBox))
118            {
119                result = new ValueSelection<String>(value);
120            }
121            else {
122                throw new IllegalArgumentException("can not handle onchange events on GUI " +
123                                                   "elements of type " + guiElement.getClass());
124            }
125        }
126        else if ("onkeydown".equals(eventName)) {
127            result = new KeyPressed
128                (HTMLVirtualKey.parseVirtualKey(eventParameters.get("key")).getKey());
129        }
130        else if ("onfocus".equals(eventName)) {
131            result = new KeyboardFocusChange();
132        }
133        else if ("onload".equals(eventName) ||
134                 "onunload".equals(eventName) || "onbeforeunload".equals(eventName) ||
135                 "onpagehide".equals(eventName) || "onpageshow".equals(eventName) ||
136                 "onsubmit".equals(eventName) || "onselect".equals(eventName) ||
137                 "onreset".equals(eventName) || "onabort".equals(eventName))
138        {
139            Console.traceln(Level.FINE, "Ignored event name \"" + eventName + "\"");
140        }
141        else {
142            throw new IllegalArgumentException("unknown event name: \"" + eventName + "\"");
143        }
144        return result;
145    }
146
147    /**
148     * <p>
149     * convenience method to retrieve coordinates from the event parameters.
150     * </p>
151     */
152    private int[] getCoordinateParameter(String              xParamName,
153                                         String              yParamName,
154                                         String              eventName,
155                                         Map<String, String> eventParameters)
156    {
157        String xCoord = eventParameters.get(xParamName);
158        if (xCoord == null) {
159            Console.traceln(Level.WARNING,
160                            "eventParameters do not contain " + xParamName + " coordinate.");
161            xCoord = "0";
162        }
163
164        String yCoord = eventParameters.get(yParamName);
165        if (yCoord == null) {
166            Console.traceln(Level.WARNING,
167                            "eventParameters do not contain " + yParamName + " coordinate.");
168            yCoord = "0";
169        }
170
171        try {
172            return new int[]
173                { Integer.parseInt(xCoord), Integer.parseInt(yCoord) };
174        }
175        catch (NumberFormatException e) {
176            throw new IllegalArgumentException("the coordinates provided by an " + eventName +
177                                               " event are no numbers");
178        }
179    }
180   
181   
182   
183}
Note: See TracBrowser for help on using the repository browser.