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

Last change on this file since 1219 was 1219, checked in by pharms, 11 years ago
  • added support for hr
  • added support for onselect
  • Property svn:mime-type set to text/plain
File size: 5.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.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                 "onsubmit".equals(eventName) || "onselect".equals(eventName))
111        {
112            Console.traceln(Level.FINE, "Ignored event name \"" + eventName + "\"");
113        }
114        else {
115            throw new IllegalArgumentException("unknown event name: \"" + eventName + "\"");
116        }
117        return result;
118    }
119
120    /**
121     * <p>
122     * TODO: comment
123     * </p>
124     *
125     * @param eventName
126     * @param eventParameters
127     * @return
128     */
129    private int[] getCoordinateParameter(String              xParamName,
130                                         String              yParamName,
131                                         String              eventName,
132                                         Map<String, String> eventParameters)
133    {
134        String xCoord = eventParameters.get(xParamName);
135        if (xCoord == null) {
136            throw new IllegalArgumentException
137                ("eventParameters do not contain " + xParamName + " coordinate.");
138        }
139
140        String yCoord = eventParameters.get(yParamName);
141        if (yCoord == null) {
142            throw new IllegalArgumentException
143                ("eventParameters do not contain " + yParamName + " coordinate.");
144        }
145
146        try {
147            return new int[]
148                { Integer.parseInt(xCoord), Integer.parseInt(yCoord) };
149        }
150        catch (NumberFormatException e) {
151            throw new IllegalArgumentException("the coordinates provided by an " + eventName +
152                " event are no numbers");
153        }
154    }
155   
156   
157   
158}
Note: See TracBrowser for help on using the repository browser.