JEditorPaneOperator.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.Container; 26import java.io.IOException; 27import java.io.InputStream; 28import java.net.URL; 29import java.util.Hashtable; 30 31import javax.swing.JEditorPane; 32import javax.swing.event.HyperlinkEvent; 33import javax.swing.event.HyperlinkListener; 34import javax.swing.text.EditorKit; 35 36import org.netbeans.jemmy.ComponentChooser; 37import org.netbeans.jemmy.TimeoutExpiredException; 38 39/** 40 * Class provides basic functions to operate with JEditorPane (selection, 41 * typing, deleting) 42 * 43 * <BR><BR>Timeouts used: <BR> 44 * JTextComponentOperator.PushKeyTimeout - time between key pressing and 45 * releasing during text typing <BR> 46 * JTextComponentOperator.BetweenKeysTimeout - time to sleep between two chars 47 * typing <BR> 48 * JTextComponentOperator.ChangeCaretPositionTimeout - maximum time to change 49 * caret position <BR> 50 * JTextComponentOperator.TypeTextTimeout - maximum time to type text <BR> 51 * ComponentOperator.WaitComponentTimeout - time to wait component displayed 52 * <BR> 53 * ComponentOperator.WaitFocusTimeout - time to wait component focus <BR> 54 * JScrollBarOperator.OneScrollClickTimeout - time for one scroll click <BR> 55 * JScrollBarOperator.WholeScrollTimeout - time for the whole scrolling <BR>. 56 * 57 * @see org.netbeans.jemmy.Timeouts 58 * 59 * @author Alexandre Iline (alexandre.iline@oracle.com) 60 */ 61public class JEditorPaneOperator extends JTextComponentOperator { 62 63 /** 64 * Identifier for a "content type" property. 65 * 66 * @see #getDump 67 */ 68 public static final String CONTENT_TYPE_DPROP = "Content type"; 69 70 /** 71 * Constructor. 72 * 73 * @param b a component 74 */ 75 public JEditorPaneOperator(JEditorPane b) { 76 super(b); 77 } 78 79 /** 80 * Constructs a JEditorPaneOperator object. 81 * 82 * @param cont a container 83 * @param chooser a component chooser specifying searching criteria. 84 * @param index an index between appropriate ones. 85 */ 86 public JEditorPaneOperator(ContainerOperator<?> cont, ComponentChooser chooser, int index) { 87 this((JEditorPane) cont. 88 waitSubComponent(new JEditorPaneFinder(chooser), 89 index)); 90 copyEnvironment(cont); 91 } 92 93 /** 94 * Constructs a JEditorPaneOperator object. 95 * 96 * @param cont a container 97 * @param chooser a component chooser specifying searching criteria. 98 */ 99 public JEditorPaneOperator(ContainerOperator<?> cont, ComponentChooser chooser) { 100 this(cont, chooser, 0); 101 } 102 103 /** 104 * Constructor. Waits component in container first. Uses cont's timeout and 105 * output for waiting and to init operator. 106 * 107 * @param cont a container 108 * @param text Button text. 109 * @param index Ordinal component index. 110 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 111 * @throws TimeoutExpiredException 112 */ 113 public JEditorPaneOperator(ContainerOperator<?> cont, String text, int index) { 114 this((JEditorPane) waitComponent(cont, 115 new JEditorPaneFinder(new JTextComponentOperator.JTextComponentByTextFinder(text, 116 cont.getComparator())), 117 index)); 118 copyEnvironment(cont); 119 } 120 121 /** 122 * Constructor. Waits component in container first. Uses cont's timeout and 123 * output for waiting and to init operator. 124 * 125 * @param cont a container 126 * @param text Button text. 127 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 128 * @throws TimeoutExpiredException 129 */ 130 public JEditorPaneOperator(ContainerOperator<?> cont, String text) { 131 this(cont, text, 0); 132 } 133 134 /** 135 * Constructor. Waits component in container first. Uses cont's timeout and 136 * output for waiting and to init operator. 137 * 138 * @param cont a container 139 * @param index Ordinal component index. 140 * @throws TimeoutExpiredException 141 */ 142 public JEditorPaneOperator(ContainerOperator<?> cont, int index) { 143 this((JEditorPane) waitComponent(cont, 144 new JEditorPaneFinder(), 145 index)); 146 copyEnvironment(cont); 147 } 148 149 /** 150 * Constructor. Waits component in container first. Uses cont's timeout and 151 * output for waiting and to init operator. 152 * 153 * @param cont a container 154 * @throws TimeoutExpiredException 155 */ 156 public JEditorPaneOperator(ContainerOperator<?> cont) { 157 this(cont, 0); 158 } 159 160 /** 161 * Searches JEditorPane in container. 162 * 163 * @param cont Container to search component in. 164 * @param chooser a component chooser specifying searching criteria. 165 * @param index Ordinal component index. 166 * @return JEditorPane instance or null if component was not found. 167 */ 168 public static JEditorPane findJEditorPane(Container cont, ComponentChooser chooser, int index) { 169 return (JEditorPane) findJTextComponent(cont, new JEditorPaneFinder(chooser), index); 170 } 171 172 /** 173 * Searches JEditorPane in container. 174 * 175 * @param cont Container to search component in. 176 * @param chooser a component chooser specifying searching criteria. 177 * @return JEditorPane instance or null if component was not found. 178 */ 179 public static JEditorPane findJEditorPane(Container cont, ComponentChooser chooser) { 180 return findJEditorPane(cont, chooser, 0); 181 } 182 183 /** 184 * Searches JEditorPane by text. 185 * 186 * @param cont Container to search component in. 187 * @param text Component text. 188 * @param ce Compare text exactly. 189 * @param ccs Compare text case sensitively. 190 * @param index Ordinal component index. 191 * @return JEditorPane instance or null if component was not found. 192 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 193 */ 194 public static JEditorPane findJEditorPane(Container cont, String text, boolean ce, boolean ccs, int index) { 195 return (findJEditorPane(cont, 196 new JEditorPaneFinder(new JTextComponentOperator.JTextComponentByTextFinder(text, 197 new DefaultStringComparator(ce, ccs))), 198 index)); 199 } 200 201 /** 202 * Searches JEditorPane by text. 203 * 204 * @param cont Container to search component in. 205 * @param text Component text. 206 * @param ce Compare text exactly. 207 * @param ccs Compare text case sensitively. 208 * @return JEditorPane instance or null if component was not found. 209 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 210 */ 211 public static JEditorPane findJEditorPane(Container cont, String text, boolean ce, boolean ccs) { 212 return findJEditorPane(cont, text, ce, ccs, 0); 213 } 214 215 /** 216 * Waits JEditorPane in container. 217 * 218 * @param cont Container to search component in. 219 * @param chooser a component chooser specifying searching criteria. 220 * @param index Ordinal component index. 221 * @return JEditorPane instance. 222 * @throws TimeoutExpiredException 223 */ 224 public static JEditorPane waitJEditorPane(Container cont, ComponentChooser chooser, int index) { 225 return (JEditorPane) waitJTextComponent(cont, new JEditorPaneFinder(chooser), index); 226 } 227 228 /** 229 * Waits JEditorPane in container. 230 * 231 * @param cont Container to search component in. 232 * @param chooser a component chooser specifying searching criteria. 233 * @return JEditorPane instance. 234 * @throws TimeoutExpiredException 235 */ 236 public static JEditorPane waitJEditorPane(Container cont, ComponentChooser chooser) { 237 return waitJEditorPane(cont, chooser, 0); 238 } 239 240 /** 241 * Waits JEditorPane by text. 242 * 243 * @param cont Container to search component in. 244 * @param text Component text. 245 * @param ce Compare text exactly. 246 * @param ccs Compare text case sensitively. 247 * @param index Ordinal component index. 248 * @return JEditorPane instance. 249 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 250 * @throws TimeoutExpiredException 251 */ 252 public static JEditorPane waitJEditorPane(Container cont, String text, boolean ce, boolean ccs, int index) { 253 return (waitJEditorPane(cont, 254 new JEditorPaneFinder(new JTextComponentOperator.JTextComponentByTextFinder(text, 255 new DefaultStringComparator(ce, ccs))), 256 index)); 257 } 258 259 /** 260 * Waits JEditorPane by text. 261 * 262 * @param cont Container to search component in. 263 * @param text Component text. 264 * @param ce Compare text exactly. 265 * @param ccs Compare text case sensitively. 266 * @return JEditorPane instance. 267 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 268 * @throws TimeoutExpiredException 269 */ 270 public static JEditorPane waitJEditorPane(Container cont, String text, boolean ce, boolean ccs) { 271 return waitJEditorPane(cont, text, ce, ccs, 0); 272 } 273 274 /** 275 * Notifies whether "PageUp" and "PageDown" should be used to change caret 276 * position. If can be useful if text takes some pages. 277 * 278 * @param yesOrNo whether to use "PageUp" and "PageDown" 279 * @deprecated vlue set by this method is not used anymore: all navigating 280 * is performed by TextDriver. 281 */ 282 @Deprecated 283 public void usePageNavigationKeys(boolean yesOrNo) { 284 } 285 286 /** 287 * Returns information about component. 288 */ 289 @Override 290 public Hashtable<String, Object> getDump() { 291 Hashtable<String, Object> result = super.getDump(); 292 result.put(CONTENT_TYPE_DPROP, ((JEditorPane) getSource()).getContentType()); 293 return result; 294 } 295 296 //////////////////////////////////////////////////////// 297 //Mapping // 298 /** 299 * Maps {@code JEditorPane.addHyperlinkListener(HyperlinkListener)} 300 * through queue 301 */ 302 public void addHyperlinkListener(final HyperlinkListener hyperlinkListener) { 303 runMapping(new MapVoidAction("addHyperlinkListener") { 304 @Override 305 public void map() { 306 ((JEditorPane) getSource()).addHyperlinkListener(hyperlinkListener); 307 } 308 }); 309 } 310 311 /** 312 * Maps {@code JEditorPane.fireHyperlinkUpdate(HyperlinkEvent)} through queue 313 */ 314 public void fireHyperlinkUpdate(final HyperlinkEvent hyperlinkEvent) { 315 runMapping(new MapVoidAction("fireHyperlinkUpdate") { 316 @Override 317 public void map() { 318 ((JEditorPane) getSource()).fireHyperlinkUpdate(hyperlinkEvent); 319 } 320 }); 321 } 322 323 /** 324 * Maps {@code JEditorPane.getContentType()} through queue 325 */ 326 public String getContentType() { 327 return (runMapping(new MapAction<String>("getContentType") { 328 @Override 329 public String map() { 330 return ((JEditorPane) getSource()).getContentType(); 331 } 332 })); 333 } 334 335 /** 336 * Maps {@code JEditorPane.getEditorKit()} through queue 337 */ 338 public EditorKit getEditorKit() { 339 return (runMapping(new MapAction<EditorKit>("getEditorKit") { 340 @Override 341 public EditorKit map() { 342 return ((JEditorPane) getSource()).getEditorKit(); 343 } 344 })); 345 } 346 347 /** 348 * Maps {@code JEditorPane.getEditorKitForContentType(String)} through queue 349 */ 350 public EditorKit getEditorKitForContentType(final String string) { 351 return (runMapping(new MapAction<EditorKit>("getEditorKitForContentType") { 352 @Override 353 public EditorKit map() { 354 return ((JEditorPane) getSource()).getEditorKitForContentType(string); 355 } 356 })); 357 } 358 359 /** 360 * Maps {@code JEditorPane.getPage()} through queue 361 */ 362 public URL getPage() { 363 return (runMapping(new MapAction<URL>("getPage") { 364 @Override 365 public URL map() { 366 return ((JEditorPane) getSource()).getPage(); 367 } 368 })); 369 } 370 371 /** 372 * Maps {@code JEditorPane.read(InputStream, Object)} through queue 373 */ 374 public void read(final InputStream inputStream, final Object object) { 375 runMapping(new MapVoidAction("read") { 376 @Override 377 public void map() throws IOException { 378 ((JEditorPane) getSource()).read(inputStream, object); 379 } 380 }); 381 } 382 383 /** 384 * Maps {@code JEditorPane.removeHyperlinkListener(HyperlinkListener)} 385 * through queue 386 */ 387 public void removeHyperlinkListener(final HyperlinkListener hyperlinkListener) { 388 runMapping(new MapVoidAction("removeHyperlinkListener") { 389 @Override 390 public void map() { 391 ((JEditorPane) getSource()).removeHyperlinkListener(hyperlinkListener); 392 } 393 }); 394 } 395 396 /** 397 * Maps {@code JEditorPane.setContentType(String)} through queue 398 */ 399 public void setContentType(final String string) { 400 runMapping(new MapVoidAction("setContentType") { 401 @Override 402 public void map() { 403 ((JEditorPane) getSource()).setContentType(string); 404 } 405 }); 406 } 407 408 /** 409 * Maps {@code JEditorPane.setEditorKit(EditorKit)} through queue 410 */ 411 public void setEditorKit(final EditorKit editorKit) { 412 runMapping(new MapVoidAction("setEditorKit") { 413 @Override 414 public void map() { 415 ((JEditorPane) getSource()).setEditorKit(editorKit); 416 } 417 }); 418 } 419 420 /** 421 * Maps 422 * {@code JEditorPane.setEditorKitForContentType(String, EditorKit)} 423 * through queue 424 */ 425 public void setEditorKitForContentType(final String string, final EditorKit editorKit) { 426 runMapping(new MapVoidAction("setEditorKitForContentType") { 427 @Override 428 public void map() { 429 ((JEditorPane) getSource()).setEditorKitForContentType(string, editorKit); 430 } 431 }); 432 } 433 434 /** 435 * Maps {@code JEditorPane.setPage(String)} through queue 436 */ 437 public void setPage(final String string) { 438 runMapping(new MapVoidAction("setPage") { 439 @Override 440 public void map() throws IOException { 441 ((JEditorPane) getSource()).setPage(string); 442 } 443 }); 444 } 445 446 /** 447 * Maps {@code JEditorPane.setPage(URL)} through queue 448 */ 449 public void setPage(final URL uRL) { 450 runMapping(new MapVoidAction("setPage") { 451 @Override 452 public void map() throws IOException { 453 ((JEditorPane) getSource()).setPage(uRL); 454 } 455 }); 456 } 457 458 //End of mapping // 459 //////////////////////////////////////////////////////// 460 /** 461 * Checks component type. 462 */ 463 public static class JEditorPaneFinder extends Finder { 464 465 /** 466 * Constructs JEditorPaneFinder. 467 * 468 * @param sf other searching criteria. 469 */ 470 public JEditorPaneFinder(ComponentChooser sf) { 471 super(JEditorPane.class, sf); 472 } 473 474 /** 475 * Constructs JEditorPaneFinder. 476 */ 477 public JEditorPaneFinder() { 478 super(JEditorPane.class); 479 } 480 } 481} 482