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

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