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

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