Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7fbc6aaf0c8b37bcf640075cfa134d8d5c649930 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*******************************************************************************
 * Copyright (c) 2008 Ketan Padegaonkar and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Ketan Padegaonkar - initial API and implementation
 *******************************************************************************/
package org.eclipse.swtbot.swt.finder.widgets;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swtbot.swt.finder.ReferenceBy;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.SWTBotWidget;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.eclipse.swtbot.swt.finder.results.ArrayResult;
import org.eclipse.swtbot.swt.finder.results.BoolResult;
import org.eclipse.swtbot.swt.finder.results.IntResult;
import org.eclipse.swtbot.swt.finder.results.ListResult;
import org.eclipse.swtbot.swt.finder.results.Result;
import org.eclipse.swtbot.swt.finder.results.StringResult;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import org.eclipse.swtbot.swt.finder.results.WidgetResult;
import org.eclipse.swtbot.swt.finder.utils.MessageFormat;
import org.eclipse.swtbot.swt.finder.utils.StringUtils;
import org.eclipse.swtbot.swt.finder.utils.TableCollection;
import org.eclipse.swtbot.swt.finder.utils.TableRow;
import org.eclipse.swtbot.swt.finder.utils.internal.Assert;
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
import org.hamcrest.SelfDescribing;

/**
 * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 * @author Joshua Gosse <jlgosse [at] ca [dot] ibm [dot] com>
 * @version $Id$
 */
@SWTBotWidget(clasz = Tree.class, preferredName = "tree", referenceBy = { ReferenceBy.LABEL })
public class SWTBotTree extends AbstractSWTBot<Tree> {

	/**
	 * Constructs an instance of this object with the given tree.
	 *
	 * @param tree the widget.
	 * @param description the description of the widget, this will be reported by {@link #toString()}
	 * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
	 */
	public SWTBotTree(Tree tree, SelfDescribing description) throws WidgetNotFoundException {
		super(tree, description);
	}

	/**
	 * Constructs an instance of this object with the given tree.
	 *
	 * @param tree the widget.
	 * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
	 */
	public SWTBotTree(Tree tree) throws WidgetNotFoundException {
		this(tree, null);
	}

	/**
	 * Gets the number of rows in the tree.
	 *
	 * @return the number of rows in the tree
	 */
	public int rowCount() {
		return syncExec(new IntResult() {
			public Integer run() {
				return widget.getItems().length;
			}
		});
	}

	/**
	 * Gets the column count of this tree.
	 *
	 * @return the number of columns in the tree
	 */
	public int columnCount() {
		return syncExec(new IntResult() {
			public Integer run() {
				return widget.getColumnCount();
			}
		});
	}

	/**
	 * Gets the columns of this tree.
	 *
	 * @return the list of columns in the tree.
	 */
	public List<String> columns() {
		final int columnCount = columnCount();
		return syncExec(new ListResult<String>() {
			public List<String> run() {
				String columns[] = new String[columnCount];
				for (int i = 0; i < columnCount; i++)
					columns[i] = widget.getColumn(i).getText();
				return new ArrayList<String>(Arrays.asList(columns));
			}
		});
	}

