| 1 | 0 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 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 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
public class SWTBotTableItem extends AbstractSWTBot<TableItem> { |
| 36 | |
|
| 37 | 10 | private Table table; |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public SWTBotTableItem(final TableItem tableItem) throws WidgetNotFoundException { |
| 44 | 14 | this(tableItem, null); |
| 45 | 14 | } |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 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 | |
|
| 63 | |
|
| 64 | |
|
| 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 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
protected void clickXY(int x, int y) { |
| 85 | 1 | log.debug(MessageFormat.format("Clicking on {0}", this)); |
| 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)); |
| 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 | |
|
| 111 | |
|
| 112 | |
|
| 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 | |
|
| 150 | |
|
| 151 | |
public void toggleCheck() { |
| 152 | 2 | setChecked(!isChecked()); |
| 153 | 2 | } |
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
public void check() { |
| 159 | 2 | setChecked(true); |
| 160 | 1 | } |
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
public void uncheck() { |
| 166 | 1 | setChecked(false); |
| 167 | 1 | } |
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 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 | |
|
| 185 | |
|
| 186 | |
|
| 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 | |
|
| 199 | |
|
| 200 | |
|
| 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())); |
| 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"); |
| 230 | 11 | } |
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 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 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
} |