|
1
|
|
package abbot.finder;
|
|
2
|
|
|
|
3
|
|
import java.awt.Container;
|
|
4
|
|
import java.awt.Component;
|
|
5
|
|
import java.awt.Window;
|
|
6
|
|
import java.util.*;
|
|
7
|
|
import javax.swing.SwingUtilities;
|
|
8
|
|
|
|
9
|
|
import abbot.i18n.Strings;
|
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
public class BasicFinder implements ComponentFinder {
|
|
16
|
|
private Hierarchy hierarchy;
|
|
17
|
|
|
|
18
|
|
private static final ComponentFinder DEFAULT =
|
|
19
|
|
new BasicFinder(new AWTHierarchy());
|
|
20
|
143
|
public static ComponentFinder getDefault() { return DEFAULT; }
|
|
21
|
|
|
|
22
|
|
private class SingleComponentHierarchy implements Hierarchy {
|
|
23
|
|
private Component root;
|
|
24
|
|
private ArrayList list = new ArrayList();
|
|
25
|
142
|
public SingleComponentHierarchy(Container root) {
|
|
26
|
142
|
this.root = root;
|
|
27
|
142
|
list.add(root);
|
|
28
|
|
}
|
|
29
|
142
|
public Collection getRoots() {
|
|
30
|
142
|
return list;
|
|
31
|
|
}
|
|
32
|
1355
|
public Collection getComponents(Component c) {
|
|
33
|
1355
|
return getHierarchy().getComponents(c);
|
|
34
|
|
}
|
|
35
|
0
|
public Container getParent(Component c) {
|
|
36
|
0
|
return getHierarchy().getParent(c);
|
|
37
|
|
}
|
|
38
|
0
|
public boolean contains(Component c) {
|
|
39
|
0
|
return getHierarchy().contains(c)
|
|
40
|
|
&& SwingUtilities.isDescendingFrom(c, root);
|
|
41
|
|
}
|
|
42
|
0
|
public void dispose(Window w) { getHierarchy().dispose(w); }
|
|
43
|
|
}
|
|
44
|
|
|
|
45
|
0
|
public BasicFinder() {
|
|
46
|
0
|
this(AWTHierarchy.getDefault());
|
|
47
|
|
}
|
|
48
|
|
|
|
49
|
622
|
public BasicFinder(Hierarchy h) {
|
|
50
|
622
|
hierarchy = h;
|
|
51
|
|
}
|
|
52
|
|
|
|
53
|
1922
|
protected Hierarchy getHierarchy() {
|
|
54
|
1922
|
return hierarchy;
|
|
55
|
|
}
|
|
56
|
|
|
|
57
|
|
|
|
58
|
|
|
|
59
|
|
|
|
60
|
|
|
|
61
|
142
|
public Component find(Container root, Matcher m)
|
|
62
|
|
throws ComponentNotFoundException, MultipleComponentsFoundException {
|
|
63
|
142
|
Hierarchy h = root != null
|
|
64
|
|
? new SingleComponentHierarchy(root) : getHierarchy();
|
|
65
|
142
|
return find(h, m);
|
|
66
|
|
}
|
|
67
|
|
|
|
68
|
|
|
|
69
|
|
|
|
70
|
|
|
|
71
|
|
|
|
72
|
567
|
public Component find(Matcher m)
|
|
73
|
|
throws ComponentNotFoundException, MultipleComponentsFoundException {
|
|
74
|
567
|
return find(getHierarchy(), m);
|
|
75
|
|
}
|
|
76
|
|
|
|
77
|
709
|
protected Component find(Hierarchy h, Matcher m)
|
|
78
|
|
throws ComponentNotFoundException, MultipleComponentsFoundException {
|
|
79
|
709
|
Set found = new HashSet();
|
|
80
|
709
|
Iterator iter = h.getRoots().iterator();
|
|
81
|
709
|
while (iter.hasNext()) {
|
|
82
|
603
|
findMatches(h, m, (Component)iter.next(), found);
|
|
83
|
|
}
|
|
84
|
709
|
if (found.size() == 0) {
|
|
85
|
467
|
String msg = Strings.get("finder.not_found",
|
|
86
|
|
new Object[] { m.toString() });
|
|
87
|
467
|
throw new ComponentNotFoundException(msg);
|
|
88
|
|
}
|
|
89
|
242
|
else if (found.size() > 1) {
|
|
90
|
0
|
Component[] list = (Component[])
|
|
91
|
|
found.toArray(new Component[found.size()]);
|
|
92
|
0
|
if (!(m instanceof MultiMatcher)) {
|
|
93
|
0
|
String msg = Strings.get("finder.multiple_found",
|
|
94
|
|
new Object[] { m.toString() });
|
|
95
|
0
|
throw new MultipleComponentsFoundException(msg, list);
|
|
96
|
|
}
|
|
97
|
0
|
return ((MultiMatcher)m).bestMatch(list);
|
|
98
|
|
}
|
|
99
|
242
|
return (Component)found.iterator().next();
|
|
100
|
|
}
|
|
101
|
|
|
|
102
|
6397
|
protected void findMatches(Hierarchy h, Matcher m,
|
|
103
|
|
Component c, Set found) {
|
|
104
|
6397
|
if (found.size() == 1 && !(m instanceof MultiMatcher))
|
|
105
|
425
|
return;
|
|
106
|
|
|
|
107
|
5972
|
Iterator iter = h.getComponents(c).iterator();
|
|
108
|
5972
|
while (iter.hasNext()) {
|
|
109
|
5794
|
findMatches(h, m, (Component)iter.next(), found);
|
|
110
|
|
}
|
|
111
|
5972
|
if (m.matches(c)) {
|
|
112
|
242
|
found.add(c);
|
|
113
|
|
}
|
|
114
|
|
}
|
|
115
|
|
}
|
|
116
|
|
|