| 1 | 8 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
package org.eclipse.swtbot.swt.finder.widgets; |
| 12 | |
|
| 13 | |
import java.util.ArrayList; |
| 14 | |
import java.util.Arrays; |
| 15 | |
import java.util.List; |
| 16 | |
|
| 17 | |
import org.eclipse.swt.SWT; |
| 18 | |
import org.eclipse.swt.widgets.Tree; |
| 19 | |
import org.eclipse.swt.widgets.TreeItem; |
| 20 | |
import org.eclipse.swtbot.swt.finder.ReferenceBy; |
| 21 | |
import org.eclipse.swtbot.swt.finder.SWTBot; |
| 22 | |
import org.eclipse.swtbot.swt.finder.SWTBotWidget; |
| 23 | |
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; |
| 24 | |
import org.eclipse.swtbot.swt.finder.results.ArrayResult; |
| 25 | |
import org.eclipse.swtbot.swt.finder.results.BoolResult; |
| 26 | |
import org.eclipse.swtbot.swt.finder.results.IntResult; |
| 27 | |
import org.eclipse.swtbot.swt.finder.results.ListResult; |
| 28 | |
import org.eclipse.swtbot.swt.finder.results.Result; |
| 29 | |
import org.eclipse.swtbot.swt.finder.results.StringResult; |
| 30 | |
import org.eclipse.swtbot.swt.finder.results.VoidResult; |
| 31 | |
import org.eclipse.swtbot.swt.finder.results.WidgetResult; |
| 32 | |
import org.eclipse.swtbot.swt.finder.utils.MessageFormat; |
| 33 | |
import org.eclipse.swtbot.swt.finder.utils.StringUtils; |
| 34 | |
import org.eclipse.swtbot.swt.finder.utils.TableCollection; |
| 35 | |
import org.eclipse.swtbot.swt.finder.utils.TableRow; |
| 36 | |
import org.eclipse.swtbot.swt.finder.utils.internal.Assert; |
| 37 | |
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition; |
| 38 | |
import org.hamcrest.SelfDescribing; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
@SWTBotWidget(clasz = Tree.class, preferredName = "tree", referenceBy = { ReferenceBy.LABEL }) |
| 46 | |
public class SWTBotTree extends AbstractSWTBot<Tree> { |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public SWTBotTree(Tree tree, SelfDescribing description) throws WidgetNotFoundException { |
| 56 | 144 | super(tree, description); |
| 57 | 144 | } |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
public SWTBotTree(Tree tree) throws WidgetNotFoundException { |
| 66 | 79 | this(tree, null); |
| 67 | 79 | } |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
public int rowCount() { |
| 75 | 9 | return syncExec(new IntResult() { |
| 76 | |
public Integer run() { |
| 77 | 9 | return widget.getItems().length; |
| 78 | |
} |
| 79 | |
}); |
| 80 | |
} |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
public int columnCount() { |
| 88 | 28 | return syncExec(new IntResult() { |
| 89 | |
public Integer run() { |
| 90 | 28 | return widget.getColumnCount(); |
| 91 | |
} |
| 92 | |
}); |
| 93 | |
} |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
public List<String> columns() { |
| 101 | 4 | final int columnCount = columnCount(); |
| 102 | 4 | return syncExec(new ListResult<String>() { |
| 103 | |
public List<String> run() { |
| 104 | 4 | String columns[] = new String[columnCount]; |
| 105 | 20 | for (int i = 0; i < columnCount; i++) |
| 106 | 16 | columns[i] = widget.getColumn(i).getText(); |
| 107 | 4 | return new ArrayList<String>(Arrays.asList(columns)); |
| 108 | |
} |
| 109 | |
}); |
| 110 | |
} |
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
public String cell(final int row, final int column) { |
| 120 | 6 | int rowCount = rowCount(); |
| 121 | 6 | int columnCount = columnCount(); |
| 122 | |
|
| 123 | 6 | Assert.isLegal(row < rowCount, "The row number (" + row + ") is more than the number of rows(" + rowCount + ") in the tree."); |
| 124 | 12 | Assert.isLegal(column < columnCount, "The column number (" + column + ") is more than the number of column(" + columnCount |
| 125 | 6 | + ") in the tree."); |
| 126 | |
|
| 127 | 6 | return syncExec(new StringResult() { |
| 128 | |
public String run() { |
| 129 | 6 | TreeItem item = widget.getItem(row); |
| 130 | 6 | return item.getText(column); |
| 131 | |
} |
| 132 | |
}); |
| 133 | |
} |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
public String cell(int row, String columnName) { |
| 143 | 3 | List<String> columns = columns(); |
| 144 | 3 | Assert.isLegal(columns.contains(columnName), "The column `" + columnName + "' is not found."); |
| 145 | 3 | int columnIndex = columns.indexOf(columnName); |
| 146 | 3 | if (columnIndex == -1) |
| 147 | 0 | return ""; |
| 148 | 3 | return cell(row, columnIndex); |
| 149 | |
} |
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
public int selectionCount() { |
| 157 | 4 | return syncExec(new IntResult() { |
| 158 | |
public Integer run() { |
| 159 | 4 | return widget.getSelectionCount(); |
| 160 | |
} |
| 161 | |
}); |
| 162 | |
} |
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
public TableCollection selection() { |
| 170 | 5 | final int columnCount = columnCount(); |
| 171 | |
|
| 172 | 5 | return syncExec(new Result<TableCollection>() { |
| 173 | |
public TableCollection run() { |
| 174 | 5 | final TableCollection selection = new TableCollection(); |
| 175 | 5 | TreeItem[] items = widget.getSelection(); |
| 176 | 10 | for (TreeItem item : items) { |
| 177 | 5 | TableRow tableRow = new TableRow(); |
| 178 | 5 | if (columnCount == 0) |
| 179 | 2 | tableRow.add(item.getText()); |
| 180 | |
else |
| 181 | 15 | for (int j = 0; j < columnCount; j++) |
| 182 | 12 | tableRow.add(item.getText(j)); |
| 183 | 5 | selection.add(tableRow); |
| 184 | |
} |
| 185 | 5 | return selection; |
| 186 | |
} |
| 187 | |
}); |
| 188 | |
} |
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
public SWTBotTree select(final String... items) { |
| 197 | 1 | waitForEnabled(); |
| 198 | 1 | setFocus(); |
| 199 | 1 | asyncExec(new VoidResult() { |
| 200 | |
public void run() { |
| 201 | 1 | List<TreeItem> selection = new ArrayList<TreeItem>(); |
| 202 | 3 | for (String item : items) { |
| 203 | 2 | SWTBotTreeItem si = getTreeItem(item); |
| 204 | 2 | selection.add(si.widget); |
| 205 | |
} |
| 206 | 1 | if (!hasStyle(widget, SWT.MULTI) && items.length > 1) |
| 207 | 0 | log.warn("Tree does not support SWT.MULTI, cannot make multiple selections"); |
| 208 | 1 | widget.setSelection(selection.toArray(new TreeItem[] {})); |
| 209 | 1 | } |
| 210 | |
}); |
| 211 | 1 | notifySelect(); |
| 212 | 1 | return this; |
| 213 | |
} |
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
public SWTBotTree select(final SWTBotTreeItem... items) { |
| 223 | 0 | assertEnabled(); |
| 224 | 0 | setFocus(); |
| 225 | 0 | asyncExec(new VoidResult() { |
| 226 | |
public void run() { |
| 227 | 0 | List<TreeItem> selection = new ArrayList<TreeItem>(); |
| 228 | 0 | for (SWTBotTreeItem treeItem : items) { |
| 229 | 0 | selection.add(treeItem.widget); |
| 230 | |
} |
| 231 | 0 | if (!hasStyle(widget, SWT.MULTI) && items.length > 1) |
| 232 | 0 | log.warn("Tree does not support SWT.MULTI, cannot make multiple selections"); |
| 233 | 0 | widget.setSelection(selection.toArray(new TreeItem[] {})); |
| 234 | 0 | } |
| 235 | |
}); |
| 236 | 0 | notifySelect(); |
| 237 | 0 | return this; |
| 238 | |
} |
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
public SWTBotTree unselect() { |
| 246 | 1 | waitForEnabled(); |
| 247 | 1 | asyncExec(new VoidResult() { |
| 248 | |
public void run() { |
| 249 | 1 | log.debug(MessageFormat.format("Unselecting all in {0}", widget)); |
| 250 | 1 | widget.deselectAll(); |
| 251 | 1 | } |
| 252 | |
}); |
| 253 | 1 | notifySelect(); |
| 254 | 1 | return this; |
| 255 | |
} |
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
public SWTBotTree select(final int... indices) { |
| 264 | 3 | waitForEnabled(); |
| 265 | 3 | setFocus(); |
| 266 | 3 | asyncExec(new VoidResult() { |
| 267 | |
public void run() { |
| 268 | 3 | log.debug(MessageFormat.format("Selecting rows [{0}] in tree{1}", StringUtils.join(indices, ", "), this)); |
| 269 | 3 | if (!hasStyle(widget, SWT.MULTI) && indices.length > 1) |
| 270 | 0 | log.warn("Tree does not support SWT.MULTI, cannot make multiple selections"); |
| 271 | 3 | TreeItem items[] = new TreeItem[indices.length]; |
| 272 | 7 | for (int i = 0; i < indices.length; i++) |
| 273 | 4 | items[i] = widget.getItem(indices[i]); |
| 274 | 3 | widget.setSelection(items); |
| 275 | 3 | } |
| 276 | |
}); |
| 277 | 3 | notifySelect(); |
| 278 | 3 | return this; |
| 279 | |
} |
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
protected void notifySelect() { |
| 285 | 5 | notify(SWT.MouseEnter); |
| 286 | 5 | notify(SWT.MouseMove); |
| 287 | 5 | notify(SWT.Activate); |
| 288 | 5 | notify(SWT.FocusIn); |
| 289 | 5 | notify(SWT.MouseDown); |
| 290 | 5 | notify(SWT.Selection); |
| 291 | 5 | notify(SWT.MouseUp); |
| 292 | 5 | notify(SWT.MouseHover); |
| 293 | 5 | notify(SWT.MouseMove); |
| 294 | 5 | notify(SWT.MouseExit); |
| 295 | 5 | notify(SWT.Deactivate); |
| 296 | 5 | notify(SWT.FocusOut); |
| 297 | 5 | } |
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
public SWTBotTreeItem expandNode(String... nodes) throws WidgetNotFoundException { |
| 307 | 9 | Assert.isNotEmpty((Object[])nodes); |
| 308 | |
|
| 309 | 8 | log.debug(MessageFormat.format("Expanding nodes {0} in tree {1}", StringUtils.join(nodes, ">"), this)); |
| 310 | |
|
| 311 | 8 | waitForEnabled(); |
| 312 | 8 | SWTBotTreeItem item = getTreeItem(nodes[0]).expand(); |
| 313 | |
|
| 314 | 8 | List<String> asList = new ArrayList<String>(Arrays.asList(nodes)); |
| 315 | 8 | asList.remove(0); |
| 316 | 8 | if (!asList.isEmpty()) |
| 317 | 1 | item = item.expandNode(asList.toArray(new String[0])); |
| 318 | |
|
| 319 | 8 | return item; |
| 320 | |
} |
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
public SWTBotTreeItem collapseNode(final String nodeText) throws WidgetNotFoundException { |
| 330 | 0 | waitForEnabled(); |
| 331 | 0 | return getTreeItem(nodeText).collapse(); |
| 332 | |
} |
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
public int visibleRowCount() { |
| 340 | 4 | return syncExec(new IntResult() { |
| 341 | |
public Integer run() { |
| 342 | 4 | TreeItem[] items = widget.getItems(); |
| 343 | 4 | return getVisibleChildrenCount(items); |
| 344 | |
} |
| 345 | |
|
| 346 | |
private int getVisibleChildrenCount(TreeItem item) { |
| 347 | 27 | if (item.getExpanded()) |
| 348 | 7 | return getVisibleChildrenCount(item.getItems()); |
| 349 | 20 | return 0; |
| 350 | |
} |
| 351 | |
|
| 352 | |
private int getVisibleChildrenCount(TreeItem[] items) { |
| 353 | 11 | int count = 0; |
| 354 | 38 | for (TreeItem item : items) { |
| 355 | 27 | int j = getVisibleChildrenCount(item) + 1; |
| 356 | 27 | count += j; |
| 357 | |
} |
| 358 | 11 | return count; |
| 359 | |
} |
| 360 | |
}); |
| 361 | |
|
| 362 | |
} |
| 363 | |
|
| 364 | |
|
| 365 | |
|
| 366 | |
|
| 367 | |
|
| 368 | |
|
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
public SWTBotTreeItem expandNode(final String nodeText, final boolean recursive) throws WidgetNotFoundException { |
| 373 | 2 | waitForEnabled(); |
| 374 | 2 | return syncExec(new Result<SWTBotTreeItem>() { |
| 375 | |
public SWTBotTreeItem run() { |
| 376 | |
SWTBotTreeItem item; |
| 377 | |
try { |
| 378 | 2 | item = getTreeItem(nodeText); |
| 379 | 2 | expandNode(item); |
| 380 | 0 | } catch (WidgetNotFoundException e) { |
| 381 | 0 | throw new RuntimeException(e); |
| 382 | |
} |
| 383 | 2 | return item; |
| 384 | |
} |
| 385 | |
|
| 386 | |
private void expandNode(SWTBotTreeItem item) throws WidgetNotFoundException { |
| 387 | 8 | item.expand(); |
| 388 | 8 | if (recursive) |
| 389 | 8 | expandTreeItem(item.widget); |
| 390 | 8 | } |
| 391 | |
|
| 392 | |
private void expandTreeItem(TreeItem node) throws WidgetNotFoundException { |
| 393 | 8 | TreeItem[] items = node.getItems(); |
| 394 | 14 | for (TreeItem item : items) { |
| 395 | 6 | expandNode(new SWTBotTreeItem(item)); |
| 396 | |
} |
| 397 | 8 | } |
| 398 | |
}); |
| 399 | |
} |
| 400 | |
|
| 401 | |
|
| 402 | |
|
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | |
|
| 407 | |
|
| 408 | |
public SWTBotTreeItem getTreeItem(final String nodeText) throws WidgetNotFoundException { |
| 409 | |
try { |
| 410 | 30 | new SWTBot().waitUntil(new DefaultCondition() { |
| 411 | |
public String getFailureMessage() { |
| 412 | 0 | return "Could not find node with text " + nodeText; |
| 413 | |
} |
| 414 | |
|
| 415 | |
public boolean test() throws Exception { |
| 416 | 30 | return getItem(nodeText) != null; |
| 417 | |
} |
| 418 | |
}); |
| 419 | 0 | } catch (TimeoutException e) { |
| 420 | 0 | throw new WidgetNotFoundException("Timed out waiting for tree item " + nodeText, e); |
| 421 | |
} |
| 422 | 30 | return new SWTBotTreeItem(getItem(nodeText)); |
| 423 | |
} |
| 424 | |
|
| 425 | |
|
| 426 | |
|
| 427 | |
|
| 428 | |
|
| 429 | |
|
| 430 | |
|
| 431 | 30 | private TreeItem getItem(final String nodeText) { |
| 432 | 60 | return syncExec(new WidgetResult<TreeItem>() { |
| 433 | |
public TreeItem run() { |
| 434 | 60 | TreeItem[] items = widget.getItems(); |
| 435 | 130 | for (TreeItem item : items) { |
| 436 | 130 | if (item.getText().equals(nodeText)) |
| 437 | 60 | return item; |
| 438 | |
} |
| 439 | 0 | return null; |
| 440 | |
} |
| 441 | |
}); |
| 442 | |
} |
| 443 | |
|
| 444 | |
|
| 445 | |
|
| 446 | |
|
| 447 | |
|
| 448 | |
|
| 449 | |
|
| 450 | |
public SWTBotTreeItem[] getAllItems() { |
| 451 | 1 | return syncExec(new ArrayResult<SWTBotTreeItem>() { |
| 452 | |
public SWTBotTreeItem[] run() { |
| 453 | 1 | TreeItem[] items = widget.getItems(); |
| 454 | 1 | SWTBotTreeItem[] result = new SWTBotTreeItem[items.length]; |
| 455 | |
|
| 456 | 5 | for (int i = 0; i < items.length; i++) |
| 457 | |
try { |
| 458 | 4 | result[i] = new SWTBotTreeItem(items[i]); |
| 459 | 0 | } catch (WidgetNotFoundException e) { |
| 460 | 0 | return new SWTBotTreeItem[0]; |
| 461 | |
} |
| 462 | 1 | return result; |
| 463 | |
} |
| 464 | |
}); |
| 465 | |
} |
| 466 | |
|
| 467 | |
|
| 468 | |
|
| 469 | |
|
| 470 | |
|
| 471 | |
|
| 472 | |
|
| 473 | |
public boolean hasItems() { |
| 474 | 2 | return syncExec(new BoolResult() { |
| 475 | |
public Boolean run() { |
| 476 | 2 | return widget.getItemCount() > 0; |
| 477 | |
} |
| 478 | |
}); |
| 479 | |
} |
| 480 | |
|
| 481 | |
} |