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