	/**
	 * Gets the cell data for the given row/column index.
	 *
	 * @param row the row index.
	 * @param column the column index.
	 * @return the cell at the location specified by the row and column
	 */
	public String cell(final int row, final int column) {
		int rowCount = rowCount();
		int columnCount = columnCount();

		Assert.isLegal(row < rowCount, "The row number (" + row + ") is more than the number of rows(" + rowCount + ") in the tree."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		Assert.isLegal(column < columnCount, "The column number (" + column + ") is more than the number of column(" + columnCount //$NON-NLS-1$ //$NON-NLS-2$
				+ ") in the tree."); //$NON-NLS-1$

		return syncExec(new StringResult() {
			public String run() {
				TreeItem item = widget.getItem(row);
				return item.getText(column);
			}
		});
	}

	/**
	 * Gets the cell data for the given row/column information.
	 *
	 * @param row the row index.
	 * @param columnName the column name.
	 * @return the cell in the tree at the specified row and column header.
	 */
	public String cell(int row, String columnName) {
		List<String> columns = columns();
		Assert.isLegal(columns.contains(columnName), "The column `" + columnName + "' is not found."); //$NON-NLS-1$ //$NON-NLS-2$
		int columnIndex = columns.indexOf(columnName);
		if (columnIndex == -1)
			return ""; //$NON-NLS-1$
		return cell(row, columnIndex);
	}

	/**
	 * Gets the current selection count.
	 *
	 * @return the number of selected items.
	 */
	public int selectionCount() {
		return syncExec(new IntResult() {
			public Integer run() {
				return widget.getSelectionCount();
			}
		});
	}

	/**
	 * Gets the table collection representing the selection.
	 *
	 * @return the selection in the tree
	 */
	public TableCollection selection() {
		final int columnCount = columnCount();

		return syncExec(new Result<TableCollection>() {
			public TableCollection run() {
				final TableCollection selection = new TableCollection();
				TreeItem[] items = widget.getSelection();
				for (TreeItem item : items) {
					TableRow tableRow = new TableRow();
					if (columnCount == 0)
						tableRow.add(item.getText());
					else
						for (int j = 0; j < columnCount; j++)
							tableRow.add(item.getText(j));
					selection.add(tableRow);
				}
				return selection;
			}
		});
	}

	/**
	 * Selects the items matching the array list.
	 *
	 * @param items the items to select.
	 * @return this same instance.
	 */
	public SWTBotTree select(final String... items) {
		waitForEnabled();
		setFocus();
		asyncExec(new VoidResult() {
			public void run() {
				List<TreeItem> selection = new ArrayList<TreeItem>();
				for (String item : items) {
					SWTBotTreeItem si = getTreeItem(item);
					selection.add(si.widget);
				}
				if (!hasStyle(widget, SWT.MULTI) && items.length > 1)
					log.warn("Tree does not support SWT.MULTI, cannot make multiple selections"); //$NON-NLS-1$
				widget.setSelection(selection.toArray(new TreeItem[selection.size()]));
			}
		});
		notifySelect();
		return this;
	}

	/**
	 * Selects the items in the array. Useful for cases where you're selecting items whose names are not unique, or
	 * items you've exposed one at a time while traversing the tree.
	 *
	 * @param items the items to select.
	 * @return this same instance.
	 */
	public SWTBotTree select(final SWTBotTreeItem... items) {
		assertEnabled();
		setFocus();
		asyncExec(new VoidResult() {
			public void run() {
				List<TreeItem> selection = new ArrayList<TreeItem>();
				for (SWTBotTreeItem treeItem : items) {
					selection.add(treeItem.widget);
				}
				if (!hasStyle(widget, SWT.MULTI) && items.length > 1)
					log.warn("Tree does not support SWT.MULTI, cannot make multiple selections"); //$NON-NLS-1$
				widget.setSelection(selection.toArray(new TreeItem[] {}));
			}
		});
		notifySelect();
		return this;
	}

	/**
	 * Unselects the selection in the tree.
	 *
	 * @return this same instance.
	 */
	public SWTBotTree unselect() {
		waitForEnabled();
		asyncExec(new VoidResult() {
			public void run() {
				log.debug(MessageFormat.format("Unselecting all in {0}", widget)); //$NON-NLS-1$
				widget.deselectAll();
			}
		});
		notifySelect();
		return this;
	}

	/**
	 * Select the indexes provided.
	 *
	 * @param indices the indices to select.
	 * @return this same instance.
	 */
	public SWTBotTree select(final int... indices) {
		waitForEnabled();
		setFocus();
		asyncExec(new VoidResult() {
			public void run() {
				log.debug(MessageFormat.format("Selecting rows [{0}] in tree{1}", StringUtils.join(indices, ", "), this)); //$NON-NLS-1$ //$NON-NLS-2$
				if (!hasStyle(widget, SWT.MULTI) && indices.length > 1)
					log.warn("Tree does not support SWT.MULTI, cannot make multiple selections"); //$NON-NLS-1$
				TreeItem items[] = new TreeItem[indices.length];
				for (int i = 0; i < indices.length; i++)
					items[i] = widget.getItem(indices[i]);
				widget.setSelection(items);
			}
		});
		notifySelect();
		return this;
	}

	/**
	 * Notifies the tree widget about selection changes
	 */
	protected void notifySelect() {
		notify(SWT.MouseEnter);
		notify(SWT.MouseMove);
		notify(SWT.Activate);
		notify(SWT.FocusIn);
		notify(SWT.MouseDown);
		notify(SWT.Selection);
		notify(SWT.MouseUp);
		notify(SWT.MouseHover);
		notify(SWT.MouseMove);
		notify(SWT.MouseExit);
		notify(SWT.Deactivate);
		notify(SWT.FocusOut);
	}

	/**
	 * Attempts to expand all nodes along the path specified by the node array parameter.
	 *
	 * @param nodes node path to expand
	 * @return the last Tree item that was expanded.
	 * @throws WidgetNotFoundException if any of the nodes on the path do not exist
	 */
	public SWTBotTreeItem expandNode(String... nodes) throws WidgetNotFoundException {
		// TODO: this method should be made iterative instead of recursive
		Assert.isNotEmpty((Object[])nodes);

		log.debug(MessageFormat.format("Expanding nodes {0} in tree {1}", StringUtils.join(nodes, ">"), this));

		waitForEnabled();
		SWTBotTreeItem item = getTreeItem(nodes[0]).expand();

		String[] tail = new String[nodes.length - 1];
		System.arraycopy(nodes, 1, tail, 0, nodes.length - 1);
		if (tail.length > 0) {
			item = item.expandNode(tail);
		}

		return item;
	}

	/**
	 * Collapses the node matching the node information.
	 *
	 * @param nodeText the text on the node.
	 * @return the Tree item that was expanded.
	 * @throws WidgetNotFoundException if the node is not found.
	 */
	public SWTBotTreeItem collapseNode(final String nodeText) throws WidgetNotFoundException {
		waitForEnabled();
		return getTreeItem(nodeText).collapse();
	}

	/**
	 * Gets the visible row count.
	 *
	 * @return the number of visible rows
	 */
	public int visibleRowCount() {
		return syncExec(new IntResult() {
			public Integer run() {
				TreeItem[] items = widget.getItems();
				return getVisibleChildrenCount(items);
			}

			private int getVisibleChildrenCount(TreeItem item) {
				if (item.getExpanded())
					return getVisibleChildrenCount(item.getItems());
				return 0;
			}

			private int getVisibleChildrenCount(TreeItem[] items) {
				int count = 0;
				for (TreeItem item : items) {
					int j = getVisibleChildrenCount(item) + 1;
					count += j;
				}
				return count;
			}
		});

	}

	/**
	 * Expands the nodes as if the plus sign was clicked.
	 *
	 * @param nodeText the node to be expanded.
	 * @param recursive if the expansion should be recursive.
	 * @return the tree item that was expanded.
	 * @throws WidgetNotFoundException if the node is not found.
	 */
	public SWTBotTreeItem expandNode(final String nodeText, final boolean recursive) throws WidgetNotFoundException {
		waitForEnabled();
		return syncExec(new Result<SWTBotTreeItem>() {
			public SWTBotTreeItem run() {
				SWTBotTreeItem item;
				try {
					item = getTreeItem(nodeText);
					expandNode(item);
				} catch (WidgetNotFoundException e) {
					throw new RuntimeException(e);
				}
				return item;
			}

			private void expandNode(SWTBotTreeItem item) throws WidgetNotFoundException {
				item.expand();
				if (recursive)
					expandTreeItem(item.widget);
			}

			private void expandTreeItem(TreeItem node) throws WidgetNotFoundException {
				TreeItem[] items = node.getItems();
				for (TreeItem item : items) {
					expandNode(new SWTBotTreeItem(item));
				}
			}
		});
	}

	/**
	 * Gets the tree item matching the given name.
	 *
	 * @param nodeText the text on the node.
	 * @return the tree item with the specified text.
	 * @throws WidgetNotFoundException if the node was not found.
	 */
	public SWTBotTreeItem getTreeItem(final String nodeText) throws WidgetNotFoundException {
		try {
			new SWTBot().waitUntil(new DefaultCondition() {
				public String getFailureMessage() {
					return "Could not find node with text " + nodeText; //$NON-NLS-1$
				}

				public boolean test() throws Exception {
					return getItem(nodeText) != null;
				}
			});
		} catch (TimeoutException e) {
			throw new WidgetNotFoundException("Timed out waiting for tree item " + nodeText, e); //$NON-NLS-1$
		}
		return new SWTBotTreeItem(getItem(nodeText));
	}

	/**
	 * Gets the item matching the given name.
	 *
	 * @param nodeText the text on the node.
	 * @return the tree item with the specified text.
	 */
	private TreeItem getItem(final String nodeText) {
		return syncExec(new WidgetResult<TreeItem>() {
			public TreeItem run() {
				TreeItem[] items = widget.getItems();
				for (TreeItem item : items) {
					if (item.getText().equals(nodeText))
						return item;
				}
				return null;
			}
		});
	}

	/**
	 * Gets all the items in the tree.
	 *
	 * @return the list of all tree items in the tree, or an empty list if there are none.
	 * @since 1.0
	 */
	public SWTBotTreeItem[] getAllItems() {
		return syncExec(new ArrayResult<SWTBotTreeItem>() {
			public SWTBotTreeItem[] run() {
				TreeItem[] items = widget.getItems();
				SWTBotTreeItem[] result = new SWTBotTreeItem[items.length];

				for (int i = 0; i < items.length; i++)
					try {
						result[i] = new SWTBotTreeItem(items[i]);
					} catch (WidgetNotFoundException e) {
						return new SWTBotTreeItem[0];
					}
				return result;
			}
		});
	}

	/**
	 * Gets if this tree has items within it.
	 *
	 * @return <code>true</code> if the tree has any items, <code>false</code> otherwise.
	 * @since 1.0
	 */
	public boolean hasItems() {
		return syncExec(new BoolResult() {
			public Boolean run() {
				return widget.getItemCount() > 0;
			}
		});
	}

}

Back to the top