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

Last change on this file since 1678 was 1080, checked in by pharms, 11 years ago
  • prevented findbugs warnings
File size: 11.2 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>
[1009]79     * Hash code of the GUI element. Used as unique identifier during its existence.
[714]80     * </p>
81     */
[743]82    private int elementHash = -1;
[714]83
84    /**
85     * <p>
[1009]86     * Previous hashes of the GUI element as the GUI element may have been destroyed and recreated.
[714]87     * </p>
88     */
[743]89    private List<Integer> formerElementHashes = new ArrayList<Integer>();
[984]90   
91    /**
92     * <p>
[990]93     * Type hierarchy of the class itself
[984]94     * </p>
95     */
[990]96    private List<String> typeHierarchy = null;
[714]97
[835]98    /*
99     * (non-Javadoc)
100     *
101     * @see
[922]102     * de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#getSecificationSimilarity(IGUIElementSpec
[835]103     * )
[573]104     */
105    @Override
[589]106    public boolean getSimilarity(IGUIElementSpec other) {
[835]107        if (this == other) {
[589]108            return true;
[573]109        }
[835]110
111        if (!(other instanceof JFCGUIElementSpec)) {
[589]112            return false;
[573]113        }
[835]114
[573]115        JFCGUIElementSpec otherSpec = (JFCGUIElementSpec) other;
[835]116
[1080]117        if (type == null ? otherSpec.type != null : !type.equals(otherSpec.type)) {
[714]118            return false;
119        }
120
[1080]121        if (icon == null ? otherSpec.icon != null : !icon.equals(otherSpec.icon)) {
[714]122            return false;
123        }
124
125        // up to now, we compared, if the basics match. Now lets compare the id, the name and the
126        // index. All may change. The name may be reset (e.g. the title of a frame using the
127        // asterisk in the case data was changed). The id may change if e.g. a dialog is closed
128        // and reopend, i.e. a new instance is created. The index may change, if later in a panel
129        // a new element is added or another one is removed. If the element hash or the name stay
130        // the same, then similarity is given. Therefore these are the first two comparisons
[835]131
[743]132        if (elementHash == otherSpec.elementHash) {
[714]133            return true;
134        }
[835]135
[714]136        if ((name != null) && (name.equals(otherSpec.name))) {
137            return true;
[573]138        }
[835]139
140        if ((((name == null) && (otherSpec.name == null)) || (("".equals(name)) && (""
141            .equals(otherSpec.name)))) &&
142            (formerNames.size() == 0) &&
143            (otherSpec.formerNames.size() == 0))
[714]144        {
145            return true;
[600]146        }
[835]147
[714]148        // if the id and the name did not stay the same, then the name should be checked first.
149        // One of all known names of one of the specs must be equal to one of the known names of the
150        // respective other spec for similarity. Furthermore, if this is given, the index should
151        // have stayed the same.
152
153        if ((otherSpec.name != null) && formerNames.contains(otherSpec.name)) {
154            return index == otherSpec.index;
155        }
156
157        if ((name != null) && otherSpec.formerNames.contains(name)) {
158            return index == otherSpec.index;
159        }
[835]160
[714]161        if (CollectionUtils.containsAny(formerNames, otherSpec.formerNames)) {
162            return index == otherSpec.index;
163        }
[835]164
[714]165        // ok. Even the names do not match. This is usually a clear indication, that the elements
166        // are distinct. However, we check, if the former ids matched. This is very unlikely
167        // to happen. But it may occur, if a GUI element does not have a name or its name stays
168        // the empty string and if this GUI element is created, destroyed, and created again.
169        // Again we are restrictive and request the index to be equal as well.
170
171        if (formerElementHashes.contains(otherSpec.elementHash)) {
172            return index == otherSpec.index;
173        }
174
175        if (otherSpec.formerElementHashes.contains(elementHash)) {
176            return index == otherSpec.index;
177        }
[835]178
[714]179        if (CollectionUtils.containsAny(formerElementHashes, otherSpec.formerElementHashes)) {
180            return index == otherSpec.index;
181        }
[835]182
[714]183        // now we can be really sure, that the GUI elements differ
[835]184
[714]185        return false;
[573]186    }
187
[835]188    /*
189     * (non-Javadoc)
190     *
[922]191     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#equals(IGUIElementSpec)
[573]192     */
193    @Override
[832]194    public boolean equals(Object other) {
[835]195        if (this == other) {
[573]196            return true;
197        }
[835]198
199        if (!(other instanceof JFCGUIElementSpec)) {
[573]200            return false;
201        }
[835]202
[573]203        JFCGUIElementSpec otherSpec = (JFCGUIElementSpec) other;
[835]204
[1080]205        return
206            (name == null ? otherSpec.name == null : name.equals(otherSpec.name)) &&
207            (type == null ? otherSpec.type == null : type.equals(otherSpec.type)) &&
208            (icon == null ? otherSpec.icon == null : icon.equals(otherSpec.icon)) &&
[743]209            (index == otherSpec.index) && (elementHash == otherSpec.elementHash);
[573]210    }
211
[835]212    /*
213     * (non-Javadoc)
214     *
[600]215     * @see java.lang.Object#hashCode()
216     */
217    @Override
218    public int hashCode() {
219        return (name + type + icon + index + elementHash).hashCode();
220    }
221
[573]222    /**
[835]223     * <p>
224     * Returns the name of the specified GUI element.
225     * </p>
226     *
[573]227     * @return the name
228     */
229    public String getName() {
[714]230        StringBuffer names = new StringBuffer();
[835]231
[714]232        if (name != null) {
233            names.append('"');
234            names.append(name);
235            names.append('"');
[589]236        }
[714]237        else {
238            names.append("NOT_SET");
239        }
[835]240
[714]241        if (formerNames.size() > 0) {
[835]242
[714]243            names.append(" (aka ");
[835]244
[714]245            for (int i = 0; i < formerNames.size(); i++) {
246                if (i > 0) {
247                    names.append("/");
248                }
249
250                names.append('"');
251                names.append(formerNames.get(i));
252                names.append('"');
253            }
[835]254
[714]255            names.append(")");
256        }
[835]257
[714]258        return names.toString();
[573]259    }
260
261    /**
[835]262     * <p>
263     * Returns the title of the specified GUI element.
264     * </p>
265     *
[573]266     * @return the title
267     */
268    public String getType() {
269        return type;
270    }
271
272    /**
[835]273     * <p>
274     * Returns the icon associated with the specified GUI element.
275     * </p>
276     *
[573]277     * @return the icon
278     */
279    public String getIcon() {
280        return icon;
281    }
282
283    /**
[835]284     * <p>
285     * Returns the index of the specified GUI element in its parent element.
286     * </p>
287     *
[573]288     * @return the index
289     */
290    public int getIndex() {
291        return index;
292    }
293
294    /**
[835]295     * <p>
296     * Returns the object hash of the specified GUI element.
297     * </p>
298     *
[573]299     * @return the elementHash
300     */
[743]301    public int getElementHash() {
[714]302        return elementHash;
[573]303    }
304
305    /**
[835]306     * <p>
307     * Sets the name of the specified GUI element.
308     * </p>
309     *
[849]310     * @param newName
[835]311     *            the name
[573]312     */
[714]313    public void setName(String newName) {
[835]314        if ((this.name != null) && (!this.name.equals(newName)) &&
[714]315            (!this.formerNames.contains(this.name)))
316        {
317            this.formerNames.add(this.name);
[589]318        }
[835]319
[714]320        this.name = newName;
[573]321    }
322
323    /**
[835]324     * <p>
325     * Sets the type of the specified GUI element.
326     * </p>
327     *
[849]328     * @param type
329     *            the type
[573]330     */
331    public void setType(String type) {
332        this.type = type;
333    }
334
335    /**
[835]336     * <p>
337     * Sets the icon associated with the specified GUI element.
338     * </p>
339     *
340     * @param icon
341     *            the icon
[573]342     */
343    public void setIcon(String icon) {
344        this.icon = icon;
345    }
346
347    /**
[835]348     * <p>
349     * Sets the index in its parent element of the specified GUI element.
350     * </p>
351     *
352     * @param index
353     *            the index
[573]354     */
355    public void setIndex(int index) {
356        this.index = index;
357    }
358
359    /**
[835]360     * <p>
361     * Sets the object hash of the specified GUI element.
362     * </p>
363     *
[849]364     * @param newElementHash
365     *            the element hash
[573]366     */
[743]367    public void setElementHash(int newElementHash) {
[835]368        if ((this.elementHash > -1) && !this.formerElementHashes.contains(this.elementHash)) {
[714]369            this.formerElementHashes.add(this.elementHash);
[589]370        }
[835]371
[714]372        this.elementHash = newElementHash;
[573]373    }
[984]374   
375    /**
376     * <p>
[990]377     * Sets the type hierarchy of the specified GUI element.
378     * @param typeHierarchy
[984]379     * </p>
380     */
[990]381    public void setTypeHierarchy(List<String> typeHierarchy){
382        this.typeHierarchy = typeHierarchy;
[984]383    }
[835]384
[714]385    /**
386     * <p>
[835]387     * Updates the specification with another specification.
[714]388     * </p>
[835]389     *
[714]390     * @param furtherSpec
[835]391     *            specification used to update the current specification
[714]392     */
393    void update(JFCGUIElementSpec other) {
394        if (other != this) {
[743]395            for (int formerElementHash : other.formerElementHashes) {
[714]396                setElementHash(formerElementHash);
397            }
398
399            if (elementHash != other.elementHash) {
400                elementHash = other.elementHash;
401            }
402
403            for (String formerName : other.formerNames) {
404                setName(formerName);
405            }
406
[1080]407            if ((name == null) || (!name.equals(other.name))) {
[714]408                setName(other.name);
409            }
410        }
411    }
412
[835]413    /*
414     * (non-Javadoc)
415     *
416     * @see java.lang.Object#toString()
417     */
[607]418    public String toString() {
[835]419        return "[" + getName() + ";\"" + type + "\";\"" + icon + "\";" + index + ";" + elementHash +
420            "]";
[607]421    }
[573]422
[966]423    @Override
[990]424    public String[] getTypeHierarchy() {
425        if (typeHierarchy == null){
[984]426                return new String[]{(getType())};
427        }
428        else
[990]429                return typeHierarchy.toArray(new String[typeHierarchy.size()]);
[966]430    }
431
[573]432}
Note: See TracBrowser for help on using the repository browser.