JListOperator.java revision 13978:1993af50385d
1/* 2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23package org.netbeans.jemmy.operators; 24 25import java.awt.Color; 26import java.awt.Component; 27import java.awt.Container; 28import java.awt.Dimension; 29import java.awt.Point; 30import java.awt.Rectangle; 31import java.util.Hashtable; 32import java.util.Vector; 33 34import javax.swing.JList; 35import javax.swing.JScrollPane; 36import javax.swing.ListCellRenderer; 37import javax.swing.ListModel; 38import javax.swing.ListSelectionModel; 39import javax.swing.event.ListSelectionListener; 40import javax.swing.plaf.ListUI; 41 42import org.netbeans.jemmy.ComponentChooser; 43import org.netbeans.jemmy.ComponentSearcher; 44import org.netbeans.jemmy.JemmyInputException; 45import org.netbeans.jemmy.Outputable; 46import org.netbeans.jemmy.QueueTool; 47import org.netbeans.jemmy.TestOut; 48import org.netbeans.jemmy.TimeoutExpiredException; 49import org.netbeans.jemmy.drivers.DriverManager; 50import org.netbeans.jemmy.drivers.MultiSelListDriver; 51import org.netbeans.jemmy.util.EmptyVisualizer; 52 53/** 54 * <BR><BR>Timeouts used: <BR> 55 * ComponentOperator.WaitComponentTimeout - time to wait component displayed 56 * <BR> 57 * ComponentOperator.WaitStateTimeout - time to wait for item, and for item to 58 * be selected <BR> 59 * JScrollBarOperator.OneScrollClickTimeout - time for one scroll click <BR> 60 * JScrollBarOperator.WholeScrollTimeout - time for the whole scrolling <BR>. 61 * 62 * @see org.netbeans.jemmy.Timeouts 63 * 64 * @author Alexandre Iline (alexandre.iline@oracle.com) 65 * 66 */ 67public class JListOperator extends JComponentOperator 68 implements Outputable { 69 70 /** 71 * Identifier for a "item" properties. 72 * 73 * @see #getDump 74 */ 75 public static final String ITEM_PREFIX_DPROP = "Item"; 76 77 /** 78 * Identifier for a "selected item" property. 79 * 80 * @see #getDump 81 */ 82 public static final String SELECTED_ITEM_PREFIX_DPROP = "SelectedItem"; 83 84 private TestOut output; 85 private MultiSelListDriver driver; 86 87 /** 88 * Constructor. 89 * 90 * @param b a component 91 */ 92 public JListOperator(JList<?> b) { 93 super(b); 94 driver = DriverManager.getMultiSelListDriver(getClass()); 95 } 96 97 /** 98 * Constructs a JListOperator object. 99 * 100 * @param cont a container 101 * @param chooser a component chooser specifying searching criteria. 102 * @param index an index between appropriate ones. 103 */ 104 public JListOperator(ContainerOperator<?> cont, ComponentChooser chooser, int index) { 105 this((JList) cont. 106 waitSubComponent(new JListFinder(chooser), 107 index)); 108 copyEnvironment(cont); 109 } 110 111 /** 112 * Constructs a JListOperator object. 113 * 114 * @param cont a container 115 * @param chooser a component chooser specifying searching criteria. 116 */ 117 public JListOperator(ContainerOperator<?> cont, ComponentChooser chooser) { 118 this(cont, chooser, 0); 119 } 120 121 /** 122 * Constructor. Waits item text first. Uses cont's timeout and output for 123 * waiting and to init operator. 124 * 125 * @param cont a container 126 * @param text Text of item which is currently selected. 127 * @param itemIndex Item index. 128 * @param index Ordinal component index. 129 * 130 */ 131 public JListOperator(ContainerOperator<?> cont, String text, int itemIndex, int index) { 132 this((JList) waitComponent(cont, 133 new JListByItemFinder(text, itemIndex, 134 cont.getComparator()), 135 index)); 136 copyEnvironment(cont); 137 } 138 139 /** 140 * Constructor. Waits component by selected item text first. Uses cont's 141 * timeout and output for waiting and to init operator. 142 * 143 * @param cont a container 144 * @param text Text of item which is currently selected. 145 * @param index Ordinal component index. 146 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 147 * 148 */ 149 public JListOperator(ContainerOperator<?> cont, String text, int index) { 150 this(cont, text, -1, index); 151 } 152 153 /** 154 * Constructor. Waits component in container first. Uses cont's timeout and 155 * output for waiting and to init operator. 156 * 157 * @param cont a container 158 * @param text Text of item which is currently selected. 159 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 160 * 161 */ 162 public JListOperator(ContainerOperator<?> cont, String text) { 163 this(cont, text, 0); 164 } 165 166 /** 167 * Constructor. Waits component in container first. Uses cont's timeout and 168 * output for waiting and to init operator. 169 * 170 * @param cont a container 171 * @param index Ordinal component index. 172 * 173 */ 174 public JListOperator(ContainerOperator<?> cont, int index) { 175 this((JList) waitComponent(cont, 176 new JListFinder(), 177 index)); 178 copyEnvironment(cont); 179 } 180 181 /** 182 * Constructor. Waits component in container first. Uses cont's timeout and 183 * output for waiting and to init operator. 184 * 185 * @param cont a container 186 * 187 */ 188 public JListOperator(ContainerOperator<?> cont) { 189 this(cont, 0); 190 } 191 192 /** 193 * Searches JList in container. 194 * 195 * @param cont Container to search component in. 196 * @param chooser org.netbeans.jemmy.ComponentChooser implementation. 197 * @param index Ordinal component index. 198 * @return JList instance or null if component was not found. 199 */ 200 public static JList<?> findJList(Container cont, ComponentChooser chooser, int index) { 201 return (JList) findComponent(cont, new JListFinder(chooser), index); 202 } 203 204 /** 205 * Searches 0'th JList in container. 206 * 207 * @param cont Container to search component in. 208 * @param chooser org.netbeans.jemmy.ComponentChooser implementation. 209 * @return JList instance or null if component was not found. 210 */ 211 public static JList<?> findJList(Container cont, ComponentChooser chooser) { 212 return findJList(cont, chooser, 0); 213 } 214 215 /** 216 * Searches JList by item. 217 * 218 * @param cont Container to search component in. 219 * @param text Item text. If null, contents is not checked. 220 * @param ce Compare text exactly. 221 * @param ccs Compare text case sensitively. 222 * @param itemIndex Index of item to compare text. If -1, selected item is 223 * checked. 224 * @param index Ordinal component index. 225 * @return JList instance or null if component was not found. 226 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 227 */ 228 public static JList<?> findJList(Container cont, String text, boolean ce, boolean ccs, int itemIndex, int index) { 229 return findJList(cont, new JListByItemFinder(text, itemIndex, new DefaultStringComparator(ce, ccs)), index); 230 } 231 232 /** 233 * Searches JList by item. 234 * 235 * @param cont Container to search component in. 236 * @param text Item text. If null, contents is not checked. 237 * @param ce Compare text exactly. 238 * @param ccs Compare text case sensitively. 239 * @param itemIndex Index of item to compare text. If -1, selected item is 240 * checked. 241 * @return JList instance or null if component was not found. 242 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 243 */ 244 public static JList<?> findJList(Container cont, String text, boolean ce, boolean ccs, int itemIndex) { 245 return findJList(cont, text, ce, ccs, itemIndex, 0); 246 } 247 248 /** 249 * Waits JList in container. 250 * 251 * @param cont Container to search component in. 252 * @param chooser org.netbeans.jemmy.ComponentChooser implementation. 253 * @param index Ordinal component index. 254 * @return JList instance or null if component was not found. 255 * 256 */ 257 public static JList<?> waitJList(Container cont, ComponentChooser chooser, int index) { 258 return (JList) waitComponent(cont, new JListFinder(chooser), index); 259 } 260 261 /** 262 * Waits 0'th JList in container. 263 * 264 * @param cont Container to search component in. 265 * @param chooser org.netbeans.jemmy.ComponentChooser implementation. 266 * @return JList instance or null if component was not found. 267 * 268 */ 269 public static JList<?> waitJList(Container cont, ComponentChooser chooser) { 270 return waitJList(cont, chooser, 0); 271 } 272 273 /** 274 * Waits JList by item. 275 * 276 * @param cont Container to search component in. 277 * @param text Item text. If null, contents is not checked. 278 * @param ce Compare text exactly. 279 * @param ccs Compare text case sensitively. 280 * @param itemIndex Index of item to compare text. If -1, selected item is 281 * checked. 282 * @param index Ordinal component index. 283 * @return JList instance or null if component was not found. 284 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 285 * 286 */ 287 public static JList<?> waitJList(Container cont, String text, boolean ce, boolean ccs, int itemIndex, int index) { 288 return waitJList(cont, new JListByItemFinder(text, itemIndex, new DefaultStringComparator(ce, ccs)), index); 289 } 290 291 /** 292 * Waits JList by item. 293 * 294 * @param cont Container to search component in. 295 * @param text Item text. If null, contents is not checked. 296 * @param ce Compare text exactly. 297 * @param ccs Compare text case sensitively. 298 * @param itemIndex Index of item to compare text. If -1, selected item is 299 * checked. 300 * @return JList instance or null if component was not found. 301 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 302 * 303 */ 304 public static JList<?> waitJList(Container cont, String text, boolean ce, boolean ccs, int itemIndex) { 305 return waitJList(cont, text, ce, ccs, itemIndex, 0); 306 } 307 308 @Override 309 public void setOutput(TestOut output) { 310 super.setOutput(output.createErrorOutput()); 311 this.output = output; 312 } 313 314 @Override 315 public TestOut getOutput() { 316 return output; 317 } 318 319 @Override 320 public void copyEnvironment(Operator anotherOperator) { 321 super.copyEnvironment(anotherOperator); 322 driver 323 = (MultiSelListDriver) DriverManager. 324 getDriver(DriverManager.MULTISELLIST_DRIVER_ID, 325 getClass(), 326 anotherOperator.getProperties()); 327 } 328 329 /** 330 * Gets point to click on itemIndex'th item. 331 * 332 * @param itemIndex an index of an item to click. 333 * @return a Point in component's coordinate system. 334 */ 335 public Point getClickPoint(int itemIndex) { 336 Rectangle rect = getCellBounds(itemIndex, itemIndex); 337 return (new Point(rect.x + rect.width / 2, 338 rect.y + rect.height / 2)); 339 } 340 341 /** 342 * Ask renderer for component to be displayed. 343 * 344 * @param itemIndex Item index. 345 * @param isSelected True if the specified cell was selected. 346 * @param cellHasFocus True if the specified cell has the focus. 347 * @return Component to be displayed. 348 */ 349 @SuppressWarnings(value = "unchecked") 350 public Component getRenderedComponent(int itemIndex, boolean isSelected, boolean cellHasFocus) { 351 return (((ListCellRenderer<Object>) getCellRenderer()). 352 getListCellRendererComponent((JList<Object>) getSource(), 353 getModel().getElementAt(itemIndex), 354 itemIndex, 355 isSelected, 356 cellHasFocus)); 357 } 358 359 /** 360 * Ask renderer for component to be displayed. Uses 361 * isSelectedIndex(itemIndex) to determine whether item is selected. 362 * Supposes item do not have focus. 363 * 364 * @param itemIndex Item index. 365 * @return Component to be displayed. 366 */ 367 public Component getRenderedComponent(int itemIndex) { 368 return getRenderedComponent(itemIndex, isSelectedIndex(itemIndex), false); 369 } 370 371 /** 372 * Searches for index'th item good from chooser's point of view. 373 * 374 * @param chooser Item verifying object. 375 * @param index Ordinal item index. 376 * @return Item index or -1 if search was insuccessful. 377 */ 378 public int findItemIndex(ListItemChooser chooser, int index) { 379 ListModel<?> model = getModel(); 380 int count = 0; 381 for (int i = 0; i < model.getSize(); i++) { 382 if (chooser.checkItem(this, i)) { 383 if (count == index) { 384 return i; 385 } else { 386 count++; 387 } 388 } 389 } 390 return -1; 391 } 392 393 /** 394 * Searches for an item good from chooser's point of view. 395 * 396 * @param chooser Item verifying object. 397 * @return Item index or -1 if serch was insuccessful. 398 * @see #findItemIndex(JListOperator.ListItemChooser, int) 399 * @see #findItemIndex(String, boolean, boolean) 400 */ 401 public int findItemIndex(ListItemChooser chooser) { 402 return findItemIndex(chooser, 0); 403 } 404 405 /** 406 * Searches for an item good from chooser's point of view. 407 * 408 * @param item a text pattern 409 * @param comparator a string comparision algorithm 410 * @param index Ordinal item index. 411 * @return Item index or -1 if serch was insuccessful. 412 * @see #findItemIndex(JListOperator.ListItemChooser, int) 413 * @see #findItemIndex(String, boolean, boolean) 414 */ 415 public int findItemIndex(String item, StringComparator comparator, int index) { 416 return findItemIndex(new BySubStringListItemChooser(item, comparator), index); 417 } 418 419 /** 420 * Searched for index'th item by text. 421 * 422 * @param item a text pattern 423 * @param ce Compare text exactly. 424 * @param cc Compare text case sensitively. 425 * @param index Ordinal item index. 426 * @return Item index or -1 if serch was insuccessful. 427 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 428 * @deprecated Use findItemIndex(String, int) or findItemIndex(String, 429 * StringComparator, int) 430 */ 431 @Deprecated 432 public int findItemIndex(String item, boolean ce, boolean cc, int index) { 433 return findItemIndex(item, new DefaultStringComparator(ce, cc), index); 434 } 435 436 /** 437 * Searched for index'th item by text. Uses StringComparator assigned to 438 * this object. 439 * 440 * @param item a text pattern 441 * @param index Ordinal item index. 442 * @return Item index or -1 if search was insuccessful. 443 */ 444 public int findItemIndex(String item, int index) { 445 return findItemIndex(item, getComparator(), index); 446 } 447 448 /** 449 * Searches for an item good from chooser's point of view. 450 * 451 * @param item a text pattern 452 * @param comparator a string comparision algorithm 453 * @return Item index or -1 if serch was insuccessful. 454 * @see #findItemIndex(JListOperator.ListItemChooser, int) 455 * @see #findItemIndex(String, boolean, boolean) 456 */ 457 public int findItemIndex(String item, StringComparator comparator) { 458 return findItemIndex(item, comparator, 0); 459 } 460 461 /** 462 * Searched item by text. 463 * 464 * @param item a text pattern 465 * @param ce Compare text exactly. 466 * @param cc Compare text case sensitively. 467 * @return Item index or -1 if search was insuccessful. 468 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 469 * @deprecated Use findItemIndex(String) or findItemIndex(String, 470 * StringComparator) 471 */ 472 @Deprecated 473 public int findItemIndex(String item, boolean ce, boolean cc) { 474 return findItemIndex(item, ce, cc, 0); 475 } 476 477 /** 478 * Searched for first item by text. Uses StringComparator assigned to this 479 * object. 480 * 481 * @param item a text pattern 482 * @return Item index or -1 if search was insuccessful. 483 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 484 */ 485 public int findItemIndex(String item) { 486 return findItemIndex(item, 0); 487 } 488 489 /** 490 * Searches for index'th item by rendered component. 491 * 492 * @param chooser Component verifying object. 493 * @param index Ordinal item index. 494 * @return Item index or -1 if serch was insuccessful. 495 * @see #getRenderedComponent(int, boolean, boolean) 496 */ 497 public int findItemIndex(ComponentChooser chooser, int index) { 498 return findItemIndex(new ByRenderedComponentListItemChooser(chooser), index); 499 } 500 501 /** 502 * Searches for an item by rendered component. 503 * 504 * @param chooser Component verifying object. 505 * @return Item index or -1 if serch was insuccessful. 506 * @see #getRenderedComponent(int, boolean, boolean) 507 */ 508 public int findItemIndex(ComponentChooser chooser) { 509 return findItemIndex(chooser, 0); 510 } 511 512 /** 513 * Clicks on item by item index. 514 * 515 * @param itemIndex Item index. 516 * @param clickCount count click. 517 * @return Click point or null if list does not contains itemIndex'th item. 518 * @throws NoSuchItemException 519 */ 520 public Object clickOnItem(final int itemIndex, final int clickCount) { 521 output.printLine("Click " + Integer.toString(clickCount) 522 + " times on JList\n : " + toStringSource()); 523 output.printGolden("Click " + Integer.toString(clickCount) 524 + " times on JList"); 525 checkIndex(itemIndex); 526 try { 527 scrollToItem(itemIndex); 528 } catch (TimeoutExpiredException e) { 529 output.printStackTrace(e); 530 } 531 if (((JList) getSource()).getModel().getSize() <= itemIndex) { 532 output.printErrLine("JList " + toStringSource() + " does not contain " 533 + Integer.toString(itemIndex) + "'th item"); 534 return null; 535 } 536 if (((JList) getSource()).getAutoscrolls()) { 537 ((JList) getSource()).ensureIndexIsVisible(itemIndex); 538 } 539 return (getQueueTool().invokeSmoothly(new QueueTool.QueueAction<Object>("Path selecting") { 540 @Override 541 public Object launch() { 542 Rectangle rect = getCellBounds(itemIndex, itemIndex); 543 if (rect == null) { 544 output.printErrLine("Impossible to determine click point for " 545 + Integer.toString(itemIndex) + "'th item"); 546 return null; 547 } 548 Point point = new Point((int) (rect.getX() + rect.getWidth() / 2), 549 (int) (rect.getY() + rect.getHeight() / 2)); 550 Object result = getModel().getElementAt(itemIndex); 551 clickMouse(point.x, point.y, clickCount); 552 return result; 553 } 554 })); 555 } 556 557 /** 558 * Finds item by item text, and do mouse click on it. 559 * 560 * @param item Item text. 561 * @param comparator a string comparision algorithm 562 * @param clickCount count click. 563 * @return Click point or null if list does not contains itemIndex'th item. 564 * @throws NoSuchItemException 565 */ 566 public Object clickOnItem(final String item, final StringComparator comparator, final int clickCount) { 567 scrollToItem(findItemIndex(item, comparator, 0)); 568 return (getQueueTool().invokeSmoothly(new QueueTool.QueueAction<Object>("Path selecting") { 569 @Override 570 public Object launch() { 571 int index = findItemIndex(item, comparator, 0); 572 if (index != -1) { 573 return clickOnItem(index, clickCount); 574 } else { 575 throw (new NoSuchItemException(item)); 576 } 577 } 578 })); 579 } 580 581 /** 582 * Finds item by item text, and do mouse click on it. 583 * 584 * @param item Item text. 585 * @param ce Compare exactly. 586 * @param cc Compare case sensitively. 587 * @param clickCount count click. 588 * @return Click point or null if list does not contains itemIndex'th item. 589 * @throws NoSuchItemException 590 * @deprecated Use clickOnItem(String, int) or clickOnItem(String, 591 * StringComparator, int) 592 */ 593 @Deprecated 594 public Object clickOnItem(String item, boolean ce, boolean cc, int clickCount) { 595 return clickOnItem(item, new DefaultStringComparator(ce, cc), clickCount); 596 } 597 598 /** 599 * Finds item by item text, and do mouse click on it. Uses StringComparator 600 * assigned to this object. 601 * 602 * @param item Item text. 603 * @param clickCount count click. 604 * @return Click point or null if list does not contains itemIndex'th item. 605 * @throws NoSuchItemException 606 */ 607 public Object clickOnItem(String item, int clickCount) { 608 return clickOnItem(item, getComparator(), clickCount); 609 } 610 611 /** 612 * Finds item by item text, and do simple mouse click on it. Uses 613 * StringComparator assigned to this object. 614 * 615 * @param item Item text. 616 * @param comparator a string comparision algorithm 617 * @return Click point or null if list does not contains itemIndex'th item. 618 * @throws NoSuchItemException 619 */ 620 public Object clickOnItem(String item, StringComparator comparator) { 621 return clickOnItem(item, comparator, 1); 622 } 623 624 /** 625 * Finds item by item text, and do simple mouse click on it. 626 * 627 * @param item Item text. 628 * @param ce Compare exactly. 629 * @param cc Compare case sensitively. 630 * @return Click point or null if list does not contains itemIndex'th item. 631 * @throws NoSuchItemException 632 * @deprecated Use clickOnItem(String) or clickOnItem(String, 633 * StringComparator) 634 */ 635 @Deprecated 636 public Object clickOnItem(String item, boolean ce, boolean cc) { 637 return clickOnItem(item, ce, cc, 1); 638 } 639 640 /** 641 * Finds item by item text, and do simple mouse click on it. Uses 642 * StringComparator assigned to this object. 643 * 644 * @param item Item text. 645 * @return Click point or null if list does not contains itemIndex'th item. 646 * @throws NoSuchItemException 647 */ 648 public Object clickOnItem(String item) { 649 return clickOnItem(item, 0); 650 } 651 652 /** 653 * Scrolls to an item if the list is on a JScrollPane component. 654 * 655 * @param itemIndex an item index. 656 * @see #scrollToItem(String, boolean, boolean) 657 * 658 * @throws NoSuchItemException 659 */ 660 public void scrollToItem(int itemIndex) { 661 output.printTrace("Scroll JList to " + Integer.toString(itemIndex) + "'th item\n : " 662 + toStringSource()); 663 output.printGolden("Scroll JList to " + Integer.toString(itemIndex) + "'th item"); 664 checkIndex(itemIndex); 665 makeComponentVisible(); 666 //try to find JScrollPane under. 667 JScrollPane scroll = (JScrollPane) getContainer(new JScrollPaneOperator.JScrollPaneFinder(ComponentSearcher. 668 getTrueChooser("JScrollPane"))); 669 if (scroll == null) { 670 return; 671 } 672 JScrollPaneOperator scroller = new JScrollPaneOperator(scroll); 673 scroller.copyEnvironment(this); 674 scroller.setVisualizer(new EmptyVisualizer()); 675 Rectangle rect = getCellBounds(itemIndex, itemIndex); 676 scroller.scrollToComponentRectangle(getSource(), 677 (int) rect.getX(), 678 (int) rect.getY(), 679 (int) rect.getWidth(), 680 (int) rect.getHeight()); 681 } 682 683 /** 684 * Scrolls to an item if the list is on a JScrollPane component. 685 * 686 * @param item Item text 687 * @param comparator a string comparision algorithm 688 * @see #scrollToItem(String, boolean, boolean) 689 * 690 */ 691 public void scrollToItem(String item, StringComparator comparator) { 692 scrollToItem(findItemIndex(item, comparator)); 693 } 694 695 /** 696 * Scrolls to an item if the list is on a JScrollPane component. 697 * 698 * @param item Item text 699 * @param ce Compare exactly. 700 * @param cc Compare case sensitively. 701 * @see #scrollToItem(String, boolean, boolean) 702 * 703 * @deprecated Use scrollToItem(String) or scrollToItem(String, 704 * StringComparator) 705 */ 706 @Deprecated 707 public void scrollToItem(String item, boolean ce, boolean cc) { 708 scrollToItem(findItemIndex(item, ce, cc)); 709 } 710 711 /** 712 * Selects an item by index. 713 * 714 * @param index an item index. 715 */ 716 public void selectItem(int index) { 717 checkIndex(index); 718 driver.selectItem(this, index); 719 if (getVerification()) { 720 waitItemSelection(index, true); 721 } 722 } 723 724 /** 725 * Selects an item by text. 726 * 727 * @param item an item text. 728 */ 729 public void selectItem(final String item) { 730 scrollToItem(findItemIndex(item)); 731 getQueueTool().invokeSmoothly(new QueueTool.QueueAction<Void>("Path selecting") { 732 @Override 733 public Void launch() { 734 driver.selectItem(JListOperator.this, findItemIndex(item)); 735 return null; 736 } 737 }); 738 } 739 740 /** 741 * Selects items by indices. 742 * 743 * @param indices item indices. 744 */ 745 public void selectItems(int[] indices) { 746 checkIndices(indices); 747 driver.selectItems(this, indices); 748 if (getVerification()) { 749 waitItemsSelection(indices, true); 750 } 751 } 752 753 /** 754 * Selects items by texts. 755 * 756 * @param items item texts. 757 */ 758 public void selectItem(String[] items) { 759 int[] indices = new int[items.length]; 760 for (int i = 0; i < items.length; i++) { 761 indices[i] = findItemIndex(items[i]); 762 } 763 selectItems(indices); 764 } 765 766 /** 767 * Waits for items to be selected. 768 * 769 * @param itemIndices item indices to be selected 770 * @param selected Selected (true) or unselected (false). 771 */ 772 public void waitItemsSelection(final int[] itemIndices, final boolean selected) { 773 getOutput().printLine("Wait items to be " 774 + (selected ? "" : "un") + "selected in component \n : " 775 + toStringSource()); 776 getOutput().printGolden("Wait items to be " 777 + (selected ? "" : "un") + "selected"); 778 waitState(new ComponentChooser() { 779 @Override 780 public boolean checkComponent(Component comp) { 781 int[] indices = getSelectedIndices(); 782 for (int i = 0; i < indices.length; i++) { 783 if (indices[i] != itemIndices[i]) { 784 return false; 785 } 786 } 787 return true; 788 } 789 790 @Override 791 public String getDescription() { 792 return ("Item has been " 793 + (selected ? "" : "un") + "selected"); 794 } 795 796 @Override 797 public String toString() { 798 return "JListOperator.waitItemsSelection.ComponentChooser{description = " + getDescription() + '}'; 799 } 800 }); 801 } 802 803 /** 804 * Waits for item to be selected. 805 * 806 * @param itemIndex an item needs to be selected 807 * @param selected Selected (true) or unselected (false). 808 */ 809 public void waitItemSelection(final int itemIndex, final boolean selected) { 810 waitItemsSelection(new int[]{itemIndex}, selected); 811 } 812 813 /** 814 * Waits for item. Uses getComparator() comparator. 815 * 816 * @param item an item text 817 * @param itemIndex Index of item to check or -1 to check selected item. 818 */ 819 public void waitItem(String item, int itemIndex) { 820 getOutput().printLine("Wait \"" + item + "\" at the " + Integer.toString(itemIndex) 821 + " position in component \n : " 822 + toStringSource()); 823 getOutput().printGolden("Wait \"" + item + "\" at the " + Integer.toString(itemIndex) 824 + " position"); 825 waitState(new JListByItemFinder(item, itemIndex, getComparator())); 826 } 827 828 /** 829 * Returns information about component. 830 */ 831 @Override 832 public Hashtable<String, Object> getDump() { 833 Hashtable<String, Object> result = super.getDump(); 834 String[] items = new String[((JList) getSource()).getModel().getSize()]; 835 for (int i = 0; i < ((JList) getSource()).getModel().getSize(); i++) { 836 items[i] = ((JList) getSource()).getModel().getElementAt(i).toString(); 837 } 838 int[] selectedIndices = ((JList) getSource()).getSelectedIndices(); 839 String[] selectedItems = new String[selectedIndices.length]; 840 for (int i = 0; i < selectedIndices.length; i++) { 841 selectedItems[i] = items[selectedIndices[i]]; 842 } 843 addToDump(result, ITEM_PREFIX_DPROP, items); 844 addToDump(result, SELECTED_ITEM_PREFIX_DPROP, selectedItems); 845 return result; 846 } 847 848 //////////////////////////////////////////////////////// 849 //Mapping // 850 /** 851 * Maps {@code JList.addListSelectionListener(ListSelectionListener)} 852 * through queue 853 */ 854 public void addListSelectionListener(final ListSelectionListener listSelectionListener) { 855 runMapping(new MapVoidAction("addListSelectionListener") { 856 @Override 857 public void map() { 858 ((JList) getSource()).addListSelectionListener(listSelectionListener); 859 } 860 }); 861 } 862 863 /** 864 * Maps {@code JList.addSelectionInterval(int, int)} through queue 865 */ 866 public void addSelectionInterval(final int i, final int i1) { 867 runMapping(new MapVoidAction("addSelectionInterval") { 868 @Override 869 public void map() { 870 ((JList) getSource()).addSelectionInterval(i, i1); 871 } 872 }); 873 } 874 875 /** 876 * Maps {@code JList.clearSelection()} through queue 877 */ 878 public void clearSelection() { 879 runMapping(new MapVoidAction("clearSelection") { 880 @Override 881 public void map() { 882 ((JList) getSource()).clearSelection(); 883 } 884 }); 885 } 886 887 /** 888 * Maps {@code JList.ensureIndexIsVisible(int)} through queue 889 */ 890 public void ensureIndexIsVisible(final int i) { 891 runMapping(new MapVoidAction("ensureIndexIsVisible") { 892 @Override 893 public void map() { 894 ((JList) getSource()).ensureIndexIsVisible(i); 895 } 896 }); 897 } 898 899 /** 900 * Maps {@code JList.getAnchorSelectionIndex()} through queue 901 */ 902 public int getAnchorSelectionIndex() { 903 return (runMapping(new MapIntegerAction("getAnchorSelectionIndex") { 904 @Override 905 public int map() { 906 return ((JList) getSource()).getAnchorSelectionIndex(); 907 } 908 })); 909 } 910 911 /** 912 * Maps {@code JList.getCellBounds(int, int)} through queue 913 */ 914 public Rectangle getCellBounds(final int i, final int i1) { 915 return (runMapping(new MapAction<Rectangle>("getCellBounds") { 916 @Override 917 public Rectangle map() { 918 return ((JList) getSource()).getCellBounds(i, i1); 919 } 920 })); 921 } 922 923 /** 924 * Maps {@code JList.getCellRenderer()} through queue 925 */ 926 public ListCellRenderer<?> getCellRenderer() { 927 return (runMapping(new MapAction<ListCellRenderer<?>>("getCellRenderer") { 928 @Override 929 public ListCellRenderer<?> map() { 930 return ((JList) getSource()).getCellRenderer(); 931 } 932 })); 933 } 934 935 /** 936 * Maps {@code JList.getFirstVisibleIndex()} through queue 937 */ 938 public int getFirstVisibleIndex() { 939 return (runMapping(new MapIntegerAction("getFirstVisibleIndex") { 940 @Override 941 public int map() { 942 return ((JList) getSource()).getFirstVisibleIndex(); 943 } 944 })); 945 } 946 947 /** 948 * Maps {@code JList.getFixedCellHeight()} through queue 949 */ 950 public int getFixedCellHeight() { 951 return (runMapping(new MapIntegerAction("getFixedCellHeight") { 952 @Override 953 public int map() { 954 return ((JList) getSource()).getFixedCellHeight(); 955 } 956 })); 957 } 958 959 /** 960 * Maps {@code JList.getFixedCellWidth()} through queue 961 */ 962 public int getFixedCellWidth() { 963 return (runMapping(new MapIntegerAction("getFixedCellWidth") { 964 @Override 965 public int map() { 966 return ((JList) getSource()).getFixedCellWidth(); 967 } 968 })); 969 } 970 971 /** 972 * Maps {@code JList.getLastVisibleIndex()} through queue 973 */ 974 public int getLastVisibleIndex() { 975 return (runMapping(new MapIntegerAction("getLastVisibleIndex") { 976 @Override 977 public int map() { 978 return ((JList) getSource()).getLastVisibleIndex(); 979 } 980 })); 981 } 982 983 /** 984 * Maps {@code JList.getLeadSelectionIndex()} through queue 985 */ 986 public int getLeadSelectionIndex() { 987 return (runMapping(new MapIntegerAction("getLeadSelectionIndex") { 988 @Override 989 public int map() { 990 return ((JList) getSource()).getLeadSelectionIndex(); 991 } 992 })); 993 } 994 995 /** 996 * Maps {@code JList.getMaxSelectionIndex()} through queue 997 */ 998 public int getMaxSelectionIndex() { 999 return (runMapping(new MapIntegerAction("getMaxSelectionIndex") { 1000 @Override 1001 public int map() { 1002 return ((JList) getSource()).getMaxSelectionIndex(); 1003 } 1004 })); 1005 } 1006 1007 /** 1008 * Maps {@code JList.getMinSelectionIndex()} through queue 1009 */ 1010 public int getMinSelectionIndex() { 1011 return (runMapping(new MapIntegerAction("getMinSelectionIndex") { 1012 @Override 1013 public int map() { 1014 return ((JList) getSource()).getMinSelectionIndex(); 1015 } 1016 })); 1017 } 1018 1019 /** 1020 * Maps {@code JList.getModel()} through queue 1021 */ 1022 public ListModel<?> getModel() { 1023 return (runMapping(new MapAction<ListModel<?>>("getModel") { 1024 @Override 1025 public ListModel<?> map() { 1026 return ((JList) getSource()).getModel(); 1027 } 1028 })); 1029 } 1030 1031 /** 1032 * Maps {@code JList.getPreferredScrollableViewportSize()} through queue 1033 */ 1034 public Dimension getPreferredScrollableViewportSize() { 1035 return (runMapping(new MapAction<Dimension>("getPreferredScrollableViewportSize") { 1036 @Override 1037 public Dimension map() { 1038 return ((JList) getSource()).getPreferredScrollableViewportSize(); 1039 } 1040 })); 1041 } 1042 1043 /** 1044 * Maps {@code JList.getPrototypeCellValue()} through queue 1045 */ 1046 public Object getPrototypeCellValue() { 1047 return (runMapping(new MapAction<Object>("getPrototypeCellValue") { 1048 @Override 1049 public Object map() { 1050 return ((JList) getSource()).getPrototypeCellValue(); 1051 } 1052 })); 1053 } 1054 1055 /** 1056 * Maps {@code JList.getScrollableBlockIncrement(Rectangle, int, int)} 1057 * through queue 1058 */ 1059 public int getScrollableBlockIncrement(final Rectangle rectangle, final int i, final int i1) { 1060 return (runMapping(new MapIntegerAction("getScrollableBlockIncrement") { 1061 @Override 1062 public int map() { 1063 return ((JList) getSource()).getScrollableBlockIncrement(rectangle, i, i1); 1064 } 1065 })); 1066 } 1067 1068 /** 1069 * Maps {@code JList.getScrollableTracksViewportHeight()} through queue 1070 */ 1071 public boolean getScrollableTracksViewportHeight() { 1072 return (runMapping(new MapBooleanAction("getScrollableTracksViewportHeight") { 1073 @Override 1074 public boolean map() { 1075 return ((JList) getSource()).getScrollableTracksViewportHeight(); 1076 } 1077 })); 1078 } 1079 1080 /** 1081 * Maps {@code JList.getScrollableTracksViewportWidth()} through queue 1082 */ 1083 public boolean getScrollableTracksViewportWidth() { 1084 return (runMapping(new MapBooleanAction("getScrollableTracksViewportWidth") { 1085 @Override 1086 public boolean map() { 1087 return ((JList) getSource()).getScrollableTracksViewportWidth(); 1088 } 1089 })); 1090 } 1091 1092 /** 1093 * Maps {@code JList.getScrollableUnitIncrement(Rectangle, int, int)} 1094 * through queue 1095 */ 1096 public int getScrollableUnitIncrement(final Rectangle rectangle, final int i, final int i1) { 1097 return (runMapping(new MapIntegerAction("getScrollableUnitIncrement") { 1098 @Override 1099 public int map() { 1100 return ((JList) getSource()).getScrollableUnitIncrement(rectangle, i, i1); 1101 } 1102 })); 1103 } 1104 1105 /** 1106 * Maps {@code JList.getSelectedIndex()} through queue 1107 */ 1108 public int getSelectedIndex() { 1109 return (runMapping(new MapIntegerAction("getSelectedIndex") { 1110 @Override 1111 public int map() { 1112 return ((JList) getSource()).getSelectedIndex(); 1113 } 1114 })); 1115 } 1116 1117 /** 1118 * Maps {@code JList.getSelectedIndices()} through queue 1119 */ 1120 public int[] getSelectedIndices() { 1121 return ((int[]) runMapping(new MapAction<Object>("getSelectedIndices") { 1122 @Override 1123 public Object map() { 1124 return ((JList) getSource()).getSelectedIndices(); 1125 } 1126 })); 1127 } 1128 1129 /** 1130 * Maps {@code JList.getSelectedValue()} through queue 1131 */ 1132 public Object getSelectedValue() { 1133 return (runMapping(new MapAction<Object>("getSelectedValue") { 1134 @Override 1135 public Object map() { 1136 return ((JList) getSource()).getSelectedValue(); 1137 } 1138 })); 1139 } 1140 1141 /** 1142 * Maps {@code JList.getSelectedValues()} through queue 1143 */ 1144 @Deprecated 1145 public Object[] getSelectedValues() { 1146 return ((Object[]) runMapping(new MapAction<Object>("getSelectedValues") { 1147 @Override 1148 public Object map() { 1149 return ((JList) getSource()).getSelectedValues(); 1150 } 1151 })); 1152 } 1153 1154 /** 1155 * Maps {@code JList.getSelectionBackground()} through queue 1156 */ 1157 public Color getSelectionBackground() { 1158 return (runMapping(new MapAction<Color>("getSelectionBackground") { 1159 @Override 1160 public Color map() { 1161 return ((JList) getSource()).getSelectionBackground(); 1162 } 1163 })); 1164 } 1165 1166 /** 1167 * Maps {@code JList.getSelectionForeground()} through queue 1168 */ 1169 public Color getSelectionForeground() { 1170 return (runMapping(new MapAction<Color>("getSelectionForeground") { 1171 @Override 1172 public Color map() { 1173 return ((JList) getSource()).getSelectionForeground(); 1174 } 1175 })); 1176 } 1177 1178 /** 1179 * Maps {@code JList.getSelectionMode()} through queue 1180 */ 1181 public int getSelectionMode() { 1182 return (runMapping(new MapIntegerAction("getSelectionMode") { 1183 @Override 1184 public int map() { 1185 return ((JList) getSource()).getSelectionMode(); 1186 } 1187 })); 1188 } 1189 1190 /** 1191 * Maps {@code JList.getSelectionModel()} through queue 1192 */ 1193 public ListSelectionModel getSelectionModel() { 1194 return (runMapping(new MapAction<ListSelectionModel>("getSelectionModel") { 1195 @Override 1196 public ListSelectionModel map() { 1197 return ((JList) getSource()).getSelectionModel(); 1198 } 1199 })); 1200 } 1201 1202 /** 1203 * Maps {@code JList.getUI()} through queue 1204 */ 1205 public ListUI getUI() { 1206 return (runMapping(new MapAction<ListUI>("getUI") { 1207 @Override 1208 public ListUI map() { 1209 return ((JList) getSource()).getUI(); 1210 } 1211 })); 1212 } 1213 1214 /** 1215 * Maps {@code JList.getValueIsAdjusting()} through queue 1216 */ 1217 public boolean getValueIsAdjusting() { 1218 return (runMapping(new MapBooleanAction("getValueIsAdjusting") { 1219 @Override 1220 public boolean map() { 1221 return ((JList) getSource()).getValueIsAdjusting(); 1222 } 1223 })); 1224 } 1225 1226 /** 1227 * Maps {@code JList.getVisibleRowCount()} through queue 1228 */ 1229 public int getVisibleRowCount() { 1230 return (runMapping(new MapIntegerAction("getVisibleRowCount") { 1231 @Override 1232 public int map() { 1233 return ((JList) getSource()).getVisibleRowCount(); 1234 } 1235 })); 1236 } 1237 1238 /** 1239 * Maps {@code JList.indexToLocation(int)} through queue 1240 */ 1241 public Point indexToLocation(final int i) { 1242 return (runMapping(new MapAction<Point>("indexToLocation") { 1243 @Override 1244 public Point map() { 1245 return ((JList) getSource()).indexToLocation(i); 1246 } 1247 })); 1248 } 1249 1250 /** 1251 * Maps {@code JList.isSelectedIndex(int)} through queue 1252 */ 1253 public boolean isSelectedIndex(final int i) { 1254 return (runMapping(new MapBooleanAction("isSelectedIndex") { 1255 @Override 1256 public boolean map() { 1257 return ((JList) getSource()).isSelectedIndex(i); 1258 } 1259 })); 1260 } 1261 1262 /** 1263 * Maps {@code JList.isSelectionEmpty()} through queue 1264 */ 1265 public boolean isSelectionEmpty() { 1266 return (runMapping(new MapBooleanAction("isSelectionEmpty") { 1267 @Override 1268 public boolean map() { 1269 return ((JList) getSource()).isSelectionEmpty(); 1270 } 1271 })); 1272 } 1273 1274 /** 1275 * Maps {@code JList.locationToIndex(Point)} through queue 1276 */ 1277 public int locationToIndex(final Point point) { 1278 return (runMapping(new MapIntegerAction("locationToIndex") { 1279 @Override 1280 public int map() { 1281 return ((JList) getSource()).locationToIndex(point); 1282 } 1283 })); 1284 } 1285 1286 /** 1287 * Maps 1288 * {@code JList.removeListSelectionListener(ListSelectionListener)} 1289 * through queue 1290 */ 1291 public void removeListSelectionListener(final ListSelectionListener listSelectionListener) { 1292 runMapping(new MapVoidAction("removeListSelectionListener") { 1293 @Override 1294 public void map() { 1295 ((JList) getSource()).removeListSelectionListener(listSelectionListener); 1296 } 1297 }); 1298 } 1299 1300 /** 1301 * Maps {@code JList.removeSelectionInterval(int, int)} through queue 1302 */ 1303 public void removeSelectionInterval(final int i, final int i1) { 1304 runMapping(new MapVoidAction("removeSelectionInterval") { 1305 @Override 1306 public void map() { 1307 ((JList) getSource()).removeSelectionInterval(i, i1); 1308 } 1309 }); 1310 } 1311 1312 /** 1313 * Maps {@code JList.setCellRenderer(ListCellRenderer)} through queue 1314 */ 1315 @SuppressWarnings(value = "unchecked") 1316 public void setCellRenderer(final ListCellRenderer<?> listCellRenderer) { 1317 runMapping(new MapVoidAction("setCellRenderer") { 1318 @Override 1319 public void map() { 1320 ((JList) getSource()).setCellRenderer(listCellRenderer); 1321 } 1322 }); 1323 } 1324 1325 /** 1326 * Maps {@code JList.setFixedCellHeight(int)} through queue 1327 */ 1328 public void setFixedCellHeight(final int i) { 1329 runMapping(new MapVoidAction("setFixedCellHeight") { 1330 @Override 1331 public void map() { 1332 ((JList) getSource()).setFixedCellHeight(i); 1333 } 1334 }); 1335 } 1336 1337 /** 1338 * Maps {@code JList.setFixedCellWidth(int)} through queue 1339 */ 1340 public void setFixedCellWidth(final int i) { 1341 runMapping(new MapVoidAction("setFixedCellWidth") { 1342 @Override 1343 public void map() { 1344 ((JList) getSource()).setFixedCellWidth(i); 1345 } 1346 }); 1347 } 1348 1349 /** 1350 * Maps {@code JList.setListData(Vector)} through queue 1351 */ 1352 @SuppressWarnings(value = "unchecked") 1353 public void setListData(final Vector<?> vector) { 1354 runMapping(new MapVoidAction("setListData") { 1355 @Override 1356 public void map() { 1357 ((JList) getSource()).setListData(vector); 1358 } 1359 }); 1360 } 1361 1362 /** 1363 * Maps {@code JList.setListData(Object[])} through queue 1364 */ 1365 @SuppressWarnings(value = "unchecked") 1366 public void setListData(final Object[] object) { 1367 runMapping(new MapVoidAction("setListData") { 1368 @Override 1369 public void map() { 1370 ((JList) getSource()).setListData(object); 1371 } 1372 }); 1373 } 1374 1375 /** 1376 * Maps {@code JList.setModel(ListModel)} through queue 1377 */ 1378 @SuppressWarnings(value = "unchecked") 1379 public void setModel(final ListModel<?> listModel) { 1380 runMapping(new MapVoidAction("setModel") { 1381 @Override 1382 public void map() { 1383 ((JList) getSource()).setModel(listModel); 1384 } 1385 }); 1386 } 1387 1388 /** 1389 * Maps {@code JList.setPrototypeCellValue(Object)} through queue 1390 */ 1391 @SuppressWarnings(value = "unchecked") 1392 public void setPrototypeCellValue(final Object object) { 1393 runMapping(new MapVoidAction("setPrototypeCellValue") { 1394 @Override 1395 public void map() { 1396 ((JList) getSource()).setPrototypeCellValue(object); 1397 } 1398 }); 1399 } 1400 1401 /** 1402 * Maps {@code JList.setSelectedIndex(int)} through queue 1403 */ 1404 public void setSelectedIndex(final int i) { 1405 runMapping(new MapVoidAction("setSelectedIndex") { 1406 @Override 1407 public void map() { 1408 ((JList) getSource()).setSelectedIndex(i); 1409 } 1410 }); 1411 } 1412 1413 /** 1414 * Maps {@code JList.setSelectedIndices(int[])} through queue 1415 */ 1416 public void setSelectedIndices(final int[] i) { 1417 runMapping(new MapVoidAction("setSelectedIndices") { 1418 @Override 1419 public void map() { 1420 ((JList) getSource()).setSelectedIndices(i); 1421 } 1422 }); 1423 } 1424 1425 /** 1426 * Maps {@code JList.setSelectedValue(Object, boolean)} through queue 1427 */ 1428 public void setSelectedValue(final Object object, final boolean b) { 1429 runMapping(new MapVoidAction("setSelectedValue") { 1430 @Override 1431 public void map() { 1432 ((JList) getSource()).setSelectedValue(object, b); 1433 } 1434 }); 1435 } 1436 1437 /** 1438 * Maps {@code JList.setSelectionBackground(Color)} through queue 1439 */ 1440 public void setSelectionBackground(final Color color) { 1441 runMapping(new MapVoidAction("setSelectionBackground") { 1442 @Override 1443 public void map() { 1444 ((JList) getSource()).setSelectionBackground(color); 1445 } 1446 }); 1447 } 1448 1449 /** 1450 * Maps {@code JList.setSelectionForeground(Color)} through queue 1451 */ 1452 public void setSelectionForeground(final Color color) { 1453 runMapping(new MapVoidAction("setSelectionForeground") { 1454 @Override 1455 public void map() { 1456 ((JList) getSource()).setSelectionForeground(color); 1457 } 1458 }); 1459 } 1460 1461 /** 1462 * Maps {@code JList.setSelectionInterval(int, int)} through queue 1463 */ 1464 public void setSelectionInterval(final int i, final int i1) { 1465 runMapping(new MapVoidAction("setSelectionInterval") { 1466 @Override 1467 public void map() { 1468 ((JList) getSource()).setSelectionInterval(i, i1); 1469 } 1470 }); 1471 } 1472 1473 /** 1474 * Maps {@code JList.setSelectionMode(int)} through queue 1475 */ 1476 public void setSelectionMode(final int i) { 1477 runMapping(new MapVoidAction("setSelectionMode") { 1478 @Override 1479 public void map() { 1480 ((JList) getSource()).setSelectionMode(i); 1481 } 1482 }); 1483 } 1484 1485 /** 1486 * Maps {@code JList.setSelectionModel(ListSelectionModel)} through queue 1487 */ 1488 public void setSelectionModel(final ListSelectionModel listSelectionModel) { 1489 runMapping(new MapVoidAction("setSelectionModel") { 1490 @Override 1491 public void map() { 1492 ((JList) getSource()).setSelectionModel(listSelectionModel); 1493 } 1494 }); 1495 } 1496 1497 /** 1498 * Maps {@code JList.setUI(ListUI)} through queue 1499 */ 1500 public void setUI(final ListUI listUI) { 1501 runMapping(new MapVoidAction("setUI") { 1502 @Override 1503 public void map() { 1504 ((JList) getSource()).setUI(listUI); 1505 } 1506 }); 1507 } 1508 1509 /** 1510 * Maps {@code JList.setValueIsAdjusting(boolean)} through queue 1511 */ 1512 public void setValueIsAdjusting(final boolean b) { 1513 runMapping(new MapVoidAction("setValueIsAdjusting") { 1514 @Override 1515 public void map() { 1516 ((JList) getSource()).setValueIsAdjusting(b); 1517 } 1518 }); 1519 } 1520 1521 /** 1522 * Maps {@code JList.setVisibleRowCount(int)} through queue 1523 */ 1524 public void setVisibleRowCount(final int i) { 1525 runMapping(new MapVoidAction("setVisibleRowCount") { 1526 @Override 1527 public void map() { 1528 ((JList) getSource()).setVisibleRowCount(i); 1529 } 1530 }); 1531 } 1532 1533 //End of mapping // 1534 //////////////////////////////////////////////////////// 1535 private void checkIndex(int index) { 1536 if (index < 0 1537 || index >= getModel().getSize()) { 1538 throw (new NoSuchItemException(index)); 1539 } 1540 } 1541 1542 private void checkIndices(int[] indices) { 1543 for (int indice : indices) { 1544 checkIndex(indice); 1545 } 1546 } 1547 1548 /** 1549 * Iterface to choose list item. 1550 */ 1551 public interface ListItemChooser { 1552 1553 /** 1554 * Should be true if item is good. 1555 * 1556 * @param oper Operator used to search item. 1557 * @param index Index of an item be checked. 1558 * @return true if the item fits the criteria 1559 */ 1560 public boolean checkItem(JListOperator oper, int index); 1561 1562 /** 1563 * Item description. 1564 * 1565 * @return a description. 1566 */ 1567 public String getDescription(); 1568 } 1569 1570 /** 1571 * Can be thrown during item selecting if list does not have item requested. 1572 */ 1573 public class NoSuchItemException extends JemmyInputException { 1574 1575 private static final long serialVersionUID = 42L; 1576 1577 /** 1578 * Constructor. 1579 * 1580 * @param item an item's text 1581 */ 1582 public NoSuchItemException(String item) { 1583 super("No such item as \"" + item + "\"", getSource()); 1584 } 1585 1586 /** 1587 * Constructor. 1588 * 1589 * @param index an item's index 1590 */ 1591 public NoSuchItemException(int index) { 1592 super("List does not contain " + index + "'th item", getSource()); 1593 } 1594 } 1595 1596 private static class BySubStringListItemChooser implements ListItemChooser { 1597 1598 String subString; 1599 StringComparator comparator; 1600 1601 public BySubStringListItemChooser(String subString, StringComparator comparator) { 1602 this.subString = subString; 1603 this.comparator = comparator; 1604 } 1605 1606 @Override 1607 public boolean checkItem(JListOperator oper, int index) { 1608 return (comparator.equals(oper.getModel().getElementAt(index).toString(), 1609 subString)); 1610 } 1611 1612 @Override 1613 public String getDescription() { 1614 return "Item containing \"" + subString + "\" string"; 1615 } 1616 1617 @Override 1618 public String toString() { 1619 return "BySubStringListItemChooser{" + "subString=" + subString + ", comparator=" + comparator + '}'; 1620 } 1621 } 1622 1623 private static class ByRenderedComponentListItemChooser implements ListItemChooser { 1624 1625 ComponentChooser chooser; 1626 1627 public ByRenderedComponentListItemChooser(ComponentChooser chooser) { 1628 this.chooser = chooser; 1629 } 1630 1631 @Override 1632 public boolean checkItem(JListOperator oper, int index) { 1633 return chooser.checkComponent(oper.getRenderedComponent(index)); 1634 } 1635 1636 @Override 1637 public String getDescription() { 1638 return chooser.getDescription(); 1639 } 1640 1641 @Override 1642 public String toString() { 1643 return "ByRenderedComponentListItemChooser{" + "chooser=" + chooser + '}'; 1644 } 1645 } 1646 1647 /** 1648 * Allows to find component by an item. 1649 */ 1650 public static class JListByItemFinder implements ComponentChooser { 1651 1652 String label; 1653 int itemIndex; 1654 StringComparator comparator; 1655 1656 /** 1657 * Constructs JListByItemFinder. 1658 * 1659 * @param lb a text pattern 1660 * @param ii item index to check. If equal to -1, selected item is 1661 * checked. 1662 * @param comparator specifies string comparision algorithm. 1663 */ 1664 public JListByItemFinder(String lb, int ii, StringComparator comparator) { 1665 label = lb; 1666 itemIndex = ii; 1667 this.comparator = comparator; 1668 } 1669 1670 /** 1671 * Constructs JListByItemFinder. 1672 * 1673 * @param lb a text pattern 1674 * @param ii item index to check. If equal to -1, selected item is 1675 * checked. 1676 */ 1677 public JListByItemFinder(String lb, int ii) { 1678 this(lb, ii, Operator.getDefaultStringComparator()); 1679 } 1680 1681 @Override 1682 public boolean checkComponent(Component comp) { 1683 if (comp instanceof JList) { 1684 if (label == null) { 1685 return true; 1686 } 1687 if (((JList) comp).getModel().getSize() > itemIndex) { 1688 int ii = itemIndex; 1689 if (ii == -1) { 1690 ii = ((JList) comp).getSelectedIndex(); 1691 if (ii == -1) { 1692 return false; 1693 } 1694 } 1695 return (comparator.equals(((JList) comp).getModel().getElementAt(ii).toString(), 1696 label)); 1697 } 1698 } 1699 return false; 1700 } 1701 1702 @Override 1703 public String getDescription() { 1704 return ("JList with text \"" + label + "\" in " 1705 + itemIndex + "'th item"); 1706 } 1707 1708 @Override 1709 public String toString() { 1710 return "JListByItemFinder{" + "label=" + label + ", itemIndex=" + itemIndex + ", comparator=" + comparator + '}'; 1711 } 1712 } 1713 1714 /** 1715 * Checks component type. 1716 */ 1717 public static class JListFinder extends Finder { 1718 1719 /** 1720 * Constructs JListFinder. 1721 * 1722 * @param sf other searching criteria. 1723 */ 1724 public JListFinder(ComponentChooser sf) { 1725 super(JList.class, sf); 1726 } 1727 1728 /** 1729 * Constructs JListFinder. 1730 */ 1731 public JListFinder() { 1732 super(JList.class); 1733 } 1734 } 1735} 1736