source: trunk/quest-ui-core/src/main/java/de/ugoe/cs/quest/plugin/mfc/eventcore/WindowsMessage.java @ 566

Last change on this file since 566 was 566, checked in by sherbold, 12 years ago
  • adapted MFC plugin to new event core
File size: 11.5 KB
Line 
1package de.ugoe.cs.quest.plugin.mfc.eventcore;
2
3import java.security.InvalidParameterException;
4import java.util.HashMap;
5import java.util.Map;
6
7import de.ugoe.cs.quest.IReplayDecorator;
8import de.ugoe.cs.quest.eventcore.IReplayable;
9import de.ugoe.cs.quest.plugin.mfc.MFCReplayDecorator;
10import de.ugoe.cs.util.StringTools;
11
12/**
13 * <p>
14 * Contains all informations about a windows message, i.e., all parameters that
15 * are read when a windows message is parsed as well as its target, hwnd, etc.
16 * </p>
17 *
18 * @author Steffen Herbold
19 * @version 1.0
20 *
21 */
22public class WindowsMessage implements IReplayable {
23
24        /**
25         * <p>
26         * Id for object serialization.
27         * </p>
28         */
29        private static final long serialVersionUID = 1L;
30
31        /**
32         * <p>
33         * Type of the message.
34         * </p>
35         */
36        final int type;
37
38        /**
39         * <p>
40         * Window class of the message target. Default: ""
41         * </p>
42         */
43        private String windowClass = "";
44
45        /**
46         * <p>
47         * Resource Id of the message target. Default: 0
48         * </p>
49         */
50        private int resourceId = 0;
51
52        /**
53         * <p>
54         * XML representation of the message target.
55         * </p>
56         */
57        private String xmlWindowDescription = "";
58
59        /**
60         * <p>
61         * String that contains the names of all parent widgets and itself, separated by dots,
62         * e.g., "GrandParent.Parent.self".
63         * </p>
64         */
65        private String parentNames = null;
66
67        /**
68         * <p>
69         * String that contains the window class of the parent widget.
70         * </p>
71         */
72        private String parentClass = null;
73
74        /**
75         * <p>
76         * LPARAM of the message. Default: 0
77         * </p>
78         */
79        private long LPARAM = 0;
80
81        /**
82         * <p>
83         * WPARAM of the message. Default: 0
84         * </p>
85         */
86        private long WPARAM = 0;
87
88        /**
89         * <p>
90         * If the LPARAM contains a HWND, this string stores the target of the HWND.
91         * </p>
92         */
93        private String LPARAMasWindowDesc = null;
94
95        /**
96         * <p>
97         * If the WPARAM contains a HWND, this string stores the target of the HWND.
98         * </p>
99         */
100        private String WPARAMasWindowDesc = null;
101
102        /**
103         * <p>
104         * Delay after sending the messages during a replay. Default: 0
105         * </p>
106         */
107        private int delay = 0;
108
109        /**
110         * <p>
111         * A map of all parameters, associated with the message, created during the
112         * parsing of messages from the logs {@code param}-nodes.
113         * </p>
114         */
115        private Map<String, String> params = new HashMap<String, String>();
116
117        /**
118         * <p>
119         * Constructor. Creates a new message with a given message type.
120         * </p>
121         *
122         * @param type
123         *            type of the message
124         */
125        public WindowsMessage(int type) {
126                this.type = type;
127        }
128
129        /**
130         * <p>
131         * Adds a parameter to the message.
132         * </p>
133         *
134         * @param type
135         *            type descriptor of the parameter
136         * @param value
137         *            value of the parameter
138         */
139        public void addParameter(String type, String value) {
140                params.put(type, value);
141                if (type.equals("LPARAM")) {
142                        LPARAM = Long.parseLong(value);
143                } else if (type.equals("WPARAM")) {
144                        WPARAM = Long.parseLong(value);
145                }
146        }
147
148        /**
149         * <p>
150         * Returns the type of the message.
151         * </p>
152         *
153         * @return type of the message
154         */
155        public int getType() {
156                return type;
157        }
158
159        /**
160         * <p>
161         * Returns the value of a parameter, given its type. If the parameter is not
162         * found, {@code null} is returned.
163         * </p>
164         *
165         * @param type
166         *            type of the parameter
167         * @return value of the parameter
168         */
169        public String getParameter(String type) {
170                return params.get(type);
171        }
172
173        /**
174         * <p>
175         * Returns the window class of the message target.
176         * </p>
177         *
178         * @return window class of the message target
179         */
180        public String getWindowClass() {
181                return windowClass;
182        }
183
184        /**
185         * <p>
186         * Returns the HWND the message is addressed to.
187         * </p>
188         *
189         * @return HWND the message is addressed to
190         */
191        public int getHwnd() {
192                int hwnd = -1;
193                String hwndString = getParameter("window.hwnd"); // possible, as
194                                                                                                                        // "window.hwnd" is
195                                                                                                                        // mandatory
196                if (hwndString != null) {
197                        hwnd = Integer.parseInt(hwndString);
198                }
199                return hwnd;
200        }
201
202        /**
203         * <p>
204         * Returns the resource Id of the message target.
205         * </p>
206         *
207         * @return resource Id of the message target
208         */
209        public int getWindowResourceId() {
210                return resourceId;
211        }
212
213        /**
214         * <p>
215         * Two {@link WindowsMessage} are equal, if their {@link #type},
216         * {@link #xmlWindowDescription}, and {@link #params} are equal.
217         * </p>
218         *
219         * @see java.lang.Object#equals(java.lang.Object)
220         */
221        @Override
222        public boolean equals(Object other) {
223                if (other == this) {
224                        return true;
225                }
226                boolean isEqual = false;
227                if (other instanceof WindowsMessage) {
228                        isEqual = ((WindowsMessage) other).type == this.type
229                                        && ((WindowsMessage) other).xmlWindowDescription
230                                                        .equals(this.xmlWindowDescription)
231                                        && ((WindowsMessage) other).params.equals(this.params);
232                }
233                return isEqual;
234        }
235
236        /*
237         * (non-Javadoc)
238         *
239         * @see java.lang.Object#hashCode()
240         */
241        @Override
242        public int hashCode() {
243                int multiplier = 17;
244                int hash = 42;
245
246                hash = multiplier * hash + type;
247                hash = multiplier * hash + xmlWindowDescription.hashCode();
248                hash = multiplier * hash + params.hashCode();
249
250                return hash;
251        }
252
253        /**
254         * <p>
255         * Returns a string representation of the message of the form
256         * "msg[target=HWND;type=TYPE]".
257         * </p>
258         *
259         * @see java.lang.Object#toString()
260         */
261        @Override
262        public String toString() {
263                return "msg[target=" + getParameter("window.hwnd") + ";type=" + type
264                                + "]";
265        }
266
267        /**
268         * <p>
269         * Retrieves the target string of a message from a given {@link WindowTree}
270         * through looking up the HWND the message is addressed to in the window
271         * tree.
272         * </p>
273         *
274         * @param windowTree
275         *            {@link WindowTree} from which the target is extracted
276         * @throws InvalidParameterException
277         *             thrown if HWND is not contained in windowTree
278         */
279        public void setTarget(WindowTree windowTree)
280                        throws InvalidParameterException {
281                int hwnd = Integer.parseInt(getParameter("window.hwnd"));
282                WindowTreeNode node = windowTree.find(hwnd);
283                if (node == null) {
284                        throw new InvalidParameterException("No window with HWND " + hwnd
285                                        + " found in window tree!");
286                } else {
287                        windowClass = node.getClassName();
288                        resourceId = node.getResourceId();
289                        xmlWindowDescription = node.xmlRepresentation();
290                        parentNames = node.getParentNames();
291                        WindowTreeNode parent = node.getParent();
292                        if (parent == null) {
293                                parentClass = "";
294                        } else {
295                                parentClass = parent.getClassName();
296                        }
297                }
298        }
299
300        /**
301         * <p>
302         * Sets the LPARAM of a message.
303         * </p>
304         *
305         * @param paramValue
306         *            value of the LPARAM
307         */
308        public void setLPARAM(long paramValue) {
309                LPARAM = paramValue;
310        }
311
312        /**
313         * <p>
314         * Sets the WPARAM of a message.
315         * </p>
316         *
317         * @param paramValue
318         *            value of the WPARAM
319         */
320        public void setWPARAM(long paramValue) {
321                WPARAM = paramValue;
322        }
323
324        /**
325         * <p>
326         * Returns the LPARAM of a message.
327         * </p>
328         *
329         * @return LPARAM of the message
330         */
331        public long getLPARAM() {
332                return LPARAM;
333        }
334
335        /**
336         * <p>
337         * Returns the WPARAM of a message.
338         * </p>
339         *
340         * @return WPARAM of the message
341         */
342        public long getWPARAM() {
343                return WPARAM;
344        }
345
346        /**
347         * <p>
348         * If the LPARAM contains a HWND, this function can be used to set a target
349         * string to identify the HWND at run-time.
350         * </p>
351         *
352         * @param windowDesc
353         *            target string
354         */
355        public void setLPARAMasWindowDesc(String windowDesc) {
356                LPARAMasWindowDesc = windowDesc;
357        }
358
359        /**
360         * <p>
361         * If the WPARAM contains a HWND, this function can be used to set a target
362         * string to identify the HWND at run-time.
363         * </p>
364         *
365         * @param windowDesc
366         *            target string
367         */
368        public void setWPARAMasWindowDesc(String windowDesc) {
369                WPARAMasWindowDesc = windowDesc;
370        }
371
372        /**
373         * <p>
374         * If the LPARAM contains a HWND and the target string for the HWND is set,
375         * this function returns the target string. Otherwise, {@code null} is
376         * returned.
377         * </p>
378         *
379         * @return target string if available; {@code null} otherwise
380         */
381        public String getLPARAMasWindowDesc() {
382                return LPARAMasWindowDesc;
383        }
384
385        /**
386         * <p>
387         * If the WPARAM contains a HWND and the target string for the HWND is set,
388         * this function returns the target string. Otherwise, {@code null} is
389         * returned.
390         * </p>
391         *
392         * @return target string if available; {@code null} otherwise
393         */
394        public String getWPARAMasWindowDesc() {
395                return WPARAMasWindowDesc;
396        }
397
398        /**
399         * <p>
400         * Returns the target string of the message.
401         * </p>
402         *
403         * @return target string of the message
404         */
405        public String getXmlWindowDescription() {
406                return xmlWindowDescription;
407        }
408
409        /**
410         * <p>
411         * Sets the target string manually.
412         * </p>
413         *
414         * @param xmlWindowDescription
415         *            target string
416         */
417        public void setXmlWindowDescription(String xmlWindowDescription) {
418                this.xmlWindowDescription = xmlWindowDescription;
419        }
420
421        /**
422         * <p>
423         * Returns the delay after this message during replays.
424         * </p>
425         *
426         * @return delay after this message
427         */
428        public int getDelay() {
429                return delay;
430        }
431
432        /**
433         * <p>
434         * Sets the delay after this message during replays.
435         * </p>
436         *
437         * @param delay
438         *            delay after this message
439         */
440        public void setDelay(int delay) {
441                this.delay = delay;
442        }
443
444        /**
445         * <p>
446         * Returns the parent names separated by dots, e.g., "GrandParent.Parent".
447         * </p>
448         *
449         * @return names of the parents
450         */
451        public String getParentNames() {
452                return parentNames;
453        }
454
455        /**
456         * <p>
457         * Returns the window class of the parent.
458         * </p>
459         *
460         * @return window classes of the parents
461         */
462        public String getParentClass() {
463                return parentClass;
464        }
465
466        /**
467         * <p>
468         * Returns the number of parameters stored together with this message.
469         * </p>
470         *
471         * @return number of parameters stored with this message
472         */
473        public int getNumParams() {
474                return params.size();
475        }
476
477        /*
478         * (non-Javadoc)
479         *
480         * @see de.ugoe.cs.quest.eventcore.IReplayable#getReplay()
481         */
482        @Override
483        public String getReplay() {
484                StringBuilder currentMsgStr = new StringBuilder(400);
485                currentMsgStr.append("  <msg type=\"" + type + "\" ");
486                currentMsgStr.append("LPARAM=\"" + LPARAM + "\" ");
487                currentMsgStr.append("WPARAM=\"" + WPARAM + "\" ");
488                currentMsgStr.append("delay=\"" + delay + "\">");
489                if (LPARAMasWindowDesc != null) {
490                        currentMsgStr.append(StringTools.ENDLINE);
491                        currentMsgStr.append("   <LPARAM>");
492                        currentMsgStr.append(StringTools.ENDLINE);
493                        currentMsgStr.append(LPARAMasWindowDesc);
494                        currentMsgStr.append(StringTools.ENDLINE);
495                        currentMsgStr.append("</LPARAM>");
496                }
497                if (WPARAMasWindowDesc != null) {
498                        currentMsgStr.append(StringTools.ENDLINE);
499                        currentMsgStr.append("   <WPARAM>");
500                        currentMsgStr.append(StringTools.ENDLINE);
501                        currentMsgStr.append(WPARAMasWindowDesc);
502                        currentMsgStr.append(StringTools.ENDLINE);
503                        currentMsgStr.append("   </WPARAM>");
504                }
505                currentMsgStr.append(StringTools.ENDLINE);
506                currentMsgStr.append(xmlWindowDescription);
507                currentMsgStr.append(StringTools.ENDLINE);
508                currentMsgStr.append("  </msg>");
509                currentMsgStr.append(StringTools.ENDLINE);
510                return currentMsgStr.toString();
511        }
512
513    /* (non-Javadoc)
514     * @see de.ugoe.cs.quest.eventcore.IReplayable#getDecorator()
515     */
516    @Override
517    public IReplayDecorator getDecorator() {
518        return MFCReplayDecorator.getInstance();
519    }
520}
Note: See TracBrowser for help on using the repository browser.