source: trunk/autoquest-plugin-mfc/src/main/java/de/ugoe/cs/autoquest/plugin/mfc/eventcore/WindowsMessage.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: 6.9 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.io.Serializable;
18import java.util.HashMap;
19import java.util.Map;
20
21import de.ugoe.cs.autoquest.plugin.mfc.guimodel.MFCGUIElement;
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 */
33public class WindowsMessage implements Serializable {
34
35    /**
36     * <p>
37     * Id for object serialization.
38     * </p>
39     */
40    private static final long serialVersionUID = 1L;
41
42    /**
43     * <p>
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>
87     * Constructor. Creates a new message with a given message's type, target, and parameters.
88     * </p>
89     *
90     * @param type
91     *            type of the message
92     * @param target
93     *            target of the message
94     * @param messageParameters
95     *            parameters of the message
96     */
97    public WindowsMessage(WindowsMessageType type,
98                          MFCGUIElement target,
99                          Map<String, Object> messageParameters)
100    {
101        this.type = type;
102        setTarget(target);
103
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     */
117    public WindowsMessage(WindowsMessageType type) {
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>
154     * Sets the message target.
155     * </p>
156     *
157     * @param target
158     *            the target
159     */
160    public void setTarget(MFCGUIElement target) {
161        this.target = target;
162        this.targetXML = target.toXML();
163    }
164
165    /**
166     * <p>
167     * Returns the target of the message.
168     * </p>
169     *
170     * @return the target
171     */
172    public MFCGUIElement getTarget() {
173        return target;
174    }
175
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>
192     * Two {@link WindowsMessage} are equal, if their {@link #type}, {@link #getTargetXML},
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 &&
207                    ((WindowsMessage) other).target.equals(this.target) &&
208                    ((WindowsMessage) other).params.equals(this.params);
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>
277     * Returns the parameters associated with this message.
278     * </p>
279     *
280     * @return the parameters
281     */
282    protected Map<String, Object> getParameters() {
283        return params;
284    }
285
286    /**
287     * <p>
288     * Returns the XML target description of this message.
289     * </p>
290     *
291     * @return the XML target description
292     */
293    public String getTargetXML() {
294        return targetXML;
295    }
296
297}
Note: See TracBrowser for help on using the repository browser.