Coverage Report - org.eclipse.swtbot.swt.finder.widgets.SWTBotTableItem
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotTableItem
94%
73/77
100%
2/2
1.029
SWTBotTableItem$1
100%
3/3
N/A
1.029
SWTBotTableItem$10
100%
4/4
N/A
1.029
SWTBotTableItem$11
0%
0/3
N/A
1.029
SWTBotTableItem$2
100%
5/5
N/A
1.029
SWTBotTableItem$3
100%
3/3
N/A
1.029
SWTBotTableItem$4
100%
3/3
N/A
1.029
SWTBotTableItem$5
100%
3/3
N/A
1.029
SWTBotTableItem$6
100%
3/3
N/A
1.029
SWTBotTableItem$7
0%
0/3
N/A
1.029
SWTBotTableItem$8
100%
6/6
100%
2/2
1.029
SWTBotTableItem$9
100%
4/4
N/A
1.029
 
 1  0
 /*******************************************************************************
 2  
  * Copyright (c) 2008,2010 http://www.inria.fr/ 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  
  *     http://www.inria.fr/ - initial API and implementation
 10  
  *     Cédric Chabanois - bug 269164
 11  
  *******************************************************************************/
 12  
 package org.eclipse.swtbot.swt.finder.widgets;
 13  
 
 14  
 import org.eclipse.swt.SWT;
 15  
 import org.eclipse.swt.graphics.Rectangle;
 16  
 import org.eclipse.swt.widgets.Event;
 17  
 import org.eclipse.swt.widgets.Table;
 18  
 import org.eclipse.swt.widgets.TableItem;
 19  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 20  
 import org.eclipse.swtbot.swt.finder.results.BoolResult;
 21  
 import org.eclipse.swtbot.swt.finder.results.Result;
 22  
 import org.eclipse.swtbot.swt.finder.results.StringResult;
 23  
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 24  
 import org.eclipse.swtbot.swt.finder.results.WidgetResult;
 25  
 import org.eclipse.swtbot.swt.finder.utils.MessageFormat;
 26  
 import org.eclipse.swtbot.swt.finder.utils.internal.Assert;
 27  
 import org.hamcrest.SelfDescribing;
 28  
 
 29  
 /**
 30  
  * @author Vincent MAHE <vmahe [at] free [dot]fr>
 31  
  * @author Cédric Chabanois
 32  
  * @version $Id$
 33  
  * @since 1.3
 34  
  */
 35  
 public class SWTBotTableItem extends AbstractSWTBot<TableItem> {
 36  
 
 37  10
         private Table        table;
 38  
 
 39  
         /**
 40  
          * @param tableItem the widget.
 41  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 42  
          */
 43  
         public SWTBotTableItem(final TableItem tableItem) throws WidgetNotFoundException {
 44  14
                 this(tableItem, null);
 45  14
         }
 46  
 
 47  
         /**
 48  
          * @param tableItem the widget.
 49  
          * @param description the description of the widget, this will be reported by {@link #toString()}
 50  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 51  
          */
 52  
         public SWTBotTableItem(final TableItem tableItem, SelfDescribing description) throws WidgetNotFoundException {
 53  14
                 super(tableItem, description);
 54  14
                 this.table = syncExec(new WidgetResult<Table>() {
 55  
                         public Table run() {
 56  14
                                 return tableItem.getParent();
 57  
                         }
 58  
                 });
 59  14
         }
 60  
 
 61  
         /**
 62  
          * Selects the current table item.
 63  
          * 
 64  
          * @return the current node.
 65  
          */
 66  
         public SWTBotTableItem select() {
 67  2
                 assertEnabled();
 68  2
                 syncExec(new VoidResult() {
 69  
                         public void run() {
 70  2
                                 table.setFocus();
 71  2
                                 table.setSelection(widget);
 72  2
                         }
 73  
                 });
 74  2
                 notifySelect();
 75  2
                 return this;
 76  
         }
 77  
 
 78  
         /**
 79  
          * Click on the table at given coordinates
 80  
          * 
 81  
          * @param x the x co-ordinate of the click
 82  
          * @param y the y co-ordinate of the click
 83  
          */
 84  
         protected void clickXY(int x, int y) {
 85  1
                 log.debug(MessageFormat.format("Clicking on {0}", this)); //$NON-NLS-1$
 86  1
                 notifyTable(SWT.MouseEnter);
 87  1
                 notifyTable(SWT.MouseMove);
 88  1
                 notifyTable(SWT.Activate);
 89  1
                 notifyTable(SWT.FocusIn);
 90  1
                 notifyTable(SWT.MouseDown, createMouseEvent(x, y, 1, SWT.NONE, 1));
 91  1
                 notifyTable(SWT.Selection, createEvent());
 92  1
                 notifyTable(SWT.MouseUp, createMouseEvent(x, y, 1, SWT.BUTTON1, 1));
 93  1
                 notifyTable(SWT.MouseHover);
 94  1
                 notifyTable(SWT.MouseMove);
 95  1
                 notifyTable(SWT.MouseExit);
 96  1
                 notifyTable(SWT.Deactivate);
 97  1
                 notifyTable(SWT.FocusOut);
 98  1
                 log.debug(MessageFormat.format("Clicked on {0}", this)); //$NON-NLS-1$
 99  1
         }
 100  
 
 101  
         private void notifyTable(int eventType, Event event) {
 102  13
                 notify(eventType, event, table);
 103  13
         }
 104  
 
 105  
         private void notifyTable(int event) {
 106  10
                 notifyTable(event, createEvent());
 107  10
         }
 108  
 
 109  
         /**
 110  
          * Clicks on this node.
 111  
          * 
 112  
          * @return the current node.
 113  
          */
 114  
         public SWTBotTableItem click() {
 115  1
                 assertEnabled();
 116  1
                 Rectangle cellBounds = syncExec(new Result<Rectangle>() {
 117  
                         public Rectangle run() {
 118  1
                                 return widget.getBounds();
 119  
                         }
 120  
                 });
 121  1
                 clickXY(cellBounds.x + (cellBounds.width / 2), cellBounds.y + (cellBounds.height / 2));
 122  1
                 return this;
 123  
         }
 124  
 
 125  
         public String getText() {
 126  4
                 return syncExec(new StringResult() {
 127  
                         public String run() {
 128  4
                                 return widget.getText();
 129  
                         }
 130  
                 });
 131  
         }
 132  
 
 133  
         public String getText(final int index) {
 134  1
                 return syncExec(new StringResult() {
 135  
                         public String run() {
 136  1
                                 return widget.getText(index);
 137  
                         }
 138  
                 });
 139  
         }
 140  
         
 141  
         public SWTBotMenu contextMenu(String text) throws WidgetNotFoundException {
 142  1
                 new SWTBotTable(table).waitForEnabled();
 143  1
                 select();
 144  1
                 notifyTable(SWT.MenuDetect);
 145  1
                 return super.contextMenu(table, text);
 146  
         }
 147  
 
 148  
         /**
 149  
          * Toggle the table item.
 150  
          */
 151  
         public void toggleCheck() {
 152  2
                 setChecked(!isChecked());
 153  2
         }
 154  
 
 155  
         /**
 156  
          * Check the table item.
 157  
          */
 158  
         public void check() {
 159  2
                 setChecked(true);
 160  1
         }
 161  
 
 162  
         /**
 163  
          * Uncheck the table item.
 164  
          */
 165  
         public void uncheck() {
 166  1
                 setChecked(false);
 167  1
         }
 168  
 
 169  
         /**
 170  
          * Gets if the checkbox button is checked.
 171  
          * 
 172  
          * @return <code>true</code> if the checkbox is checked. Otherwise <code>false</code>.
 173  
          */
 174  
         public boolean isChecked() {
 175  7
                 assertIsCheck();
 176  7
                 return syncExec(new BoolResult() {
 177  
                         public Boolean run() {
 178  7
                                 return widget.getChecked();
 179  
                         }
 180  
                 });
 181  
         }
 182  
 
 183  
         /**
 184  
          * Gets if the checkbox button is grayed.
 185  
          * 
 186  
          * @return <code>true</code> if the checkbox is grayed, <code>false</code> otherwise.
 187  
          */
 188  
         public boolean isGrayed() {
 189  0
                 assertIsCheck();
 190  0
                 return syncExec(new BoolResult() {
 191  
                         public Boolean run() {
 192  0
                                 return widget.getGrayed();
 193  
                         }
 194  
                 });
 195  
         }
 196  
 
 197  
         /**
 198  
          * Creates an event for CheckboxTableItem case.
 199  
          * 
 200  
          * @return an event that encapsulates {@link #widget} and {@link #display}.
 201  
          */
 202  4
         private Event createCheckEvent() {
 203  4
                 Event event = createEvent();
 204  4
                 event.detail = SWT.CHECK;
 205  4
                 return event;
 206  
         }
 207  
 
 208  
         protected Event createEvent() {
 209  17
                 Event event = super.createEvent();
 210  17
                 event.widget = table;
 211  17
                 event.item = widget;
 212  17
                 return event;
 213  
         }
 214  
 
 215  
         private void setChecked(final boolean checked) {
 216  5
                 assertEnabled();
 217  5
                 assertIsCheck();
 218  4
                 syncExec(new VoidResult() {
 219  
                         public void run() {
 220  4
                                 TableItem item = widget;
 221  4
                                 log.debug(MessageFormat.format("Setting state to {0} on: {1}", (checked ? "checked" : "unchecked"), item.getText())); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
 222  4
                                 item.setChecked(checked);
 223  4
                         }
 224  
                 });
 225  4
                 notifyCheck();
 226  4
         }
 227  
 
 228  
         private void assertIsCheck() {
 229  12
                 Assert.isLegal(hasStyle(table, SWT.CHECK), "The table does not have the style SWT.CHECK"); //$NON-NLS-1$
 230  11
         }
 231  
 
 232  
         /**
 233  
          * notify listeners about checkbox state change.
 234  
          * 
 235  
          * @since 1.3
 236  
          */
 237  
         private void notifyCheck() {
 238  4
                 syncExec(new VoidResult() {
 239  
                         public void run() {
 240  4
                                 table.notifyListeners(SWT.Selection, createCheckEvent());
 241  4
                         }
 242  
                 });
 243  4
         }
 244  
 
 245  
         private void notifySelect() {
 246  2
                 syncExec(new VoidResult() {
 247  
                         public void run() {
 248  2
                                 table.notifyListeners(SWT.Selection, createSelectionEvent());
 249  2
                         }
 250  
                 });
 251  2
         }
 252  
 
 253  
         protected void assertEnabled() {
 254  8
                 new SWTBotTable(table).assertEnabled();
 255  8
         }
 256  
 
 257  2
         private Event createSelectionEvent() {
 258  2
                 Event event = createEvent();
 259  2
                 event.item = widget;
 260  2
                 event.widget = table;
 261  2
                 return event;
 262  
         }
 263  
         
 264  
         public boolean isEnabled() {
 265  0
                 return syncExec(new BoolResult() {
 266  
                         public Boolean run() {
 267  0
                                 return table.isEnabled();
 268  
                         }
 269  
                 });
 270  
         }
 271  
 
 272  
 //        protected Rectangle absoluteLocation() {
 273  
 //                return syncExec(new Result<Rectangle>() {
 274  
 //                        public Rectangle run() {
 275  
 //                                return display.map(widget.getParent(), null, widget.getBounds());
 276  
 //                        }
 277  
 //                });
 278  
 //        }
 279  
 //        
 280  
 //        /**
 281  
 //         * Click on the center of the widget.
 282  
 //         * 
 283  
 //         * @param post
 284  
 //         *                                 Whether or not {@link Display#post} should be used
 285  
 //         */
 286  
 //        private SWTBotTableItem click(final boolean post) {
 287  
 //                if (post) {
 288  
 //                        Rectangle location = absoluteLocation();
 289  
 //                        click(location.x, location.y, true);
 290  
 //                }
 291  
 //                else
 292  
 //                        click();
 293  
 //                return this;
 294  
 //        }
 295  
 //        
 296  
 //        /**
 297  
 //         * Right click on the center of the widget.
 298  
 //         * 
 299  
 //         * @param post
 300  
 //         *                                 Whether or not {@link Display#post} should be used
 301  
 //         */
 302  
 //        private SWTBotTableItem rightClick(final boolean post) {
 303  
 //                if (post) {
 304  
 //                        Rectangle location = absoluteLocation();
 305  
 //                        rightClick(location.x, location.y, true);
 306  
 //                }
 307  
 //                else
 308  
 //                        rightClick();
 309  
 //                return this;
 310  
 //        }
 311  
 //
 312  
 //        /**
 313  
 //         * Moves the cursor to the center of the widget
 314  
 //         */
 315  
 //        private SWTBotTableItem moveMouseToWidget() {
 316  
 //                syncExec(new VoidResult() {
 317  
 //                        public void run() {
 318  
 //                                Rectangle location = absoluteLocation();
 319  
 //                                moveMouse(location.x, location.y);
 320  
 //                        }
 321  
 //                });
 322  
 //                return this;
 323  
 //        }
 324  
 }