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

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