001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s): Alexandre Iline.
025: *
026: * The Original Software is the Jemmy library.
027: * The Initial Developer of the Original Software is Alexandre Iline.
028: * All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: *
041: *
042: *
043: * $Id: JButtonOperator.java,v 1.8 2007/10/05 11:35:55 jskrivanek Exp $ $Revision: 1.8 $ $Date: 2007/10/05 11:35:55 $
044: *
045: */
046:
047: package org.netbeans.jemmy.operators;
048:
049: import org.netbeans.jemmy.ComponentChooser;
050: import org.netbeans.jemmy.ComponentSearcher;
051: import org.netbeans.jemmy.TimeoutExpiredException;
052:
053: import java.awt.Component;
054: import java.awt.Container;
055:
056: import java.util.Hashtable;
057:
058: import javax.swing.JButton;
059:
060: /**
061: *
062: * <BR><BR>Timeouts used: <BR>
063: * AbstractButtonOperator.PushButtonTimeout - time between button pressing and releasing<BR>
064: * ComponentOperator.WaitComponentTimeout - time to wait button displayed <BR>
065: * ComponentOperator.WaitComponentEnabledTimeout - time to wait button enabled <BR>.
066: *
067: * @see org.netbeans.jemmy.Timeouts
068: *
069: * @author Alexandre Iline (alexandre.iline@sun.com)
070: *
071: */
072:
073: public class JButtonOperator extends AbstractButtonOperator {
074:
075: /**
076: * Identifier for a "default button" property.
077: * @see #getDump
078: */
079: public static final String IS_DEFAULT_DPROP = "Default button";
080:
081: /**
082: * Constructor.
083: * @param b a component
084: */
085: public JButtonOperator(JButton b) {
086: super (b);
087: }
088:
089: /**
090: * Constructs a JButtonOperator object.
091: * @param cont container
092: * @param chooser a component chooser specifying searching criteria.
093: * @param index an index between appropriate ones.
094: */
095: public JButtonOperator(ContainerOperator cont,
096: ComponentChooser chooser, int index) {
097: this ((JButton) cont.waitSubComponent(
098: new JButtonFinder(chooser), index));
099: copyEnvironment(cont);
100: }
101:
102: /**
103: * Constructs a JButtonOperator object.
104: * @param cont container
105: * @param chooser a component chooser specifying searching criteria.
106: */
107: public JButtonOperator(ContainerOperator cont,
108: ComponentChooser chooser) {
109: this (cont, chooser, 0);
110: }
111:
112: /**
113: * Constructor.
114: * Waits component in container first.
115: * Uses cont's timeout and output for waiting and to init operator.
116: * @param cont container
117: * @param text Button text.
118: * @param index Ordinal component index.
119: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
120: * @throws TimeoutExpiredException
121: */
122: public JButtonOperator(ContainerOperator cont, String text,
123: int index) {
124: this ((JButton) waitComponent(cont, new JButtonFinder(
125: new AbstractButtonOperator.AbstractButtonByLabelFinder(
126: text, cont.getComparator())), index));
127: copyEnvironment(cont);
128: }
129:
130: /**
131: * Constructor.
132: * Waits component in container first.
133: * Uses cont's timeout and output for waiting and to init operator.
134: * @param cont container
135: * @param text Button text.
136: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
137: * @throws TimeoutExpiredException
138: */
139: public JButtonOperator(ContainerOperator cont, String text) {
140: this (cont, text, 0);
141: }
142:
143: /**
144: * Constructor.
145: * Waits component in container first.
146: * Uses cont's timeout and output for waiting and to init operator.
147: * @param cont container
148: * @param index Ordinal component index.
149: * @throws TimeoutExpiredException
150: */
151: public JButtonOperator(ContainerOperator cont, int index) {
152: this ((JButton) waitComponent(cont, new JButtonFinder(), index));
153: copyEnvironment(cont);
154: }
155:
156: /**
157: * Constructor.
158: * Waits component in container first.
159: * Uses cont's timeout and output for waiting and to init operator.
160: * @param cont container
161: * @throws TimeoutExpiredException
162: */
163: public JButtonOperator(ContainerOperator cont) {
164: this (cont, 0);
165: }
166:
167: /**
168: * Searches JButton in container.
169: * @param cont Container to search component in.
170: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
171: * @param index Ordinal component index.
172: * @return JButton instance or null if component was not found.
173: */
174: public static JButton findJButton(Container cont,
175: ComponentChooser chooser, int index) {
176: return ((JButton) findAbstractButton(cont, new JButtonFinder(
177: chooser), index));
178: }
179:
180: /**
181: * Searches 0'th JButton in container.
182: * @param cont Container to search component in.
183: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
184: * @return JButton instance or null if component was not found.
185: */
186: public static JButton findJButton(Container cont,
187: ComponentChooser chooser) {
188: return (findJButton(cont, chooser, 0));
189: }
190:
191: /**
192: * Searches JButton by text.
193: * @param cont Container to search component in.
194: * @param text Button text. If null, contents is not checked.
195: * @param ce Compare text exactly.
196: * @param ccs Compare text case sensitively.
197: * @param index Ordinal component index.
198: * @return JButton instance or null if component was not found.
199: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
200: */
201: public static JButton findJButton(Container cont, String text,
202: boolean ce, boolean ccs, int index) {
203: return (findJButton(cont, new JButtonFinder(
204: new AbstractButtonOperator.AbstractButtonByLabelFinder(
205: text, new DefaultStringComparator(ce, ccs))),
206: index));
207: }
208:
209: /**
210: * Searches JButton by text.
211: * @param cont Container to search component in.
212: * @param text Button text. If null, contents is not checked.
213: * @param ce Compare text exactly.
214: * @param ccs Compare text case sensitively.
215: * @return JButton instance or null if component was not found.
216: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
217: */
218: public static JButton findJButton(Container cont, String text,
219: boolean ce, boolean ccs) {
220: return (findJButton(cont, text, ce, ccs, 0));
221: }
222:
223: /**
224: * Waits JButton in container.
225: * @param cont Container to search component in.
226: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
227: * @param index Ordinal component index.
228: * @return JButton instance.
229: * @throws TimeoutExpiredException
230: */
231: public static JButton waitJButton(Container cont,
232: ComponentChooser chooser, int index) {
233: return ((JButton) waitAbstractButton(cont, new JButtonFinder(
234: chooser), index));
235: }
236:
237: /**
238: * Waits 0'th JButton in container.
239: * @param cont Container to search component in.
240: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
241: * @return JButton instance.
242: * @throws TimeoutExpiredException
243: */
244: public static JButton waitJButton(Container cont,
245: ComponentChooser chooser) {
246: return (waitJButton(cont, chooser, 0));
247: }
248:
249: /**
250: * Waits JButton by text.
251: * @param cont Container to search component in.
252: * @param text Button text. If null, contents is not checked.
253: * @param ce Compare text exactly.
254: * @param ccs Compare text case sensitively.
255: * @param index Ordinal component index.
256: * @return JButton instance.
257: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
258: * @throws TimeoutExpiredException
259: */
260: public static JButton waitJButton(Container cont, String text,
261: boolean ce, boolean ccs, int index) {
262: return (waitJButton(cont, new JButtonFinder(
263: new AbstractButtonOperator.AbstractButtonByLabelFinder(
264: text, new DefaultStringComparator(ce, ccs))),
265: index));
266: }
267:
268: /**
269: * Waits JButton by text.
270: * @param cont Container to search component in.
271: * @param text Button text. If null, contents is not checked.
272: * @param ce Compare text exactly.
273: * @param ccs Compare text case sensitively.
274: * @return JButton instance.
275: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
276: * @throws TimeoutExpiredException
277: */
278: public static JButton waitJButton(Container cont, String text,
279: boolean ce, boolean ccs) {
280: return (waitJButton(cont, text, ce, ccs, 0));
281: }
282:
283: /**
284: * Returns information about component.
285: */
286: public Hashtable getDump() {
287: Hashtable result = super .getDump();
288: result.remove(AbstractButtonOperator.IS_SELECTED_DPROP);
289: result.put(IS_DEFAULT_DPROP, ((JButton) getSource())
290: .isDefaultButton() ? "true" : "false");
291: return (result);
292: }
293:
294: ////////////////////////////////////////////////////////
295: //Mapping //
296:
297: /**Maps <code>JButton.isDefaultButton()</code> through queue*/
298: public boolean isDefaultButton() {
299: return (runMapping(new MapBooleanAction("isDefaultButton") {
300: public boolean map() {
301: return (((JButton) getSource()).isDefaultButton());
302: }
303: }));
304: }
305:
306: /**Maps <code>JButton.isDefaultCapable()</code> through queue*/
307: public boolean isDefaultCapable() {
308: return (runMapping(new MapBooleanAction("isDefaultCapable") {
309: public boolean map() {
310: return (((JButton) getSource()).isDefaultCapable());
311: }
312: }));
313: }
314:
315: /**Maps <code>JButton.setDefaultCapable(boolean)</code> through queue*/
316: public void setDefaultCapable(final boolean b) {
317: runMapping(new MapVoidAction("setDefaultCapable") {
318: public void map() {
319: ((JButton) getSource()).setDefaultCapable(b);
320: }
321: });
322: }
323:
324: //End of mapping //
325: ////////////////////////////////////////////////////////
326:
327: /**
328: * Prepares the button to click.
329: */
330: protected void prepareToClick() {
331: makeComponentVisible();
332: }
333:
334: /**
335: * Checks component type.
336: */
337: public static class JButtonFinder extends Finder {
338: /**
339: * Constructs JButtonFinder.
340: * @param sf other searching criteria.
341: */
342: public JButtonFinder(ComponentChooser sf) {
343: super (JButton.class, sf);
344: }
345:
346: /**
347: * Constructs JButtonFinder.
348: */
349: public JButtonFinder() {
350: super (JButton.class);
351: }
352: }
353: }
|