JButtonOperator.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.util.Hashtable; 27 28import javax.swing.JButton; 29 30import org.netbeans.jemmy.ComponentChooser; 31import org.netbeans.jemmy.TimeoutExpiredException; 32 33/** 34 * 35 * <BR><BR>Timeouts used: <BR> 36 * AbstractButtonOperator.PushButtonTimeout - time between button pressing and 37 * releasing<BR> 38 * ComponentOperator.WaitComponentTimeout - time to wait button displayed <BR> 39 * ComponentOperator.WaitComponentEnabledTimeout - time to wait button enabled 40 * <BR>. 41 * 42 * @see org.netbeans.jemmy.Timeouts 43 * 44 * @author Alexandre Iline (alexandre.iline@oracle.com) 45 * 46 */ 47public class JButtonOperator extends AbstractButtonOperator { 48 49 /** 50 * Identifier for a "default button" property. 51 * 52 * @see #getDump 53 */ 54 public static final String IS_DEFAULT_DPROP = "Default button"; 55 56 /** 57 * Constructor. 58 * 59 * @param b a component 60 */ 61 public JButtonOperator(JButton b) { 62 super(b); 63 } 64 65 /** 66 * Constructs a JButtonOperator object. 67 * 68 * @param cont container 69 * @param chooser a component chooser specifying searching criteria. 70 * @param index an index between appropriate ones. 71 */ 72 public JButtonOperator(ContainerOperator<?> cont, ComponentChooser chooser, int index) { 73 this((JButton) cont. 74 waitSubComponent(new JButtonFinder(chooser), 75 index)); 76 copyEnvironment(cont); 77 } 78 79 /** 80 * Constructs a JButtonOperator object. 81 * 82 * @param cont container 83 * @param chooser a component chooser specifying searching criteria. 84 */ 85 public JButtonOperator(ContainerOperator<?> cont, ComponentChooser chooser) { 86 this(cont, chooser, 0); 87 } 88 89 /** 90 * Constructor. Waits component in container first. Uses cont's timeout and 91 * output for waiting and to init operator. 92 * 93 * @param cont container 94 * @param text Button text. 95 * @param index Ordinal component index. 96 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 97 * @throws TimeoutExpiredException 98 */ 99 public JButtonOperator(ContainerOperator<?> cont, String text, int index) { 100 this((JButton) waitComponent(cont, 101 new JButtonFinder(new AbstractButtonOperator.AbstractButtonByLabelFinder(text, 102 cont.getComparator())), 103 index)); 104 copyEnvironment(cont); 105 } 106 107 /** 108 * Constructor. Waits component in container first. Uses cont's timeout and 109 * output for waiting and to init operator. 110 * 111 * @param cont container 112 * @param text Button text. 113 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 114 * @throws TimeoutExpiredException 115 */ 116 public JButtonOperator(ContainerOperator<?> cont, String text) { 117 this(cont, text, 0); 118 } 119 120 /** 121 * Constructor. Waits component in container first. Uses cont's timeout and 122 * output for waiting and to init operator. 123 * 124 * @param cont container 125 * @param index Ordinal component index. 126 * @throws TimeoutExpiredException 127 */ 128 public JButtonOperator(ContainerOperator<?> cont, int index) { 129 this((JButton) waitComponent(cont, 130 new JButtonFinder(), 131 index)); 132 copyEnvironment(cont); 133 } 134 135 /** 136 * Constructor. Waits component in container first. Uses cont's timeout and 137 * output for waiting and to init operator. 138 * 139 * @param cont container 140 * @throws TimeoutExpiredException 141 */ 142 public JButtonOperator(ContainerOperator<?> cont) { 143 this(cont, 0); 144 } 145 146 /** 147 * Searches JButton in container. 148 * 149 * @param cont Container to search component in. 150 * @param chooser org.netbeans.jemmy.ComponentChooser implementation. 151 * @param index Ordinal component index. 152 * @return JButton instance or null if component was not found. 153 */ 154 public static JButton findJButton(Container cont, ComponentChooser chooser, int index) { 155 return (JButton) findAbstractButton(cont, new JButtonFinder(chooser), index); 156 } 157 158 /** 159 * Searches 0'th JButton in container. 160 * 161 * @param cont Container to search component in. 162 * @param chooser org.netbeans.jemmy.ComponentChooser implementation. 163 * @return JButton instance or null if component was not found. 164 */ 165 public static JButton findJButton(Container cont, ComponentChooser chooser) { 166 return findJButton(cont, chooser, 0); 167 } 168 169 /** 170 * Searches JButton by text. 171 * 172 * @param cont Container to search component in. 173 * @param text Button text. If null, contents is not checked. 174 * @param ce Compare text exactly. 175 * @param ccs Compare text case sensitively. 176 * @param index Ordinal component index. 177 * @return JButton instance or null if component was not found. 178 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 179 */ 180 public static JButton findJButton(Container cont, String text, boolean ce, boolean ccs, int index) { 181 return (findJButton(cont, 182 new JButtonFinder(new AbstractButtonOperator.AbstractButtonByLabelFinder(text, 183 new DefaultStringComparator(ce, ccs))), 184 index)); 185 } 186 187 /** 188 * Searches JButton by text. 189 * 190 * @param cont Container to search component in. 191 * @param text Button text. If null, contents is not checked. 192 * @param ce Compare text exactly. 193 * @param ccs Compare text case sensitively. 194 * @return JButton instance or null if component was not found. 195 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 196 */ 197 public static JButton findJButton(Container cont, String text, boolean ce, boolean ccs) { 198 return findJButton(cont, text, ce, ccs, 0); 199 } 200 201 /** 202 * Waits JButton in container. 203 * 204 * @param cont Container to search component in. 205 * @param chooser org.netbeans.jemmy.ComponentChooser implementation. 206 * @param index Ordinal component index. 207 * @return JButton instance. 208 * @throws TimeoutExpiredException 209 */ 210 public static JButton waitJButton(Container cont, ComponentChooser chooser, int index) { 211 return (JButton) waitAbstractButton(cont, new JButtonFinder(chooser), index); 212 } 213 214 /** 215 * Waits 0'th JButton in container. 216 * 217 * @param cont Container to search component in. 218 * @param chooser org.netbeans.jemmy.ComponentChooser implementation. 219 * @return JButton instance. 220 * @throws TimeoutExpiredException 221 */ 222 public static JButton waitJButton(Container cont, ComponentChooser chooser) { 223 return waitJButton(cont, chooser, 0); 224 } 225 226 /** 227 * Waits JButton by text. 228 * 229 * @param cont Container to search component in. 230 * @param text Button text. If null, contents is not checked. 231 * @param ce Compare text exactly. 232 * @param ccs Compare text case sensitively. 233 * @param index Ordinal component index. 234 * @return JButton instance. 235 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 236 * @throws TimeoutExpiredException 237 */ 238 public static JButton waitJButton(Container cont, String text, boolean ce, boolean ccs, int index) { 239 return (waitJButton(cont, 240 new JButtonFinder(new AbstractButtonOperator.AbstractButtonByLabelFinder(text, 241 new DefaultStringComparator(ce, ccs))), 242 index)); 243 } 244 245 /** 246 * Waits JButton by text. 247 * 248 * @param cont Container to search component in. 249 * @param text Button text. If null, contents is not checked. 250 * @param ce Compare text exactly. 251 * @param ccs Compare text case sensitively. 252 * @return JButton instance. 253 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 254 * @throws TimeoutExpiredException 255 */ 256 public static JButton waitJButton(Container cont, String text, boolean ce, boolean ccs) { 257 return waitJButton(cont, text, ce, ccs, 0); 258 } 259 260 /** 261 * Returns information about component. 262 */ 263 @Override 264 public Hashtable<String, Object> getDump() { 265 Hashtable<String, Object> result = super.getDump(); 266 result.remove(AbstractButtonOperator.IS_SELECTED_DPROP); 267 result.put(IS_DEFAULT_DPROP, ((JButton) getSource()).isDefaultButton() ? "true" : "false"); 268 return result; 269 } 270 271 //////////////////////////////////////////////////////// 272 //Mapping // 273 /** 274 * Maps {@code JButton.isDefaultButton()} through queue 275 */ 276 public boolean isDefaultButton() { 277 return (runMapping(new MapBooleanAction("isDefaultButton") { 278 @Override 279 public boolean map() { 280 return ((JButton) getSource()).isDefaultButton(); 281 } 282 })); 283 } 284 285 /** 286 * Maps {@code JButton.isDefaultCapable()} through queue 287 */ 288 public boolean isDefaultCapable() { 289 return (runMapping(new MapBooleanAction("isDefaultCapable") { 290 @Override 291 public boolean map() { 292 return ((JButton) getSource()).isDefaultCapable(); 293 } 294 })); 295 } 296 297 /** 298 * Maps {@code JButton.setDefaultCapable(boolean)} through queue 299 */ 300 public void setDefaultCapable(final boolean b) { 301 runMapping(new MapVoidAction("setDefaultCapable") { 302 @Override 303 public void map() { 304 ((JButton) getSource()).setDefaultCapable(b); 305 } 306 }); 307 } 308 309 //End of mapping // 310 //////////////////////////////////////////////////////// 311 /** 312 * Prepares the button to click. 313 */ 314 protected void prepareToClick() { 315 makeComponentVisible(); 316 } 317 318 /** 319 * Checks component type. 320 */ 321 public static class JButtonFinder extends Finder { 322 323 /** 324 * Constructs JButtonFinder. 325 * 326 * @param sf other searching criteria. 327 */ 328 public JButtonFinder(ComponentChooser sf) { 329 super(JButton.class, sf); 330 } 331 332 /** 333 * Constructs JButtonFinder. 334 */ 335 public JButtonFinder() { 336 super(JButton.class); 337 } 338 } 339} 340