source: trunk/autoquest-plugin-mfc/src/main/java/de/ugoe/cs/autoquest/plugin/mfc/eventcore/MFCEventTypeFactory.java @ 2215

Last change on this file since 2215 was 946, checked in by pharms, 12 years ago
  • added support for mouse click coordinates
File size: 7.6 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.mfc.eventcore;
16
17import java.util.Map;
18
19import de.ugoe.cs.autoquest.eventcore.IEventType;
20import de.ugoe.cs.autoquest.eventcore.gui.IInteraction;
21import de.ugoe.cs.autoquest.eventcore.gui.KeyPressed;
22import de.ugoe.cs.autoquest.eventcore.gui.KeyReleased;
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.ValueSelection;
27import de.ugoe.cs.autoquest.keyboardmaps.VirtualKey;
28
29/**
30 * <p>
31 * Creates the GUI event types (i.e., {@link IInteraction}s) for MFC events.
32 * </p>
33 *
34 * @version 1.0
35 * @author Patrick Harms
36 */
37public class MFCEventTypeFactory {
38
39    /**
40     * <p>
41     * Instance of the singleton
42     * </p>
43     */
44    private static MFCEventTypeFactory instance = new MFCEventTypeFactory();
45
46    /**
47     * <p>
48     * Constructor. Creates a new MFCEventTypeFactory. Private to preserve singleton property.
49     * </p>
50     *
51     */
52    private MFCEventTypeFactory() {}
53
54    /**
55     * <p>
56     * Returns the instance of the MFCEventTypeFactory.
57     * </p>
58     *
59     * @return the instance
60     */
61    public static MFCEventTypeFactory getInstance() {
62        return instance;
63    }
64
65    /**
66     * <p>
67     * Returns the event type based on the name and parameters of a MFC event.
68     * </p>
69     *
70     * @param eventName
71     *            name of the MFC event
72     * @param messageParameters
73     *            parameters of the MFC event
74     * @return the event type
75     */
76    public IEventType getEventType(String eventName, Map<String, String> messageParameters) {
77        // TODO create scrolls were necessary and correct the coordinates of scrolls and mouse
78        // clicks
79        if ("LeftClickButton".equals(eventName)) {
80            return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1);
81        }
82        else if ("LeftClickListBox".equals(eventName)) {
83            return new ValueSelection<Integer>(getSelectedValue(messageParameters));
84        }
85        else if ("TabChange".equals(eventName)) {
86            return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1);
87        }
88        else if ("LeftClickCommand".equals(eventName)) {
89            return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1);
90        }
91        else if ("LeftClickSysCommand".equals(eventName)) {
92            return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1);
93        }
94        else if ("NCLeftClickSysCommand".equals(eventName)) {
95            return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1);
96        }
97        else if ("LeftClickMenuItemCmd".equals(eventName)) {
98            return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1);
99        }
100        else if ("HScroll_TrackBar".equals(eventName)) {
101            return new ValueSelection<Integer>(getSelectedValue(messageParameters));
102        }
103        else if ("VScroll_TrackBar".equals(eventName)) {
104            return new ValueSelection<Integer>(getSelectedValue(messageParameters));
105        }
106        else if ("HScroll_ScrollBar".equals(eventName)) {
107            return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1);
108        }
109        else if ("VScroll_ScrollBar".equals(eventName)) {
110            return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1);
111        }
112        else if ("VScrollNC".equals(eventName)) {
113            return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1);
114        }
115        else if ("LeftClickSetFocus".equals(eventName)) {
116            return new KeyboardFocusChange();
117        }
118        else if ("LeftClickChangeFocus".equals(eventName)) {
119            return new KeyboardFocusChange();
120        }
121        else if ("KeyDown".equals(eventName)) {
122            return new KeyPressed(getKey(messageParameters));
123        }
124        else if ("KeyUp".equals(eventName)) {
125            return new KeyReleased(getKey(messageParameters));
126        }
127        else if ("SysKeyDown".equals(eventName)) {
128            return new KeyPressed(getKey(messageParameters));
129        }
130        else if ("SysKeyUp".equals(eventName)) {
131            return new KeyReleased(getKey(messageParameters));
132        }
133        else if ("LeftClickCoordinates".equals(eventName)) {
134            return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1);
135        }
136        else if ("NCLeftClickCoordinates".equals(eventName)) {
137            return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1);
138        }
139        else if ("NCLeftClickCoordinates2".equals(eventName)) {
140            return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1);
141        }
142        else if ("LeftClickCoordinatesTargetChanged".equals(eventName)) {
143            return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1);
144        }
145        else if ("LeftClickCoordinatesTargetChanged2".equals(eventName)) {
146            return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1);
147        }
148        else {
149            throw new IllegalArgumentException("unknown event name: " + eventName);
150        }
151    }
152
153    /**
154     * <p>
155     * If the message parameters contain information about a key that has been pressed, the
156     * associated {@link VirtualKey} is returned.
157     * </p>
158     *
159     * @param messageParameters
160     *            the message parameters
161     * @return key extracted from the message parameters
162     * @throws IllegalArgumentException
163     *             thrown if the messageParameters do not contain information about a key
164     */
165    private VirtualKey getKey(Map<String, String> messageParameters)
166        throws IllegalArgumentException
167    {
168        String value = null;
169
170        if (messageParameters != null) {
171            value = messageParameters.get("key");
172        }
173
174        if (value == null) {
175            throw new IllegalArgumentException
176                ("no parameter \"key\" provided for key event. Please correct the event " +
177                 "generation rules");
178        }
179       
180        return WindowsVirtualKey.parseVirtualKey(value).getKey();
181    }
182
183    /**
184     * <p>
185     * If the message parameters contain information about a scroll position, the respective
186     * position is returned.
187     * </p>
188     *
189     * @param messageParameters
190     *            the message parameters
191     * @return the scroll position
192     * @throws IllegalArgumentException
193     *             thrown if the messageParameters do not contain information about a scroll
194     *             position
195     */
196    private int getSelectedValue(Map<String, String> messageParameters)
197        throws IllegalArgumentException
198    {
199        String value = null;
200
201        if (messageParameters != null) {
202            value = messageParameters.get("scrollPos");
203        }
204
205        if (value == null) {
206            throw new IllegalArgumentException
207                ("no parameter \"scrollPos\" provided for scroll event. Please correct the event " +
208                 "generation rules");
209        }
210       
211        return Integer.parseInt(value);
212    }
213
214}
Note: See TracBrowser for help on using the repository browser.