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