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

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