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
RevLine 
[1054]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.
[1047]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;
[1250]22import de.ugoe.cs.autoquest.eventcore.gui.KeyPressed;
[1047]23import de.ugoe.cs.autoquest.eventcore.gui.KeyboardFocusChange;
24import de.ugoe.cs.autoquest.eventcore.gui.MouseButtonInteraction;
25import de.ugoe.cs.autoquest.eventcore.gui.MouseClick;
[1069]26import de.ugoe.cs.autoquest.eventcore.gui.MouseDoubleClick;
[1047]27import de.ugoe.cs.autoquest.eventcore.gui.Scroll;
[1059]28import de.ugoe.cs.autoquest.eventcore.gui.TextInput;
[1223]29import de.ugoe.cs.autoquest.eventcore.gui.ValueSelection;
30import de.ugoe.cs.autoquest.eventcore.guimodel.ICheckBox;
[1247]31import de.ugoe.cs.autoquest.eventcore.guimodel.IComboBox;
[1059]32import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
[1223]33import de.ugoe.cs.autoquest.eventcore.guimodel.IRadioButton;
[1059]34import de.ugoe.cs.autoquest.eventcore.guimodel.ITextArea;
35import de.ugoe.cs.autoquest.eventcore.guimodel.ITextField;
[1250]36import de.ugoe.cs.autoquest.keyboardmaps.VirtualKey;
[1047]37import de.ugoe.cs.util.console.Console;
38
[1054]39/**
40 * <p>
[1276]41 * convenience class to instantiate the correct event type based on an event name, event parameters,
42 * and the target GUI element.
[1054]43 * </p>
44 *
45 * @author Patrick Harms
[1059]46 * @author Fabian Glaser
[1054]47 */
[1047]48public class HTMLEventTypeFactory {
[1054]49   
[1276]50    /**
51     * <p>
52     * single instance
53     * </p>
54     */
[1047]55    private static HTMLEventTypeFactory instance = new HTMLEventTypeFactory();
56
[1054]57    /**
58     * <p>
[1276]59     * private constructor to implement correct singleton
[1054]60     * </p>
61     */
[1047]62    private HTMLEventTypeFactory() {}
63
[1054]64    /**
65     * <p>
[1276]66     * singleton retrieval
[1054]67     * </p>
68     */
[1047]69    public static HTMLEventTypeFactory getInstance() {
70        return instance;
71    }
72
[1054]73    /**
74     * <p>
[1276]75     * returns the event type for the given event name, the parameters as well as the target GUI
76     * element
[1054]77     * </p>
78     *
[1276]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
[1054]88     */
[1276]89    public IEventType getEventType(String              eventName,
90                                   Map<String, String> eventParameters,
91                                   IGUIElement         guiElement)
92    {
[1047]93        IInteraction result = null;
94
95        if ("onscroll".equals(eventName)) {
[1069]96            int[] coordinates =
97                getCoordinateParameter("scrollX", "scrollY", eventName, eventParameters);
98           
[1047]99            result = new Scroll(coordinates[0], coordinates[1]);
100        }
101        else if ("onclick".equals(eventName)) {
[1069]102            int[] coordinates = getCoordinateParameter("X", "Y", eventName, eventParameters);
[1047]103            result =
104                new MouseClick(MouseButtonInteraction.Button.LEFT, coordinates[0], coordinates[1]);
105        }
[1069]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        }
[1047]111        else if ("onchange".equals(eventName)) {
[1059]112            String value = eventParameters.get("selectedValue");
113           
114            if ((guiElement instanceof ITextArea) || (guiElement instanceof ITextField)) {
115                result = new TextInput(value, null);
116            }
[1247]117            else if ((guiElement instanceof IRadioButton) || (guiElement instanceof ICheckBox) ||
118                     (guiElement instanceof IComboBox))
119            {
[1223]120                result = new ValueSelection<String>(value);
121            }
[1059]122            else {
123                throw new IllegalArgumentException("can not handle onchange events on GUI " +
124                                                   "elements of type " + guiElement.getClass());
125            }
[1047]126        }
[1250]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        }
[1047]137        else if ("onfocus".equals(eventName)) {
138            result = new KeyboardFocusChange();
139        }
[1435]140        else if ("onload".equals(eventName) ||
141                 "onunload".equals(eventName) || "onbeforeunload".equals(eventName) ||
[1194]142                 "onpagehide".equals(eventName) || "onpageshow".equals(eventName) ||
[1223]143                 "onsubmit".equals(eventName) || "onselect".equals(eventName) ||
[1435]144                 "onreset".equals(eventName) || "onabort".equals(eventName))
[1047]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>
[1276]156     * convenience method to retrieve coordinates from the event parameters.
[1047]157     * </p>
158     */
[1069]159    private int[] getCoordinateParameter(String              xParamName,
160                                         String              yParamName,
161                                         String              eventName,
162                                         Map<String, String> eventParameters)
163    {
164        String xCoord = eventParameters.get(xParamName);
[1047]165        if (xCoord == null) {
[1354]166            Console.traceln(Level.WARNING,
167                            "eventParameters do not contain " + xParamName + " coordinate.");
[1239]168            xCoord = "0";
[1047]169        }
170
[1069]171        String yCoord = eventParameters.get(yParamName);
[1047]172        if (yCoord == null) {
[1354]173            Console.traceln(Level.WARNING,
174                            "eventParameters do not contain " + yParamName + " coordinate.");
[1239]175            yCoord = "0";
[1047]176        }
177
178        try {
179            return new int[]
180                { Integer.parseInt(xCoord), Integer.parseInt(yCoord) };
181        }
182        catch (NumberFormatException e) {
[1069]183            throw new IllegalArgumentException("the coordinates provided by an " + eventName +
[1354]184                                               " event are no numbers");
[1047]185        }
186    }
[1059]187   
188   
189   
[1047]190}
Note: See TracBrowser for help on using the repository browser.