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
RevLine 
[927]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.
[837]14
[922]15package de.ugoe.cs.autoquest.plugin.mfc.guimodel;
[619]16
[1]17import java.util.ArrayList;
18import java.util.List;
19
[2146]20import de.ugoe.cs.autoquest.eventcore.IEventTargetSpec;
[922]21import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec;
[1]22import de.ugoe.cs.util.StringTools;
23
24/**
25 * <p>
[940]26 * This class implements a node in the {@link MFCWindowTree} that is maintained during parsing a
[619]27 * session.
[1]28 * </p>
29 * <p>
[619]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.
[1]32 * </p>
33 *
34 * @author Steffen Herbold
[171]35 * @version 1.0
[1]36 */
[619]37public class MFCGUIElementSpec implements IGUIElementSpec {
[1]38
[619]39    /**
40     * <p>
[778]41     * Id for object serialization.
42     * </p>
43     */
44    private static final long serialVersionUID = 1L;
45
46    /**
47     * <p>
[619]48     * current name of the window
49     * </p>
50     */
51    private String name;
[1]52
[619]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>();
[1]59
[619]60    /**
61     * <p>
62     * Handle of the window. Used as unique identifier during its existence.
63     * </p>
64     */
65    private long hwnd;
[1]66
[619]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>();
[1]73
[619]74    /**
75     * <p>
76     * Resource id of the window.
77     * </p>
78     */
79    private final int resourceId;
[1]80
[619]81    /**
82     * <p>
83     * type (class name) of the window.
84     * </p>
85     */
86    private final String type;
[1]87
[619]88    /**
89     * <p>
90     * True, if the window is modal.
91     * </p>
92     */
93    private final boolean isModal;
[171]94
[619]95    /**
96     * <p>
[1006]97     * Creates a new MFCGUIElementSpec.
[619]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     */
[1006]111    public MFCGUIElementSpec(long hwnd, String name, int resourceId, String type, boolean isModal)
[619]112    {
113        this.hwnd = hwnd;
114        this.name = name;
115        this.resourceId = resourceId;
116        this.type = type;
117        this.isModal = isModal;
118    }
[1]119
[619]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();
[837]129
[619]130        if (name != null) {
131            names.append('"');
132            names.append(name);
133            names.append('"');
134        }
135        else {
136            names.append("NOT_SET");
137        }
[837]138
[619]139        if (formerNames.size() > 0) {
[837]140
[619]141            names.append(" (aka ");
[837]142
[619]143            for (int i = 0; i < formerNames.size(); i++) {
144                if (i > 0) {
145                    names.append("/");
146                }
[1]147
[619]148                names.append('"');
149                names.append(formerNames.get(i));
150                names.append('"');
151            }
[837]152
[619]153            names.append(")");
154        }
[837]155
[619]156        return names.toString();
157    }
[1]158
[619]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    }
[1]169
[619]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    }
[1]180
[837]181    /*
182     * (non-Javadoc)
183     *
[922]184     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#getType()
[619]185     */
186    @Override
187    public String getType() {
188        return type;
189    }
[1]190
[619]191    /**
192     * <p>
[837]193     * Returns the modality of the specified GUI element.
[619]194     * </p>
[837]195     *
196     * @return the modality
[619]197     */
198    public boolean isModal() {
199        return isModal;
200    }
[1]201
[619]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) {
[837]211        if ((this.name != null) && (!this.name.equals(newName)) &&
[619]212            (!this.formerNames.contains(this.name)))
213        {
214            this.formerNames.add(this.name);
215        }
[837]216
[619]217        this.name = newName;
218    }
[1]219
[619]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        }
[837]232
[619]233        this.hwnd = newHwnd;
234    }
[1]235
[837]236    /*
237     * (non-Javadoc)
238     *
239     * @see
[922]240     * de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#getSimilarity(de.ugoe.cs.autoquest.eventcore
[837]241     * .guimodel.IGUIElementSpec)
[619]242     */
243    @Override
[2146]244    public boolean getSimilarity(IEventTargetSpec other) {
[837]245
[619]246        if (this == other) {
247            return true;
248        }
[837]249
[619]250        if (!(other instanceof MFCGUIElementSpec)) {
251            return false;
252        }
[837]253
[619]254        MFCGUIElementSpec otherSpec = (MFCGUIElementSpec) other;
[1]255
[1184]256        if ((type == null) ? otherSpec.type != null : !type.equals(otherSpec.type)) {
[619]257            return false;
258        }
[1]259
[619]260        if (isModal != otherSpec.isModal) {
261            return false;
262        }
[1]263
[619]264        if (resourceId != otherSpec.resourceId) {
265            return false;
266        }
[171]267
[619]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
[837]273
[619]274        if (hwnd == otherSpec.hwnd) {
275            return true;
276        }
[837]277
[619]278        if ((name != null) && (name.equals(otherSpec.name))) {
279            return true;
280        }
[837]281
282        if ((((name == null) && (otherSpec.name == null)) || (("".equals(name)) && (""
283            .equals(otherSpec.name)))) &&
284            (formerNames.size() == 0) &&
285            (otherSpec.formerNames.size() == 0))
[619]286        {
287            return true;
288        }
[837]289
[619]290        // if the hwnd and the name did not stay the same, then the name should be checked first.
[715]291        // The current name of one of the specs must be contained in the former names of the
[619]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.
[1]297
[715]298        if ((otherSpec.name != null) && formerNames.contains(otherSpec.name)) {
[619]299            return true;
300        }
301
302        if ((name != null) && otherSpec.formerNames.contains(name)) {
303            return true;
304        }
[837]305
[715]306        // ok. Even the names do not match. This is usually a clear indication, that the elements
[619]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
[837]316
[619]317        return false;
318    }
319
[837]320    /*
321     * (non-Javadoc)
322     *
[922]323     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#equals(IGUIElementSpec)
[619]324     */
325    @Override
[832]326    public boolean equals(Object other) {
[837]327
[619]328        if (this == other) {
329            return true;
330        }
[837]331
[619]332        if (!(other instanceof MFCGUIElementSpec)) {
333            return false;
334        }
[837]335
[619]336        MFCGUIElementSpec otherSpec = (MFCGUIElementSpec) other;
[837]337
338        return (hwnd == otherSpec.hwnd) && (isModal == otherSpec.isModal) &&
[619]339            (resourceId == otherSpec.resourceId) &&
[1184]340            (type == null ? otherSpec.type == null : type.equals(otherSpec.type)) &&
341            (name == null ? otherSpec.name == null : name.equals(otherSpec.name));
[619]342    }
343
[837]344    /*
345     * (non-Javadoc)
346     *
[619]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>
[837]357     * Returns a string identifier of the window:<br>
[619]358     * {@code [resourceId;"windowName";"className";modality]}
359     * </p>
360     *
361     * @return identifier string of the window
362     */
363    @Override
364    public String toString() {
[837]365        return "[" + resourceId + ";" + getName() + ";\"" + type + "\";" + isModal + ";" + hwnd +
366            "]";
[619]367    }
368
369    /**
370     * <p>
[837]371     * Returns the XML representation of this specification.
[619]372     * </p>
[837]373     *
374     * @return the XML representation
[619]375     */
376    String toXML() {
[837]377        return "<window name=\"" + (name != null ? StringTools.xmlEntityReplacement(name) : "") +
378            "\" class=\"" + StringTools.xmlEntityReplacement(type) + "\" resourceId=\"" +
379            resourceId + "\" isModal=\"" + isModal + "\"/>";
[619]380    }
381
382    /**
383     * <p>
[837]384     * Updates the specification with another specification.
[619]385     * </p>
[837]386     *
[619]387     * @param furtherSpec
[837]388     *            specification used to update the current specification
[619]389     */
390    void update(IGUIElementSpec furtherSpec) {
391        MFCGUIElementSpec other = (MFCGUIElementSpec) furtherSpec;
[837]392
[619]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
[1184]406            if (name == null ? other.name != null : !name.equals(other.name)) {
[619]407                setName(other.name);
408            }
409        }
410    }
411
[966]412    @Override
[990]413    public String[] getTypeHierarchy() {
414        return new String[] { type };
[966]415    }
416
[1]417}
Note: See TracBrowser for help on using the repository browser.