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

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
File size: 10.7 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 window 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 window as the window may have been destroyed and recreated.
87     * </p>
88     */
89    private List<Integer> formerElementHashes = new ArrayList<Integer>();
90
91    /*
92     * (non-Javadoc)
93     *
94     * @see
95     * de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#getSecificationSimilarity(IGUIElementSpec
96     * )
97     */
98    @Override
99    public boolean getSimilarity(IGUIElementSpec other) {
100        if (this == other) {
101            return true;
102        }
103
104        if (!(other instanceof JFCGUIElementSpec)) {
105            return false;
106        }
107
108        JFCGUIElementSpec otherSpec = (JFCGUIElementSpec) other;
109
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
124
125        if (elementHash == otherSpec.elementHash) {
126            return true;
127        }
128
129        if ((name != null) && (name.equals(otherSpec.name))) {
130            return true;
131        }
132
133        if ((((name == null) && (otherSpec.name == null)) || (("".equals(name)) && (""
134            .equals(otherSpec.name)))) &&
135            (formerNames.size() == 0) &&
136            (otherSpec.formerNames.size() == 0))
137        {
138            return true;
139        }
140
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        }
153
154        if (CollectionUtils.containsAny(formerNames, otherSpec.formerNames)) {
155            return index == otherSpec.index;
156        }
157
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        }
171
172        if (CollectionUtils.containsAny(formerElementHashes, otherSpec.formerElementHashes)) {
173            return index == otherSpec.index;
174        }
175
176        // now we can be really sure, that the GUI elements differ
177
178        return false;
179    }
180
181    /*
182     * (non-Javadoc)
183     *
184     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#equals(IGUIElementSpec)
185     */
186    @Override
187    public boolean equals(Object other) {
188        if (this == other) {
189            return true;
190        }
191
192        if (!(other instanceof JFCGUIElementSpec)) {
193            return false;
194        }
195
196        JFCGUIElementSpec otherSpec = (JFCGUIElementSpec) other;
197
198        return ((name == otherSpec.name) || ((name != null) && (name.equals(otherSpec.name)))) &&
199            ((type == otherSpec.type) || ((type != null) && (type.equals(otherSpec.type)))) &&
200            ((icon == otherSpec.icon) || ((icon != null) && (icon.equals(otherSpec.icon)))) &&
201            (index == otherSpec.index) && (elementHash == otherSpec.elementHash);
202    }
203
204    /*
205     * (non-Javadoc)
206     *
207     * @see java.lang.Object#hashCode()
208     */
209    @Override
210    public int hashCode() {
211        return (name + type + icon + index + elementHash).hashCode();
212    }
213
214    /**
215     * <p>
216     * Returns the name of the specified GUI element.
217     * </p>
218     *
219     * @return the name
220     */
221    public String getName() {
222        StringBuffer names = new StringBuffer();
223
224        if (name != null) {
225            names.append('"');
226            names.append(name);
227            names.append('"');
228        }
229        else {
230            names.append("NOT_SET");
231        }
232
233        if (formerNames.size() > 0) {
234
235            names.append(" (aka ");
236
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            }
246
247            names.append(")");
248        }
249
250        return names.toString();
251    }
252
253    /**
254     * <p>
255     * Returns the title of the specified GUI element.
256     * </p>
257     *
258     * @return the title
259     */
260    public String getType() {
261        return type;
262    }
263
264    /**
265     * <p>
266     * Returns the icon associated with the specified GUI element.
267     * </p>
268     *
269     * @return the icon
270     */
271    public String getIcon() {
272        return icon;
273    }
274
275    /**
276     * <p>
277     * Returns the index of the specified GUI element in its parent element.
278     * </p>
279     *
280     * @return the index
281     */
282    public int getIndex() {
283        return index;
284    }
285
286    /**
287     * <p>
288     * Returns the object hash of the specified GUI element.
289     * </p>
290     *
291     * @return the elementHash
292     */
293    public int getElementHash() {
294        return elementHash;
295    }
296
297    /**
298     * <p>
299     * Sets the name of the specified GUI element.
300     * </p>
301     *
302     * @param newName
303     *            the name
304     */
305    public void setName(String newName) {
306        if ((this.name != null) && (!this.name.equals(newName)) &&
307            (!this.formerNames.contains(this.name)))
308        {
309            this.formerNames.add(this.name);
310        }
311
312        this.name = newName;
313    }
314
315    /**
316     * <p>
317     * Sets the type of the specified GUI element.
318     * </p>
319     *
320     * @param type
321     *            the type
322     */
323    public void setType(String type) {
324        this.type = type;
325    }
326
327    /**
328     * <p>
329     * Sets the icon associated with the specified GUI element.
330     * </p>
331     *
332     * @param icon
333     *            the icon
334     */
335    public void setIcon(String icon) {
336        this.icon = icon;
337    }
338
339    /**
340     * <p>
341     * Sets the index in its parent element of the specified GUI element.
342     * </p>
343     *
344     * @param index
345     *            the index
346     */
347    public void setIndex(int index) {
348        this.index = index;
349    }
350
351    /**
352     * <p>
353     * Sets the object hash of the specified GUI element.
354     * </p>
355     *
356     * @param newElementHash
357     *            the element hash
358     */
359    public void setElementHash(int newElementHash) {
360        if ((this.elementHash > -1) && !this.formerElementHashes.contains(this.elementHash)) {
361            this.formerElementHashes.add(this.elementHash);
362        }
363
364        this.elementHash = newElementHash;
365    }
366
367    /**
368     * <p>
369     * Updates the specification with another specification.
370     * </p>
371     *
372     * @param furtherSpec
373     *            specification used to update the current specification
374     */
375    void update(JFCGUIElementSpec other) {
376        if (other != this) {
377            for (int formerElementHash : other.formerElementHashes) {
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
389            if ((name != other.name) && (name != null) && (!name.equals(other.name))) {
390                setName(other.name);
391            }
392        }
393    }
394
395    /*
396     * (non-Javadoc)
397     *
398     * @see java.lang.Object#toString()
399     */
400    public String toString() {
401        return "[" + getName() + ";\"" + type + "\";\"" + icon + "\";" + index + ";" + elementHash +
402            "]";
403    }
404
405}
Note: See TracBrowser for help on using the repository browser.