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.awt.Label; 28import java.util.Hashtable; 29 30import org.netbeans.jemmy.ComponentChooser; 31import org.netbeans.jemmy.TimeoutExpiredException; 32 33/** 34 * <BR><BR>Timeouts used: <BR> 35 * ComponentOperator.WaitComponentTimeout - time to wait component displayed 36 * <BR>. 37 * 38 * @see org.netbeans.jemmy.Timeouts 39 * @author Alexandre Iline (alexandre.iline@oracle.com) 40 * 41 */ 42public class LabelOperator extends ComponentOperator { 43 44 /** 45 * Identifier for a "text" property. 46 * 47 * @see #getDump 48 */ 49 public static final String TEXT_DPROP = "Text"; 50 51 /** 52 * Constructor. 53 * 54 * @param b a component 55 */ 56 public LabelOperator(Label b) { 57 super(b); 58 } 59 60 /** 61 * Constructs a LabelOperator object. 62 * 63 * @param cont a container 64 * @param chooser a component chooser specifying searching criteria. 65 * @param index an index between appropriate ones. 66 */ 67 public LabelOperator(ContainerOperator<?> cont, ComponentChooser chooser, int index) { 68 this((Label) cont. 69 waitSubComponent(new LabelFinder(chooser), 70 index)); 71 copyEnvironment(cont); 72 } 73 74 /** 75 * Constructs a LabelOperator object. 76 * 77 * @param cont a container 78 * @param chooser a component chooser specifying searching criteria. 79 */ 80 public LabelOperator(ContainerOperator<?> cont, ComponentChooser chooser) { 81 this(cont, chooser, 0); 82 } 83 84 /** 85 * Constructor. Waits component in container first. Uses cont's timeout and 86 * output for waiting and to init operator. 87 * 88 * @param cont a container 89 * @param text Button text. 90 * @param index Ordinal component index. 91 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 92 * @throws TimeoutExpiredException 93 */ 94 public LabelOperator(ContainerOperator<?> cont, String text, int index) { 95 this((Label) waitComponent(cont, 96 new LabelByLabelFinder(text, 97 cont.getComparator()), 98 index)); 99 copyEnvironment(cont); 100 } 101 102 /** 103 * Constructor. Waits component in container first. Uses cont's timeout and 104 * output for waiting and to init operator. 105 * 106 * @param cont a container 107 * @param text Button text. 108 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 109 * @throws TimeoutExpiredException 110 */ 111 public LabelOperator(ContainerOperator<?> cont, String text) { 112 this(cont, text, 0); 113 } 114 115 /** 116 * Constructor. Waits component in container first. Uses cont's timeout and 117 * output for waiting and to init operator. 118 * 119 * @param cont a container 120 * @param index Ordinal component index. 121 * @throws TimeoutExpiredException 122 */ 123 public LabelOperator(ContainerOperator<?> cont, int index) { 124 this((Label) waitComponent(cont, 125 new LabelFinder(), 126 index)); 127 copyEnvironment(cont); 128 } 129 130 /** 131 * Constructor. Waits component in container first. Uses cont's timeout and 132 * output for waiting and to init operator. 133 * 134 * @param cont a container 135 * @throws TimeoutExpiredException 136 */ 137 public LabelOperator(ContainerOperator<?> cont) { 138 this(cont, 0); 139 } 140 141 /** 142 * Searches Label in container. 143 * 144 * @param cont Container to search component in. 145 * @param chooser a component chooser specifying searching criteria. 146 * @param index Ordinal component index. 147 * @return Label instance or null if component was not found. 148 */ 149 public static Label findLabel(Container cont, ComponentChooser chooser, int index) { 150 return (Label) findComponent(cont, new LabelFinder(chooser), index); 151 } 152 153 /** 154 * Searches Label in container. 155 * 156 * @param cont Container to search component in. 157 * @param chooser a component chooser specifying searching criteria. 158 * @return Label instance or null if component was not found. 159 */ 160 public static Label findLabel(Container cont, ComponentChooser chooser) { 161 return findLabel(cont, chooser, 0); 162 } 163 164 /** 165 * Searches Label by text. 166 * 167 * @param cont Container to search component in. 168 * @param text Component text. 169 * @param ce Compare text exactly. 170 * @param ccs Compare text case sensitively. 171 * @param index Ordinal component index. 172 * @return Label instance or null if component was not found. 173 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 174 */ 175 public static Label findLabel(Container cont, String text, boolean ce, boolean ccs, int index) { 176 return findLabel(cont, new LabelByLabelFinder(text, new DefaultStringComparator(ce, ccs)), index); 177 } 178 179 /** 180 * Searches Label by text. 181 * 182 * @param cont Container to search component in. 183 * @param text Component text. 184 * @param ce Compare text exactly. 185 * @param ccs Compare text case sensitively. 186 * @return Label instance or null if component was not found. 187 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 188 */ 189 public static Label findLabel(Container cont, String text, boolean ce, boolean ccs) { 190 return findLabel(cont, text, ce, ccs, 0); 191 } 192 193 /** 194 * Waits Label in container. 195 * 196 * @param cont Container to search component in. 197 * @param chooser a component chooser specifying searching criteria. 198 * @param index Ordinal component index. 199 * @return Label instance. 200 * @throws TimeoutExpiredException 201 */ 202 public static Label waitLabel(final Container cont, final ComponentChooser chooser, final int index) { 203 return (Label) waitComponent(cont, new LabelFinder(chooser), index); 204 } 205 206 /** 207 * Waits Label in container. 208 * 209 * @param cont Container to search component in. 210 * @param chooser a component chooser specifying searching criteria. 211 * @return Label instance. 212 * @throws TimeoutExpiredException 213 */ 214 public static Label waitLabel(Container cont, ComponentChooser chooser) { 215 return waitLabel(cont, chooser, 0); 216 } 217 218 /** 219 * Waits Label by text. 220 * 221 * @param cont Container to search component in. 222 * @param text Component text. 223 * @param ce Compare text exactly. 224 * @param ccs Compare text case sensitively. 225 * @param index Ordinal component index. 226 * @return Label instance. 227 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 228 * @throws TimeoutExpiredException 229 */ 230 public static Label waitLabel(Container cont, String text, boolean ce, boolean ccs, int index) { 231 return waitLabel(cont, new LabelByLabelFinder(text, new DefaultStringComparator(ce, ccs)), index); 232 } 233 234 /** 235 * Waits Label by text. 236 * 237 * @param cont Container to search component in. 238 * @param text Component text. 239 * @param ce Compare text exactly. 240 * @param ccs Compare text case sensitively. 241 * @return Label instance. 242 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 243 * @throws TimeoutExpiredException 244 */ 245 public static Label waitLabel(Container cont, String text, boolean ce, boolean ccs) { 246 return waitLabel(cont, text, ce, ccs, 0); 247 } 248 249 @Override 250 public Hashtable<String, Object> getDump() { 251 Hashtable<String, Object> result = super.getDump(); 252 if (((Label) getSource()).getText() != null) { 253 result.put(TEXT_DPROP, ((Label) getSource()).getText()); 254 } else { 255 result.put(TEXT_DPROP, "null"); 256 } 257 return result; 258 } 259 260 //////////////////////////////////////////////////////// 261 //Mapping // 262 /** 263 * Maps {@code Label.getAlignment()} through queue 264 */ 265 public int getAlignment() { 266 return (runMapping(new MapIntegerAction("getAlignment") { 267 @Override 268 public int map() { 269 return ((Label) getSource()).getAlignment(); 270 } 271 })); 272 } 273 274 /** 275 * Maps {@code Label.getText()} through queue 276 */ 277 public String getText() { 278 return (runMapping(new MapAction<String>("getText") { 279 @Override 280 public String map() { 281 return ((Label) getSource()).getText(); 282 } 283 })); 284 } 285 286 /** 287 * Maps {@code Label.setAlignment(int)} through queue 288 */ 289 public void setAlignment(final int i) { 290 runMapping(new MapVoidAction("setAlignment") { 291 @Override 292 public void map() { 293 ((Label) getSource()).setAlignment(i); 294 } 295 }); 296 } 297 298 /** 299 * Maps {@code Label.setText(String)} through queue 300 */ 301 public void setText(final String string) { 302 runMapping(new MapVoidAction("setText") { 303 @Override 304 public void map() { 305 ((Label) getSource()).setText(string); 306 } 307 }); 308 } 309 310 //End of mapping // 311 //////////////////////////////////////////////////////// 312 /** 313 * Allows to find component by LabelByLabelFinder. 314 */ 315 public static class LabelByLabelFinder implements ComponentChooser { 316 317 String label; 318 StringComparator comparator; 319 320 /** 321 * Constructs LabelByLabelFinder. 322 * 323 * @param lb a text pattern 324 * @param comparator specifies string comparison algorithm. 325 */ 326 public LabelByLabelFinder(String lb, StringComparator comparator) { 327 label = lb; 328 this.comparator = comparator; 329 } 330 331 /** 332 * Constructs LabelByLabelFinder. 333 * 334 * @param lb a text pattern 335 */ 336 public LabelByLabelFinder(String lb) { 337 this(lb, Operator.getDefaultStringComparator()); 338 } 339 340 @Override 341 public boolean checkComponent(Component comp) { 342 if (comp instanceof Label) { 343 if (((Label) comp).getText() != null) { 344 return (comparator.equals(((Label) comp).getText(), 345 label)); 346 } 347 } 348 return false; 349 } 350 351 @Override 352 public String getDescription() { 353 return "Label with text \"" + label + "\""; 354 } 355 356 @Override 357 public String toString() { 358 return "LabelByLabelFinder{" + "label=" + label + ", comparator=" + comparator + '}'; 359 } 360 } 361 362 /** 363 * Checks component type. 364 */ 365 public static class LabelFinder extends Finder { 366 367 /** 368 * Constructs LabelFinder. 369 * 370 * @param sf other searching criteria. 371 */ 372 public LabelFinder(ComponentChooser sf) { 373 super(Label.class, sf); 374 } 375 376 /** 377 * Constructs LabelFinder. 378 */ 379 public LabelFinder() { 380 super(Label.class); 381 } 382 } 383} 384