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

Last change on this file since 1184 was 1184, checked in by pharms, 11 years ago
  • remove a find bugs warning
File size: 11.3 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>
[1006]96     * Creates a new MFCGUIElementSpec.
[619]97     * </p>
98     *
99     * @param hwnd
100     *            hwnd of the window
101     * @param name
102     *            name of the window
103     * @param resourceId
104     *            resource id of the window
105     * @param type
106     *            type, i.e. class name of the window
107     * @param isModal
108     *            modality of the window
109     */
[1006]110    public MFCGUIElementSpec(long hwnd, String name, int resourceId, String type, boolean isModal)
[619]111    {
112        this.hwnd = hwnd;
113        this.name = name;
114        this.resourceId = resourceId;
115        this.type = type;
116        this.isModal = isModal;
117    }
[1]118
[619]119    /**
120     * <p>
121     * Returns the name of the window.
122     * </p>
123     *
124     * @return name of the window
125     */
126    public String getName() {
127        StringBuffer names = new StringBuffer();
[837]128
[619]129        if (name != null) {
130            names.append('"');
131            names.append(name);
132            names.append('"');
133        }
134        else {
135            names.append("NOT_SET");
136        }
[837]137
[619]138        if (formerNames.size() > 0) {
[837]139
[619]140            names.append(" (aka ");
[837]141
[619]142            for (int i = 0; i < formerNames.size(); i++) {
143                if (i > 0) {
144                    names.append("/");
145                }
[1]146
[619]147                names.append('"');
148                names.append(formerNames.get(i));
149                names.append('"');
150            }
[837]151
[619]152            names.append(")");
153        }
[837]154
[619]155        return names.toString();
156    }
[1]157
[619]158    /**
159     * <p>
160     * Returns the hwnd of the window.
161     * </p>
162     *
163     * @return hwnd of the window
164     */
165    public long getHwnd() {
166        return hwnd;
167    }
[1]168
[619]169    /**
170     * <p>
171     * Returns the resource id of the window.
172     * </p>
173     *
174     * @return resource id of the window
175     */
176    public int getResourceId() {
177        return resourceId;
178    }
[1]179
[837]180    /*
181     * (non-Javadoc)
182     *
[922]183     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#getType()
[619]184     */
185    @Override
186    public String getType() {
187        return type;
188    }
[1]189
[619]190    /**
191     * <p>
[837]192     * Returns the modality of the specified GUI element.
[619]193     * </p>
[837]194     *
195     * @return the modality
[619]196     */
197    public boolean isModal() {
198        return isModal;
199    }
[1]200
[619]201    /**
202     * <p>
203     * Sets the name of the window.
204     * </p>
205     *
206     * @param text
207     *            new name of the window
208     */
209    public void setName(String newName) {
[837]210        if ((this.name != null) && (!this.name.equals(newName)) &&
[619]211            (!this.formerNames.contains(this.name)))
212        {
213            this.formerNames.add(this.name);
214        }
[837]215
[619]216        this.name = newName;
217    }
[1]218
[619]219    /**
220     * <p>
221     * Sets the hwnd of the window.
222     * </p>
223     *
224     * @param text
225     *            new name of the window
226     */
227    public void setHwnd(long newHwnd) {
228        if (!this.formerHwnds.contains(this.hwnd)) {
229            this.formerHwnds.add(this.hwnd);
230        }
[837]231
[619]232        this.hwnd = newHwnd;
233    }
[1]234
[837]235    /*
236     * (non-Javadoc)
237     *
238     * @see
[922]239     * de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#getSimilarity(de.ugoe.cs.autoquest.eventcore
[837]240     * .guimodel.IGUIElementSpec)
[619]241     */
242    @Override
243    public boolean getSimilarity(IGUIElementSpec other) {
[837]244
[619]245        if (this == other) {
246            return true;
247        }
[837]248
[619]249        if (!(other instanceof MFCGUIElementSpec)) {
250            return false;
251        }
[837]252
[619]253        MFCGUIElementSpec otherSpec = (MFCGUIElementSpec) other;
[1]254
[1184]255        if ((type == null) ? otherSpec.type != null : !type.equals(otherSpec.type)) {
[619]256            return false;
257        }
[1]258
[619]259        if (isModal != otherSpec.isModal) {
260            return false;
261        }
[1]262
[619]263        if (resourceId != otherSpec.resourceId) {
264            return false;
265        }
[171]266
[619]267        // up to now, we compared, if the basics match. Now lets compare the id and the
268        // name. Both may change. The name may be reset (e.g. the title of a frame using the
269        // asterisk in the case data was changed). The id may change if e.g. a dialog is closed
270        // and reopend, i.e. a new instance is created. If one of them stays the same, then
271        // similarity is given. Therefore these are the first two comparisons
[837]272
[619]273        if (hwnd == otherSpec.hwnd) {
274            return true;
275        }
[837]276
[619]277        if ((name != null) && (name.equals(otherSpec.name))) {
278            return true;
279        }
[837]280
281        if ((((name == null) && (otherSpec.name == null)) || (("".equals(name)) && (""
282            .equals(otherSpec.name)))) &&
283            (formerNames.size() == 0) &&
284            (otherSpec.formerNames.size() == 0))
[619]285        {
286            return true;
287        }
[837]288
[619]289        // if the hwnd and the name did not stay the same, then the name should be checked first.
[715]290        // The current name of one of the specs must be contained in the former names of the
[619]291        // respective other spec for similarity. Either of the specs should contain the name of the
292        // respective other spec in its former names. We can rely on this, as in the MFC context
293        // we get to know each name change. I.e. although currently the names of the specs differ,
294        // once they were identical. But it is sufficient to do it for the current names of the
295        // elements, as only one of them may have experienced more name changes then the other.
[1]296
[715]297        if ((otherSpec.name != null) && formerNames.contains(otherSpec.name)) {
[619]298            return true;
299        }
300
301        if ((name != null) && otherSpec.formerNames.contains(name)) {
302            return true;
303        }
[837]304
[715]305        // ok. Even the names do not match. This is usually a clear indication, that the elements
[619]306        // are distinct. However, we check, if the former handles matched. This is very unlikely
307        // to happen. But it may occur, if a GUI element does not have a name or its name stays
308        // the empty string and if this GUI element is created, destroyed, and created again.
309
310        if (formerHwnds.contains(otherSpec.hwnd) || otherSpec.formerHwnds.contains(hwnd)) {
311            return true;
312        }
313
314        // now we can be really sure, that the GUI elements differ
[837]315
[619]316        return false;
317    }
318
[837]319    /*
320     * (non-Javadoc)
321     *
[922]322     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#equals(IGUIElementSpec)
[619]323     */
324    @Override
[832]325    public boolean equals(Object other) {
[837]326
[619]327        if (this == other) {
328            return true;
329        }
[837]330
[619]331        if (!(other instanceof MFCGUIElementSpec)) {
332            return false;
333        }
[837]334
[619]335        MFCGUIElementSpec otherSpec = (MFCGUIElementSpec) other;
[837]336
337        return (hwnd == otherSpec.hwnd) && (isModal == otherSpec.isModal) &&
[619]338            (resourceId == otherSpec.resourceId) &&
[1184]339            (type == null ? otherSpec.type == null : type.equals(otherSpec.type)) &&
340            (name == null ? otherSpec.name == null : name.equals(otherSpec.name));
[619]341    }
342
[837]343    /*
344     * (non-Javadoc)
345     *
[619]346     * @see java.lang.Object#hashCode()
347     */
348    @Override
349    public int hashCode() {
350        // reuse only invariable elements
351        return (type + isModal + resourceId).hashCode();
352    }
353
354    /**
355     * <p>
[837]356     * Returns a string identifier of the window:<br>
[619]357     * {@code [resourceId;"windowName";"className";modality]}
358     * </p>
359     *
360     * @return identifier string of the window
361     */
362    @Override
363    public String toString() {
[837]364        return "[" + resourceId + ";" + getName() + ";\"" + type + "\";" + isModal + ";" + hwnd +
365            "]";
[619]366    }
367
368    /**
369     * <p>
[837]370     * Returns the XML representation of this specification.
[619]371     * </p>
[837]372     *
373     * @return the XML representation
[619]374     */
375    String toXML() {
[837]376        return "<window name=\"" + (name != null ? StringTools.xmlEntityReplacement(name) : "") +
377            "\" class=\"" + StringTools.xmlEntityReplacement(type) + "\" resourceId=\"" +
378            resourceId + "\" isModal=\"" + isModal + "\"/>";
[619]379    }
380
381    /**
382     * <p>
[837]383     * Updates the specification with another specification.
[619]384     * </p>
[837]385     *
[619]386     * @param furtherSpec
[837]387     *            specification used to update the current specification
[619]388     */
389    void update(IGUIElementSpec furtherSpec) {
390        MFCGUIElementSpec other = (MFCGUIElementSpec) furtherSpec;
[837]391
[619]392        if (other != this) {
393            for (long formerHwnd : other.formerHwnds) {
394                setHwnd(formerHwnd);
395            }
396
397            if (hwnd != other.hwnd) {
398                hwnd = other.hwnd;
399            }
400
401            for (String formerName : other.formerNames) {
402                setName(formerName);
403            }
404
[1184]405            if (name == null ? other.name != null : !name.equals(other.name)) {
[619]406                setName(other.name);
407            }
408        }
409    }
410
[966]411    @Override
[990]412    public String[] getTypeHierarchy() {
413        return new String[] { type };
[966]414    }
415
[1]416}
Note: See TracBrowser for help on using the repository browser.