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 | |
---|
15 | package de.ugoe.cs.autoquest.plugin.jfc.guimodel; |
---|
16 | |
---|
17 | import java.util.ArrayList; |
---|
18 | import java.util.List; |
---|
19 | |
---|
20 | import org.apache.commons.collections15.CollectionUtils; |
---|
21 | |
---|
22 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; |
---|
23 | import 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 | */ |
---|
33 | public 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 != otherSpec.type) && ((type != null) && (!type.equals(otherSpec.type)))) { |
---|
118 | return false; |
---|
119 | } |
---|
120 | |
---|
121 | if ((icon != otherSpec.icon) && ((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 ((name == otherSpec.name) || ((name != null) && (name.equals(otherSpec.name)))) && |
---|
206 | ((type == otherSpec.type) || ((type != null) && (type.equals(otherSpec.type)))) && |
---|
207 | ((icon == otherSpec.icon) || ((icon != null) && (icon.equals(otherSpec.icon)))) && |
---|
208 | (index == otherSpec.index) && (elementHash == otherSpec.elementHash); |
---|
209 | } |
---|
210 | |
---|
211 | /* |
---|
212 | * (non-Javadoc) |
---|
213 | * |
---|
214 | * @see java.lang.Object#hashCode() |
---|
215 | */ |
---|
216 | @Override |
---|
217 | public int hashCode() { |
---|
218 | return (name + type + icon + index + elementHash).hashCode(); |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * <p> |
---|
223 | * Returns the name of the specified GUI element. |
---|
224 | * </p> |
---|
225 | * |
---|
226 | * @return the name |
---|
227 | */ |
---|
228 | public String getName() { |
---|
229 | StringBuffer names = new StringBuffer(); |
---|
230 | |
---|
231 | if (name != null) { |
---|
232 | names.append('"'); |
---|
233 | names.append(name); |
---|
234 | names.append('"'); |
---|
235 | } |
---|
236 | else { |
---|
237 | names.append("NOT_SET"); |
---|
238 | } |
---|
239 | |
---|
240 | if (formerNames.size() > 0) { |
---|
241 | |
---|
242 | names.append(" (aka "); |
---|
243 | |
---|
244 | for (int i = 0; i < formerNames.size(); i++) { |
---|
245 | if (i > 0) { |
---|
246 | names.append("/"); |
---|
247 | } |
---|
248 | |
---|
249 | names.append('"'); |
---|
250 | names.append(formerNames.get(i)); |
---|
251 | names.append('"'); |
---|
252 | } |
---|
253 | |
---|
254 | names.append(")"); |
---|
255 | } |
---|
256 | |
---|
257 | return names.toString(); |
---|
258 | } |
---|
259 | |
---|
260 | /** |
---|
261 | * <p> |
---|
262 | * Returns the title of the specified GUI element. |
---|
263 | * </p> |
---|
264 | * |
---|
265 | * @return the title |
---|
266 | */ |
---|
267 | public String getType() { |
---|
268 | return type; |
---|
269 | } |
---|
270 | |
---|
271 | /** |
---|
272 | * <p> |
---|
273 | * Returns the icon associated with the specified GUI element. |
---|
274 | * </p> |
---|
275 | * |
---|
276 | * @return the icon |
---|
277 | */ |
---|
278 | public String getIcon() { |
---|
279 | return icon; |
---|
280 | } |
---|
281 | |
---|
282 | /** |
---|
283 | * <p> |
---|
284 | * Returns the index of the specified GUI element in its parent element. |
---|
285 | * </p> |
---|
286 | * |
---|
287 | * @return the index |
---|
288 | */ |
---|
289 | public int getIndex() { |
---|
290 | return index; |
---|
291 | } |
---|
292 | |
---|
293 | /** |
---|
294 | * <p> |
---|
295 | * Returns the object hash of the specified GUI element. |
---|
296 | * </p> |
---|
297 | * |
---|
298 | * @return the elementHash |
---|
299 | */ |
---|
300 | public int getElementHash() { |
---|
301 | return elementHash; |
---|
302 | } |
---|
303 | |
---|
304 | /** |
---|
305 | * <p> |
---|
306 | * Sets the name of the specified GUI element. |
---|
307 | * </p> |
---|
308 | * |
---|
309 | * @param newName |
---|
310 | * the name |
---|
311 | */ |
---|
312 | public void setName(String newName) { |
---|
313 | if ((this.name != null) && (!this.name.equals(newName)) && |
---|
314 | (!this.formerNames.contains(this.name))) |
---|
315 | { |
---|
316 | this.formerNames.add(this.name); |
---|
317 | } |
---|
318 | |
---|
319 | this.name = newName; |
---|
320 | } |
---|
321 | |
---|
322 | /** |
---|
323 | * <p> |
---|
324 | * Sets the type of the specified GUI element. |
---|
325 | * </p> |
---|
326 | * |
---|
327 | * @param type |
---|
328 | * the type |
---|
329 | */ |
---|
330 | public void setType(String type) { |
---|
331 | this.type = type; |
---|
332 | } |
---|
333 | |
---|
334 | /** |
---|
335 | * <p> |
---|
336 | * Sets the icon associated with the specified GUI element. |
---|
337 | * </p> |
---|
338 | * |
---|
339 | * @param icon |
---|
340 | * the icon |
---|
341 | */ |
---|
342 | public void setIcon(String icon) { |
---|
343 | this.icon = icon; |
---|
344 | } |
---|
345 | |
---|
346 | /** |
---|
347 | * <p> |
---|
348 | * Sets the index in its parent element of the specified GUI element. |
---|
349 | * </p> |
---|
350 | * |
---|
351 | * @param index |
---|
352 | * the index |
---|
353 | */ |
---|
354 | public void setIndex(int index) { |
---|
355 | this.index = index; |
---|
356 | } |
---|
357 | |
---|
358 | /** |
---|
359 | * <p> |
---|
360 | * Sets the object hash of the specified GUI element. |
---|
361 | * </p> |
---|
362 | * |
---|
363 | * @param newElementHash |
---|
364 | * the element hash |
---|
365 | */ |
---|
366 | public void setElementHash(int newElementHash) { |
---|
367 | if ((this.elementHash > -1) && !this.formerElementHashes.contains(this.elementHash)) { |
---|
368 | this.formerElementHashes.add(this.elementHash); |
---|
369 | } |
---|
370 | |
---|
371 | this.elementHash = newElementHash; |
---|
372 | } |
---|
373 | |
---|
374 | /** |
---|
375 | * <p> |
---|
376 | * Sets the type hierarchy of the specified GUI element. |
---|
377 | * @param typeHierarchy |
---|
378 | * </p> |
---|
379 | */ |
---|
380 | public void setTypeHierarchy(List<String> typeHierarchy){ |
---|
381 | this.typeHierarchy = typeHierarchy; |
---|
382 | } |
---|
383 | |
---|
384 | /** |
---|
385 | * <p> |
---|
386 | * Updates the specification with another specification. |
---|
387 | * </p> |
---|
388 | * |
---|
389 | * @param furtherSpec |
---|
390 | * specification used to update the current specification |
---|
391 | */ |
---|
392 | void update(JFCGUIElementSpec other) { |
---|
393 | if (other != this) { |
---|
394 | for (int formerElementHash : other.formerElementHashes) { |
---|
395 | setElementHash(formerElementHash); |
---|
396 | } |
---|
397 | |
---|
398 | if (elementHash != other.elementHash) { |
---|
399 | elementHash = other.elementHash; |
---|
400 | } |
---|
401 | |
---|
402 | for (String formerName : other.formerNames) { |
---|
403 | setName(formerName); |
---|
404 | } |
---|
405 | |
---|
406 | if ((name != other.name) && (name != null) && (!name.equals(other.name))) { |
---|
407 | setName(other.name); |
---|
408 | } |
---|
409 | } |
---|
410 | } |
---|
411 | |
---|
412 | /* |
---|
413 | * (non-Javadoc) |
---|
414 | * |
---|
415 | * @see java.lang.Object#toString() |
---|
416 | */ |
---|
417 | public String toString() { |
---|
418 | return "[" + getName() + ";\"" + type + "\";\"" + icon + "\";" + index + ";" + elementHash + |
---|
419 | "]"; |
---|
420 | } |
---|
421 | |
---|
422 | @Override |
---|
423 | public String[] getTypeHierarchy() { |
---|
424 | if (typeHierarchy == null){ |
---|
425 | return new String[]{(getType())}; |
---|
426 | } |
---|
427 | else |
---|
428 | return typeHierarchy.toArray(new String[typeHierarchy.size()]); |
---|
429 | } |
---|
430 | |
---|
431 | } |
---|