[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> |
---|
[1009] | 79 | * Hash code of the GUI element. Used as unique identifier during its existence. |
---|
[714] | 80 | * </p> |
---|
| 81 | */ |
---|
[743] | 82 | private int elementHash = -1; |
---|
[714] | 83 | |
---|
| 84 | /** |
---|
| 85 | * <p> |
---|
[1009] | 86 | * Previous hashes of the GUI element as the GUI element may have been destroyed and recreated. |
---|
[714] | 87 | * </p> |
---|
| 88 | */ |
---|
[743] | 89 | private List<Integer> formerElementHashes = new ArrayList<Integer>(); |
---|
[984] | 90 | |
---|
| 91 | /** |
---|
| 92 | * <p> |
---|
[990] | 93 | * Type hierarchy of the class itself |
---|
[984] | 94 | * </p> |
---|
| 95 | */ |
---|
[990] | 96 | private List<String> typeHierarchy = null; |
---|
[714] | 97 | |
---|
[835] | 98 | /* |
---|
| 99 | * (non-Javadoc) |
---|
| 100 | * |
---|
| 101 | * @see |
---|
[922] | 102 | * de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#getSecificationSimilarity(IGUIElementSpec |
---|
[835] | 103 | * ) |
---|
[573] | 104 | */ |
---|
| 105 | @Override |
---|
[589] | 106 | public boolean getSimilarity(IGUIElementSpec other) { |
---|
[835] | 107 | if (this == other) { |
---|
[589] | 108 | return true; |
---|
[573] | 109 | } |
---|
[835] | 110 | |
---|
| 111 | if (!(other instanceof JFCGUIElementSpec)) { |
---|
[589] | 112 | return false; |
---|
[573] | 113 | } |
---|
[835] | 114 | |
---|
[573] | 115 | JFCGUIElementSpec otherSpec = (JFCGUIElementSpec) other; |
---|
[835] | 116 | |
---|
[714] | 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 |
---|
[835] | 131 | |
---|
[743] | 132 | if (elementHash == otherSpec.elementHash) { |
---|
[714] | 133 | return true; |
---|
| 134 | } |
---|
[835] | 135 | |
---|
[714] | 136 | if ((name != null) && (name.equals(otherSpec.name))) { |
---|
| 137 | return true; |
---|
[573] | 138 | } |
---|
[835] | 139 | |
---|
| 140 | if ((((name == null) && (otherSpec.name == null)) || (("".equals(name)) && ("" |
---|
| 141 | .equals(otherSpec.name)))) && |
---|
| 142 | (formerNames.size() == 0) && |
---|
| 143 | (otherSpec.formerNames.size() == 0)) |
---|
[714] | 144 | { |
---|
| 145 | return true; |
---|
[600] | 146 | } |
---|
[835] | 147 | |
---|
[714] | 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 | } |
---|
[835] | 160 | |
---|
[714] | 161 | if (CollectionUtils.containsAny(formerNames, otherSpec.formerNames)) { |
---|
| 162 | return index == otherSpec.index; |
---|
| 163 | } |
---|
[835] | 164 | |
---|
[714] | 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 | } |
---|
[835] | 178 | |
---|
[714] | 179 | if (CollectionUtils.containsAny(formerElementHashes, otherSpec.formerElementHashes)) { |
---|
| 180 | return index == otherSpec.index; |
---|
| 181 | } |
---|
[835] | 182 | |
---|
[714] | 183 | // now we can be really sure, that the GUI elements differ |
---|
[835] | 184 | |
---|
[714] | 185 | return false; |
---|
[573] | 186 | } |
---|
| 187 | |
---|
[835] | 188 | /* |
---|
| 189 | * (non-Javadoc) |
---|
| 190 | * |
---|
[922] | 191 | * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#equals(IGUIElementSpec) |
---|
[573] | 192 | */ |
---|
| 193 | @Override |
---|
[832] | 194 | public boolean equals(Object other) { |
---|
[835] | 195 | if (this == other) { |
---|
[573] | 196 | return true; |
---|
| 197 | } |
---|
[835] | 198 | |
---|
| 199 | if (!(other instanceof JFCGUIElementSpec)) { |
---|
[573] | 200 | return false; |
---|
| 201 | } |
---|
[835] | 202 | |
---|
[573] | 203 | JFCGUIElementSpec otherSpec = (JFCGUIElementSpec) other; |
---|
[835] | 204 | |
---|
| 205 | return ((name == otherSpec.name) || ((name != null) && (name.equals(otherSpec.name)))) && |
---|
[573] | 206 | ((type == otherSpec.type) || ((type != null) && (type.equals(otherSpec.type)))) && |
---|
| 207 | ((icon == otherSpec.icon) || ((icon != null) && (icon.equals(otherSpec.icon)))) && |
---|
[743] | 208 | (index == otherSpec.index) && (elementHash == otherSpec.elementHash); |
---|
[573] | 209 | } |
---|
| 210 | |
---|
[835] | 211 | /* |
---|
| 212 | * (non-Javadoc) |
---|
| 213 | * |
---|
[600] | 214 | * @see java.lang.Object#hashCode() |
---|
| 215 | */ |
---|
| 216 | @Override |
---|
| 217 | public int hashCode() { |
---|
| 218 | return (name + type + icon + index + elementHash).hashCode(); |
---|
| 219 | } |
---|
| 220 | |
---|
[573] | 221 | /** |
---|
[835] | 222 | * <p> |
---|
| 223 | * Returns the name of the specified GUI element. |
---|
| 224 | * </p> |
---|
| 225 | * |
---|
[573] | 226 | * @return the name |
---|
| 227 | */ |
---|
| 228 | public String getName() { |
---|
[714] | 229 | StringBuffer names = new StringBuffer(); |
---|
[835] | 230 | |
---|
[714] | 231 | if (name != null) { |
---|
| 232 | names.append('"'); |
---|
| 233 | names.append(name); |
---|
| 234 | names.append('"'); |
---|
[589] | 235 | } |
---|
[714] | 236 | else { |
---|
| 237 | names.append("NOT_SET"); |
---|
| 238 | } |
---|
[835] | 239 | |
---|
[714] | 240 | if (formerNames.size() > 0) { |
---|
[835] | 241 | |
---|
[714] | 242 | names.append(" (aka "); |
---|
[835] | 243 | |
---|
[714] | 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 | } |
---|
[835] | 253 | |
---|
[714] | 254 | names.append(")"); |
---|
| 255 | } |
---|
[835] | 256 | |
---|
[714] | 257 | return names.toString(); |
---|
[573] | 258 | } |
---|
| 259 | |
---|
| 260 | /** |
---|
[835] | 261 | * <p> |
---|
| 262 | * Returns the title of the specified GUI element. |
---|
| 263 | * </p> |
---|
| 264 | * |
---|
[573] | 265 | * @return the title |
---|
| 266 | */ |
---|
| 267 | public String getType() { |
---|
| 268 | return type; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | /** |
---|
[835] | 272 | * <p> |
---|
| 273 | * Returns the icon associated with the specified GUI element. |
---|
| 274 | * </p> |
---|
| 275 | * |
---|
[573] | 276 | * @return the icon |
---|
| 277 | */ |
---|
| 278 | public String getIcon() { |
---|
| 279 | return icon; |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | /** |
---|
[835] | 283 | * <p> |
---|
| 284 | * Returns the index of the specified GUI element in its parent element. |
---|
| 285 | * </p> |
---|
| 286 | * |
---|
[573] | 287 | * @return the index |
---|
| 288 | */ |
---|
| 289 | public int getIndex() { |
---|
| 290 | return index; |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | /** |
---|
[835] | 294 | * <p> |
---|
| 295 | * Returns the object hash of the specified GUI element. |
---|
| 296 | * </p> |
---|
| 297 | * |
---|
[573] | 298 | * @return the elementHash |
---|
| 299 | */ |
---|
[743] | 300 | public int getElementHash() { |
---|
[714] | 301 | return elementHash; |
---|
[573] | 302 | } |
---|
| 303 | |
---|
| 304 | /** |
---|
[835] | 305 | * <p> |
---|
| 306 | * Sets the name of the specified GUI element. |
---|
| 307 | * </p> |
---|
| 308 | * |
---|
[849] | 309 | * @param newName |
---|
[835] | 310 | * the name |
---|
[573] | 311 | */ |
---|
[714] | 312 | public void setName(String newName) { |
---|
[835] | 313 | if ((this.name != null) && (!this.name.equals(newName)) && |
---|
[714] | 314 | (!this.formerNames.contains(this.name))) |
---|
| 315 | { |
---|
| 316 | this.formerNames.add(this.name); |
---|
[589] | 317 | } |
---|
[835] | 318 | |
---|
[714] | 319 | this.name = newName; |
---|
[573] | 320 | } |
---|
| 321 | |
---|
| 322 | /** |
---|
[835] | 323 | * <p> |
---|
| 324 | * Sets the type of the specified GUI element. |
---|
| 325 | * </p> |
---|
| 326 | * |
---|
[849] | 327 | * @param type |
---|
| 328 | * the type |
---|
[573] | 329 | */ |
---|
| 330 | public void setType(String type) { |
---|
| 331 | this.type = type; |
---|
| 332 | } |
---|
| 333 | |
---|
| 334 | /** |
---|
[835] | 335 | * <p> |
---|
| 336 | * Sets the icon associated with the specified GUI element. |
---|
| 337 | * </p> |
---|
| 338 | * |
---|
| 339 | * @param icon |
---|
| 340 | * the icon |
---|
[573] | 341 | */ |
---|
| 342 | public void setIcon(String icon) { |
---|
| 343 | this.icon = icon; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | /** |
---|
[835] | 347 | * <p> |
---|
| 348 | * Sets the index in its parent element of the specified GUI element. |
---|
| 349 | * </p> |
---|
| 350 | * |
---|
| 351 | * @param index |
---|
| 352 | * the index |
---|
[573] | 353 | */ |
---|
| 354 | public void setIndex(int index) { |
---|
| 355 | this.index = index; |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | /** |
---|
[835] | 359 | * <p> |
---|
| 360 | * Sets the object hash of the specified GUI element. |
---|
| 361 | * </p> |
---|
| 362 | * |
---|
[849] | 363 | * @param newElementHash |
---|
| 364 | * the element hash |
---|
[573] | 365 | */ |
---|
[743] | 366 | public void setElementHash(int newElementHash) { |
---|
[835] | 367 | if ((this.elementHash > -1) && !this.formerElementHashes.contains(this.elementHash)) { |
---|
[714] | 368 | this.formerElementHashes.add(this.elementHash); |
---|
[589] | 369 | } |
---|
[835] | 370 | |
---|
[714] | 371 | this.elementHash = newElementHash; |
---|
[573] | 372 | } |
---|
[984] | 373 | |
---|
| 374 | /** |
---|
| 375 | * <p> |
---|
[990] | 376 | * Sets the type hierarchy of the specified GUI element. |
---|
| 377 | * @param typeHierarchy |
---|
[984] | 378 | * </p> |
---|
| 379 | */ |
---|
[990] | 380 | public void setTypeHierarchy(List<String> typeHierarchy){ |
---|
| 381 | this.typeHierarchy = typeHierarchy; |
---|
[984] | 382 | } |
---|
[835] | 383 | |
---|
[714] | 384 | /** |
---|
| 385 | * <p> |
---|
[835] | 386 | * Updates the specification with another specification. |
---|
[714] | 387 | * </p> |
---|
[835] | 388 | * |
---|
[714] | 389 | * @param furtherSpec |
---|
[835] | 390 | * specification used to update the current specification |
---|
[714] | 391 | */ |
---|
| 392 | void update(JFCGUIElementSpec other) { |
---|
| 393 | if (other != this) { |
---|
[743] | 394 | for (int formerElementHash : other.formerElementHashes) { |
---|
[714] | 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 | |
---|
[835] | 406 | if ((name != other.name) && (name != null) && (!name.equals(other.name))) { |
---|
[714] | 407 | setName(other.name); |
---|
| 408 | } |
---|
| 409 | } |
---|
| 410 | } |
---|
| 411 | |
---|
[835] | 412 | /* |
---|
| 413 | * (non-Javadoc) |
---|
| 414 | * |
---|
| 415 | * @see java.lang.Object#toString() |
---|
| 416 | */ |
---|
[607] | 417 | public String toString() { |
---|
[835] | 418 | return "[" + getName() + ";\"" + type + "\";\"" + icon + "\";" + index + ";" + elementHash + |
---|
| 419 | "]"; |
---|
[607] | 420 | } |
---|
[573] | 421 | |
---|
[966] | 422 | @Override |
---|
[990] | 423 | public String[] getTypeHierarchy() { |
---|
| 424 | if (typeHierarchy == null){ |
---|
[984] | 425 | return new String[]{(getType())}; |
---|
| 426 | } |
---|
| 427 | else |
---|
[990] | 428 | return typeHierarchy.toArray(new String[typeHierarchy.size()]); |
---|
[966] | 429 | } |
---|
| 430 | |
---|
[573] | 431 | } |
---|