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

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