1 | package de.ugoe.cs.quest.plugin.mfc.guimodel;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.List;
|
---|
5 |
|
---|
6 | import de.ugoe.cs.quest.eventcore.guimodel.IGUIElementSpec;
|
---|
7 | import de.ugoe.cs.util.StringTools;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * <p>
|
---|
11 | * This class implements a node in the {@link WindowTree} that is maintained during parsing a
|
---|
12 | * session.
|
---|
13 | * </p>
|
---|
14 | * <p>
|
---|
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.
|
---|
17 | * </p>
|
---|
18 | *
|
---|
19 | * @author Steffen Herbold
|
---|
20 | * @version 1.0
|
---|
21 | */
|
---|
22 | public class MFCGUIElementSpec implements IGUIElementSpec {
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * <p>
|
---|
26 | * current name of the window
|
---|
27 | * </p>
|
---|
28 | */
|
---|
29 | private String name;
|
---|
30 |
|
---|
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>();
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * <p>
|
---|
40 | * Handle of the window. Used as unique identifier during its existence.
|
---|
41 | * </p>
|
---|
42 | */
|
---|
43 | private long hwnd;
|
---|
44 |
|
---|
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>();
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * <p>
|
---|
54 | * Resource id of the window.
|
---|
55 | * </p>
|
---|
56 | */
|
---|
57 | private final int resourceId;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * <p>
|
---|
61 | * type (class name) of the window.
|
---|
62 | * </p>
|
---|
63 | */
|
---|
64 | private final String type;
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * <p>
|
---|
68 | * True, if the window is modal.
|
---|
69 | * </p>
|
---|
70 | */
|
---|
71 | private final boolean isModal;
|
---|
72 |
|
---|
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 | }
|
---|
106 |
|
---|
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 | }
|
---|
134 |
|
---|
135 | names.append('"');
|
---|
136 | names.append(formerNames.get(i));
|
---|
137 | names.append('"');
|
---|
138 | }
|
---|
139 |
|
---|
140 | names.append(")");
|
---|
141 | }
|
---|
142 |
|
---|
143 | return names.toString();
|
---|
144 | }
|
---|
145 |
|
---|
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 | }
|
---|
156 |
|
---|
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 | }
|
---|
167 |
|
---|
168 | /* (non-Javadoc)
|
---|
169 | * @see de.ugoe.cs.quest.eventcore.guimodel.IGUIElementSpec#getType()
|
---|
170 | */
|
---|
171 | @Override
|
---|
172 | public String getType() {
|
---|
173 | return type;
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * <p>
|
---|
178 | * TODO: comment
|
---|
179 | * </p>
|
---|
180 | *
|
---|
181 | * @return
|
---|
182 | */
|
---|
183 | public boolean isModal() {
|
---|
184 | return isModal;
|
---|
185 | }
|
---|
186 |
|
---|
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 | }
|
---|
205 |
|
---|
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 | }
|
---|
221 |
|
---|
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;
|
---|
237 |
|
---|
238 | if ((type != otherSpec.type) && ((type != null) && (!type.equals(otherSpec.type)))) {
|
---|
239 | return false;
|
---|
240 | }
|
---|
241 |
|
---|
242 | if (isModal != otherSpec.isModal) {
|
---|
243 | return false;
|
---|
244 | }
|
---|
245 |
|
---|
246 | if (resourceId != otherSpec.resourceId) {
|
---|
247 | return false;
|
---|
248 | }
|
---|
249 |
|
---|
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.
|
---|
272 | // The current name of one of the specs must be contained in the former names of the
|
---|
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.
|
---|
278 |
|
---|
279 | if ((otherSpec.name != null) && formerNames.contains(otherSpec.name)) {
|
---|
280 | return true;
|
---|
281 | }
|
---|
282 |
|
---|
283 | if ((name != null) && otherSpec.formerNames.contains(name)) {
|
---|
284 | return true;
|
---|
285 | }
|
---|
286 |
|
---|
287 | // ok. Even the names do not match. This is usually a clear indication, that the elements
|
---|
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=\"" +
|
---|
357 | isModal + "\"/>";
|
---|
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 |
|
---|
390 | }
|
---|