1 | 1 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.eclipse.swtbot.swt.finder; |
12 | |
|
13 | |
import java.util.ArrayList; |
14 | |
import java.util.Collections; |
15 | |
import java.util.Comparator; |
16 | |
import java.util.List; |
17 | |
|
18 | |
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; |
19 | |
import org.eclipse.swtbot.swt.finder.utils.StringUtils; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | 1 | public enum ReferenceBy { |
25 | 1 | TEXT(100) { |
26 | |
protected boolean isCompatibleWith(ReferenceBy other) { |
27 | 279 | return other != LABEL && other != TOOLTIP && other != MNEMONIC && other != MESSAGE; |
28 | |
} |
29 | |
|
30 | |
public String matcherMethod() { |
31 | 1 | return "withText(" + argumentName() + ")"; |
32 | |
} |
33 | |
|
34 | |
public String methodNameSuffix() { |
35 | 1 | return ""; |
36 | |
} |
37 | |
}, |
38 | 1 | MNEMONIC(100) { |
39 | |
protected boolean isCompatibleWith(ReferenceBy other) { |
40 | 151 | return other != LABEL && other != TOOLTIP && other != TEXT && other != MESSAGE; |
41 | |
} |
42 | |
|
43 | |
public String matcherMethod() { |
44 | 1 | return "withMnemonic(" + argumentName() + ")"; |
45 | |
} |
46 | |
|
47 | |
public String methodNameSuffix() { |
48 | 1 | return ""; |
49 | |
} |
50 | |
|
51 | |
public String argumentName() { |
52 | 1 | return "mnemonicText"; |
53 | |
} |
54 | |
}, |
55 | 1 | LABEL(100) { |
56 | |
protected boolean isCompatibleWith(ReferenceBy other) { |
57 | 87 | return other != TEXT && other != TOOLTIP && other != MNEMONIC && other != MESSAGE; |
58 | |
} |
59 | |
|
60 | |
public String matcherMethod() { |
61 | 1 | return "withLabel(" + argumentName() + ")"; |
62 | |
} |
63 | |
}, |
64 | 1 | TOOLTIP(90) { |
65 | |
protected boolean isCompatibleWith(ReferenceBy other) { |
66 | 48 | return other != TEXT && other != LABEL && other != MNEMONIC && other != MESSAGE; |
67 | |
} |
68 | |
|
69 | |
public String matcherMethod() { |
70 | 1 | return "withTooltip(" + argumentName() + ")"; |
71 | |
} |
72 | |
}, |
73 | 1 | MESSAGE(90) { |
74 | |
protected boolean isCompatibleWith(ReferenceBy other) { |
75 | 32 | return other != TEXT && other != LABEL && other != MNEMONIC && other != TOOLTIP && other != IN_GROUP; |
76 | |
} |
77 | |
|
78 | |
public String matcherMethod() { |
79 | 0 | return "withMessage(" + argumentName() + ")"; |
80 | |
} |
81 | |
}, |
82 | 1 | ID_KEY_VALUE(100) { |
83 | |
protected boolean isCompatibleWith(ReferenceBy other) { |
84 | 44 | return false; |
85 | |
} |
86 | |
|
87 | |
public String methodArgument() { |
88 | 1 | return "String key, String value"; |
89 | |
} |
90 | |
|
91 | |
public String matcherMethod() { |
92 | 1 | return "withId(key, value)"; |
93 | |
} |
94 | |
|
95 | |
public String paramJavaDoc() { |
96 | 0 | return "@param key the key set on the widget.\n" + "@param value the value for the key.\n"; |
97 | |
} |
98 | |
|
99 | |
public String methodNameSuffix() { |
100 | 1 | return "WithId"; |
101 | |
} |
102 | |
|
103 | |
public String argumentName() { |
104 | 0 | return "key/value"; |
105 | |
} |
106 | |
}, |
107 | 1 | ID_VALUE(100) { |
108 | |
protected boolean isCompatibleWith(ReferenceBy other) { |
109 | 21 | return false; |
110 | |
} |
111 | |
|
112 | |
public String methodArgument() { |
113 | 0 | return "String value"; |
114 | |
} |
115 | |
|
116 | |
public String matcherMethod() { |
117 | 0 | return "withId(value)"; |
118 | |
} |
119 | |
|
120 | |
public String paramJavaDoc() { |
121 | 0 | return "@param value the value for the key {@link " + SWTBotPreferences.class.getName() + "#DEFAULT_KEY}.\n"; |
122 | |
} |
123 | |
|
124 | |
public String methodNameSuffix() { |
125 | 0 | return "WithId"; |
126 | |
} |
127 | |
|
128 | |
public String argumentName() { |
129 | 0 | return "value"; |
130 | |
} |
131 | |
}, |
132 | 1 | NONE(100) { |
133 | |
protected boolean isCompatibleWith(ReferenceBy other) { |
134 | 11 | return false; |
135 | |
} |
136 | |
|
137 | |
public String methodArgument() { |
138 | 1 | return ""; |
139 | |
} |
140 | |
|
141 | |
public String methodNameSuffix() { |
142 | 1 | return ""; |
143 | |
} |
144 | |
|
145 | |
public String matcherMethod() { |
146 | 1 | return ""; |
147 | |
} |
148 | |
|
149 | |
public String paramJavaDoc() { |
150 | 0 | return ""; |
151 | |
} |
152 | |
}, |
153 | 1 | IN_GROUP(80) { |
154 | |
public String methodNameSuffix() { |
155 | 1 | return "InGroup"; |
156 | |
} |
157 | |
|
158 | |
public String matcherMethod() { |
159 | 1 | return "inGroup(inGroup)"; |
160 | |
} |
161 | |
}; |
162 | 8 | private final int priority; |
163 | |
|
164 | 18 | private ReferenceBy(int priority) { |
165 | 9 | this.priority = priority; |
166 | 9 | } |
167 | |
|
168 | |
protected boolean isCompatibleWith(ReferenceBy other) { |
169 | 5 | return true; |
170 | |
} |
171 | |
|
172 | |
protected boolean canExistByItself() { |
173 | 9 | return true; |
174 | |
} |
175 | |
|
176 | |
public String describeJavaDoc() { |
177 | 0 | return "with the specified <code>" + argumentName() + "</code>"; |
178 | |
} |
179 | |
|
180 | |
public String methodArgument() { |
181 | 4 | return "String " + argumentName(); |
182 | |
} |
183 | |
|
184 | |
public String argumentName() { |
185 | 7 | return StringUtils.toCamelCase(name().toLowerCase()); |
186 | |
} |
187 | |
|
188 | |
public String methodNameSuffix() { |
189 | 2 | return "With" + StringUtils.capitalize(name().toLowerCase()); |
190 | |
} |
191 | |
|
192 | |
public abstract String matcherMethod(); |
193 | |
|
194 | |
protected static Comparator<ReferenceBy> comparator() { |
195 | 13 | return new Comparator<ReferenceBy>() { |
196 | |
public int compare(ReferenceBy o1, ReferenceBy o2) { |
197 | 4 | return o2.priority - o1.priority; |
198 | |
} |
199 | |
|
200 | |
}; |
201 | |
} |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
public static List<List<ReferenceBy>> getCombinations(ReferenceBy... references) { |
208 | 1 | return sort(filter(new CombinationGenerator<ReferenceBy>(references.length, references))); |
209 | |
} |
210 | |
|
211 | |
private static List<List<ReferenceBy>> sort(List<List<ReferenceBy>> combinations) { |
212 | 15 | for (List<ReferenceBy> list : combinations) { |
213 | 13 | Collections.sort(list, ReferenceBy.comparator()); |
214 | |
} |
215 | 1 | return combinations; |
216 | |
} |
217 | |
|
218 | |
private static List<List<ReferenceBy>> filter(CombinationGenerator<ReferenceBy> uptoCombinationGenerator) { |
219 | 1 | ArrayList<List<ReferenceBy>> result = new ArrayList<List<ReferenceBy>>(); |
220 | 513 | for (List<ReferenceBy> list : uptoCombinationGenerator) { |
221 | 511 | if (list.size() == 1) { |
222 | 9 | if (list.get(0).canExistByItself()) |
223 | 9 | result.add(list); |
224 | 502 | } else if (isCompatible(list)) |
225 | 4 | result.add(list); |
226 | |
} |
227 | 1 | return result; |
228 | |
} |
229 | |
|
230 | |
private static boolean isCompatible(List<ReferenceBy> list) { |
231 | 1075 | for (ReferenceBy aReference : list) { |
232 | 1800 | for (ReferenceBy otherReference : list) { |
233 | 1160 | if (aReference != otherReference) |
234 | 654 | if (!aReference.isCompatibleWith(otherReference)) |
235 | 498 | return false; |
236 | |
} |
237 | |
} |
238 | 4 | return true; |
239 | |
} |
240 | |
|
241 | |
public String javadoc() { |
242 | 0 | return "the " + argumentName() + " on the widget."; |
243 | |
} |
244 | |
|
245 | |
public String paramJavaDoc() { |
246 | 0 | return "@param " + argumentName() + " " + javadoc() + "\n"; |
247 | |
} |
248 | |
|
249 | |
} |