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

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