1 | 13 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.eclipse.swtbot.swt.finder.widgets; |
16 | |
|
17 | |
import java.util.ArrayList; |
18 | |
import java.util.List; |
19 | |
|
20 | |
import org.eclipse.swt.SWT; |
21 | |
import org.eclipse.swt.graphics.Rectangle; |
22 | |
import org.eclipse.swt.widgets.Event; |
23 | |
import org.eclipse.swt.widgets.Table; |
24 | |
import org.eclipse.swt.widgets.TableColumn; |
25 | |
import org.eclipse.swt.widgets.TableItem; |
26 | |
import org.eclipse.swtbot.swt.finder.ReferenceBy; |
27 | |
import org.eclipse.swtbot.swt.finder.SWTBot; |
28 | |
import org.eclipse.swtbot.swt.finder.SWTBotWidget; |
29 | |
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; |
30 | |
import org.eclipse.swtbot.swt.finder.results.IntResult; |
31 | |
import org.eclipse.swtbot.swt.finder.results.ListResult; |
32 | |
import org.eclipse.swtbot.swt.finder.results.Result; |
33 | |
import org.eclipse.swtbot.swt.finder.results.StringResult; |
34 | |
import org.eclipse.swtbot.swt.finder.results.VoidResult; |
35 | |
import org.eclipse.swtbot.swt.finder.results.WidgetResult; |
36 | |
import org.eclipse.swtbot.swt.finder.utils.MessageFormat; |
37 | |
import org.eclipse.swtbot.swt.finder.utils.StringUtils; |
38 | |
import org.eclipse.swtbot.swt.finder.utils.TableCollection; |
39 | |
import org.eclipse.swtbot.swt.finder.utils.TableRow; |
40 | |
import org.eclipse.swtbot.swt.finder.utils.internal.Assert; |
41 | |
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition; |
42 | |
import org.hamcrest.SelfDescribing; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
@SWTBotWidget(clasz = Table.class, preferredName = "table", referenceBy = { ReferenceBy.LABEL }) |
49 | |
public class SWTBotTable extends AbstractSWTBot<Table> { |
50 | |
|
51 | |
|
52 | 13 | private TableItem lastSelectionItem; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public SWTBotTable(Table table) throws WidgetNotFoundException { |
61 | 11 | this(table, null); |
62 | 11 | } |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public SWTBotTable(Table table, SelfDescribing description) throws WidgetNotFoundException { |
72 | 59 | super(table, description); |
73 | 59 | } |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public int rowCount() { |
81 | 35 | return syncExec(new IntResult() { |
82 | |
public Integer run() { |
83 | 35 | return widget.getItemCount(); |
84 | |
} |
85 | |
}); |
86 | |
} |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public int columnCount() { |
94 | 22 | return syncExec(new IntResult() { |
95 | |
public Integer run() { |
96 | 22 | return widget.getColumnCount(); |
97 | |
} |
98 | |
}); |
99 | |
} |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public List<String> columns() { |
107 | 19 | return syncExec(new ListResult<String>() { |
108 | |
public List<String> run() { |
109 | 19 | ArrayList<String> result = new ArrayList<String>(); |
110 | |
|
111 | 19 | TableColumn[] columns = widget.getColumns(); |
112 | |
|
113 | 95 | for (int i : widget.getColumnOrder()) { |
114 | 76 | result.add(columns[i].getText()); |
115 | |
} |
116 | |
|
117 | 19 | return result; |
118 | |
} |
119 | |
}); |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public int indexOfColumn(String column) { |
128 | 5 | return columns().indexOf(column); |
129 | |
} |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public SWTBotTableColumn header(final String label) throws WidgetNotFoundException { |
139 | 2 | TableColumn column = syncExec(new Result<TableColumn>() { |
140 | |
public TableColumn run() { |
141 | 2 | TableColumn[] columns = widget.getColumns(); |
142 | 2 | for (TableColumn column : columns) { |
143 | 2 | if (column.getText().equals(label)) |
144 | 2 | return column; |
145 | |
} |
146 | 0 | return null; |
147 | |
} |
148 | |
}); |
149 | 2 | return new SWTBotTableColumn(column, widget); |
150 | |
} |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
public String cell(final int row, final int column) { |
160 | 14 | assertIsLegalCell(row, column); |
161 | |
|
162 | 11 | return syncExec(new StringResult() { |
163 | |
public String run() { |
164 | 11 | TableItem item = widget.getItem(row); |
165 | 11 | return item.getText(column); |
166 | |
} |
167 | |
}); |
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
public String cell(int row, String columnName) { |
178 | 7 | Assert.isLegal(columns().contains(columnName), "The column `" + columnName + "' is not found."); |
179 | 6 | List<String> columns = columns(); |
180 | 6 | int columnIndex = columns.indexOf(columnName); |
181 | 6 | if (columnIndex == -1) |
182 | 0 | return ""; |
183 | 6 | return cell(row, columnIndex); |
184 | |
} |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
public int selectionCount() { |
192 | 3 | return syncExec(new IntResult() { |
193 | |
public Integer run() { |
194 | 3 | return widget.getSelectionCount(); |
195 | |
} |
196 | |
}); |
197 | |
} |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
public TableCollection selection() { |
205 | 4 | final int columnCount = columnCount(); |
206 | 4 | return syncExec(new Result<TableCollection>() { |
207 | |
public TableCollection run() { |
208 | 4 | final TableCollection selection = new TableCollection(); |
209 | 4 | TableItem[] items = widget.getSelection(); |
210 | 8 | for (TableItem item : items) { |
211 | 4 | TableRow tableRow = new TableRow(); |
212 | 20 | for (int j = 0; j < columnCount; j++) |
213 | 16 | tableRow.add(item.getText(j)); |
214 | 4 | selection.add(tableRow); |
215 | |
} |
216 | 4 | return selection; |
217 | |
} |
218 | |
}); |
219 | |
} |
220 | |
|
221 | |
private void assertIsLegalRowIndex(final int rowIndex) { |
222 | 15 | Assert.isLegal(rowIndex < rowCount(), "The row number: " + rowIndex + " does not exist in the table"); |
223 | 13 | } |
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
public void select(final String... items) { |
232 | 3 | waitForEnabled(); |
233 | 3 | setFocus(); |
234 | 3 | int[] itemIndicies = new int[items.length]; |
235 | 5 | for(int i = 0; i < items.length; i++) { |
236 | 3 | itemIndicies[i] = indexOf(items[i]); |
237 | 3 | Assert.isLegal(itemIndicies[i] >= 0, "Could not find item:" + items[i] + " in table"); |
238 | |
} |
239 | 2 | select(itemIndicies); |
240 | 2 | notifySelect(); |
241 | 2 | } |
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
public int indexOf(final String item) { |
251 | 5 | return syncExec(new IntResult() { |
252 | |
public Integer run() { |
253 | 5 | TableItem[] items = widget.getItems(); |
254 | 68 | for (int i = 0; i < items.length; i++) { |
255 | 66 | TableItem tableItem = items[i]; |
256 | 66 | if (tableItem.getText().equals(item)) |
257 | 3 | return i; |
258 | |
} |
259 | 2 | return -1; |
260 | |
} |
261 | |
}); |
262 | |
} |
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
public boolean containsItem(final String item) { |
269 | 0 | return indexOf(item) != -1; |
270 | |
} |
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
public int indexOf(final String item, final int column) { |
281 | 6 | return syncExec(new IntResult() { |
282 | |
public Integer run() { |
283 | 6 | TableItem[] items = widget.getItems(); |
284 | 54 | for (int i = 0; i < items.length; i++) { |
285 | 52 | TableItem tableItem = items[i]; |
286 | 52 | if (tableItem.getText(column).equals(item)) |
287 | 4 | return i; |
288 | |
} |
289 | 2 | return -1; |
290 | |
} |
291 | |
}); |
292 | |
} |
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
public int indexOf(final String item, final String column) { |
303 | 3 | return indexOf(item, indexOfColumn(column)); |
304 | |
} |
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
public void unselect() { |
310 | 12 | waitForEnabled(); |
311 | 12 | setFocus(); |
312 | 12 | asyncExec(new VoidResult() { |
313 | |
public void run() { |
314 | 12 | log.debug(MessageFormat.format("Unselecting all in {0}", widget)); |
315 | 12 | widget.deselectAll(); |
316 | 12 | } |
317 | |
}); |
318 | 12 | notifySelect(); |
319 | 12 | } |
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
public void select(final int... indices) { |
327 | 11 | waitForEnabled(); |
328 | 11 | if (indices.length > 1) |
329 | 3 | assertMultiSelect(); |
330 | 11 | setFocus(); |
331 | 11 | log.debug(MessageFormat.format("Selecting rows {0} in table {1}", StringUtils.join(indices, ", "), this)); |
332 | 11 | unselect(); |
333 | 24 | for (int i = 0; i < indices.length; i++) |
334 | 15 | additionalSelectAndNotify(indices[i]); |
335 | 9 | } |
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
private void additionalSelectAndNotify(final int j) { |
341 | 15 | assertIsLegalRowIndex(j); |
342 | 13 | asyncExec(new VoidResult() { |
343 | |
public void run() { |
344 | 13 | lastSelectionItem = widget.getItem(j); |
345 | 13 | widget.select(j); |
346 | 13 | } |
347 | |
}); |
348 | 13 | notifySelect(); |
349 | 13 | } |
350 | |
|
351 | |
private void assertMultiSelect() { |
352 | 3 | Assert.isLegal(hasStyle(widget, SWT.MULTI), "Table does not support multi selection."); |
353 | 3 | } |
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
protected void notifySelect() { |
359 | 27 | notify(SWT.MouseEnter); |
360 | 27 | notify(SWT.MouseMove); |
361 | 27 | notify(SWT.Activate); |
362 | 27 | notify(SWT.FocusIn); |
363 | 27 | notify(SWT.MouseDown); |
364 | 27 | notify(SWT.Selection, selectionEvent()); |
365 | 27 | notify(SWT.MouseUp); |
366 | 27 | notify(SWT.MouseHover); |
367 | 27 | notify(SWT.MouseMove); |
368 | 27 | notify(SWT.MouseExit); |
369 | 27 | notify(SWT.Deactivate); |
370 | 27 | notify(SWT.FocusOut); |
371 | 27 | } |
372 | |
|
373 | |
private Event selectionEvent() { |
374 | 27 | Event createEvent = createEvent(); |
375 | 27 | createEvent.item = lastSelectionItem; |
376 | 27 | return createEvent; |
377 | |
} |
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
public void click(final int row, final int column) { |
387 | 2 | assertIsLegalCell(row, column); |
388 | |
|
389 | 2 | setFocus(); |
390 | 2 | select(row); |
391 | 2 | asyncExec(new VoidResult() { |
392 | |
public void run() { |
393 | 2 | TableItem item = widget.getItem(row); |
394 | 2 | Rectangle cellBounds = item.getBounds(column); |
395 | 2 | clickXY(cellBounds.x + (cellBounds.width / 2), cellBounds.y + (cellBounds.height / 2)); |
396 | 2 | } |
397 | |
}); |
398 | 2 | } |
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
public void doubleClick(final int row, final int column) { |
408 | 1 | assertIsLegalCell(row, column); |
409 | 1 | setFocus(); |
410 | 1 | asyncExec(new VoidResult() { |
411 | |
public void run() { |
412 | 1 | TableItem item = widget.getItem(row); |
413 | 1 | Rectangle cellBounds = item.getBounds(column); |
414 | |
|
415 | 1 | widget.setSelection(row); |
416 | 1 | doubleClickXY(cellBounds.x + (cellBounds.width / 2), cellBounds.y + (cellBounds.height / 2)); |
417 | 1 | } |
418 | |
}); |
419 | 1 | } |
420 | |
|
421 | |
|
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
protected void assertIsLegalCell(final int row, final int column) { |
429 | 17 | int rowCount = rowCount(); |
430 | 17 | int columnCount = columnCount(); |
431 | |
|
432 | 17 | Assert.isLegal(row < rowCount, "The row number (" + row + ") is more than the number of rows(" + rowCount + ") in the table."); |
433 | 30 | Assert.isLegal((column < columnCount) || ((columnCount == 0) && (column == 0)), "The column number (" + column |
434 | 15 | + ") is more than the number of column(" + columnCount + ") in the table."); |
435 | 14 | } |
436 | |
|
437 | |
|
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
|
444 | |
|
445 | |
public SWTBotTableItem getTableItem(final String itemText) throws WidgetNotFoundException { |
446 | |
try { |
447 | 13 | new SWTBot().waitUntil(new DefaultCondition() { |
448 | |
public String getFailureMessage() { |
449 | 0 | return "Could not find node with text " + itemText; |
450 | |
} |
451 | |
|
452 | |
public boolean test() throws Exception { |
453 | 13 | return getItem(itemText) != null; |
454 | |
} |
455 | |
}); |
456 | 0 | } catch (TimeoutException e) { |
457 | 0 | throw new WidgetNotFoundException("Timed out waiting for table item " + itemText, e); |
458 | |
} |
459 | 13 | return new SWTBotTableItem(getItem(itemText)); |
460 | |
} |
461 | |
|
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
|
468 | 13 | private TableItem getItem(final String itemText) { |
469 | 26 | return syncExec(new WidgetResult<TableItem>() { |
470 | |
public TableItem run() { |
471 | 26 | TableItem[] items = widget.getItems(); |
472 | 76 | for (int i = 0; i < items.length; i++) { |
473 | 76 | TableItem item = items[i]; |
474 | 76 | if (item.getText().equals(itemText)) |
475 | 26 | return item; |
476 | |
} |
477 | 0 | return null; |
478 | |
} |
479 | |
}); |
480 | |
} |
481 | |
|
482 | |
|
483 | |
|
484 | |
|
485 | |
|
486 | |
|
487 | |
|
488 | |
|
489 | |
|
490 | |
public SWTBotTableItem getTableItem(final int row) throws WidgetNotFoundException { |
491 | |
try { |
492 | 1 | new SWTBot().waitUntil(new DefaultCondition() { |
493 | |
public String getFailureMessage() { |
494 | 0 | return "Could not find table item for row " + row; |
495 | |
} |
496 | |
|
497 | |
public boolean test() throws Exception { |
498 | 1 | return getItem(row) != null; |
499 | |
} |
500 | |
}); |
501 | 0 | } catch (TimeoutException e) { |
502 | 0 | throw new WidgetNotFoundException("Timed out waiting for table item in row " + row, e); |
503 | |
} |
504 | 1 | return new SWTBotTableItem(getItem(row)); |
505 | |
} |
506 | |
|
507 | |
|
508 | |
|
509 | |
|
510 | |
|
511 | |
|
512 | |
|
513 | 1 | private TableItem getItem(final int row) { |
514 | 2 | return syncExec(new WidgetResult<TableItem>() { |
515 | |
public TableItem run() { |
516 | 2 | return widget.getItem(row); |
517 | |
} |
518 | |
|
519 | |
}); |
520 | |
} |
521 | |
|
522 | |
} |