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.Component; 26import java.awt.Container; 27import java.util.Hashtable; 28 29import javax.swing.Icon; 30import javax.swing.JLabel; 31import javax.swing.plaf.LabelUI; 32 33import org.netbeans.jemmy.ComponentChooser; 34 35/** 36 * <BR><BR>Timeouts used: <BR> 37 * ComponentOperator.WaitComponentTimeout - time to wait component displayed 38 * <BR> 39 * ComponentOperator.WaitStateTimeout - time to wait for text <BR>. 40 * 41 * @see org.netbeans.jemmy.Timeouts 42 * @author Alexandre Iline (alexandre.iline@oracle.com) 43 * 44 */ 45public class JLabelOperator extends JComponentOperator { 46 47 /** 48 * Identifier for a "text" property. 49 * 50 * @see #getDump 51 */ 52 public static final String TEXT_DPROP = "Text"; 53 54 /** 55 * Constructor. 56 * 57 * @param b a component 58 */ 59 public JLabelOperator(JLabel b) { 60 super(b); 61 } 62 63 /** 64 * Constructs a JLabelOperator object. 65 * 66 * @param cont a container 67 * @param chooser a component chooser specifying searching criteria. 68 * @param index an index between appropriate ones. 69 */ 70 public JLabelOperator(ContainerOperator<?> cont, ComponentChooser chooser, int index) { 71 this((JLabel) cont. 72 waitSubComponent(new JLabelFinder(chooser), 73 index)); 74 copyEnvironment(cont); 75 } 76 77 /** 78 * Constructs a JLabelOperator object. 79 * 80 * @param cont a container 81 * @param chooser a component chooser specifying searching criteria. 82 */ 83 public JLabelOperator(ContainerOperator<?> cont, ComponentChooser chooser) { 84 this(cont, chooser, 0); 85 } 86 87 /** 88 * Constructor. Waits component in container first. Uses cont's timeout and 89 * output for waiting and to init operator. 90 * 91 * @param cont a container 92 * @param text Button text. 93 * @param index Ordinal component index. 94 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 95 * 96 */ 97 public JLabelOperator(ContainerOperator<?> cont, String text, int index) { 98 this((JLabel) waitComponent(cont, 99 new JLabelByLabelFinder(text, 100 cont.getComparator()), 101 index)); 102 copyEnvironment(cont); 103 } 104 105 /** 106 * Constructor. Waits component in container first. Uses cont's timeout and 107 * output for waiting and to init operator. 108 * 109 * @param cont a container 110 * @param text Button text. 111 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 112 * 113 */ 114 public JLabelOperator(ContainerOperator<?> cont, String text) { 115 this(cont, text, 0); 116 } 117 118 /** 119 * Constructor. Waits component in container first. Uses cont's timeout and 120 * output for waiting and to init operator. 121 * 122 * @param cont a container 123 * @param index Ordinal component index. 124 * 125 */ 126 public JLabelOperator(ContainerOperator<?> cont, int index) { 127 this((JLabel) waitComponent(cont, 128 new JLabelFinder(), 129 index)); 130 copyEnvironment(cont); 131 } 132 133 /** 134 * Constructor. Waits component in container first. Uses cont's timeout and 135 * output for waiting and to init operator. 136 * 137 * @param cont a container 138 * 139 */ 140 public JLabelOperator(ContainerOperator<?> cont) { 141 this(cont, 0); 142 } 143 144 /** 145 * Searches JLabel in container. 146 * 147 * @param cont Container to search component in. 148 * @param chooser a component chooser specifying searching criteria. 149 * @param index Ordinal component index. 150 * @return JLabel instance or null if component was not found. 151 */ 152 public static JLabel findJLabel(Container cont, ComponentChooser chooser, int index) { 153 return (JLabel) findComponent(cont, new JLabelFinder(chooser), index); 154 } 155 156 /** 157 * Searches JLabel in container. 158 * 159 * @param cont Container to search component in. 160 * @param chooser a component chooser specifying searching criteria. 161 * @return JLabel instance or null if component was not found. 162 */ 163 public static JLabel findJLabel(Container cont, ComponentChooser chooser) { 164 return findJLabel(cont, chooser, 0); 165 } 166 167 /** 168 * Searches JLabel by text. 169 * 170 * @param cont Container to search component in. 171 * @param text Component text. 172 * @param ce Compare text exactly. 173 * @param ccs Compare text case sensitively. 174 * @param index Ordinal component index. 175 * @return JLabel instance or null if component was not found. 176 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 177 */ 178 public static JLabel findJLabel(Container cont, String text, boolean ce, boolean ccs, int index) { 179 return findJLabel(cont, new JLabelByLabelFinder(text, new DefaultStringComparator(ce, ccs)), index); 180 } 181 182 /** 183 * Searches JLabel by text. 184 * 185 * @param cont Container to search component in. 186 * @param text Component text. 187 * @param ce Compare text exactly. 188 * @param ccs Compare text case sensitively. 189 * @return JLabel instance or null if component was not found. 190 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 191 */ 192 public static JLabel findJLabel(Container cont, String text, boolean ce, boolean ccs) { 193 return findJLabel(cont, text, ce, ccs, 0); 194 } 195 196 /** 197 * Waits JLabel in container. 198 * 199 * @param cont Container to search component in. 200 * @param chooser a component chooser specifying searching criteria. 201 * @param index Ordinal component index. 202 * @return JLabel instance. 203 * 204 */ 205 public static JLabel waitJLabel(final Container cont, final ComponentChooser chooser, final int index) { 206 return (JLabel) waitComponent(cont, new JLabelFinder(chooser), index); 207 } 208 209 /** 210 * Waits JLabel in container. 211 * 212 * @param cont Container to search component in. 213 * @param chooser a component chooser specifying searching criteria. 214 * @return JLabel instance. 215 * 216 */ 217 public static JLabel waitJLabel(Container cont, ComponentChooser chooser) { 218 return waitJLabel(cont, chooser, 0); 219 } 220 221 /** 222 * Waits JLabel by text. 223 * 224 * @param cont Container to search component in. 225 * @param text Component text. 226 * @param ce Compare text exactly. 227 * @param ccs Compare text case sensitively. 228 * @param index Ordinal component index. 229 * @return JLabel instance. 230 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 231 * 232 */ 233 public static JLabel waitJLabel(Container cont, String text, boolean ce, boolean ccs, int index) { 234 return waitJLabel(cont, new JLabelByLabelFinder(text, new DefaultStringComparator(ce, ccs)), index); 235 } 236 237 /** 238 * Waits JLabel by text. 239 * 240 * @param cont Container to search component in. 241 * @param text Component text. 242 * @param ce Compare text exactly. 243 * @param ccs Compare text case sensitively. 244 * @return JLabel instance. 245 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 246 * 247 */ 248 public static JLabel waitJLabel(Container cont, String text, boolean ce, boolean ccs) { 249 return waitJLabel(cont, text, ce, ccs, 0); 250 } 251 252 /** 253 * Waits for text. Uses getComparator() comparator. 254 * 255 * @param text Text to wait for. 256 */ 257 public void waitText(String text) { 258 getOutput().printLine("Wait \"" + text + "\" text in component \n : " 259 + toStringSource()); 260 getOutput().printGolden("Wait \"" + text + "\" text"); 261 waitState(new JLabelByLabelFinder(text, getComparator())); 262 } 263 264 /** 265 * Returns information about component. 266 */ 267 @Override 268 public Hashtable<String, Object> getDump() { 269 Hashtable<String, Object> result = super.getDump(); 270 if (((JLabel) getSource()).getText() != null) { 271 result.put(TEXT_DPROP, ((JLabel) getSource()).getText()); 272 } else { 273 result.put(TEXT_DPROP, "null"); 274 } 275 return result; 276 } 277 278 //////////////////////////////////////////////////////// 279 //Mapping // 280 /** 281 * Maps {@code JLabel.getDisabledIcon()} through queue 282 */ 283 public Icon getDisabledIcon() { 284 return (runMapping(new MapAction<Icon>("getDisabledIcon") { 285 @Override 286 public Icon map() { 287 return ((JLabel) getSource()).getDisabledIcon(); 288 } 289 })); 290 } 291 292 /** 293 * Maps {@code JLabel.getDisplayedMnemonic()} through queue 294 */ 295 public int getDisplayedMnemonic() { 296 return (runMapping(new MapIntegerAction("getDisplayedMnemonic") { 297 @Override 298 public int map() { 299 return ((JLabel) getSource()).getDisplayedMnemonic(); 300 } 301 })); 302 } 303 304 /** 305 * Maps {@code JLabel.getHorizontalAlignment()} through queue 306 */ 307 public int getHorizontalAlignment() { 308 return (runMapping(new MapIntegerAction("getHorizontalAlignment") { 309 @Override 310 public int map() { 311 return ((JLabel) getSource()).getHorizontalAlignment(); 312 } 313 })); 314 } 315 316 /** 317 * Maps {@code JLabel.getHorizontalTextPosition()} through queue 318 */ 319 public int getHorizontalTextPosition() { 320 return (runMapping(new MapIntegerAction("getHorizontalTextPosition") { 321 @Override 322 public int map() { 323 return ((JLabel) getSource()).getHorizontalTextPosition(); 324 } 325 })); 326 } 327 328 /** 329 * Maps {@code JLabel.getIcon()} through queue 330 */ 331 public Icon getIcon() { 332 return (runMapping(new MapAction<Icon>("getIcon") { 333 @Override 334 public Icon map() { 335 return ((JLabel) getSource()).getIcon(); 336 } 337 })); 338 } 339 340 /** 341 * Maps {@code JLabel.getIconTextGap()} through queue 342 */ 343 public int getIconTextGap() { 344 return (runMapping(new MapIntegerAction("getIconTextGap") { 345 @Override 346 public int map() { 347 return ((JLabel) getSource()).getIconTextGap(); 348 } 349 })); 350 } 351 352 /** 353 * Maps {@code JLabel.getLabelFor()} through queue 354 */ 355 public Component getLabelFor() { 356 return (runMapping(new MapAction<Component>("getLabelFor") { 357 @Override 358 public Component map() { 359 return ((JLabel) getSource()).getLabelFor(); 360 } 361 })); 362 } 363 364 /** 365 * Maps {@code JLabel.getText()} through queue 366 */ 367 public String getText() { 368 return (runMapping(new MapAction<String>("getText") { 369 @Override 370 public String map() { 371 return ((JLabel) getSource()).getText(); 372 } 373 })); 374 } 375 376 /** 377 * Maps {@code JLabel.getUI()} through queue 378 */ 379 public LabelUI getUI() { 380 return (runMapping(new MapAction<LabelUI>("getUI") { 381 @Override 382 public LabelUI map() { 383 return ((JLabel) getSource()).getUI(); 384 } 385 })); 386 } 387 388 /** 389 * Maps {@code JLabel.getVerticalAlignment()} through queue 390 */ 391 public int getVerticalAlignment() { 392 return (runMapping(new MapIntegerAction("getVerticalAlignment") { 393 @Override 394 public int map() { 395 return ((JLabel) getSource()).getVerticalAlignment(); 396 } 397 })); 398 } 399 400 /** 401 * Maps {@code JLabel.getVerticalTextPosition()} through queue 402 */ 403 public int getVerticalTextPosition() { 404 return (runMapping(new MapIntegerAction("getVerticalTextPosition") { 405 @Override 406 public int map() { 407 return ((JLabel) getSource()).getVerticalTextPosition(); 408 } 409 })); 410 } 411 412 /** 413 * Maps {@code JLabel.setDisabledIcon(Icon)} through queue 414 */ 415 public void setDisabledIcon(final Icon icon) { 416 runMapping(new MapVoidAction("setDisabledIcon") { 417 @Override 418 public void map() { 419 ((JLabel) getSource()).setDisabledIcon(icon); 420 } 421 }); 422 } 423 424 /** 425 * Maps {@code JLabel.setDisplayedMnemonic(char)} through queue 426 */ 427 public void setDisplayedMnemonic(final char c) { 428 runMapping(new MapVoidAction("setDisplayedMnemonic") { 429 @Override 430 public void map() { 431 ((JLabel) getSource()).setDisplayedMnemonic(c); 432 } 433 }); 434 } 435 436 /** 437 * Maps {@code JLabel.setDisplayedMnemonic(int)} through queue 438 */ 439 public void setDisplayedMnemonic(final int i) { 440 runMapping(new MapVoidAction("setDisplayedMnemonic") { 441 @Override 442 public void map() { 443 ((JLabel) getSource()).setDisplayedMnemonic(i); 444 } 445 }); 446 } 447 448 /** 449 * Maps {@code JLabel.setHorizontalAlignment(int)} through queue 450 */ 451 public void setHorizontalAlignment(final int i) { 452 runMapping(new MapVoidAction("setHorizontalAlignment") { 453 @Override 454 public void map() { 455 ((JLabel) getSource()).setHorizontalAlignment(i); 456 } 457 }); 458 } 459 460 /** 461 * Maps {@code JLabel.setHorizontalTextPosition(int)} through queue 462 */ 463 public void setHorizontalTextPosition(final int i) { 464 runMapping(new MapVoidAction("setHorizontalTextPosition") { 465 @Override 466 public void map() { 467 ((JLabel) getSource()).setHorizontalTextPosition(i); 468 } 469 }); 470 } 471 472 /** 473 * Maps {@code JLabel.setIcon(Icon)} through queue 474 */ 475 public void setIcon(final Icon icon) { 476 runMapping(new MapVoidAction("setIcon") { 477 @Override 478 public void map() { 479 ((JLabel) getSource()).setIcon(icon); 480 } 481 }); 482 } 483 484 /** 485 * Maps {@code JLabel.setIconTextGap(int)} through queue 486 */ 487 public void setIconTextGap(final int i) { 488 runMapping(new MapVoidAction("setIconTextGap") { 489 @Override 490 public void map() { 491 ((JLabel) getSource()).setIconTextGap(i); 492 } 493 }); 494 } 495 496 /** 497 * Maps {@code JLabel.setLabelFor(Component)} through queue 498 */ 499 public void setLabelFor(final Component component) { 500 runMapping(new MapVoidAction("setLabelFor") { 501 @Override 502 public void map() { 503 ((JLabel) getSource()).setLabelFor(component); 504 } 505 }); 506 } 507 508 /** 509 * Maps {@code JLabel.setText(String)} through queue 510 */ 511 public void setText(final String string) { 512 runMapping(new MapVoidAction("setText") { 513 @Override 514 public void map() { 515 ((JLabel) getSource()).setText(string); 516 } 517 }); 518 } 519 520 /** 521 * Maps {@code JLabel.setUI(LabelUI)} through queue 522 */ 523 public void setUI(final LabelUI labelUI) { 524 runMapping(new MapVoidAction("setUI") { 525 @Override 526 public void map() { 527 ((JLabel) getSource()).setUI(labelUI); 528 } 529 }); 530 } 531 532 /** 533 * Maps {@code JLabel.setVerticalAlignment(int)} through queue 534 */ 535 public void setVerticalAlignment(final int i) { 536 runMapping(new MapVoidAction("setVerticalAlignment") { 537 @Override 538 public void map() { 539 ((JLabel) getSource()).setVerticalAlignment(i); 540 } 541 }); 542 } 543 544 /** 545 * Maps {@code JLabel.setVerticalTextPosition(int)} through queue 546 */ 547 public void setVerticalTextPosition(final int i) { 548 runMapping(new MapVoidAction("setVerticalTextPosition") { 549 @Override 550 public void map() { 551 ((JLabel) getSource()).setVerticalTextPosition(i); 552 } 553 }); 554 } 555 556 //End of mapping // 557 //////////////////////////////////////////////////////// 558 /** 559 * Allows to find component by text. 560 */ 561 public static class JLabelByLabelFinder implements ComponentChooser { 562 563 String label; 564 StringComparator comparator; 565 566 /** 567 * Constructs JLabelByLabelFinder. 568 * 569 * @param lb a text pattern 570 * @param comparator specifies string comparision algorithm. 571 */ 572 public JLabelByLabelFinder(String lb, StringComparator comparator) { 573 label = lb; 574 this.comparator = comparator; 575 } 576 577 /** 578 * Constructs JLabelByLabelFinder. 579 * 580 * @param lb a text pattern 581 */ 582 public JLabelByLabelFinder(String lb) { 583 this(lb, Operator.getDefaultStringComparator()); 584 } 585 586 @Override 587 public boolean checkComponent(Component comp) { 588 if (comp instanceof JLabel) { 589 if (((JLabel) comp).getText() != null) { 590 return (comparator.equals(((JLabel) comp).getText(), 591 label)); 592 } 593 } 594 return false; 595 } 596 597 @Override 598 public String getDescription() { 599 return "JLabel with text \"" + label + "\""; 600 } 601 602 @Override 603 public String toString() { 604 return "JLabelByLabelFinder{" + "label=" + label + ", comparator=" + comparator + '}'; 605 } 606 } 607 608 /** 609 * Checks component type. 610 */ 611 public static class JLabelFinder extends Finder { 612 613 /** 614 * Constructs JLabelFinder. 615 * 616 * @param sf other searching criteria. 617 */ 618 public JLabelFinder(ComponentChooser sf) { 619 super(JLabel.class, sf); 620 } 621 622 /** 623 * Constructs JLabelFinder. 624 */ 625 public JLabelFinder() { 626 super(JLabel.class); 627 } 628 } 629} 630