Coverage Report - org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotTreeItem
84%
156/184
77%
14/18
1.306
SWTBotTreeItem$1
100%
3/3
N/A
1.306
SWTBotTreeItem$10
100%
5/5
N/A
1.306
SWTBotTreeItem$11
100%
3/3
N/A
1.306
SWTBotTreeItem$12
100%
3/3
N/A
1.306
SWTBotTreeItem$13
100%
4/4
N/A
1.306
SWTBotTreeItem$14
100%
9/9
100%
2/2
1.306
SWTBotTreeItem$15
100%
3/3
N/A
1.306
SWTBotTreeItem$16
100%
5/5
100%
2/2
1.306
SWTBotTreeItem$17
100%
4/4
N/A
1.306
SWTBotTreeItem$18
100%
3/3
N/A
1.306
SWTBotTreeItem$19
100%
3/3
N/A
1.306
SWTBotTreeItem$2
100%
3/3
N/A
1.306
SWTBotTreeItem$20
100%
7/7
100%
2/2
1.306
SWTBotTreeItem$21
75%
3/4
50%
1/2
1.306
SWTBotTreeItem$22
85%
6/7
75%
3/4
1.306
SWTBotTreeItem$23
0%
0/3
N/A
1.306
SWTBotTreeItem$3
88%
8/9
75%
3/4
1.306
SWTBotTreeItem$4
100%
3/3
N/A
1.306
SWTBotTreeItem$5
100%
3/3
N/A
1.306
SWTBotTreeItem$6
100%
4/4
N/A
1.306
SWTBotTreeItem$7
0%
0/4
N/A
1.306
SWTBotTreeItem$8
100%
7/7
100%
2/2
1.306
SWTBotTreeItem$9
100%
8/8
100%
4/4
1.306
 
 1  14
 /*******************************************************************************
 2  
  * Copyright (c) 2008, 2010 Ketan Padegaonkar and others.
 3  
  * All rights reserved. This program and the accompanying materials
 4  
  * are made available under the terms of the Eclipse Public License v1.0
 5  
  * which accompanies this distribution, and is available at
 6  
  * http://www.eclipse.org/legal/epl-v10.html
 7  
  *
 8  
  * Contributors:
 9  
  *     Ketan Padegaonkar - initial API and implementation
 10  
  *     Cédric Chabanois - http://swtbot.org/bugzilla/show_bug.cgi?id=10
 11  
  *     Ketan Patel - https://bugs.eclipse.org/bugs/show_bug.cgi?id=259720
 12  
  *******************************************************************************/
 13  
 package org.eclipse.swtbot.swt.finder.widgets;
 14  
 
 15  
 import java.util.ArrayList;
 16  
 import java.util.Arrays;
 17  
 import java.util.List;
 18  
 
 19  
 import org.eclipse.swt.SWT;
 20  
 import org.eclipse.swt.graphics.Point;
 21  
 import org.eclipse.swt.graphics.Rectangle;
 22  
 import org.eclipse.swt.widgets.Event;
 23  
 import org.eclipse.swt.widgets.Tree;
 24  
 import org.eclipse.swt.widgets.TreeItem;
 25  
 import org.eclipse.swtbot.swt.finder.SWTBot;
 26  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 27  
 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
 28  
 import org.eclipse.swtbot.swt.finder.results.ArrayResult;
 29  
 import org.eclipse.swtbot.swt.finder.results.BoolResult;
 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.SWTUtils;
 38  
 import org.eclipse.swtbot.swt.finder.utils.TableRow;
 39  
 import org.eclipse.swtbot.swt.finder.utils.TextDescription;
 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  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 46  
  * @author Ketan Patel
 47  
  * @author Joshua Gosse <jlgosse [at] ca [dot] ibm [dot] com>
 48  
  * @version $Id$
 49  
  */
 50  
 public class SWTBotTreeItem extends AbstractSWTBot<TreeItem> {
 51  
 //        private static final int        expandKey        = SWT.getPlatform().equals("gtk") ? SWT.KEYPAD_ADD : SWT.ARROW_RIGHT;
 52  
 //        private static final int        collapseKey        = SWT.getPlatform().equals("gtk") ? SWT.KEYPAD_SUBTRACT : SWT.ARROW_LEFT;
 53  13
         private Tree                                tree;
 54  
 
 55  
         /**
 56  
          * @param treeItem the widget.
 57  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 58  
          */
 59  
         public SWTBotTreeItem(final TreeItem treeItem) throws WidgetNotFoundException {
 60  50
                 this(treeItem, null);
 61  50
         }
 62  
 
 63  
         /**
 64  
          * @param treeItem the widget.
 65  
          * @param description the description of the widget, this will be reported by {@link #toString()}
 66  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 67  
          */
 68  
         public SWTBotTreeItem(final TreeItem treeItem, SelfDescribing description) throws WidgetNotFoundException {
 69  63
                 super(treeItem, description);
 70  63
                 this.tree = syncExec(new WidgetResult<Tree>() {
 71  
                         public Tree run() {
 72  63
                                 return treeItem.getParent();
 73  
                         }
 74  
                 });
 75  63
         }
 76  
 
 77  
         /**
 78  
          * Returns the text stored at the given column index in the receiver, or empty string if the text has not been set.
 79  
          * Throws an exception if the column is greater than the number of columns in the tree.
 80  
          * 
 81  
          * @param column the column index.
 82  
          * @return the cell at the location specified by the column
 83  
          */
 84  
         public String cell(final int column) {
 85  9
                 if (column == 0) {
 86  2
                         return getText();
 87  
                 }
 88  7
                 int columnCount = new SWTBotTree(tree).columnCount();
 89  7
                 Assert.isLegal(column < columnCount, java.text.MessageFormat.format("The column index ({0}) is more than the number of column({1}) in the tree.", column, columnCount)); //$NON-NLS-1$
 90  5
                 return syncExec(new StringResult() {
 91  
                         public String run() {
 92  5
                                 return widget.getText(column);
 93  
                         }
 94  
                 });
 95  
         }
 96  
 
 97  
         /**
 98  
          * Returns the table row representation of cell values
 99  
          * 
 100  
          * @return the cell values for this item
 101  
          */
 102  
         public TableRow row() {
 103  1
                 return syncExec(new Result<TableRow>() {
 104  
                         public TableRow run() {
 105  1
                                 int columnCount = tree.getColumnCount();
 106  1
                                 TableRow tableRow = new TableRow();
 107  1
                                 if (columnCount == 0)
 108  0
                                         tableRow.add(widget.getText());
 109  
                                 else
 110  5
                                         for (int j = 0; j < columnCount; j++)
 111  4
                                                 tableRow.add(widget.getText(j));
 112  1
                                 return tableRow;
 113  
                         }
 114  
                 });
 115  
         }
 116  
 
 117  
         /**
 118  
          * Returns the number of items contained in the receiver that are direct item children of the receiver.
 119  
          * 
 120  
          * @return the number of items
 121  
          */
 122  
         public int rowCount() {
 123  11
                 return syncExec(new IntResult() {
 124  
                         public Integer run() {
 125  11
                                 return widget.getItemCount();
 126  
                         }
 127  
                 });
 128  
         }
 129  
 
 130  
         /**
 131  
          * Gets the nodes at the given, zero-relative index in the receiver. Throws an exception if the index is out of
 132  
          * range.
 133  
          * 
 134  
          * @param row the index of the item to return
 135  
          * @return the item at the given index
 136  
          */
 137  
         public SWTBotTreeItem getNode(final int row) {
 138  7
                 int rowCount = rowCount();
 139  7
                 Assert.isLegal(row < rowCount, java.text.MessageFormat.format("The row number ({0}) is more than the number of rows({1}) in the tree.", row, rowCount)); //$NON-NLS-1$
 140  6
                 return syncExec(new Result<SWTBotTreeItem>() {
 141  
                         public SWTBotTreeItem run() {
 142  6
                                 return new SWTBotTreeItem(widget.getItem(row));
 143  
                         }
 144  
                 });
 145  
         }
 146  
 
 147  
         /**
 148  
          * Gets the cell data for the given row/column index.
 149  
          * 
 150  
          * @param row the row index.
 151  
          * @param column the column index.
 152  
          * @return the cell at the location specified by the row and column
 153  
          * @see #getNode(int)
 154  
          * @see #cell(int)
 155  
          */
 156  
         public String cell(final int row, final int column) {
 157  3
                 return getNode(row).cell(column);
 158  
         }
 159  
 
 160  
         /**
 161  
          * Expands the tree item to simulate click the plus sign.
 162  
          * 
 163  
          * @return the tree item, after expanding it.
 164  
          */
 165  
         public SWTBotTreeItem expand() {
 166  26
                 assertEnabled();
 167  
 
 168  26
                 if (isExpanded()){
 169  0
                         log.warn(MessageFormat.format("Tree item {0} is already expanded. Won''t expand it again.", this));
 170  0
                         return this;
 171  
                 }
 172  
 
 173  26
                 preExpandNotify();
 174  26
                 asyncExec(new VoidResult() {
 175  
                         public void run() {
 176  26
                                 widget.setExpanded(true);
 177  26
                         }
 178  
                 });
 179  26
                 postExpandNotify();
 180  26
                 return this;
 181  
         }
 182  
 
 183  
         /**
 184  
          * Collapses the tree item to simulate click the plus sign.
 185  
          *
 186  
          * @return the tree item, after collapsing it.
 187  
          */
 188  
         public SWTBotTreeItem collapse() {
 189  0
                 assertEnabled();
 190  
 
 191  0
                 if (!isExpanded()){
 192  0
                         log.warn(MessageFormat.format("Tree item {0} is already collapsed. Won''t collapse it again.", this));
 193  0
                         return this;
 194  
                 }
 195  
 
 196  0
                 preCollapseNotify();
 197  0
                 asyncExec(new VoidResult() {
 198  
                         public void run() {
 199  0
                                 widget.setExpanded(false);
 200  0
                         }
 201  
                 });
 202  0
                 postCollapseNotify();
 203  0
                 return this;
 204  
         }
 205  
 
 206  
         private void preExpandNotify() {
 207  26
                 notifyTree(SWT.Expand, createEvent());
 208  26
         }
 209  
 
 210  
         private void postExpandNotify() {
 211  26
                 notifyTree(SWT.MouseMove);
 212  26
                 notifyTree(SWT.Activate);
 213  26
                 notifyTree(SWT.FocusIn);
 214  26
                 notifyTree(SWT.MouseDown);
 215  26
                 notifyTree(SWT.MeasureItem);
 216  26
                 notifyTree(SWT.Deactivate);
 217  26
                 notifyTree(SWT.FocusOut);
 218  26
         }
 219  
 
 220  
         private void preCollapseNotify() {
 221  0
                 notifyTree(SWT.Collapse, createEvent());
 222  0
         }
 223  
 
 224  
         private void postCollapseNotify() {
 225  0
                 notifyTree(SWT.MouseMove);
 226  0
                 notifyTree(SWT.Activate);
 227  0
                 notifyTree(SWT.FocusIn);
 228  0
                 notifyTree(SWT.MouseDown);
 229  0
                 notifyTree(SWT.MeasureItem);
 230  0
                 notifyTree(SWT.Deactivate);
 231  0
                 notifyTree(SWT.FocusOut);
 232  0
         }
 233  
 
 234  
         private void notifyTree(int eventType) {
 235  237
                 notify(eventType, createEvent(), tree);
 236  237
         }
 237  
 
 238  
         private void notifyTree(int eventType, Event event) {
 239  36
                 notify(eventType, event, tree);
 240  36
         }
 241  
 
 242  
         protected Event createEvent() {
 243  271
                 Event event = super.createEvent();
 244  271
                 event.widget = tree;
 245  271
                 event.item = widget;
 246  271
                 return event;
 247  
         }
 248  
 
 249  
         /**
 250  
          * Gets the nodes of the tree item.
 251  
          * 
 252  
          * @return the list of nodes in the treeitem.
 253  
          */
 254  
         public List<String> getNodes() {
 255  1
                 return syncExec(new ListResult<String>() {
 256  
                         public List<String> run() {
 257  1
                                 TreeItem[] items = widget.getItems();
 258  1
                                 List<String> result = new ArrayList<String>(items.length);
 259  3
                                 for (TreeItem item : items)
 260  2
                                         result.add(item.getText());
 261  1
                                 return result;
 262  
                         }
 263  
                 });
 264  
 
 265  
         }
 266  
 
 267  
         /**
 268  
          * Expands the node matching the given node texts.
 269  
          * 
 270  
          * @param nodes the text on the node.
 271  
          * @return the last tree node that was expanded or <code>null</code> if none exists.
 272  
          * @throws WidgetNotFoundException if any of the nodes on the path do not exist
 273  
          */
 274  
         public SWTBotTreeItem expandNode(final String... nodes) {
 275  6
                 Assert.isNotEmpty((Object[]) nodes);
 276  6
                 assertEnabled();
 277  
 
 278  6
                 SWTBotTreeItem item = this;
 279  14
                 for (String node : nodes)
 280  8
                         item = item.getNode(node).expand();
 281  
 
 282  6
                 return item;
 283  
         }
 284  
 
 285  
         /**
 286  
          * Collapses the node matching the given node text.
 287  
          *
 288  
          * @param nodeText the text on the node.
 289  
          * @return the node that was collapsed or <code>null</code> if not match exists.
 290  
          */
 291  
         public SWTBotTreeItem collapseNode(final String nodeText) {
 292  0
                 assertEnabled();
 293  0
                 return getNode(nodeText).collapse();
 294  
         }
 295  
 
 296  
         /**
 297  
          * Gets the node matching the given node text and index.
 298  
          * 
 299  
          * @param nodeText the text on the node.
 300  
          * @param index the n'th node with the nodeText.
 301  
          * @return the node with the specified text or <code>WidgetNotFoundException</code> if not match exists.
 302  
          * @since 2.0
 303  
          */
 304  
         public SWTBotTreeItem getNode(final String nodeText, final int index) {
 305  13
                 List<SWTBotTreeItem> nodes = getNodes(nodeText);
 306  13
                 Assert.isTrue(index < nodes.size(), MessageFormat.format("The index ({0}) was more than the number of nodes ({1}) in the tree.", index, nodes.size()));
 307  12
                 return nodes.get(index);
 308  
         }
 309  
 
 310  
         /**
 311  
          * Gets all nodes matching the given node text.
 312  
          * 
 313  
          * @param nodeText the text on the node.
 314  
          * @return the nodes with the specified text or <code>WidgetNotFoundException</code> if not match exists.
 315  
          * @since 2.0
 316  
          */
 317  
         public List<SWTBotTreeItem> getNodes(final String nodeText) {
 318  13
                 List<SWTBotTreeItem> foundItems = syncExec(new ListResult<SWTBotTreeItem>() {
 319  
                         public List<SWTBotTreeItem> run() {
 320  13
                                 TreeItem[] items = widget.getItems();
 321  13
                                 List<SWTBotTreeItem> results = new ArrayList<SWTBotTreeItem>();
 322  34
                                 for (TreeItem treeItem : items) {
 323  21
                                         if (treeItem.getText().equals(nodeText))
 324  13
                                                 results.add(new SWTBotTreeItem(treeItem, new TextDescription("Tree node with text: " + nodeText)));
 325  
                                 }
 326  13
                                 return results;
 327  
                         }
 328  
                 });
 329  13
                 if (foundItems.isEmpty())
 330  0
                         throw new WidgetNotFoundException("Could not find node with text: " + nodeText); //$NON-NLS-1$
 331  13
                 return foundItems;
 332  
         }
 333  
 
 334  
         /**
 335  
          * Gets the first node found matching the given node text.
 336  
          * 
 337  
          * @param nodeText the text on the node.
 338  
          * @return the first node with the specified text or <code>WidgetNotFoundException</code> if not match exists.
 339  
          * @since 1.2
 340  
          */
 341  
         public SWTBotTreeItem getNode(final String nodeText) {
 342  11
                 return getNode(nodeText, 0);
 343  
         }
 344  
 
 345  
         /**
 346  
          * Selects the current tree item.
 347  
          * 
 348  
          * @return the current node.
 349  
          * @since 1.0
 350  
          */
 351  
         public SWTBotTreeItem select() {
 352  1
                 assertEnabled();
 353  1
                 syncExec(new VoidResult() {
 354  
                         public void run() {
 355  1
                                 tree.setFocus();
 356  1
                                 tree.setSelection(widget);
 357  1
                         }
 358  
                 });
 359  1
                 notifySelect();
 360  1
                 return this;
 361  
         }
 362  
 
 363  
         /**
 364  
          * Click on the table at given coordinates
 365  
          * 
 366  
          * @param x the x co-ordinate of the click
 367  
          * @param y the y co-ordinate of the click
 368  
          * @since 1.2
 369  
          */
 370  
         @Override
 371  
         protected void clickXY(int x, int y) {
 372  2
                 log.debug(MessageFormat.format("Clicking on {0}", this)); //$NON-NLS-1$
 373  2
                 notifyTree(SWT.MouseEnter);
 374  2
                 notifyTree(SWT.MouseMove);
 375  2
                 notifyTree(SWT.Activate);
 376  2
                 notifyTree(SWT.FocusIn);
 377  2
                 notifyTree(SWT.MouseDown, createMouseEvent(x, y, 1, SWT.NONE, 1));
 378  2
                 notifyTree(SWT.MouseUp, createMouseEvent(x, y, 1, SWT.BUTTON1, 1));
 379  2
                 notifyTree(SWT.Selection, createSelectionEvent(SWT.BUTTON1));
 380  2
                 notifyTree(SWT.MouseHover);
 381  2
                 notifyTree(SWT.MouseMove);
 382  2
                 notifyTree(SWT.MouseExit);
 383  2
                 notifyTree(SWT.Deactivate);
 384  2
                 notifyTree(SWT.FocusOut);
 385  2
                 log.debug(MessageFormat.format("Clicked on {0}", this)); //$NON-NLS-1$
 386  2
         }
 387  
 
 388  
         /**
 389  
          * Clicks on this node.
 390  
          * 
 391  
          * @return the current node.
 392  
          * @since 1.2
 393  
          */
 394  
         public SWTBotTreeItem click() {
 395  1
                 assertEnabled();
 396  1
                 Rectangle cellBounds = syncExec(new Result<Rectangle>() {
 397  
                         public Rectangle run() {
 398  1
                                 return widget.getBounds();
 399  
                         }
 400  
                 });
 401  1
                 Point center = getCenter(cellBounds);
 402  1
                 clickXY(center.x, center.y);
 403  1
                 return this;
 404  
         }
 405  
 
 406  
         /**
 407  
          * Clicks on this node at the given column index.
 408  
          * 
 409  
          * @return the current node.
 410  
          * @since 2.0
 411  
          */
 412  
         public SWTBotTreeItem click(final int column) {
 413  1
                 assertEnabled();
 414  1
                 Rectangle cellBounds = syncExec(new Result<Rectangle>() {
 415  
                         public Rectangle run() {
 416  1
                                 return widget.getBounds(column);
 417  
                         }
 418  
                 });
 419  1
                 Point center = getCenter(cellBounds);
 420  1
                 clickXY(center.x, center.y);
 421  1
                 return this;
 422  
         }
 423  
         
 424  
         /**
 425  
          * Get the center of the given rectangle.
 426  
          * @param bounds the rectangle
 427  
          * @return the center.
 428  
          */
 429  
         private Point getCenter(Rectangle bounds) {
 430  2
                 return new Point (bounds.x + (bounds.width / 2), bounds.y + (bounds.height / 2));
 431  
         }
 432  
         
 433  
         /**
 434  
          * Double clicks on this node.
 435  
          * 
 436  
          * @return the current node.
 437  
          * @since 1.2
 438  
          */
 439  
         public SWTBotTreeItem doubleClick() {
 440  2
                 assertEnabled();
 441  2
                 asyncExec(new VoidResult() {
 442  
                         public void run() {
 443  2
                                 tree.setSelection(widget);
 444  2
                         }
 445  
                 });
 446  2
                 notifyTree(SWT.Selection, createSelectionEvent(SWT.BUTTON1));
 447  2
                 notifyTree(SWT.MouseDown);
 448  2
                 notifyTree(SWT.MouseUp);
 449  2
                 notifyTree(SWT.MouseDown);
 450  2
                 notifyTree(SWT.MouseDoubleClick);
 451  2
                 notifyTree(SWT.DefaultSelection);
 452  2
                 notifyTree(SWT.MouseUp);
 453  2
                 return this;
 454  
         }
 455  
 
 456  
         /**
 457  
          * Selects the items matching the array provided.
 458  
          * 
 459  
          * @param items the items to select.
 460  
          * @return the current node.
 461  
          * @since 1.0
 462  
          */
 463  
         public SWTBotTreeItem select(final String... items) {
 464  1
                 assertEnabled();
 465  1
                 final List<String> nodes = Arrays.asList(items);
 466  1
                 Assert.isTrue(getNodes().containsAll(nodes));
 467  
 
 468  1
                 syncExec(new VoidResult() {
 469  
                         public void run() {
 470  1
                                 ArrayList<TreeItem> selection = new ArrayList<TreeItem>();
 471  
 
 472  3
                                 for (String item : items) {
 473  2
                                         SWTBotTreeItem si = getTreeItem(item);
 474  2
                                         selection.add(si.widget);
 475  
                                 }
 476  1
                                 tree.setFocus();
 477  1
                                 tree.setSelection(selection.toArray(new TreeItem[] {}));
 478  1
                         }
 479  
                 });
 480  
 
 481  1
                 notifySelect();
 482  1
                 return this;
 483  
         }
 484  
 
 485  
         /**
 486  
          * Selects the item matching the given name.
 487  
          * 
 488  
          * @param item the items to select.
 489  
          * @return the current node.
 490  
          * @since 1.0
 491  
          */
 492  
         public SWTBotTreeItem select(final String item) {
 493  0
                 return select(new String[] { item });
 494  
         }
 495  
 
 496  
         /**
 497  
          * notifies the tree widget about selection changes.
 498  
          * 
 499  
          * @since 1.0
 500  
          */
 501  
         private void notifySelect() {
 502  2
                 notifyTree(SWT.MouseEnter);
 503  2
                 notifyTree(SWT.MouseMove);
 504  2
                 notifyTree(SWT.Activate);
 505  2
                 notifyTree(SWT.FocusIn);
 506  2
                 notifyTree(SWT.MouseDown);
 507  2
                 notifyTree(SWT.Selection);
 508  2
                 notifyTree(SWT.MouseUp);
 509  2
                 notifyTree(SWT.MouseHover);
 510  2
                 notifyTree(SWT.MouseMove);
 511  2
                 notifyTree(SWT.MouseExit);
 512  2
                 notifyTree(SWT.Deactivate);
 513  2
                 notifyTree(SWT.FocusOut);
 514  2
         }
 515  
 
 516  
         @Override
 517  
         public String getText() {
 518  11
                 return SWTUtils.getText(widget);
 519  
         }
 520  
 
 521  
         @Override
 522  
         public SWTBotMenu contextMenu(String text) {
 523  1
                 new SWTBotTree(tree).waitForEnabled();
 524  1
                 select();
 525  1
                 notifyTree(SWT.MouseDown, createMouseEvent(0, 0, 3, 0, 1));
 526  1
                 notifyTree(SWT.MouseUp, createMouseEvent(0, 0, 3, 0, 1));
 527  1
                 notifyTree(SWT.MenuDetect);
 528  1
                 return super.contextMenu(tree, text);
 529  
         }
 530  
 
 531  
         /**
 532  
          * Toggle the tree item.
 533  
          * 
 534  
          * @since 1.3
 535  
          */
 536  
         public void toggleCheck() {
 537  2
                 setChecked(!isChecked());
 538  2
         }
 539  
 
 540  
         /**
 541  
          * Check the tree item.
 542  
          * 
 543  
          * @since 1.3
 544  
          */
 545  
         public void check() {
 546  2
                 setChecked(true);
 547  1
         }
 548  
 
 549  
         /**
 550  
          * Uncheck the tree item.
 551  
          * 
 552  
          * @since 1.3
 553  
          */
 554  
         public void uncheck() {
 555  1
                 setChecked(false);
 556  1
         }
 557  
 
 558  
         /**
 559  
          * Gets if the checkbox button is checked.
 560  
          * 
 561  
          * @return <code>true</code> if the checkbox is checked. Otherwise <code>false</code>.
 562  
          * @since 1.3
 563  
          */
 564  
         public boolean isChecked() {
 565  7
                 assertIsCheck();
 566  7
                 return syncExec(new BoolResult() {
 567  
                         public Boolean run() {
 568  7
                                 return widget.getChecked();
 569  
                         }
 570  
                 });
 571  
         }
 572  
 
 573  
         /**
 574  
          * Creates an event for CheckboxTreeItem case.
 575  
          * 
 576  
          * @return an event that encapsulates {@link #widget} and {@link #display}.
 577  
          */
 578  4
         private Event createCheckEvent() {
 579  4
                 Event event = createEvent();
 580  4
                 event.time = (int) System.currentTimeMillis();
 581  4
                 event.item = widget;
 582  4
                 event.widget = tree;
 583  4
                 event.detail = SWT.CHECK;
 584  4
                 return event;
 585  
         }
 586  
 
 587  
         private void setChecked(final boolean checked) {
 588  5
                 assertEnabled();
 589  5
                 assertIsCheck();
 590  4
                 syncExec(new VoidResult() {
 591  
                         public void run() {
 592  4
                                 log.debug(MessageFormat.format("Setting state to {0} on: {1}", (checked ? "checked" : "unchecked"), widget.getText())); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
 593  4
                                 widget.setChecked(checked);
 594  4
                         }
 595  
                 });
 596  4
                 notifyCheck();
 597  4
         }
 598  
 
 599  
         private void assertIsCheck() {
 600  12
                 Assert.isLegal(hasStyle(tree, SWT.CHECK), "The tree does not have the style SWT.CHECK"); //$NON-NLS-1$
 601  11
         }
 602  
 
 603  
         /**
 604  
          * notify listeners about checkbox state change.
 605  
          * 
 606  
          * @since 1.3
 607  
          */
 608  
         private void notifyCheck() {
 609  4
                 syncExec(new VoidResult() {
 610  
                         public void run() {
 611  4
                                 tree.notifyListeners(SWT.Selection, createCheckEvent());
 612  4
                         }
 613  
                 });
 614  4
         }
 615  
 
 616  
         protected void assertEnabled() {
 617  69
                 new SWTBotTree(tree).assertEnabled();
 618  69
         }
 619  
 
 620  
         /**
 621  
          * @return <code>true</code> if the item is selected, <code>false</code> otherwise.
 622  
          * @since 2.0
 623  
          */
 624  
         public boolean isSelected() {
 625  2
                 return UIThreadRunnable.syncExec(new BoolResult() {
 626  
                         public Boolean run() {
 627  2
                                 return Arrays.asList(tree.getSelection()).contains(widget);
 628  
                         }
 629  
                 });
 630  
         }
 631  
         
 632  
          /**
 633  
      * Gets if the item is expanded.
 634  
      * 
 635  
      * @return <code>true</code> if the item is expanded,
 636  
      *         <code>false</code> otherwise.
 637  
      * @since 2.0
 638  
      */
 639  
     public boolean isExpanded() {
 640  26
         assertEnabled();
 641  26
         return UIThreadRunnable.syncExec(new BoolResult() {
 642  
             public Boolean run() {
 643  26
                 return widget.getExpanded();
 644  
             }
 645  
         });
 646  
     }
 647  
 
 648  
 
 649  
         /**
 650  
          * Gets all the items in this tree node.
 651  
          * 
 652  
          * @return all the items in this tree node.
 653  
          */
 654  
         public SWTBotTreeItem[] getItems() {
 655  1
                 return syncExec(new ArrayResult<SWTBotTreeItem>() {
 656  
                         public SWTBotTreeItem[] run() {
 657  1
                                 TreeItem[] items = widget.getItems();
 658  1
                                 SWTBotTreeItem[] children = new SWTBotTreeItem[items.length];
 659  3
                                 for (int i = 0; i < items.length; i++) {
 660  2
                                         children[i] = new SWTBotTreeItem(items[i]);
 661  
                                 }
 662  1
                                 return children;
 663  
                         }
 664  
                 });
 665  
         }
 666  
 
 667  
         /**
 668  
          * Gets the tree item matching the given name.
 669  
          * 
 670  
          * @param nodeText the text on the node.
 671  
          * @return the tree item with the specified text.
 672  
          * @throws WidgetNotFoundException if the node was not found.
 673  
          */
 674  2
         private SWTBotTreeItem getTreeItem(final String nodeText) throws WidgetNotFoundException {
 675  
                 try {
 676  2
                         new SWTBot().waitUntil(new DefaultCondition() {
 677  
                                 public String getFailureMessage() {
 678  0
                                         return "Could not find node with text " + nodeText; //$NON-NLS-1$
 679  
                                 }
 680  
 
 681  
                                 public boolean test() throws Exception {
 682  2
                                         return getItem(nodeText) != null;
 683  
                                 }
 684  
                         });
 685  0
                 } catch (TimeoutException e) {
 686  0
                         throw new WidgetNotFoundException("Timed out waiting for tree item " + nodeText, e); //$NON-NLS-1$
 687  
                 }
 688  2
                 return new SWTBotTreeItem(getItem(nodeText));
 689  
         }
 690  
 
 691  
         /**
 692  
          * Gets the item matching the given name.
 693  
          * 
 694  
          * @param nodeText the text on the node.
 695  
          * @return the tree item with the specified text.
 696  
          */
 697  2
         private TreeItem getItem(final String nodeText) {
 698  4
                 return syncExec(new WidgetResult<TreeItem>() {
 699  
                         public TreeItem run() {
 700  4
                                 TreeItem[] items = widget.getItems();
 701  6
                                 for (TreeItem item : items) {
 702  6
                                         if (item.getText().equals(nodeText))
 703  4
                                                 return item;
 704  
                                 }
 705  0
                                 return null;
 706  
                         }
 707  
                 });
 708  
         }
 709  
 
 710  
         public boolean isEnabled() {
 711  0
                 return syncExec(new BoolResult() {
 712  
                         public Boolean run() {
 713  0
                                 return tree.isEnabled();
 714  
                         }
 715  
                 });
 716  
         }
 717  
 
 718  
 }