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