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

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
File size: 7.4 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        if ("LeftClickButton".equals(eventName)) {
78            return new MouseClick(MouseButtonInteraction.Button.LEFT);
79        }
80        else if ("LeftClickListBox".equals(eventName)) {
81            return new ValueSelection<Integer>(getSelectedValue(messageParameters));
82        }
83        else if ("TabChange".equals(eventName)) {
84            return new MouseClick(MouseButtonInteraction.Button.LEFT);
85        }
86        else if ("LeftClickCommand".equals(eventName)) {
87            return new MouseClick(MouseButtonInteraction.Button.LEFT);
88        }
89        else if ("LeftClickSysCommand".equals(eventName)) {
90            return new MouseClick(MouseButtonInteraction.Button.LEFT);
91        }
92        else if ("NCLeftClickSysCommand".equals(eventName)) {
93            return new MouseClick(MouseButtonInteraction.Button.LEFT);
94        }
95        else if ("LeftClickMenuItemCmd".equals(eventName)) {
96            return new MouseClick(MouseButtonInteraction.Button.LEFT);
97        }
98        else if ("HScroll_TrackBar".equals(eventName)) {
99            return new ValueSelection<Integer>(getSelectedValue(messageParameters));
100        }
101        else if ("VScroll_TrackBar".equals(eventName)) {
102            return new ValueSelection<Integer>(getSelectedValue(messageParameters));
103        }
104        else if ("HScroll_ScrollBar".equals(eventName)) {
105            return new MouseClick(MouseButtonInteraction.Button.LEFT);
106        }
107        else if ("VScroll_ScrollBar".equals(eventName)) {
108            return new MouseClick(MouseButtonInteraction.Button.LEFT);
109        }
110        else if ("VScrollNC".equals(eventName)) {
111            return new MouseClick(MouseButtonInteraction.Button.LEFT);
112        }
113        else if ("LeftClickSetFocus".equals(eventName)) {
114            return new KeyboardFocusChange();
115        }
116        else if ("LeftClickChangeFocus".equals(eventName)) {
117            return new KeyboardFocusChange();
118        }
119        else if ("KeyDown".equals(eventName)) {
120            return new KeyPressed(getKey(messageParameters));
121        }
122        else if ("KeyUp".equals(eventName)) {
123            return new KeyReleased(getKey(messageParameters));
124        }
125        else if ("SysKeyDown".equals(eventName)) {
126            return new KeyPressed(getKey(messageParameters));
127        }
128        else if ("SysKeyUp".equals(eventName)) {
129            return new KeyReleased(getKey(messageParameters));
130        }
131        else if ("LeftClickCoordinates".equals(eventName)) {
132            return new MouseClick(MouseButtonInteraction.Button.LEFT);
133        }
134        else if ("NCLeftClickCoordinates".equals(eventName)) {
135            return new MouseClick(MouseButtonInteraction.Button.LEFT);
136        }
137        else if ("NCLeftClickCoordinates2".equals(eventName)) {
138            return new MouseClick(MouseButtonInteraction.Button.LEFT);
139        }
140        else if ("LeftClickCoordinatesTargetChanged".equals(eventName)) {
141            return new MouseClick(MouseButtonInteraction.Button.LEFT);
142        }
143        else if ("LeftClickCoordinatesTargetChanged2".equals(eventName)) {
144            return new MouseClick(MouseButtonInteraction.Button.LEFT);
145        }
146        else {
147            throw new IllegalArgumentException("unknown event name: " + eventName);
148        }
149    }
150
151    /**
152     * <p>
153     * If the message parameters contain information about a key that has been pressed, the
154     * associated {@link VirtualKey} is returned.
155     * </p>
156     *
157     * @param messageParameters
158     *            the message parameters
159     * @return key extracted from the message parameters
160     * @throws IllegalArgumentException
161     *             thrown if the messageParameters do not contain information about a key
162     */
163    private VirtualKey getKey(Map<String, String> messageParameters)
164        throws IllegalArgumentException
165    {
166        String value = null;
167
168        if (messageParameters != null) {
169            value = messageParameters.get("key");
170        }
171
172        if (value == null) {
173            throw new IllegalArgumentException
174                ("no parameter \"key\" provided for key event. Please correct the event " +
175                 "generation rules");
176        }
177       
178        return WindowsVirtualKey.parseVirtualKey(value).getKey();
179    }
180
181    /**
182     * <p>
183     * If the message parameters contain information about a scroll position, the respective
184     * position is returned.
185     * </p>
186     *
187     * @param messageParameters
188     *            the message parameters
189     * @return the scroll position
190     * @throws IllegalArgumentException
191     *             thrown if the messageParameters do not contain information about a scroll
192     *             position
193     */
194    private int getSelectedValue(Map<String, String> messageParameters)
195        throws IllegalArgumentException
196    {
197        String value = null;
198
199        if (messageParameters != null) {
200            value = messageParameters.get("scrollPos");
201        }
202
203        if (value == null) {
204            throw new IllegalArgumentException
205                ("no parameter \"scrollPos\" provided for scroll event. Please correct the event " +
206                 "generation rules");
207        }
208       
209        return Integer.parseInt(value);
210    }
211
212}
Note: See TracBrowser for help on using the repository browser.