[619] | 1 | package de.ugoe.cs.quest.plugin.mfc.guimodel;
|
---|
| 2 |
|
---|
[1] | 3 | import java.util.ArrayList;
|
---|
| 4 | import java.util.List;
|
---|
| 5 |
|
---|
[619] | 6 | import de.ugoe.cs.quest.eventcore.guimodel.IGUIElementSpec;
|
---|
[1] | 7 | import de.ugoe.cs.util.StringTools;
|
---|
| 8 |
|
---|
| 9 | /**
|
---|
| 10 | * <p>
|
---|
[619] | 11 | * This class implements a node in the {@link WindowTree} that is maintained during parsing a
|
---|
| 12 | * session.
|
---|
[1] | 13 | * </p>
|
---|
| 14 | * <p>
|
---|
[619] | 15 | * The window tree is structure that contains the hierarchy of the windows of a application as well
|
---|
| 16 | * as basic information about each window: the hwnd; its name; its resource id; its class name.
|
---|
[1] | 17 | * </p>
|
---|
| 18 | *
|
---|
| 19 | * @author Steffen Herbold
|
---|
[171] | 20 | * @version 1.0
|
---|
[1] | 21 | */
|
---|
[619] | 22 | public class MFCGUIElementSpec implements IGUIElementSpec {
|
---|
[1] | 23 |
|
---|
[619] | 24 | /**
|
---|
| 25 | * <p>
|
---|
| 26 | * current name of the window
|
---|
| 27 | * </p>
|
---|
| 28 | */
|
---|
| 29 | private String name;
|
---|
[1] | 30 |
|
---|
[619] | 31 | /**
|
---|
| 32 | * <p>
|
---|
| 33 | * previous names of the window as it may have changed over time.
|
---|
| 34 | * </p>
|
---|
| 35 | */
|
---|
| 36 | private List<String> formerNames = new ArrayList<String>();
|
---|
[1] | 37 |
|
---|
[619] | 38 | /**
|
---|
| 39 | * <p>
|
---|
| 40 | * Handle of the window. Used as unique identifier during its existence.
|
---|
| 41 | * </p>
|
---|
| 42 | */
|
---|
| 43 | private long hwnd;
|
---|
[1] | 44 |
|
---|
[619] | 45 | /**
|
---|
| 46 | * <p>
|
---|
| 47 | * previous handles of the window as the window may have been destroyed and recreated
|
---|
| 48 | * </p>
|
---|
| 49 | */
|
---|
| 50 | private List<Long> formerHwnds = new ArrayList<Long>();
|
---|
[1] | 51 |
|
---|
[619] | 52 | /**
|
---|
| 53 | * <p>
|
---|
| 54 | * Resource id of the window.
|
---|
| 55 | * </p>
|
---|
| 56 | */
|
---|
| 57 | private final int resourceId;
|
---|
[1] | 58 |
|
---|
[619] | 59 | /**
|
---|
| 60 | * <p>
|
---|
| 61 | * type (class name) of the window.
|
---|
| 62 | * </p>
|
---|
| 63 | */
|
---|
| 64 | private final String type;
|
---|
[1] | 65 |
|
---|
[619] | 66 | /**
|
---|
| 67 | * <p>
|
---|
| 68 | * True, if the window is modal.
|
---|
| 69 | * </p>
|
---|
| 70 | */
|
---|
| 71 | private final boolean isModal;
|
---|
[171] | 72 |
|
---|
[619] | 73 | /**
|
---|
| 74 | * <p>
|
---|
| 75 | * Creates a new WindowTreeNode.
|
---|
| 76 | * </p>
|
---|
| 77 | * <p>
|
---|
| 78 | * The constructor is protected WindowTreeNode may only be created from the WindowTree.
|
---|
| 79 | * </p>
|
---|
| 80 | *
|
---|
| 81 | * @param hwnd
|
---|
| 82 | * hwnd of the window
|
---|
| 83 | * @param parent
|
---|
| 84 | * reference to the parent's WindowTreeNode
|
---|
| 85 | * @param name
|
---|
| 86 | * name of the window
|
---|
| 87 | * @param resourceId
|
---|
| 88 | * resource id of the window
|
---|
| 89 | * @param type
|
---|
| 90 | * type, i.e. class name of the window
|
---|
| 91 | * @param isModal
|
---|
| 92 | * modality of the window
|
---|
| 93 | */
|
---|
| 94 | protected MFCGUIElementSpec(long hwnd,
|
---|
| 95 | String name,
|
---|
| 96 | int resourceId,
|
---|
| 97 | String type,
|
---|
| 98 | boolean isModal)
|
---|
| 99 | {
|
---|
| 100 | this.hwnd = hwnd;
|
---|
| 101 | this.name = name;
|
---|
| 102 | this.resourceId = resourceId;
|
---|
| 103 | this.type = type;
|
---|
| 104 | this.isModal = isModal;
|
---|
| 105 | }
|
---|
[1] | 106 |
|
---|
[619] | 107 | /**
|
---|
| 108 | * <p>
|
---|
| 109 | * Returns the name of the window.
|
---|
| 110 | * </p>
|
---|
| 111 | *
|
---|
| 112 | * @return name of the window
|
---|
| 113 | */
|
---|
| 114 | public String getName() {
|
---|
| 115 | StringBuffer names = new StringBuffer();
|
---|
| 116 |
|
---|
| 117 | if (name != null) {
|
---|
| 118 | names.append('"');
|
---|
| 119 | names.append(name);
|
---|
| 120 | names.append('"');
|
---|
| 121 | }
|
---|
| 122 | else {
|
---|
| 123 | names.append("NOT_SET");
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | if (formerNames.size() > 0) {
|
---|
| 127 |
|
---|
| 128 | names.append(" (aka ");
|
---|
| 129 |
|
---|
| 130 | for (int i = 0; i < formerNames.size(); i++) {
|
---|
| 131 | if (i > 0) {
|
---|
| 132 | names.append("/");
|
---|
| 133 | }
|
---|
[1] | 134 |
|
---|
[619] | 135 | names.append('"');
|
---|
| 136 | names.append(formerNames.get(i));
|
---|
| 137 | names.append('"');
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | names.append(")");
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | return names.toString();
|
---|
| 144 | }
|
---|
[1] | 145 |
|
---|
[619] | 146 | /**
|
---|
| 147 | * <p>
|
---|
| 148 | * Returns the hwnd of the window.
|
---|
| 149 | * </p>
|
---|
| 150 | *
|
---|
| 151 | * @return hwnd of the window
|
---|
| 152 | */
|
---|
| 153 | public long getHwnd() {
|
---|
| 154 | return hwnd;
|
---|
| 155 | }
|
---|
[1] | 156 |
|
---|
[619] | 157 | /**
|
---|
| 158 | * <p>
|
---|
| 159 | * Returns the resource id of the window.
|
---|
| 160 | * </p>
|
---|
| 161 | *
|
---|
| 162 | * @return resource id of the window
|
---|
| 163 | */
|
---|
| 164 | public int getResourceId() {
|
---|
| 165 | return resourceId;
|
---|
| 166 | }
|
---|
[1] | 167 |
|
---|
[619] | 168 | /* (non-Javadoc)
|
---|
| 169 | * @see de.ugoe.cs.quest.eventcore.guimodel.IGUIElementSpec#getType()
|
---|
| 170 | */
|
---|
| 171 | @Override
|
---|
| 172 | public String getType() {
|
---|
| 173 | return type;
|
---|
| 174 | }
|
---|
[1] | 175 |
|
---|
[619] | 176 | /**
|
---|
| 177 | * <p>
|
---|
| 178 | * TODO: comment
|
---|
| 179 | * </p>
|
---|
| 180 | *
|
---|
| 181 | * @return
|
---|
| 182 | */
|
---|
| 183 | public boolean isModal() {
|
---|
| 184 | return isModal;
|
---|
| 185 | }
|
---|
[1] | 186 |
|
---|
[619] | 187 | /**
|
---|
| 188 | * <p>
|
---|
| 189 | * Sets the name of the window.
|
---|
| 190 | * </p>
|
---|
| 191 | *
|
---|
| 192 | * @param text
|
---|
| 193 | * new name of the window
|
---|
| 194 | */
|
---|
| 195 | public void setName(String newName) {
|
---|
| 196 | if ((this.name != null) &&
|
---|
| 197 | (!this.name.equals(newName)) &&
|
---|
| 198 | (!this.formerNames.contains(this.name)))
|
---|
| 199 | {
|
---|
| 200 | this.formerNames.add(this.name);
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | this.name = newName;
|
---|
| 204 | }
|
---|
[1] | 205 |
|
---|
[619] | 206 | /**
|
---|
| 207 | * <p>
|
---|
| 208 | * Sets the hwnd of the window.
|
---|
| 209 | * </p>
|
---|
| 210 | *
|
---|
| 211 | * @param text
|
---|
| 212 | * new name of the window
|
---|
| 213 | */
|
---|
| 214 | public void setHwnd(long newHwnd) {
|
---|
| 215 | if (!this.formerHwnds.contains(this.hwnd)) {
|
---|
| 216 | this.formerHwnds.add(this.hwnd);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | this.hwnd = newHwnd;
|
---|
| 220 | }
|
---|
[1] | 221 |
|
---|
[619] | 222 | /* (non-Javadoc)
|
---|
| 223 | * @see de.ugoe.cs.quest.eventcore.guimodel.IGUIElementSpec#getSimilarity(de.ugoe.cs.quest.eventcore.guimodel.IGUIElementSpec)
|
---|
| 224 | */
|
---|
| 225 | @Override
|
---|
| 226 | public boolean getSimilarity(IGUIElementSpec other) {
|
---|
| 227 |
|
---|
| 228 | if (this == other) {
|
---|
| 229 | return true;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | if (!(other instanceof MFCGUIElementSpec)) {
|
---|
| 233 | return false;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | MFCGUIElementSpec otherSpec = (MFCGUIElementSpec) other;
|
---|
[1] | 237 |
|
---|
[619] | 238 | if ((type != otherSpec.type) && ((type != null) && (!type.equals(otherSpec.type)))) {
|
---|
| 239 | return false;
|
---|
| 240 | }
|
---|
[1] | 241 |
|
---|
[619] | 242 | if (isModal != otherSpec.isModal) {
|
---|
| 243 | return false;
|
---|
| 244 | }
|
---|
[1] | 245 |
|
---|
[619] | 246 | if (resourceId != otherSpec.resourceId) {
|
---|
| 247 | return false;
|
---|
| 248 | }
|
---|
[171] | 249 |
|
---|
[619] | 250 | // up to now, we compared, if the basics match. Now lets compare the id and the
|
---|
| 251 | // name. Both may change. The name may be reset (e.g. the title of a frame using the
|
---|
| 252 | // asterisk in the case data was changed). The id may change if e.g. a dialog is closed
|
---|
| 253 | // and reopend, i.e. a new instance is created. If one of them stays the same, then
|
---|
| 254 | // similarity is given. Therefore these are the first two comparisons
|
---|
| 255 |
|
---|
| 256 | if (hwnd == otherSpec.hwnd) {
|
---|
| 257 | return true;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | if ((name != null) && (name.equals(otherSpec.name))) {
|
---|
| 261 | return true;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | if ((((name == null) && (otherSpec.name == null)) ||
|
---|
| 265 | (("".equals(name)) && ("".equals(otherSpec.name)))) &&
|
---|
| 266 | (formerNames.size() == 0) && (otherSpec.formerNames.size() == 0))
|
---|
| 267 | {
|
---|
| 268 | return true;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | // if the hwnd and the name did not stay the same, then the name should be checked first.
|
---|
[715] | 272 | // The current name of one of the specs must be contained in the former names of the
|
---|
[619] | 273 | // respective other spec for similarity. Either of the specs should contain the name of the
|
---|
| 274 | // respective other spec in its former names. We can rely on this, as in the MFC context
|
---|
| 275 | // we get to know each name change. I.e. although currently the names of the specs differ,
|
---|
| 276 | // once they were identical. But it is sufficient to do it for the current names of the
|
---|
| 277 | // elements, as only one of them may have experienced more name changes then the other.
|
---|
[1] | 278 |
|
---|
[715] | 279 | if ((otherSpec.name != null) && formerNames.contains(otherSpec.name)) {
|
---|
[619] | 280 | return true;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | if ((name != null) && otherSpec.formerNames.contains(name)) {
|
---|
| 284 | return true;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
[715] | 287 | // ok. Even the names do not match. This is usually a clear indication, that the elements
|
---|
[619] | 288 | // are distinct. However, we check, if the former handles matched. This is very unlikely
|
---|
| 289 | // to happen. But it may occur, if a GUI element does not have a name or its name stays
|
---|
| 290 | // the empty string and if this GUI element is created, destroyed, and created again.
|
---|
| 291 |
|
---|
| 292 | if (formerHwnds.contains(otherSpec.hwnd) || otherSpec.formerHwnds.contains(hwnd)) {
|
---|
| 293 | return true;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | // now we can be really sure, that the GUI elements differ
|
---|
| 297 |
|
---|
| 298 | return false;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | /* (non-Javadoc)
|
---|
| 302 | * @see de.ugoe.cs.quest.eventcore.guimodel.IGUIElementSpec#equals(IGUIElementSpec)
|
---|
| 303 | */
|
---|
| 304 | @Override
|
---|
| 305 | public boolean equals(IGUIElementSpec other) {
|
---|
| 306 |
|
---|
| 307 | if (this == other) {
|
---|
| 308 | return true;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | if (!(other instanceof MFCGUIElementSpec)) {
|
---|
| 312 | return false;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | MFCGUIElementSpec otherSpec = (MFCGUIElementSpec) other;
|
---|
| 316 |
|
---|
| 317 | return
|
---|
| 318 | (hwnd == otherSpec.hwnd) && (isModal == otherSpec.isModal) &&
|
---|
| 319 | (resourceId == otherSpec.resourceId) &&
|
---|
| 320 | ((type == otherSpec.type) || ((type != null) && (type.equals(otherSpec.type)))) &&
|
---|
| 321 | ((name == otherSpec.name) || ((name != null) && (name.equals(otherSpec.name))));
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | /* (non-Javadoc)
|
---|
| 325 | * @see java.lang.Object#hashCode()
|
---|
| 326 | */
|
---|
| 327 | @Override
|
---|
| 328 | public int hashCode() {
|
---|
| 329 | // reuse only invariable elements
|
---|
| 330 | return (type + isModal + resourceId).hashCode();
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | /**
|
---|
| 334 | * <p>
|
---|
| 335 | * Returns a string identfier of the window:<br>
|
---|
| 336 | * {@code [resourceId;"windowName";"className";modality]}
|
---|
| 337 | * </p>
|
---|
| 338 | *
|
---|
| 339 | * @return identifier string of the window
|
---|
| 340 | */
|
---|
| 341 | @Override
|
---|
| 342 | public String toString() {
|
---|
| 343 | return "[" + resourceId + ";" + getName() + ";\"" + type + "\";" + isModal + ";" +
|
---|
| 344 | hwnd + "]";
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | /**
|
---|
| 348 | * <p>
|
---|
| 349 | * TODO: comment
|
---|
| 350 | * </p>
|
---|
| 351 | */
|
---|
| 352 | String toXML() {
|
---|
| 353 | return
|
---|
| 354 | "<window name=\"" + (name != null ? StringTools.xmlEntityReplacement(name) : "") +
|
---|
| 355 | "\" class=\"" + StringTools.xmlEntityReplacement(type) +
|
---|
| 356 | "\" resourceId=\"" + resourceId + "\" isModal=\"" +
|
---|
[648] | 357 | isModal + "\"/>";
|
---|
[619] | 358 | }
|
---|
| 359 |
|
---|
| 360 | /**
|
---|
| 361 | * <p>
|
---|
| 362 | * TODO: comment
|
---|
| 363 | * </p>
|
---|
| 364 | *
|
---|
| 365 | * @param furtherSpec
|
---|
| 366 | */
|
---|
| 367 | void update(IGUIElementSpec furtherSpec) {
|
---|
| 368 | MFCGUIElementSpec other = (MFCGUIElementSpec) furtherSpec;
|
---|
| 369 |
|
---|
| 370 | if (other != this) {
|
---|
| 371 | for (long formerHwnd : other.formerHwnds) {
|
---|
| 372 | setHwnd(formerHwnd);
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | if (hwnd != other.hwnd) {
|
---|
| 376 | hwnd = other.hwnd;
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | for (String formerName : other.formerNames) {
|
---|
| 380 | setName(formerName);
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | if ((name != other.name) && (name != null) && (!name.equals(other.name)))
|
---|
| 384 | {
|
---|
| 385 | setName(other.name);
|
---|
| 386 | }
|
---|
| 387 | }
|
---|
| 388 | }
|
---|
| 389 |
|
---|
[1] | 390 | }
|
---|