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

Last change on this file since 1080 was 1080, checked in by pharms, 11 years ago
  • prevented findbugs warnings
File size: 11.2 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;
21
22import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
23import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec;
24
25/**
26 * <p>
27 * Implements the specification of {@link IGUIElement} for {@link JFCGUIElement}s.
28 * </p>
29 *
30 * @version 1.0
31 * @author Patrick Harms
32 */
33public class JFCGUIElementSpec implements IGUIElementSpec {
34
35    /**
36     * <p>
37     * Id for object serialization.
38     * </p>
39     */
40    private static final long serialVersionUID = 1L;
41
42    /**
43     * <p>
44     * Current name of the GUI element
45     * </p>
46     */
47    private String name;
48
49    /**
50     * <p>
51     * Previous names of the GUI element as it may have changed over time.
52     * </p>
53     */
54    private List<String> formerNames = new ArrayList<String>();
55
56    /**
57     * <p>
58     * Type of the GUI element, i.e., its Java class.
59     * </p>
60     */
61    private String type = null;
62
63    /**
64     * <p>
65     * Icon associated with the GUI element.
66     * </p>
67     */
68    private String icon = null;
69
70    /**
71     * <p>
72     * Index of the GUI element in its parent element.
73     * </p>
74     */
75    private int index = -1;
76
77    /**
78     * <p>
79     * Hash code of the GUI element. Used as unique identifier during its existence.
80     * </p>
81     */
82    private int elementHash = -1;
83
84    /**
85     * <p>
86     * Previous hashes of the GUI element as the GUI element may have been destroyed and recreated.
87     * </p>
88     */
89    private List<Integer> formerElementHashes = new ArrayList<Integer>();
90   
91    /**
92     * <p>
93     * Type hierarchy of the class itself
94     * </p>
95     */
96    private List<String> typeHierarchy = null;
97
98    /*
99     * (non-Javadoc)
100     *
101     * @see
102     * de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#getSecificationSimilarity(IGUIElementSpec
103     * )
104     */
105    @Override
106    public boolean getSimilarity(IGUIElementSpec other) {
107        if (this == other) {
108            return true;
109        }
110
111        if (!(other instanceof JFCGUIElementSpec)) {
112            return false;
113        }
114
115        JFCGUIElementSpec otherSpec = (JFCGUIElementSpec) other;
116
117        if (type == null ? otherSpec.type != null : !type.equals(otherSpec.type)) {
118            return false;
119        }
120
121        if (icon == null ? otherSpec.icon != null : !icon.equals(otherSpec.icon)) {
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
131
132        if (elementHash == otherSpec.elementHash) {
133            return true;
134        }
135
136        if ((name != null) && (name.equals(otherSpec.name))) {
137            return true;
138        }
139
140        if ((((name == null) && (otherSpec.name == null)) || (("".equals(name)) && (""
141            .equals(otherSpec.name)))) &&
142            (formerNames.size() == 0) &&
143            (otherSpec.formerNames.size() == 0))
144        {
145            return true;
146        }
147
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        }
160
161        if (CollectionUtils.containsAny(formerNames, otherSpec.formerNames)) {
162            return index == otherSpec.index;
163        }
164
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        }
178
179        if (CollectionUtils.containsAny(formerElementHashes, otherSpec.formerElementHashes)) {
180            return index == otherSpec.index;
181        }
182
183        // now we can be really sure, that the GUI elements differ
184
185        return false;
186    }
187
188    /*
189     * (non-Javadoc)
190     *
191     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#equals(IGUIElementSpec)
192     */
193    @Override
194    public boolean equals(Object other) {
195        if (this == other) {
196            return true;
197        }
198
199        if (!(other instanceof JFCGUIElementSpec)) {
200            return false;
201        }
202
203        JFCGUIElementSpec otherSpec = (JFCGUIElementSpec) other;
204
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)) &&
209            (index == otherSpec.index) && (elementHash == otherSpec.elementHash);
210    }
211
212    /*
213     * (non-Javadoc)
214     *
215     * @see java.lang.Object#hashCode()
216     */
217    @Override
218    public int hashCode() {
219        return (name + type + icon + index + elementHash).hashCode();
220    }
221
222    /**
223     * <p>
224     * Returns the name of the specified GUI element.
225     * </p>
226     *
227     * @return the name
228     */
229    public String getName() {
230        StringBuffer names = new StringBuffer();
231
232        if (name != null) {
233            names.append('"');
234            names.append(name);
235            names.append('"');
236        }
237        else {
238            names.append("NOT_SET");
239        }
240
241        if (formerNames.size() > 0) {
242
243            names.append(" (aka ");
244
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            }
254
255            names.append(")");
256        }
257
258        return names.toString();
259    }
260
261    /**
262     * <p>
263     * Returns the title of the specified GUI element.
264     * </p>
265     *
266     * @return the title
267     */
268    public String getType() {
269        return type;
270    }
271
272    /**
273     * <p>
274     * Returns the icon associated with the specified GUI element.
275     * </p>
276     *
277     * @return the icon
278     */
279    public String getIcon() {
280        return icon;
281    }
282
283    /**
284     * <p>
285     * Returns the index of the specified GUI element in its parent element.
286     * </p>
287     *
288     * @return the index
289     */
290    public int getIndex() {
291        return index;
292    }
293
294    /**
295     * <p>
296     * Returns the object hash of the specified GUI element.
297     * </p>
298     *
299     * @return the elementHash
300     */
301    public int getElementHash() {
302        return elementHash;
303    }
304
305    /**
306     * <p>
307     * Sets the name of the specified GUI element.
308     * </p>
309     *
310     * @param newName
311     *            the name
312     */
313    public void setName(String newName) {
314        if ((this.name != null) && (!this.name.equals(newName)) &&
315            (!this.formerNames.contains(this.name)))
316        {
317            this.formerNames.add(this.name);
318        }
319
320        this.name = newName;
321    }
322
323    /**
324     * <p>
325     * Sets the type of the specified GUI element.
326     * </p>
327     *
328     * @param type
329     *            the type
330     */
331    public void setType(String type) {
332        this.type = type;
333    }
334
335    /**
336     * <p>
337     * Sets the icon associated with the specified GUI element.
338     * </p>
339     *
340     * @param icon
341     *            the icon
342     */
343    public void setIcon(String icon) {
344        this.icon = icon;
345    }
346
347    /**
348     * <p>
349     * Sets the index in its parent element of the specified GUI element.
350     * </p>
351     *
352     * @param index
353     *            the index
354     */
355    public void setIndex(int index) {
356        this.index = index;
357    }
358
359    /**
360     * <p>
361     * Sets the object hash of the specified GUI element.
362     * </p>
363     *
364     * @param newElementHash
365     *            the element hash
366     */
367    public void setElementHash(int newElementHash) {
368        if ((this.elementHash > -1) && !this.formerElementHashes.contains(this.elementHash)) {
369            this.formerElementHashes.add(this.elementHash);
370        }
371
372        this.elementHash = newElementHash;
373    }
374   
375    /**
376     * <p>
377     * Sets the type hierarchy of the specified GUI element.
378     * @param typeHierarchy
379     * </p>
380     */
381    public void setTypeHierarchy(List<String> typeHierarchy){
382        this.typeHierarchy = typeHierarchy;
383    }
384
385    /**
386     * <p>
387     * Updates the specification with another specification.
388     * </p>
389     *
390     * @param furtherSpec
391     *            specification used to update the current specification
392     */
393    void update(JFCGUIElementSpec other) {
394        if (other != this) {
395            for (int formerElementHash : other.formerElementHashes) {
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
407            if ((name == null) || (!name.equals(other.name))) {
408                setName(other.name);
409            }
410        }
411    }
412
413    /*
414     * (non-Javadoc)
415     *
416     * @see java.lang.Object#toString()
417     */
418    public String toString() {
419        return "[" + getName() + ";\"" + type + "\";\"" + icon + "\";" + index + ";" + elementHash +
420            "]";
421    }
422
423    @Override
424    public String[] getTypeHierarchy() {
425        if (typeHierarchy == null){
426                return new String[]{(getType())};
427        }
428        else
429                return typeHierarchy.toArray(new String[typeHierarchy.size()]);
430    }
431
432}
Note: See TracBrowser for help on using the repository browser.