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: CheckboxOperator.java,v 1.11 2007/10/05 11:35:58 jskrivanek Exp $ $Revision: 1.11 $ $Date: 2007/10/05 11:35:58 $
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.JemmyException;
052: import org.netbeans.jemmy.JemmyProperties;
053: import org.netbeans.jemmy.Outputable;
054: import org.netbeans.jemmy.TestOut;
055: import org.netbeans.jemmy.TimeoutExpiredException;
056: import org.netbeans.jemmy.Timeouts;
057:
058: import org.netbeans.jemmy.drivers.ButtonDriver;
059: import org.netbeans.jemmy.drivers.DriverManager;
060:
061: import java.awt.Component;
062: import java.awt.Container;
063:
064: import java.awt.Checkbox;
065: import java.awt.CheckboxGroup;
066: import java.awt.event.ItemListener;
067:
068: import java.util.Hashtable;
069:
070: /**
071: *
072: * <BR><BR>Timeouts used: <BR>
073: * ButtonOperator.PushButtonTimeout - time between checkbox pressing and releasing<BR>
074: * ComponentOperator.WaitComponentTimeout - time to wait checkbox displayed <BR>
075: * ComponentOperator.WaitComponentEnabledTimeout - time to wait checkbox enabled <BR>.
076: *
077: * @see org.netbeans.jemmy.Timeouts
078: *
079: * @author Alexandre Iline (alexandre.iline@sun.com)
080: *
081: */
082:
083: public class CheckboxOperator extends ComponentOperator implements
084: Outputable {
085:
086: /**
087: * Identifier for a label property.
088: * @see #getDump
089: */
090: public static final String TEXT_DPROP = "Label";
091:
092: private TestOut output;
093: ButtonDriver driver;
094:
095: /**
096: * Constructor.
097: * @param b a component
098: */
099: public CheckboxOperator(Checkbox b) {
100: super (b);
101: driver = DriverManager.getButtonDriver(getClass());
102: }
103:
104: /**
105: * Constructs an CheckboxOperator object.
106: * @param cont container
107: * @param chooser a component chooser specifying searching criteria.
108: * @param index an index between appropriate ones.
109: */
110: public CheckboxOperator(ContainerOperator cont,
111: ComponentChooser chooser, int index) {
112: this ((Checkbox) cont.waitSubComponent(new CheckboxFinder(
113: chooser), index));
114: copyEnvironment(cont);
115: }
116:
117: /**
118: * Constructs an CheckboxOperator object.
119: * @param cont container
120: * @param chooser a component chooser specifying searching criteria.
121: */
122: public CheckboxOperator(ContainerOperator cont,
123: ComponentChooser chooser) {
124: this (cont, chooser, 0);
125: }
126:
127: /**
128: * Constructor.
129: * Waits component in container first.
130: * Uses cont's timeout and output for waiting and to init operator.
131: * @param cont container
132: * @param text Checkbox text.
133: * @param index Ordinal component index.
134: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
135: * @throws TimeoutExpiredException
136: */
137: public CheckboxOperator(ContainerOperator cont, String text,
138: int index) {
139: this ((Checkbox) waitComponent(cont, new CheckboxByLabelFinder(
140: text, cont.getComparator()), index));
141: copyEnvironment(cont);
142: }
143:
144: /**
145: * Constructor.
146: * Waits component in container first.
147: * Uses cont's timeout and output for waiting and to init operator.
148: * @param cont container
149: * @param text Checkbox text.
150: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
151: * @throws TimeoutExpiredException
152: */
153: public CheckboxOperator(ContainerOperator cont, String text) {
154: this (cont, text, 0);
155: }
156:
157: /**
158: * Constructor.
159: * Waits component in container first.
160: * Uses cont's timeout and output for waiting and to init operator.
161: * @param cont container
162: * @param index Ordinal component index.
163: * @throws TimeoutExpiredException
164: */
165: public CheckboxOperator(ContainerOperator cont, int index) {
166: this (
167: (Checkbox) waitComponent(cont, new CheckboxFinder(),
168: index));
169: copyEnvironment(cont);
170: }
171:
172: /**
173: * Constructor.
174: * Waits component in container first.
175: * Uses cont's timeout and output for waiting and to init operator.
176: * @param cont container
177: * @throws TimeoutExpiredException
178: */
179: public CheckboxOperator(ContainerOperator cont) {
180: this (cont, 0);
181: }
182:
183: /**
184: * Searches Checkbox in container.
185: * @param cont Container to search component in.
186: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
187: * @param index Ordinal component index.
188: * @return Checkbox instance or null if component was not found.
189: */
190: public static Checkbox findCheckbox(Container cont,
191: ComponentChooser chooser, int index) {
192: return ((Checkbox) findComponent(cont, new CheckboxFinder(
193: chooser), index));
194: }
195:
196: /**
197: * Searches 0'th Checkbox in container.
198: * @param cont Container to search component in.
199: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
200: * @return Checkbox instance or null if component was not found.
201: */
202: public static Checkbox findCheckbox(Container cont,
203: ComponentChooser chooser) {
204: return (findCheckbox(cont, chooser, 0));
205: }
206:
207: /**
208: * Searches Checkbox by text.
209: * @param cont Container to search component in.
210: * @param text Checkbox text. If null, contents is not checked.
211: * @param ce Compare text exactly.
212: * @param ccs Compare text case sensitively.
213: * @param index Ordinal component index.
214: * @return Checkbox instance or null if component was not found.
215: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
216: */
217: public static Checkbox findCheckbox(Container cont, String text,
218: boolean ce, boolean ccs, int index) {
219: return (findCheckbox(cont, new CheckboxByLabelFinder(text,
220: new DefaultStringComparator(ce, ccs)), index));
221: }
222:
223: /**
224: * Searches Checkbox by text.
225: * @param cont Container to search component in.
226: * @param text Checkbox text. If null, contents is not checked.
227: * @param ce Compare text exactly.
228: * @param ccs Compare text case sensitively.
229: * @return Checkbox instance or null if component was not found.
230: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
231: */
232: public static Checkbox findCheckbox(Container cont, String text,
233: boolean ce, boolean ccs) {
234: return (findCheckbox(cont, text, ce, ccs, 0));
235: }
236:
237: /**
238: * Waits Checkbox in container.
239: * @param cont Container to search component in.
240: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
241: * @param index Ordinal component index.
242: * @return Checkbox instance.
243: * @throws TimeoutExpiredException
244: */
245: public static Checkbox waitCheckbox(Container cont,
246: ComponentChooser chooser, int index) {
247: return ((Checkbox) waitComponent(cont, new CheckboxFinder(
248: chooser), index));
249: }
250:
251: /**
252: * Waits 0'th Checkbox in container.
253: * @param cont Container to search component in.
254: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
255: * @return Checkbox instance.
256: * @throws TimeoutExpiredException
257: */
258: public static Checkbox waitCheckbox(Container cont,
259: ComponentChooser chooser) {
260: return (waitCheckbox(cont, chooser, 0));
261: }
262:
263: /**
264: * Waits Checkbox by text.
265: * @param cont Container to search component in.
266: * @param text Checkbox text. If null, contents is not checked.
267: * @param ce Compare text exactly.
268: * @param ccs Compare text case sensitively.
269: * @param index Ordinal component index.
270: * @return Checkbox instance.
271: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
272: * @throws TimeoutExpiredException
273: */
274: public static Checkbox waitCheckbox(Container cont, String text,
275: boolean ce, boolean ccs, int index) {
276: return (waitCheckbox(cont, new CheckboxByLabelFinder(text,
277: new DefaultStringComparator(ce, ccs)), index));
278: }
279:
280: /**
281: * Waits Checkbox by text.
282: * @param cont Container to search component in.
283: * @param text Checkbox text. If null, contents is not checked.
284: * @param ce Compare text exactly.
285: * @param ccs Compare text case sensitively.
286: * @return Checkbox instance.
287: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
288: * @throws TimeoutExpiredException
289: */
290: public static Checkbox waitCheckbox(Container cont, String text,
291: boolean ce, boolean ccs) {
292: return (waitCheckbox(cont, text, ce, ccs, 0));
293: }
294:
295: public void setOutput(TestOut out) {
296: output = out;
297: super .setOutput(output.createErrorOutput());
298: }
299:
300: public TestOut getOutput() {
301: return (output);
302: }
303:
304: public void copyEnvironment(Operator anotherOperator) {
305: super .copyEnvironment(anotherOperator);
306: driver = (ButtonDriver) DriverManager.getDriver(
307: DriverManager.BUTTON_DRIVER_ID, getClass(),
308: anotherOperator.getProperties());
309: }
310:
311: /**
312: * Changes selection if necessary.
313: * Uses a ButtonDriver registered for this operator.
314: * @param newValue a button selection.
315: */
316: public void changeSelection(boolean newValue) {
317: makeComponentVisible();
318: if (getState() != newValue) {
319: try {
320: waitComponentEnabled();
321: } catch (InterruptedException e) {
322: throw (new JemmyException("Interrupted!", e));
323: }
324: output.printLine("Change checkbox selection to "
325: + (newValue ? "true" : "false") + "\n :"
326: + toStringSource());
327: output.printGolden("Change checkbox selection to "
328: + (newValue ? "true" : "false"));
329: driver.push(this );
330: if (getVerification()) {
331: waitSelected(newValue);
332: }
333: }
334: }
335:
336: /**
337: * Runs <code>changeSelection(boolean)</code> method in a separate thread.
338: * @param selected a button selection.
339: */
340: public void changeSelectionNoBlock(boolean selected) {
341: produceNoBlocking(new NoBlockingAction(
342: "Button selection changing") {
343: public Object doAction(Object param) {
344: changeSelection(((Boolean) param).booleanValue());
345: return (null);
346: }
347: }, selected ? Boolean.TRUE : Boolean.FALSE);
348: }
349:
350: /**
351: * Waits for button to be selected.
352: * @param selected selection.
353: */
354: public void waitSelected(final boolean selected) {
355: getOutput().printLine(
356: "Wait button to be selected \n : "
357: + toStringSource());
358: getOutput().printGolden("Wait button to be selected");
359: waitState(new ComponentChooser() {
360: public boolean checkComponent(Component comp) {
361: return (getState() == selected);
362: }
363:
364: public String getDescription() {
365: return ("Items has been " + (selected ? "" : "un") + "selected");
366: }
367: });
368: }
369:
370: /**
371: * Returns information about component.
372: */
373: public Hashtable getDump() {
374: Hashtable result = super .getDump();
375: result.put(TEXT_DPROP, ((Checkbox) getSource()).getLabel());
376: return (result);
377: }
378:
379: ////////////////////////////////////////////////////////
380: //Mapping //
381:
382: /**Maps <code>Checkbox.addItemListener(ItemListener)</code> through queue*/
383: public void addItemListener(final ItemListener itemListener) {
384: runMapping(new MapVoidAction("addItemListener") {
385: public void map() {
386: ((Checkbox) getSource()).addItemListener(itemListener);
387: }
388: });
389: }
390:
391: /**Maps <code>Checkbox.getCheckboxGroup()</code> through queue*/
392: public CheckboxGroup getCheckboxGroup() {
393: return ((CheckboxGroup) runMapping(new MapAction(
394: "getCheckboxGroup") {
395: public Object map() {
396: return (((Checkbox) getSource()).getCheckboxGroup());
397: }
398: }));
399: }
400:
401: /**Maps <code>Checkbox.getLabel()</code> through queue*/
402: public String getLabel() {
403: return ((String) runMapping(new MapAction("getLabel") {
404: public Object map() {
405: return (((Checkbox) getSource()).getLabel());
406: }
407: }));
408: }
409:
410: /**Maps <code>Checkbox.getState()</code> through queue*/
411: public boolean getState() {
412: return (runMapping(new MapBooleanAction("getState") {
413: public boolean map() {
414: return (((Checkbox) getSource()).getState());
415: }
416: }));
417: }
418:
419: /**Maps <code>Checkbox.removeItemListener(ItemListener)</code> through queue*/
420: public void removeItemListener(final ItemListener itemListener) {
421: runMapping(new MapVoidAction("removeItemListener") {
422: public void map() {
423: ((Checkbox) getSource())
424: .removeItemListener(itemListener);
425: }
426: });
427: }
428:
429: /**Maps <code>Checkbox.setCheckboxGroup(CheckboxGroup)</code> through queue*/
430: public void setCheckboxGroup(final CheckboxGroup grp) {
431: runMapping(new MapVoidAction("setCheckboxGroup") {
432: public void map() {
433: ((Checkbox) getSource()).setCheckboxGroup(grp);
434: }
435: });
436: }
437:
438: /**Maps <code>Checkbox.setLabel(String)</code> through queue*/
439: public void setLabel(final String string) {
440: runMapping(new MapVoidAction("setLabel") {
441: public void map() {
442: ((Checkbox) getSource()).setLabel(string);
443: }
444: });
445: }
446:
447: /**Maps <code>Checkbox.setState(boolean)</code> through queue*/
448: public void setState(final boolean state) {
449: runMapping(new MapVoidAction("setState") {
450: public void map() {
451: ((Checkbox) getSource()).setState(state);
452: }
453: });
454: }
455:
456: //End of mapping //
457: ////////////////////////////////////////////////////////
458:
459: /**
460: * Allows to find component by label.
461: */
462: public static class CheckboxByLabelFinder implements
463: ComponentChooser {
464: String label;
465: StringComparator comparator;
466:
467: /**
468: * Constructs CheckboxByLabelFinder.
469: * @param lb a label pattern
470: * @param comparator specifies string comparision algorithm.
471: */
472: public CheckboxByLabelFinder(String lb,
473: StringComparator comparator) {
474: label = lb;
475: this .comparator = comparator;
476: }
477:
478: /**
479: * Constructs CheckboxByLabelFinder.
480: * @param lb a label pattern
481: */
482: public CheckboxByLabelFinder(String lb) {
483: this (lb, Operator.getDefaultStringComparator());
484: }
485:
486: public boolean checkComponent(Component comp) {
487: if (comp instanceof Checkbox) {
488: if (((Checkbox) comp).getLabel() != null) {
489: return (comparator.equals(((Checkbox) comp)
490: .getLabel(), label));
491: }
492: }
493: return (false);
494: }
495:
496: public String getDescription() {
497: return ("Checkbox with label \"" + label + "\"");
498: }
499: }
500:
501: /**
502: * Checks component type.
503: */
504: public static class CheckboxFinder extends Finder {
505: /**
506: * Constructs CheckboxFinder.
507: * @param sf other searching criteria.
508: */
509: public CheckboxFinder(ComponentChooser sf) {
510: super (Checkbox.class, sf);
511: }
512:
513: /**
514: * Constructs CheckboxFinder.
515: */
516: public CheckboxFinder() {
517: super (Checkbox.class);
518: }
519: }
520: }
|