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

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