Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPatrick Tasse2019-06-11 17:35:19 -0400
committerPatrick Tasse2019-06-12 09:22:35 -0400
commitfffc7dc2ce68def00c154d5784781a0b2842cd7c (patch)
tree843fc18fcbcff2efcf2516699f115da7524dfe41
parent464aefb3e17cd20e06ef52158b359d9550aafb09 (diff)
downloadorg.eclipse.swtbot-fffc7dc2ce68def00c154d5784781a0b2842cd7c.tar.gz
org.eclipse.swtbot-fffc7dc2ce68def00c154d5784781a0b2842cd7c.tar.xz
org.eclipse.swtbot-fffc7dc2ce68def00c154d5784781a0b2842cd7c.zip
Bug 530419: Add SWTBotTree.selectAll() and SWTBotTreeItem.selectAll()
The method SWTBotTree.selectAll() is added. It selects all expanded items in the tree. The method SWTBotTreeItem.selectAll() is added. It selects all expanded items that are descendants of the tree item. Change-Id: I0d41169e258506356220521163fcb6def0b95c07 Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
-rw-r--r--org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeItemTest.java29
-rw-r--r--org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeTest.java16
-rw-r--r--org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTree.java53
-rw-r--r--org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeItem.java61
4 files changed, 158 insertions, 1 deletions
diff --git a/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeItemTest.java b/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeItemTest.java
index 33657aad..c0b7b60f 100644
--- a/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeItemTest.java
+++ b/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeItemTest.java
@@ -27,6 +27,7 @@ import org.eclipse.swtbot.swt.finder.exceptions.AssertionFailedException;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.results.Result;
import org.eclipse.swtbot.swt.finder.test.AbstractControlExampleTest;
+import org.eclipse.swtbot.swt.finder.utils.TableCollection;
import org.eclipse.swtbot.swt.finder.utils.TableRow;
import org.junit.Before;
import org.junit.Rule;
@@ -145,6 +146,34 @@ public class SWTBotTreeItemTest extends AbstractControlExampleTest {
}
}
+ @Test
+ public void selectsAll() {
+ bot.radio("SWT.MULTI").click();
+ tree = bot.tree();
+ SWTBotTreeItem node2 = tree.getTreeItem("Node 2").expand();
+ node2.selectAll();
+ node2.getNode("Node 2.2").expand();
+ assertEquals(2, tree.selectionCount());
+ TableCollection selection = tree.selection();
+ assertEquals("Node 2.1", selection.get(0, 0));
+ assertEquals("Node 2.2", selection.get(1, 0));
+ node2.selectAll();
+ assertEquals(3, tree.selectionCount());
+ selection = tree.selection();
+ assertEquals("Node 2.1", selection.get(0, 0));
+ assertEquals("Node 2.2", selection.get(1, 0));
+ assertEquals("Node 2.2.1", selection.get(2, 0));
+ SWTBotTreeItem node3 = tree.getTreeItem("Node 3");
+ node3.selectAll();
+ assertEquals(1, tree.selectionCount());
+ selection = tree.selection();
+ assertEquals("Node 3", selection.get(0, 0));
+ SWTBotTreeItem node4 = tree.getTreeItem("Node 4");
+ node4.selectAll();
+ assertEquals(1, tree.selectionCount());
+ selection = tree.selection();
+ assertEquals("Node 4", selection.get(0, 0));
+ }
@Test
public void canCheckANode() throws Exception {
diff --git a/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeTest.java b/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeTest.java
index 954b4f00..dbb76505 100644
--- a/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeTest.java
+++ b/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeTest.java
@@ -135,6 +135,22 @@ public class SWTBotTreeTest extends AbstractControlExampleTest {
}
@Test
+ public void selectsAll() {
+ bot.radio("SWT.MULTI").click();
+ tree = bot.treeInGroup("Tree");
+ tree.getTreeItem("Node 3").expand();
+ tree.selectAll();
+ tree.getTreeItem("Node 2").expand();
+ assertEquals(5, tree.selectionCount());
+ TableCollection selection = tree.selection();
+ assertEquals("Node 1", selection.get(0, 0));
+ assertEquals("Node 2", selection.get(1, 0));
+ assertEquals("Node 3", selection.get(2, 0));
+ assertEquals("Node 3.1", selection.get(3, 0));
+ assertEquals("Node 4", selection.get(4, 0));
+ }
+
+ @Test
public void unselectsSelection() throws Exception {
bot.radio("SWT.MULTI").click();
bot.checkBox("Listen").select();
diff --git a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTree.java b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTree.java
index 54fc9a96..df91efa1 100644
--- a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTree.java
+++ b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTree.java
@@ -278,6 +278,59 @@ public class SWTBotTree extends AbstractSWTBotControl<Tree> {
}
/**
+ * Selects all expanded items in the tree. The tree must have the SWT.MULTI
+ * style.
+ *
+ * @since 2.8
+ */
+ public void selectAll() {
+ assertMultiSelect();
+ waitForEnabled();
+ setFocus();
+ TreeItem first = syncExec(new Result<TreeItem>() {
+ @Override
+ public TreeItem run() {
+ if (widget.getItemCount() == 0) {
+ return null;
+ }
+ return widget.getItem(0);
+ }
+ });
+ if (first == null) {
+ return;
+ }
+ notifySelect(first, SWT.NONE);
+ TreeItem last = syncExec(new Result<TreeItem>() {
+ @Override
+ public TreeItem run() {
+ if (widget.getItemCount() == 0) {
+ return null;
+ }
+ TreeItem last = widget.getItem(widget.getItemCount() - 1);
+ while (last.getExpanded() && last.getItemCount() > 0) {
+ last = last.getItem(last.getItemCount() - 1);
+ }
+ return last;
+ }
+ });
+ if (last != null && last != first) {
+ notifySelect(last, SWT.MOD2, selectAllRunnable());
+ }
+ }
+
+ /**
+ * Selects all expanded tree items.
+ */
+ private Runnable selectAllRunnable() {
+ return new Runnable() {
+ @Override
+ public void run() {
+ widget.selectAll();
+ }
+ };
+ }
+
+ /**
* Unselects the selection in the tree.
*
* @return this same instance.
diff --git a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeItem.java b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeItem.java
index 32083a2a..832f2451 100644
--- a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeItem.java
+++ b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeItem.java
@@ -580,18 +580,77 @@ public class SWTBotTreeItem extends AbstractSWTBot<TreeItem> {
}
/**
+ * Selects all expanded items that are descendants of the tree item. Replaces
+ * the current selection. Selects the tree item if it is collapsed or has no
+ * children. The tree must have the SWT.MULTI style.
+ *
+ * @since 2.8
+ */
+ public void selectAll() {
+ assertMultiSelect();
+ waitForEnabled();
+ setFocus();
+ TreeItem[] items = syncExec(new ArrayResult<TreeItem>() {
+ @Override
+ public TreeItem[] run() {
+ if (!widget.getExpanded()) {
+ return new TreeItem[] { widget };
+ }
+ return getExpandedChildren(widget).toArray(new TreeItem[0]);
+ }
+ });
+ if (items.length == 0) {
+ return;
+ }
+ notifySelect(items[0], SWT.NONE);
+ if (items.length > 1) {
+ notifySelect(items[items.length - 1], SWT.MOD2, selectRunnable(items));
+ }
+ }
+
+ private List<TreeItem> getExpandedChildren(TreeItem item) {
+ List<TreeItem> items = new ArrayList<TreeItem>();
+ for (TreeItem child : item.getItems()) {
+ items.add(child);
+ if (child.getExpanded() && child.getItemCount() > 0) {
+ items.addAll(getExpandedChildren(child));
+ }
+ }
+ return items;
+ }
+
+ /**
+ * Selects the specified items.
+ *
+ * @param items
+ * the tree items to select
+ */
+ private Runnable selectRunnable(final TreeItem[] items) {
+ return new Runnable() {
+ @Override
+ public void run() {
+ tree.setSelection(items);
+ }
+ };
+ }
+
+ /**
* Selects the specified item and sends notifications.
*
* @param selected the tree item to select.
* @param stateMask the state of the keyboard modifier keys.
*/
private void notifySelect(TreeItem selected, int stateMask) {
+ notifySelect(selected, stateMask, selectRunnable(selected, (stateMask & SWT.MOD1) != 0));
+ }
+
+ private void notifySelect(TreeItem selected, int stateMask, Runnable runnable) {
SWTBotTreeItem item = new SWTBotTreeItem(selected);
notifyTree(SWT.MouseEnter, item.createMouseEvent(0, SWT.NONE, 0));
notifyTree(SWT.Activate, super.createEvent());
notifyTree(SWT.FocusIn, super.createEvent());
notifyTree(SWT.MouseDown, item.createMouseEvent(1, stateMask, 1));
- notifyTree(SWT.Selection, item.createSelectionEvent(stateMask | SWT.BUTTON1), selectRunnable(selected, (stateMask & SWT.MOD1) != 0));
+ notifyTree(SWT.Selection, item.createSelectionEvent(stateMask | SWT.BUTTON1), runnable);
notifyTree(SWT.MouseUp, item.createMouseEvent(1, stateMask | SWT.BUTTON1, 1));
}

Back to the top