[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 | |
---|
[774] | 17 | import java.io.Serializable; |
---|
[619] | 18 | import java.util.HashMap; |
---|
| 19 | import java.util.Map; |
---|
| 20 | |
---|
[922] | 21 | import de.ugoe.cs.autoquest.plugin.mfc.guimodel.MFCGUIElement; |
---|
[619] | 22 | |
---|
| 23 | /** |
---|
| 24 | * <p> |
---|
| 25 | * Contains all informations about a windows message, i.e., all parameters that are read when a |
---|
| 26 | * windows message is parsed as well as its target, hwnd, etc. |
---|
| 27 | * </p> |
---|
| 28 | * |
---|
| 29 | * @author Steffen Herbold |
---|
| 30 | * @version 1.0 |
---|
| 31 | * |
---|
| 32 | */ |
---|
[837] | 33 | public class WindowsMessage implements Serializable { |
---|
[619] | 34 | |
---|
| 35 | /** |
---|
| 36 | * <p> |
---|
[774] | 37 | * Id for object serialization. |
---|
| 38 | * </p> |
---|
| 39 | */ |
---|
| 40 | private static final long serialVersionUID = 1L; |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * <p> |
---|
[619] | 44 | * Type of the message. |
---|
| 45 | * </p> |
---|
| 46 | */ |
---|
| 47 | final WindowsMessageType type; |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * <p> |
---|
| 51 | * LPARAM of the message. Default: 0 |
---|
| 52 | * </p> |
---|
| 53 | */ |
---|
| 54 | private long LPARAM = 0; |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * <p> |
---|
| 58 | * WPARAM of the message. Default: 0 |
---|
| 59 | * </p> |
---|
| 60 | */ |
---|
| 61 | private long WPARAM = 0; |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * <p> |
---|
| 65 | * A map of all parameters, associated with the message, created during the parsing of messages |
---|
| 66 | * from the logs {@code param}-nodes. |
---|
| 67 | * </p> |
---|
| 68 | */ |
---|
| 69 | private Map<String, Object> params = new HashMap<String, Object>(); |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | * <p> |
---|
| 73 | * the target GUI element to which the message was sent |
---|
| 74 | * </p> |
---|
| 75 | */ |
---|
| 76 | private MFCGUIElement target; |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | * <p> |
---|
| 80 | * an XML representation of the target to preserve it as it was when this message was created |
---|
| 81 | * </p> |
---|
| 82 | */ |
---|
| 83 | protected String targetXML; |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | * <p> |
---|
[849] | 87 | * Constructor. Creates a new message with a given message's type, target, and parameters. |
---|
[619] | 88 | * </p> |
---|
| 89 | * |
---|
| 90 | * @param type |
---|
| 91 | * type of the message |
---|
[837] | 92 | * @param target |
---|
[849] | 93 | * target of the message |
---|
| 94 | * @param messageParameters |
---|
| 95 | * parameters of the message |
---|
[619] | 96 | */ |
---|
[837] | 97 | public WindowsMessage(WindowsMessageType type, |
---|
| 98 | MFCGUIElement target, |
---|
[619] | 99 | Map<String, Object> messageParameters) |
---|
| 100 | { |
---|
| 101 | this.type = type; |
---|
| 102 | setTarget(target); |
---|
[837] | 103 | |
---|
[619] | 104 | for (Map.Entry<String, Object> entry : messageParameters.entrySet()) { |
---|
| 105 | addParameter(entry.getKey(), entry.getValue()); |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /** |
---|
| 110 | * <p> |
---|
| 111 | * Constructor. Creates a new message with a given message type. |
---|
| 112 | * </p> |
---|
| 113 | * |
---|
| 114 | * @param type |
---|
| 115 | * type of the message |
---|
| 116 | */ |
---|
[837] | 117 | public WindowsMessage(WindowsMessageType type) { |
---|
[619] | 118 | this.type = type; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | /** |
---|
| 122 | * <p> |
---|
| 123 | * Adds a parameter to the message. |
---|
| 124 | * </p> |
---|
| 125 | * |
---|
| 126 | * @param type |
---|
| 127 | * type descriptor of the parameter |
---|
| 128 | * @param value |
---|
| 129 | * value of the parameter |
---|
| 130 | */ |
---|
| 131 | public void addParameter(String type, Object value) { |
---|
| 132 | params.put(type, value); |
---|
| 133 | if (type.equals("LPARAM")) { |
---|
| 134 | LPARAM = (Long) value; |
---|
| 135 | } |
---|
| 136 | else if (type.equals("WPARAM")) { |
---|
| 137 | WPARAM = (Long) value; |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /** |
---|
| 142 | * <p> |
---|
| 143 | * Returns the type of the message. |
---|
| 144 | * </p> |
---|
| 145 | * |
---|
| 146 | * @return type of the message |
---|
| 147 | */ |
---|
| 148 | public WindowsMessageType getType() { |
---|
| 149 | return type; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | /** |
---|
| 153 | * <p> |
---|
[837] | 154 | * Sets the message target. |
---|
[619] | 155 | * </p> |
---|
[837] | 156 | * |
---|
| 157 | * @param target |
---|
| 158 | * the target |
---|
[619] | 159 | */ |
---|
| 160 | public void setTarget(MFCGUIElement target) { |
---|
| 161 | this.target = target; |
---|
| 162 | this.targetXML = target.toXML(); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | /** |
---|
| 166 | * <p> |
---|
[837] | 167 | * Returns the target of the message. |
---|
[619] | 168 | * </p> |
---|
[837] | 169 | * |
---|
| 170 | * @return the target |
---|
[619] | 171 | */ |
---|
| 172 | public MFCGUIElement getTarget() { |
---|
| 173 | return target; |
---|
| 174 | } |
---|
[837] | 175 | |
---|
[619] | 176 | /** |
---|
| 177 | * <p> |
---|
| 178 | * Returns the value of a parameter, given its type. If the parameter is not found, {@code null} |
---|
| 179 | * is returned. |
---|
| 180 | * </p> |
---|
| 181 | * |
---|
| 182 | * @param type |
---|
| 183 | * type of the parameter |
---|
| 184 | * @return value of the parameter |
---|
| 185 | */ |
---|
| 186 | public Object getParameter(String type) { |
---|
| 187 | return params.get(type); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /** |
---|
| 191 | * <p> |
---|
[849] | 192 | * Two {@link WindowsMessage} are equal, if their {@link #type}, {@link #getTargetXML}, |
---|
[619] | 193 | * and {@link #params} are equal. |
---|
| 194 | * </p> |
---|
| 195 | * |
---|
| 196 | * @see java.lang.Object#equals(java.lang.Object) |
---|
| 197 | */ |
---|
| 198 | @Override |
---|
| 199 | public boolean equals(Object other) { |
---|
| 200 | if (other == this) { |
---|
| 201 | return true; |
---|
| 202 | } |
---|
| 203 | boolean isEqual = false; |
---|
| 204 | if (other instanceof WindowsMessage) { |
---|
| 205 | isEqual = |
---|
| 206 | ((WindowsMessage) other).type == this.type && |
---|
[837] | 207 | ((WindowsMessage) other).target.equals(this.target) && |
---|
| 208 | ((WindowsMessage) other).params.equals(this.params); |
---|
[619] | 209 | } |
---|
| 210 | return isEqual; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | /* |
---|
| 214 | * (non-Javadoc) |
---|
| 215 | * |
---|
| 216 | * @see java.lang.Object#hashCode() |
---|
| 217 | */ |
---|
| 218 | @Override |
---|
| 219 | public int hashCode() { |
---|
| 220 | int multiplier = 17; |
---|
| 221 | int hash = 42; |
---|
| 222 | |
---|
| 223 | hash = multiplier * hash + type.hashCode(); |
---|
| 224 | hash = multiplier * hash + target.hashCode(); |
---|
| 225 | hash = multiplier * hash + params.hashCode(); |
---|
| 226 | |
---|
| 227 | return hash; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | /** |
---|
| 231 | * <p> |
---|
| 232 | * Returns a string representation of the message of the form "msg[target=HWND;type=TYPE]". |
---|
| 233 | * </p> |
---|
| 234 | * |
---|
| 235 | * @see java.lang.Object#toString() |
---|
| 236 | */ |
---|
| 237 | @Override |
---|
| 238 | public String toString() { |
---|
| 239 | return "msg[target=" + getParameter("window.hwnd") + ";type=" + type + "]"; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | /** |
---|
| 243 | * <p> |
---|
| 244 | * Returns the LPARAM of a message. |
---|
| 245 | * </p> |
---|
| 246 | * |
---|
| 247 | * @return LPARAM of the message |
---|
| 248 | */ |
---|
| 249 | public long getLPARAM() { |
---|
| 250 | return LPARAM; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | /** |
---|
| 254 | * <p> |
---|
| 255 | * Returns the WPARAM of a message. |
---|
| 256 | * </p> |
---|
| 257 | * |
---|
| 258 | * @return WPARAM of the message |
---|
| 259 | */ |
---|
| 260 | public long getWPARAM() { |
---|
| 261 | return WPARAM; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | /** |
---|
| 265 | * <p> |
---|
| 266 | * Returns the number of parameters stored together with this message. |
---|
| 267 | * </p> |
---|
| 268 | * |
---|
| 269 | * @return number of parameters stored with this message |
---|
| 270 | */ |
---|
| 271 | public int getNumParams() { |
---|
| 272 | return params.size(); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | /** |
---|
| 276 | * <p> |
---|
[837] | 277 | * Returns the parameters associated with this message. |
---|
[619] | 278 | * </p> |
---|
[837] | 279 | * |
---|
| 280 | * @return the parameters |
---|
[619] | 281 | */ |
---|
| 282 | protected Map<String, Object> getParameters() { |
---|
| 283 | return params; |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | /** |
---|
| 287 | * <p> |
---|
[837] | 288 | * Returns the XML target description of this message. |
---|
[619] | 289 | * </p> |
---|
[837] | 290 | * |
---|
| 291 | * @return the XML target description |
---|
[619] | 292 | */ |
---|
| 293 | public String getTargetXML() { |
---|
| 294 | return targetXML; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | } |
---|