[927] | 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. |
---|
[837] | 14 | |
---|
[922] | 15 | package de.ugoe.cs.autoquest.plugin.mfc.eventcore; |
---|
[619] | 16 | |
---|
| 17 | import java.util.Map; |
---|
| 18 | |
---|
[922] | 19 | import de.ugoe.cs.autoquest.eventcore.IEventType; |
---|
| 20 | import de.ugoe.cs.autoquest.eventcore.gui.IInteraction; |
---|
| 21 | import de.ugoe.cs.autoquest.eventcore.gui.KeyPressed; |
---|
| 22 | import de.ugoe.cs.autoquest.eventcore.gui.KeyReleased; |
---|
| 23 | import de.ugoe.cs.autoquest.eventcore.gui.KeyboardFocusChange; |
---|
| 24 | import de.ugoe.cs.autoquest.eventcore.gui.MouseButtonInteraction; |
---|
| 25 | import de.ugoe.cs.autoquest.eventcore.gui.MouseClick; |
---|
| 26 | import de.ugoe.cs.autoquest.eventcore.gui.ValueSelection; |
---|
| 27 | import de.ugoe.cs.autoquest.keyboardmaps.VirtualKey; |
---|
[619] | 28 | |
---|
| 29 | /** |
---|
| 30 | * <p> |
---|
[837] | 31 | * Creates the GUI event types (i.e., {@link IInteraction}s) for MFC events. |
---|
[619] | 32 | * </p> |
---|
| 33 | * |
---|
[837] | 34 | * @version 1.0 |
---|
| 35 | * @author Patrick Harms |
---|
[619] | 36 | */ |
---|
| 37 | public class MFCEventTypeFactory { |
---|
[837] | 38 | |
---|
[619] | 39 | /** |
---|
| 40 | * <p> |
---|
[837] | 41 | * Instance of the singleton |
---|
[619] | 42 | * </p> |
---|
| 43 | */ |
---|
| 44 | private static MFCEventTypeFactory instance = new MFCEventTypeFactory(); |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * <p> |
---|
[837] | 48 | * Constructor. Creates a new MFCEventTypeFactory. Private to preserve singleton property. |
---|
[619] | 49 | * </p> |
---|
[837] | 50 | * |
---|
[619] | 51 | */ |
---|
[837] | 52 | private MFCEventTypeFactory() {} |
---|
[619] | 53 | |
---|
| 54 | /** |
---|
| 55 | * <p> |
---|
[837] | 56 | * Returns the instance of the MFCEventTypeFactory. |
---|
[619] | 57 | * </p> |
---|
[837] | 58 | * |
---|
| 59 | * @return the instance |
---|
[619] | 60 | */ |
---|
| 61 | public static MFCEventTypeFactory getInstance() { |
---|
| 62 | return instance; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * <p> |
---|
[837] | 67 | * Returns the event type based on the name and parameters of a MFC event. |
---|
[619] | 68 | * </p> |
---|
[837] | 69 | * |
---|
| 70 | * @param eventName |
---|
| 71 | * name of the MFC event |
---|
| 72 | * @param messageParameters |
---|
| 73 | * parameters of the MFC event |
---|
| 74 | * @return the event type |
---|
[619] | 75 | */ |
---|
[837] | 76 | public IEventType getEventType(String eventName, Map<String, String> messageParameters) { |
---|
[946] | 77 | // TODO create scrolls were necessary and correct the coordinates of scrolls and mouse |
---|
| 78 | // clicks |
---|
[619] | 79 | if ("LeftClickButton".equals(eventName)) { |
---|
[946] | 80 | return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1); |
---|
[619] | 81 | } |
---|
| 82 | else if ("LeftClickListBox".equals(eventName)) { |
---|
| 83 | return new ValueSelection<Integer>(getSelectedValue(messageParameters)); |
---|
| 84 | } |
---|
| 85 | else if ("TabChange".equals(eventName)) { |
---|
[946] | 86 | return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1); |
---|
[619] | 87 | } |
---|
| 88 | else if ("LeftClickCommand".equals(eventName)) { |
---|
[946] | 89 | return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1); |
---|
[619] | 90 | } |
---|
| 91 | else if ("LeftClickSysCommand".equals(eventName)) { |
---|
[946] | 92 | return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1); |
---|
[619] | 93 | } |
---|
| 94 | else if ("NCLeftClickSysCommand".equals(eventName)) { |
---|
[946] | 95 | return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1); |
---|
[619] | 96 | } |
---|
| 97 | else if ("LeftClickMenuItemCmd".equals(eventName)) { |
---|
[946] | 98 | return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1); |
---|
[619] | 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)) { |
---|
[946] | 107 | return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1); |
---|
[619] | 108 | } |
---|
| 109 | else if ("VScroll_ScrollBar".equals(eventName)) { |
---|
[946] | 110 | return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1); |
---|
[619] | 111 | } |
---|
| 112 | else if ("VScrollNC".equals(eventName)) { |
---|
[946] | 113 | return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1); |
---|
[619] | 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)) { |
---|
[946] | 134 | return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1); |
---|
[619] | 135 | } |
---|
| 136 | else if ("NCLeftClickCoordinates".equals(eventName)) { |
---|
[946] | 137 | return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1); |
---|
[619] | 138 | } |
---|
| 139 | else if ("NCLeftClickCoordinates2".equals(eventName)) { |
---|
[946] | 140 | return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1); |
---|
[619] | 141 | } |
---|
| 142 | else if ("LeftClickCoordinatesTargetChanged".equals(eventName)) { |
---|
[946] | 143 | return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1); |
---|
[619] | 144 | } |
---|
| 145 | else if ("LeftClickCoordinatesTargetChanged2".equals(eventName)) { |
---|
[946] | 146 | return new MouseClick(MouseButtonInteraction.Button.LEFT, -1, -1); |
---|
[619] | 147 | } |
---|
| 148 | else { |
---|
| 149 | throw new IllegalArgumentException("unknown event name: " + eventName); |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | /** |
---|
[837] | 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 |
---|
[619] | 164 | */ |
---|
[837] | 165 | private VirtualKey getKey(Map<String, String> messageParameters) |
---|
| 166 | throws IllegalArgumentException |
---|
| 167 | { |
---|
[619] | 168 | String value = null; |
---|
[837] | 169 | |
---|
[619] | 170 | if (messageParameters != null) { |
---|
| 171 | value = messageParameters.get("key"); |
---|
| 172 | } |
---|
[837] | 173 | |
---|
[619] | 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> |
---|
[837] | 185 | * If the message parameters contain information about a scroll position, the respective |
---|
| 186 | * position is returned. |
---|
[619] | 187 | * </p> |
---|
[837] | 188 | * |
---|
[619] | 189 | * @param messageParameters |
---|
[837] | 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 |
---|
[619] | 195 | */ |
---|
[837] | 196 | private int getSelectedValue(Map<String, String> messageParameters) |
---|
| 197 | throws IllegalArgumentException |
---|
| 198 | { |
---|
[619] | 199 | String value = null; |
---|
[837] | 200 | |
---|
[619] | 201 | if (messageParameters != null) { |
---|
| 202 | value = messageParameters.get("scrollPos"); |
---|
| 203 | } |
---|
[837] | 204 | |
---|
[619] | 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 | } |
---|