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
Line 
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.
14
15package de.ugoe.cs.autoquest.plugin.jfc.guimodel;
16
17import java.util.ArrayList;
18import java.util.List;
19
20import org.apache.commons.collections15.CollectionUtils;
21import org.apache.commons.lang.mutable.MutableInt;
22
23import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
24import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec;
25
26/**
27 * <p>
28 * Implements the specification of {@link IGUIElement} for {@link JFCGUIElement}s.
29 * </p>
30 *
31 * @version 1.0
32 * @author Patrick Harms
33 */
34public class JFCGUIElementSpec implements IGUIElementSpec {
35
36    /**
37     * <p>
38     * Id for object serialization.
39     * </p>
40     */
41    private static final long serialVersionUID = 1L;
42
43    /**
44     * <p>
45     * Current name of the GUI element
46     * </p>
47     */
48    private String name;
49
50    /**
51     * <p>
52     * Previous names of the GUI element as it may have changed over time.
53     * </p>
54     */
55    private List<String> formerNames = new ArrayList<String>();
56
57    /**
58     * <p>
59     * Type of the GUI element, i.e., its Java class.
60     * </p>
61     */
62    private String type = null;
63
64    /**
65     * <p>
66     * Icon associated with the GUI element.
67     * </p>
68     */
69    private String icon = null;
70
71    /**
72     * <p>
73     * Index of the GUI element in its parent element.
74     * </p>
75     */
76    private int index = -1;
77
78    /**
79     * <p>
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>
83     */   
84    private MutableInt altIndex = new MutableInt(-1);
85
86    /**
87     * <p>
88     * Hash code of the GUI element. Used as unique identifier during its existence.
89     * </p>
90     */
91    private int elementHash = -1;
92
93    /**
94     * <p>
95     * Previous hashes of the GUI element as the GUI element may have been destroyed and recreated.
96     * </p>
97     */
98    private List<Integer> formerElementHashes = new ArrayList<Integer>();
99
100    /**
101     * <p>
102     * Type hierarchy of the class itself
103     * </p>
104     */
105    private List<String> typeHierarchy = null;
106
107    /*
108     * (non-Javadoc)
109     *
110     * @see
111     * de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#getSecificationSimilarity(IGUIElementSpec
112     * )
113     */
114    @Override
115    public boolean getSimilarity(IGUIElementSpec other) {
116        if (this == other) {
117            return true;
118        }
119
120        if (!(other instanceof JFCGUIElementSpec)) {
121            return false;
122        }
123
124        JFCGUIElementSpec otherSpec = (JFCGUIElementSpec) other;
125
126        if (type == null ? otherSpec.type != null : !type.equals(otherSpec.type)) {
127            return false;
128        }
129
130        if (icon == null ? otherSpec.icon != null : !icon.equals(otherSpec.icon)) {
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
140
141        if (elementHash == otherSpec.elementHash) {
142            return true;
143        }
144
145        if ((name != null) && (name.equals(otherSpec.name))) {
146            return true;
147        }
148
149        if ((((name == null) && (otherSpec.name == null)) || (("".equals(name)) && (""
150            .equals(otherSpec.name)))) &&
151            (formerNames.size() == 0) &&
152            (otherSpec.formerNames.size() == 0))
153        {
154            return true;
155        }
156
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        }
169
170        if (CollectionUtils.containsAny(formerNames, otherSpec.formerNames)) {
171            return index == otherSpec.index;
172        }
173
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        }
187
188        if (CollectionUtils.containsAny(formerElementHashes, otherSpec.formerElementHashes)) {
189            return index == otherSpec.index;
190        }
191
192        // now we can be really sure, that the GUI elements differ
193
194        return false;
195    }
196
197    /*
198     * (non-Javadoc)
199     *
200     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#equals(IGUIElementSpec)
201     */
202    @Override
203    public boolean equals(Object other) {
204        if (this == other) {
205            return true;
206        }
207
208        if (!(other instanceof JFCGUIElementSpec)) {
209            return false;
210        }
211
212        JFCGUIElementSpec otherSpec = (JFCGUIElementSpec) other;
213
214        return (name == null ? otherSpec.name == null : name.equals(otherSpec.name)) &&
215            (type == null ? otherSpec.type == null : type.equals(otherSpec.type)) &&
216            (icon == null ? otherSpec.icon == null : icon.equals(otherSpec.icon)) &&
217            (index == otherSpec.index) && (elementHash == otherSpec.elementHash);
218    }
219
220    /*
221     * (non-Javadoc)
222     *
223     * @see java.lang.Object#hashCode()
224     */
225    @Override
226    public int hashCode() {
227        return (name + type + icon + index + elementHash).hashCode();
228    }
229
230    /**
231     * <p>
232     * Returns the name of the specified GUI element.
233     * </p>
234     *
235     * @return the name
236     */
237    public String getName() {
238        StringBuffer names = new StringBuffer();
239
240        if (name != null) {
241            names.append('"');
242            names.append(name);
243            names.append('"');
244        }
245        else {
246            names.append("NOT_SET");
247        }
248
249        if (formerNames.size() > 0) {
250
251            names.append(" (aka ");
252
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            }
262
263            names.append(")");
264        }
265
266        return names.toString();
267    }
268
269    /**
270     * <p>
271     * Returns the title of the specified GUI element.
272     * </p>
273     *
274     * @return the title
275     */
276    public String getType() {
277        return type;
278    }
279
280    /**
281     * <p>
282     * Returns the icon associated with the specified GUI element.
283     * </p>
284     *
285     * @return the icon
286     */
287    public String getIcon() {
288        return icon;
289    }
290
291    /**
292     * <p>
293     * Returns the index of the specified GUI element in its parent element.
294     * </p>
295     *
296     * @return the index
297     */
298    public int getIndex() {
299        return index;
300    }
301
302    /**
303     * <p>
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() {
311        return altIndex.intValue();
312    }
313
314    /**
315     * <p>
316     * Returns the object hash of the specified GUI element.
317     * </p>
318     *
319     * @return the elementHash
320     */
321    public int getElementHash() {
322        return elementHash;
323    }
324
325    /**
326     * <p>
327     * Sets the alternative index.
328     * </p>
329     *
330     * @param newAltIndex
331     *            the index
332     */
333    public void setAltIndex(int newAltIndex) {
334        altIndex.setValue(newAltIndex);
335    }
336
337    /**
338     * <p>
339     * Sets the name of the specified GUI element.
340     * </p>
341     *
342     * @param newName
343     *            the name
344     */
345    public void setName(String newName) {
346        if ((this.name != null) && (!this.name.equals(newName)) &&
347            (!this.formerNames.contains(this.name)))
348        {
349            this.formerNames.add(this.name);
350        }
351
352        this.name = newName;
353    }
354
355    /**
356     * <p>
357     * Sets the type of the specified GUI element.
358     * </p>
359     *
360     * @param type
361     *            the type
362     */
363    public void setType(String type) {
364        this.type = type;
365    }
366
367    /**
368     * <p>
369     * Sets the icon associated with the specified GUI element.
370     * </p>
371     *
372     * @param icon
373     *            the icon
374     */
375    public void setIcon(String icon) {
376        this.icon = icon;
377    }
378
379    /**
380     * <p>
381     * Sets the index in its parent element of the specified GUI element.
382     * </p>
383     *
384     * @param index
385     *            the index
386     */
387    public void setIndex(int index) {
388        this.index = index;
389    }
390
391    /**
392     * <p>
393     * Sets the object hash of the specified GUI element.
394     * </p>
395     *
396     * @param newElementHash
397     *            the element hash
398     */
399    public void setElementHash(int newElementHash) {
400        if ((this.elementHash > -1) && !this.formerElementHashes.contains(this.elementHash)) {
401            this.formerElementHashes.add(this.elementHash);
402        }
403
404        this.elementHash = newElementHash;
405    }
406
407    /**
408     * <p>
409     * Sets the type hierarchy of the specified GUI element.
410     *
411     * @param typeHierarchy
412     *            </p>
413     */
414    public void setTypeHierarchy(List<String> typeHierarchy) {
415        this.typeHierarchy = typeHierarchy;
416    }
417
418    /**
419     * <p>
420     * Updates the specification with another specification.
421     * </p>
422     *
423     * @param furtherSpec
424     *            specification used to update the current specification
425     */
426    void update(JFCGUIElementSpec other) {
427        if (other != this) {
428            for (int formerElementHash : other.formerElementHashes) {
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
440            if ((name == null) || (!name.equals(other.name))) {
441                setName(other.name);
442            }
443            other.altIndex = this.altIndex;
444        }
445    }
446
447    /*
448     * (non-Javadoc)
449     *
450     * @see java.lang.Object#toString()
451     */
452    public String toString() {
453        return "[" + getName() + ";\"" + type + "\";\"" + icon + "\";" + index + ";" + elementHash +
454            "]";
455    }
456
457    @Override
458    public String[] getTypeHierarchy() {
459        if (typeHierarchy == null) {
460            return new String[]
461                { (getType()) };
462        }
463        else
464            return typeHierarchy.toArray(new String[typeHierarchy.size()]);
465    }
466
467}
Note: See TracBrowser for help on using the repository browser.