source: trunk/autoquest-plugin-jfc/src/main/java/de/ugoe/cs/autoquest/plugin/jfc/guimodel/JFCGUIElementSpec.java @ 966

Last change on this file since 966 was 966, checked in by sherbold, 12 years ago
  • extended IGUIElementSpec interface to account for possible type hierarchies and added dummy implementations for the extension
File size: 10.9 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.
[835]14
[922]15package de.ugoe.cs.autoquest.plugin.jfc.guimodel;
[573]16
[714]17import java.util.ArrayList;
[589]18import java.util.List;
19
20import org.apache.commons.collections15.CollectionUtils;
21
[922]22import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
23import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec;
[573]24
25/**
26 * <p>
[835]27 * Implements the specification of {@link IGUIElement} for {@link JFCGUIElement}s.
[573]28 * </p>
29 *
[835]30 * @version 1.0
31 * @author Patrick Harms
[573]32 */
33public class JFCGUIElementSpec implements IGUIElementSpec {
34
[835]35    /**
36     * <p>
37     * Id for object serialization.
38     * </p>
39     */
[778]40    private static final long serialVersionUID = 1L;
41
[714]42    /**
43     * <p>
[835]44     * Current name of the GUI element
[714]45     * </p>
46     */
47    private String name;
48
49    /**
50     * <p>
[835]51     * Previous names of the GUI element as it may have changed over time.
[714]52     * </p>
53     */
54    private List<String> formerNames = new ArrayList<String>();
55
[835]56    /**
57     * <p>
58     * Type of the GUI element, i.e., its Java class.
59     * </p>
60     */
[588]61    private String type = null;
[835]62
63    /**
64     * <p>
65     * Icon associated with the GUI element.
66     * </p>
67     */
[588]68    private String icon = null;
[835]69
70    /**
71     * <p>
72     * Index of the GUI element in its parent element.
73     * </p>
74     */
[588]75    private int index = -1;
[835]76
[714]77    /**
78     * <p>
[835]79     * Hash code of the window element. Used as unique identifier during its existence.
[714]80     * </p>
81     */
[743]82    private int elementHash = -1;
[714]83
84    /**
85     * <p>
[835]86     * Previous hashes of the window as the window may have been destroyed and recreated.
[714]87     * </p>
88     */
[743]89    private List<Integer> formerElementHashes = new ArrayList<Integer>();
[714]90
[835]91    /*
92     * (non-Javadoc)
93     *
94     * @see
[922]95     * de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#getSecificationSimilarity(IGUIElementSpec
[835]96     * )
[573]97     */
98    @Override
[589]99    public boolean getSimilarity(IGUIElementSpec other) {
[835]100        if (this == other) {
[589]101            return true;
[573]102        }
[835]103
104        if (!(other instanceof JFCGUIElementSpec)) {
[589]105            return false;
[573]106        }
[835]107
[573]108        JFCGUIElementSpec otherSpec = (JFCGUIElementSpec) other;
[835]109
[714]110        if ((type != otherSpec.type) && ((type != null) && (!type.equals(otherSpec.type)))) {
111            return false;
112        }
113
114        if ((icon != otherSpec.icon) && ((icon != null) && (!icon.equals(otherSpec.icon)))) {
115            return false;
116        }
117
118        // up to now, we compared, if the basics match. Now lets compare the id, the name and the
119        // index. All may change. The name may be reset (e.g. the title of a frame using the
120        // asterisk in the case data was changed). The id may change if e.g. a dialog is closed
121        // and reopend, i.e. a new instance is created. The index may change, if later in a panel
122        // a new element is added or another one is removed. If the element hash or the name stay
123        // the same, then similarity is given. Therefore these are the first two comparisons
[835]124
[743]125        if (elementHash == otherSpec.elementHash) {
[714]126            return true;
127        }
[835]128
[714]129        if ((name != null) && (name.equals(otherSpec.name))) {
130            return true;
[573]131        }
[835]132
133        if ((((name == null) && (otherSpec.name == null)) || (("".equals(name)) && (""
134            .equals(otherSpec.name)))) &&
135            (formerNames.size() == 0) &&
136            (otherSpec.formerNames.size() == 0))
[714]137        {
138            return true;
[600]139        }
[835]140
[714]141        // if the id and the name did not stay the same, then the name should be checked first.
142        // One of all known names of one of the specs must be equal to one of the known names of the
143        // respective other spec for similarity. Furthermore, if this is given, the index should
144        // have stayed the same.
145
146        if ((otherSpec.name != null) && formerNames.contains(otherSpec.name)) {
147            return index == otherSpec.index;
148        }
149
150        if ((name != null) && otherSpec.formerNames.contains(name)) {
151            return index == otherSpec.index;
152        }
[835]153
[714]154        if (CollectionUtils.containsAny(formerNames, otherSpec.formerNames)) {
155            return index == otherSpec.index;
156        }
[835]157
[714]158        // ok. Even the names do not match. This is usually a clear indication, that the elements
159        // are distinct. However, we check, if the former ids matched. This is very unlikely
160        // to happen. But it may occur, if a GUI element does not have a name or its name stays
161        // the empty string and if this GUI element is created, destroyed, and created again.
162        // Again we are restrictive and request the index to be equal as well.
163
164        if (formerElementHashes.contains(otherSpec.elementHash)) {
165            return index == otherSpec.index;
166        }
167
168        if (otherSpec.formerElementHashes.contains(elementHash)) {
169            return index == otherSpec.index;
170        }
[835]171
[714]172        if (CollectionUtils.containsAny(formerElementHashes, otherSpec.formerElementHashes)) {
173            return index == otherSpec.index;
174        }
[835]175
[714]176        // now we can be really sure, that the GUI elements differ
[835]177
[714]178        return false;
[573]179    }
180
[835]181    /*
182     * (non-Javadoc)
183     *
[922]184     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#equals(IGUIElementSpec)
[573]185     */
186    @Override
[832]187    public boolean equals(Object other) {
[835]188        if (this == other) {
[573]189            return true;
190        }
[835]191
192        if (!(other instanceof JFCGUIElementSpec)) {
[573]193            return false;
194        }
[835]195
[573]196        JFCGUIElementSpec otherSpec = (JFCGUIElementSpec) other;
[835]197
198        return ((name == otherSpec.name) || ((name != null) && (name.equals(otherSpec.name)))) &&
[573]199            ((type == otherSpec.type) || ((type != null) && (type.equals(otherSpec.type)))) &&
200            ((icon == otherSpec.icon) || ((icon != null) && (icon.equals(otherSpec.icon)))) &&
[743]201            (index == otherSpec.index) && (elementHash == otherSpec.elementHash);
[573]202    }
203
[835]204    /*
205     * (non-Javadoc)
206     *
[600]207     * @see java.lang.Object#hashCode()
208     */
209    @Override
210    public int hashCode() {
211        return (name + type + icon + index + elementHash).hashCode();
212    }
213
[573]214    /**
[835]215     * <p>
216     * Returns the name of the specified GUI element.
217     * </p>
218     *
[573]219     * @return the name
220     */
221    public String getName() {
[714]222        StringBuffer names = new StringBuffer();
[835]223
[714]224        if (name != null) {
225            names.append('"');
226            names.append(name);
227            names.append('"');
[589]228        }
[714]229        else {
230            names.append("NOT_SET");
231        }
[835]232
[714]233        if (formerNames.size() > 0) {
[835]234
[714]235            names.append(" (aka ");
[835]236
[714]237            for (int i = 0; i < formerNames.size(); i++) {
238                if (i > 0) {
239                    names.append("/");
240                }
241
242                names.append('"');
243                names.append(formerNames.get(i));
244                names.append('"');
245            }
[835]246
[714]247            names.append(")");
248        }
[835]249
[714]250        return names.toString();
[573]251    }
252
253    /**
[835]254     * <p>
255     * Returns the title of the specified GUI element.
256     * </p>
257     *
[573]258     * @return the title
259     */
260    public String getType() {
261        return type;
262    }
263
264    /**
[835]265     * <p>
266     * Returns the icon associated with the specified GUI element.
267     * </p>
268     *
[573]269     * @return the icon
270     */
271    public String getIcon() {
272        return icon;
273    }
274
275    /**
[835]276     * <p>
277     * Returns the index of the specified GUI element in its parent element.
278     * </p>
279     *
[573]280     * @return the index
281     */
282    public int getIndex() {
283        return index;
284    }
285
286    /**
[835]287     * <p>
288     * Returns the object hash of the specified GUI element.
289     * </p>
290     *
[573]291     * @return the elementHash
292     */
[743]293    public int getElementHash() {
[714]294        return elementHash;
[573]295    }
296
297    /**
[835]298     * <p>
299     * Sets the name of the specified GUI element.
300     * </p>
301     *
[849]302     * @param newName
[835]303     *            the name
[573]304     */
[714]305    public void setName(String newName) {
[835]306        if ((this.name != null) && (!this.name.equals(newName)) &&
[714]307            (!this.formerNames.contains(this.name)))
308        {
309            this.formerNames.add(this.name);
[589]310        }
[835]311
[714]312        this.name = newName;
[573]313    }
314
315    /**
[835]316     * <p>
317     * Sets the type of the specified GUI element.
318     * </p>
319     *
[849]320     * @param type
321     *            the type
[573]322     */
323    public void setType(String type) {
324        this.type = type;
325    }
326
327    /**
[835]328     * <p>
329     * Sets the icon associated with the specified GUI element.
330     * </p>
331     *
332     * @param icon
333     *            the icon
[573]334     */
335    public void setIcon(String icon) {
336        this.icon = icon;
337    }
338
339    /**
[835]340     * <p>
341     * Sets the index in its parent element of the specified GUI element.
342     * </p>
343     *
344     * @param index
345     *            the index
[573]346     */
347    public void setIndex(int index) {
348        this.index = index;
349    }
350
351    /**
[835]352     * <p>
353     * Sets the object hash of the specified GUI element.
354     * </p>
355     *
[849]356     * @param newElementHash
357     *            the element hash
[573]358     */
[743]359    public void setElementHash(int newElementHash) {
[835]360        if ((this.elementHash > -1) && !this.formerElementHashes.contains(this.elementHash)) {
[714]361            this.formerElementHashes.add(this.elementHash);
[589]362        }
[835]363
[714]364        this.elementHash = newElementHash;
[573]365    }
[835]366
[714]367    /**
368     * <p>
[835]369     * Updates the specification with another specification.
[714]370     * </p>
[835]371     *
[714]372     * @param furtherSpec
[835]373     *            specification used to update the current specification
[714]374     */
375    void update(JFCGUIElementSpec other) {
376        if (other != this) {
[743]377            for (int formerElementHash : other.formerElementHashes) {
[714]378                setElementHash(formerElementHash);
379            }
380
381            if (elementHash != other.elementHash) {
382                elementHash = other.elementHash;
383            }
384
385            for (String formerName : other.formerNames) {
386                setName(formerName);
387            }
388
[835]389            if ((name != other.name) && (name != null) && (!name.equals(other.name))) {
[714]390                setName(other.name);
391            }
392        }
393    }
394
[835]395    /*
396     * (non-Javadoc)
397     *
398     * @see java.lang.Object#toString()
399     */
[607]400    public String toString() {
[835]401        return "[" + getName() + ";\"" + type + "\";\"" + icon + "\";" + index + ";" + elementHash +
402            "]";
[607]403    }
[573]404
[966]405    @Override
406    public String[] getClassHierarchy() {
407        // TODO Auto-generated method stub
408        System.out.println("TODO: implement IGUIElementSpec.getClassHierarchy ");
409        return null;
410    }
411
[573]412}
Note: See TracBrowser for help on using the repository browser.