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