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 MFCGUIElementSpec.
|
---|
97 | * </p>
|
---|
98 | *
|
---|
99 | * @param hwnd
|
---|
100 | * hwnd of the window
|
---|
101 | * @param name
|
---|
102 | * name of the window
|
---|
103 | * @param resourceId
|
---|
104 | * resource id of the window
|
---|
105 | * @param type
|
---|
106 | * type, i.e. class name of the window
|
---|
107 | * @param isModal
|
---|
108 | * modality of the window
|
---|
109 | */
|
---|
110 | public MFCGUIElementSpec(long hwnd, String name, int resourceId, String type, boolean isModal)
|
---|
111 | {
|
---|
112 | this.hwnd = hwnd;
|
---|
113 | this.name = name;
|
---|
114 | this.resourceId = resourceId;
|
---|
115 | this.type = type;
|
---|
116 | this.isModal = isModal;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * <p>
|
---|
121 | * Returns the name of the window.
|
---|
122 | * </p>
|
---|
123 | *
|
---|
124 | * @return name of the window
|
---|
125 | */
|
---|
126 | public String getName() {
|
---|
127 | StringBuffer names = new StringBuffer();
|
---|
128 |
|
---|
129 | if (name != null) {
|
---|
130 | names.append('"');
|
---|
131 | names.append(name);
|
---|
132 | names.append('"');
|
---|
133 | }
|
---|
134 | else {
|
---|
135 | names.append("NOT_SET");
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (formerNames.size() > 0) {
|
---|
139 |
|
---|
140 | names.append(" (aka ");
|
---|
141 |
|
---|
142 | for (int i = 0; i < formerNames.size(); i++) {
|
---|
143 | if (i > 0) {
|
---|
144 | names.append("/");
|
---|
145 | }
|
---|
146 |
|
---|
147 | names.append('"');
|
---|
148 | names.append(formerNames.get(i));
|
---|
149 | names.append('"');
|
---|
150 | }
|
---|
151 |
|
---|
152 | names.append(")");
|
---|
153 | }
|
---|
154 |
|
---|
155 | return names.toString();
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * <p>
|
---|
160 | * Returns the hwnd of the window.
|
---|
161 | * </p>
|
---|
162 | *
|
---|
163 | * @return hwnd of the window
|
---|
164 | */
|
---|
165 | public long getHwnd() {
|
---|
166 | return hwnd;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * <p>
|
---|
171 | * Returns the resource id of the window.
|
---|
172 | * </p>
|
---|
173 | *
|
---|
174 | * @return resource id of the window
|
---|
175 | */
|
---|
176 | public int getResourceId() {
|
---|
177 | return resourceId;
|
---|
178 | }
|
---|
179 |
|
---|
180 | /*
|
---|
181 | * (non-Javadoc)
|
---|
182 | *
|
---|
183 | * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#getType()
|
---|
184 | */
|
---|
185 | @Override
|
---|
186 | public String getType() {
|
---|
187 | return type;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * <p>
|
---|
192 | * Returns the modality of the specified GUI element.
|
---|
193 | * </p>
|
---|
194 | *
|
---|
195 | * @return the modality
|
---|
196 | */
|
---|
197 | public boolean isModal() {
|
---|
198 | return isModal;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * <p>
|
---|
203 | * Sets the name of the window.
|
---|
204 | * </p>
|
---|
205 | *
|
---|
206 | * @param text
|
---|
207 | * new name of the window
|
---|
208 | */
|
---|
209 | public void setName(String newName) {
|
---|
210 | if ((this.name != null) && (!this.name.equals(newName)) &&
|
---|
211 | (!this.formerNames.contains(this.name)))
|
---|
212 | {
|
---|
213 | this.formerNames.add(this.name);
|
---|
214 | }
|
---|
215 |
|
---|
216 | this.name = newName;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * <p>
|
---|
221 | * Sets the hwnd of the window.
|
---|
222 | * </p>
|
---|
223 | *
|
---|
224 | * @param text
|
---|
225 | * new name of the window
|
---|
226 | */
|
---|
227 | public void setHwnd(long newHwnd) {
|
---|
228 | if (!this.formerHwnds.contains(this.hwnd)) {
|
---|
229 | this.formerHwnds.add(this.hwnd);
|
---|
230 | }
|
---|
231 |
|
---|
232 | this.hwnd = newHwnd;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /*
|
---|
236 | * (non-Javadoc)
|
---|
237 | *
|
---|
238 | * @see
|
---|
239 | * de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#getSimilarity(de.ugoe.cs.autoquest.eventcore
|
---|
240 | * .guimodel.IGUIElementSpec)
|
---|
241 | */
|
---|
242 | @Override
|
---|
243 | public boolean getSimilarity(IGUIElementSpec other) {
|
---|
244 |
|
---|
245 | if (this == other) {
|
---|
246 | return true;
|
---|
247 | }
|
---|
248 |
|
---|
249 | if (!(other instanceof MFCGUIElementSpec)) {
|
---|
250 | return false;
|
---|
251 | }
|
---|
252 |
|
---|
253 | MFCGUIElementSpec otherSpec = (MFCGUIElementSpec) other;
|
---|
254 |
|
---|
255 | if ((type == null) ? otherSpec.type != null : !type.equals(otherSpec.type)) {
|
---|
256 | return false;
|
---|
257 | }
|
---|
258 |
|
---|
259 | if (isModal != otherSpec.isModal) {
|
---|
260 | return false;
|
---|
261 | }
|
---|
262 |
|
---|
263 | if (resourceId != otherSpec.resourceId) {
|
---|
264 | return false;
|
---|
265 | }
|
---|
266 |
|
---|
267 | // up to now, we compared, if the basics match. Now lets compare the id and the
|
---|
268 | // name. Both may change. The name may be reset (e.g. the title of a frame using the
|
---|
269 | // asterisk in the case data was changed). The id may change if e.g. a dialog is closed
|
---|
270 | // and reopend, i.e. a new instance is created. If one of them stays the same, then
|
---|
271 | // similarity is given. Therefore these are the first two comparisons
|
---|
272 |
|
---|
273 | if (hwnd == otherSpec.hwnd) {
|
---|
274 | return true;
|
---|
275 | }
|
---|
276 |
|
---|
277 | if ((name != null) && (name.equals(otherSpec.name))) {
|
---|
278 | return true;
|
---|
279 | }
|
---|
280 |
|
---|
281 | if ((((name == null) && (otherSpec.name == null)) || (("".equals(name)) && (""
|
---|
282 | .equals(otherSpec.name)))) &&
|
---|
283 | (formerNames.size() == 0) &&
|
---|
284 | (otherSpec.formerNames.size() == 0))
|
---|
285 | {
|
---|
286 | return true;
|
---|
287 | }
|
---|
288 |
|
---|
289 | // if the hwnd and the name did not stay the same, then the name should be checked first.
|
---|
290 | // The current name of one of the specs must be contained in the former names of the
|
---|
291 | // respective other spec for similarity. Either of the specs should contain the name of the
|
---|
292 | // respective other spec in its former names. We can rely on this, as in the MFC context
|
---|
293 | // we get to know each name change. I.e. although currently the names of the specs differ,
|
---|
294 | // once they were identical. But it is sufficient to do it for the current names of the
|
---|
295 | // elements, as only one of them may have experienced more name changes then the other.
|
---|
296 |
|
---|
297 | if ((otherSpec.name != null) && formerNames.contains(otherSpec.name)) {
|
---|
298 | return true;
|
---|
299 | }
|
---|
300 |
|
---|
301 | if ((name != null) && otherSpec.formerNames.contains(name)) {
|
---|
302 | return true;
|
---|
303 | }
|
---|
304 |
|
---|
305 | // ok. Even the names do not match. This is usually a clear indication, that the elements
|
---|
306 | // are distinct. However, we check, if the former handles matched. This is very unlikely
|
---|
307 | // to happen. But it may occur, if a GUI element does not have a name or its name stays
|
---|
308 | // the empty string and if this GUI element is created, destroyed, and created again.
|
---|
309 |
|
---|
310 | if (formerHwnds.contains(otherSpec.hwnd) || otherSpec.formerHwnds.contains(hwnd)) {
|
---|
311 | return true;
|
---|
312 | }
|
---|
313 |
|
---|
314 | // now we can be really sure, that the GUI elements differ
|
---|
315 |
|
---|
316 | return false;
|
---|
317 | }
|
---|
318 |
|
---|
319 | /*
|
---|
320 | * (non-Javadoc)
|
---|
321 | *
|
---|
322 | * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#equals(IGUIElementSpec)
|
---|
323 | */
|
---|
324 | @Override
|
---|
325 | public boolean equals(Object other) {
|
---|
326 |
|
---|
327 | if (this == other) {
|
---|
328 | return true;
|
---|
329 | }
|
---|
330 |
|
---|
331 | if (!(other instanceof MFCGUIElementSpec)) {
|
---|
332 | return false;
|
---|
333 | }
|
---|
334 |
|
---|
335 | MFCGUIElementSpec otherSpec = (MFCGUIElementSpec) other;
|
---|
336 |
|
---|
337 | return (hwnd == otherSpec.hwnd) && (isModal == otherSpec.isModal) &&
|
---|
338 | (resourceId == otherSpec.resourceId) &&
|
---|
339 | (type == null ? otherSpec.type == null : type.equals(otherSpec.type)) &&
|
---|
340 | (name == null ? otherSpec.name == null : name.equals(otherSpec.name));
|
---|
341 | }
|
---|
342 |
|
---|
343 | /*
|
---|
344 | * (non-Javadoc)
|
---|
345 | *
|
---|
346 | * @see java.lang.Object#hashCode()
|
---|
347 | */
|
---|
348 | @Override
|
---|
349 | public int hashCode() {
|
---|
350 | // reuse only invariable elements
|
---|
351 | return (type + isModal + resourceId).hashCode();
|
---|
352 | }
|
---|
353 |
|
---|
354 | /**
|
---|
355 | * <p>
|
---|
356 | * Returns a string identifier of the window:<br>
|
---|
357 | * {@code [resourceId;"windowName";"className";modality]}
|
---|
358 | * </p>
|
---|
359 | *
|
---|
360 | * @return identifier string of the window
|
---|
361 | */
|
---|
362 | @Override
|
---|
363 | public String toString() {
|
---|
364 | return "[" + resourceId + ";" + getName() + ";\"" + type + "\";" + isModal + ";" + hwnd +
|
---|
365 | "]";
|
---|
366 | }
|
---|
367 |
|
---|
368 | /**
|
---|
369 | * <p>
|
---|
370 | * Returns the XML representation of this specification.
|
---|
371 | * </p>
|
---|
372 | *
|
---|
373 | * @return the XML representation
|
---|
374 | */
|
---|
375 | String toXML() {
|
---|
376 | return "<window name=\"" + (name != null ? StringTools.xmlEntityReplacement(name) : "") +
|
---|
377 | "\" class=\"" + StringTools.xmlEntityReplacement(type) + "\" resourceId=\"" +
|
---|
378 | resourceId + "\" isModal=\"" + isModal + "\"/>";
|
---|
379 | }
|
---|
380 |
|
---|
381 | /**
|
---|
382 | * <p>
|
---|
383 | * Updates the specification with another specification.
|
---|
384 | * </p>
|
---|
385 | *
|
---|
386 | * @param furtherSpec
|
---|
387 | * specification used to update the current specification
|
---|
388 | */
|
---|
389 | void update(IGUIElementSpec furtherSpec) {
|
---|
390 | MFCGUIElementSpec other = (MFCGUIElementSpec) furtherSpec;
|
---|
391 |
|
---|
392 | if (other != this) {
|
---|
393 | for (long formerHwnd : other.formerHwnds) {
|
---|
394 | setHwnd(formerHwnd);
|
---|
395 | }
|
---|
396 |
|
---|
397 | if (hwnd != other.hwnd) {
|
---|
398 | hwnd = other.hwnd;
|
---|
399 | }
|
---|
400 |
|
---|
401 | for (String formerName : other.formerNames) {
|
---|
402 | setName(formerName);
|
---|
403 | }
|
---|
404 |
|
---|
405 | if (name == null ? other.name != null : !name.equals(other.name)) {
|
---|
406 | setName(other.name);
|
---|
407 | }
|
---|
408 | }
|
---|
409 | }
|
---|
410 |
|
---|
411 | @Override
|
---|
412 | public String[] getTypeHierarchy() {
|
---|
413 | return new String[] { type };
|
---|
414 | }
|
---|
415 |
|
---|
416 | }
|
---|