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

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