source: trunk/quest-plugin-mfc/src/main/java/de/ugoe/cs/quest/plugin/mfc/guimodel/MFCGUIElementSpec.java @ 832

Last change on this file since 832 was 832, checked in by sherbold, 12 years ago
  • corrected signature of JFCGUIElementSpec and MFCGUIElementSpec
File size: 10.8 KB
Line 
1package de.ugoe.cs.quest.plugin.mfc.guimodel;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import de.ugoe.cs.quest.eventcore.guimodel.IGUIElementSpec;
7import de.ugoe.cs.util.StringTools;
8
9/**
10 * <p>
11 * This class implements a node in the {@link WindowTree} that is maintained during parsing a
12 * session.
13 * </p>
14 * <p>
15 * The window tree is structure that contains the hierarchy of the windows of a application as well
16 * as basic information about each window: the hwnd; its name; its resource id; its class name.
17 * </p>
18 *
19 * @author Steffen Herbold
20 * @version 1.0
21 */
22public class MFCGUIElementSpec implements IGUIElementSpec {
23
24    /**
25     * <p>
26     * Id for object serialization.
27     * </p>
28     */
29    private static final long serialVersionUID = 1L;
30
31    /**
32     * <p>
33     * current name of the window
34     * </p>
35     */
36    private String name;
37
38    /**
39     * <p>
40     * previous names of the window as it may have changed over time.
41     * </p>
42     */
43    private List<String> formerNames = new ArrayList<String>();
44
45    /**
46     * <p>
47     * Handle of the window. Used as unique identifier during its existence.
48     * </p>
49     */
50    private long hwnd;
51
52    /**
53     * <p>
54     * previous handles of the window as the window may have been destroyed and recreated
55     * </p>
56     */
57    private List<Long> formerHwnds = new ArrayList<Long>();
58
59    /**
60     * <p>
61     * Resource id of the window.
62     * </p>
63     */
64    private final int resourceId;
65
66    /**
67     * <p>
68     * type (class name) of the window.
69     * </p>
70     */
71    private final String type;
72
73    /**
74     * <p>
75     * True, if the window is modal.
76     * </p>
77     */
78    private final boolean isModal;
79
80    /**
81     * <p>
82     * Creates a new WindowTreeNode.
83     * </p>
84     * <p>
85     * The constructor is protected WindowTreeNode may only be created from the WindowTree.
86     * </p>
87     *
88     * @param hwnd
89     *            hwnd of the window
90     * @param parent
91     *            reference to the parent's WindowTreeNode
92     * @param name
93     *            name of the window
94     * @param resourceId
95     *            resource id of the window
96     * @param type
97     *            type, i.e. class name of the window
98     * @param isModal
99     *            modality of the window
100     */
101    protected MFCGUIElementSpec(long    hwnd,
102                                String  name,
103                                int     resourceId,
104                                String  type,
105                                boolean isModal)
106    {
107        this.hwnd = hwnd;
108        this.name = name;
109        this.resourceId = resourceId;
110        this.type = type;
111        this.isModal = isModal;
112    }
113
114    /**
115     * <p>
116     * Returns the name of the window.
117     * </p>
118     *
119     * @return name of the window
120     */
121    public String getName() {
122        StringBuffer names = new StringBuffer();
123       
124        if (name != null) {
125            names.append('"');
126            names.append(name);
127            names.append('"');
128        }
129        else {
130            names.append("NOT_SET");
131        }
132       
133        if (formerNames.size() > 0) {
134           
135            names.append(" (aka ");
136           
137            for (int i = 0; i < formerNames.size(); i++) {
138                if (i > 0) {
139                    names.append("/");
140                }
141
142                names.append('"');
143                names.append(formerNames.get(i));
144                names.append('"');
145            }
146           
147            names.append(")");
148        }
149       
150        return names.toString();
151    }
152
153    /**
154     * <p>
155     * Returns the hwnd of the window.
156     * </p>
157     *
158     * @return hwnd of the window
159     */
160    public long getHwnd() {
161        return hwnd;
162    }
163
164    /**
165     * <p>
166     * Returns the resource id of the window.
167     * </p>
168     *
169     * @return resource id of the window
170     */
171    public int getResourceId() {
172        return resourceId;
173    }
174
175    /* (non-Javadoc)
176     * @see de.ugoe.cs.quest.eventcore.guimodel.IGUIElementSpec#getType()
177     */
178    @Override
179    public String getType() {
180        return type;
181    }
182
183    /**
184     * <p>
185     * TODO: comment
186     * </p>
187     *
188     * @return
189     */
190    public boolean isModal() {
191        return isModal;
192    }
193
194    /**
195     * <p>
196     * Sets the name of the window.
197     * </p>
198     *
199     * @param text
200     *            new name of the window
201     */
202    public void setName(String newName) {
203        if ((this.name != null) &&
204            (!this.name.equals(newName)) &&
205            (!this.formerNames.contains(this.name)))
206        {
207            this.formerNames.add(this.name);
208        }
209       
210        this.name = newName;
211    }
212
213    /**
214     * <p>
215     * Sets the hwnd of the window.
216     * </p>
217     *
218     * @param text
219     *            new name of the window
220     */
221    public void setHwnd(long newHwnd) {
222        if (!this.formerHwnds.contains(this.hwnd)) {
223            this.formerHwnds.add(this.hwnd);
224        }
225       
226        this.hwnd = newHwnd;
227    }
228
229    /* (non-Javadoc)
230     * @see de.ugoe.cs.quest.eventcore.guimodel.IGUIElementSpec#getSimilarity(de.ugoe.cs.quest.eventcore.guimodel.IGUIElementSpec)
231     */
232    @Override
233    public boolean getSimilarity(IGUIElementSpec other) {
234       
235        if (this == other) {
236            return true;
237        }
238       
239        if (!(other instanceof MFCGUIElementSpec)) {
240            return false;
241        }
242       
243        MFCGUIElementSpec otherSpec = (MFCGUIElementSpec) other;
244
245        if ((type != otherSpec.type) && ((type != null) && (!type.equals(otherSpec.type)))) {
246            return false;
247        }
248
249        if (isModal != otherSpec.isModal) {
250            return false;
251        }
252
253        if (resourceId != otherSpec.resourceId) {
254            return false;
255        }
256
257        // up to now, we compared, if the basics match. Now lets compare the id and the
258        // name. Both may change. The name may be reset (e.g. the title of a frame using the
259        // asterisk in the case data was changed). The id may change if e.g. a dialog is closed
260        // and reopend, i.e. a new instance is created. If one of them stays the same, then
261        // similarity is given. Therefore these are the first two comparisons
262       
263        if (hwnd == otherSpec.hwnd) {
264            return true;
265        }
266       
267        if ((name != null) && (name.equals(otherSpec.name))) {
268            return true;
269        }
270       
271        if ((((name == null) && (otherSpec.name == null)) ||
272             (("".equals(name)) && ("".equals(otherSpec.name)))) &&
273            (formerNames.size() == 0) && (otherSpec.formerNames.size() == 0))
274        {
275            return true;
276        }
277       
278        // if the hwnd and the name did not stay the same, then the name should be checked first.
279        // The current name of one of the specs must be contained in the former names of the
280        // respective other spec for similarity. Either of the specs should contain the name of the
281        // respective other spec in its former names. We can rely on this, as in the MFC context
282        // we get to know each name change. I.e. although currently the names of the specs differ,
283        // once they were identical. But it is sufficient to do it for the current names of the
284        // elements, as only one of them may have experienced more name changes then the other.
285
286        if ((otherSpec.name != null) && formerNames.contains(otherSpec.name)) {
287            return true;
288        }
289
290        if ((name != null) && otherSpec.formerNames.contains(name)) {
291            return true;
292        }
293       
294        // ok. Even the names do not match. This is usually a clear indication, that the elements
295        // are distinct. However, we check, if the former handles matched. This is very unlikely
296        // to happen. But it may occur, if a GUI element does not have a name or its name stays
297        // the empty string and if this GUI element is created, destroyed, and created again.
298
299        if (formerHwnds.contains(otherSpec.hwnd) || otherSpec.formerHwnds.contains(hwnd)) {
300            return true;
301        }
302
303        // now we can be really sure, that the GUI elements differ
304       
305        return false;
306    }
307
308    /* (non-Javadoc)
309     * @see de.ugoe.cs.quest.eventcore.guimodel.IGUIElementSpec#equals(IGUIElementSpec)
310     */
311    @Override
312    public boolean equals(Object other) {
313       
314        if (this == other) {
315            return true;
316        }
317       
318        if (!(other instanceof MFCGUIElementSpec)) {
319            return false;
320        }
321       
322        MFCGUIElementSpec otherSpec = (MFCGUIElementSpec) other;
323       
324        return
325            (hwnd == otherSpec.hwnd) && (isModal == otherSpec.isModal) &&
326            (resourceId == otherSpec.resourceId) &&
327            ((type == otherSpec.type) || ((type != null) && (type.equals(otherSpec.type)))) &&
328            ((name == otherSpec.name) || ((name != null) && (name.equals(otherSpec.name))));
329    }
330
331    /* (non-Javadoc)
332     * @see java.lang.Object#hashCode()
333     */
334    @Override
335    public int hashCode() {
336        // reuse only invariable elements
337        return (type + isModal + resourceId).hashCode();
338    }
339
340    /**
341     * <p>
342     * Returns a string identfier of the window:<br>
343     * {@code [resourceId;"windowName";"className";modality]}
344     * </p>
345     *
346     * @return identifier string of the window
347     */
348    @Override
349    public String toString() {
350        return "[" + resourceId + ";" + getName() + ";\"" + type + "\";" + isModal + ";" +
351            hwnd + "]";
352    }
353
354    /**
355     * <p>
356     * TODO: comment
357     * </p>
358     */
359    String toXML() {
360        return
361            "<window name=\"" + (name != null ? StringTools.xmlEntityReplacement(name) : "") +
362            "\" class=\"" + StringTools.xmlEntityReplacement(type) +
363            "\" resourceId=\"" + resourceId + "\" isModal=\"" +
364            isModal + "\"/>";
365    }
366
367    /**
368     * <p>
369     * TODO: comment
370     * </p>
371     *
372     * @param furtherSpec
373     */
374    void update(IGUIElementSpec furtherSpec) {
375        MFCGUIElementSpec other = (MFCGUIElementSpec) furtherSpec;
376       
377        if (other != this) {
378            for (long formerHwnd : other.formerHwnds) {
379                setHwnd(formerHwnd);
380            }
381
382            if (hwnd != other.hwnd) {
383                hwnd = other.hwnd;
384            }
385
386            for (String formerName : other.formerNames) {
387                setName(formerName);
388            }
389
390            if ((name != other.name) && (name != null) && (!name.equals(other.name)))
391            {
392                setName(other.name);
393            }
394        }
395    }
396
397}
Note: See TracBrowser for help on using the repository browser.