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