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: ChoiceOperator.java,v 1.11 2007/10/05 11:35:24 jskrivanek Exp $ $Revision: 1.11 $ $Date: 2007/10/05 11:35:24 $
044: *
045: */
046:
047: package org.netbeans.jemmy.operators;
048:
049: import org.netbeans.jemmy.Action;
050: import org.netbeans.jemmy.ComponentChooser;
051: import org.netbeans.jemmy.ComponentSearcher;
052: import org.netbeans.jemmy.JemmyException;
053: import org.netbeans.jemmy.JemmyProperties;
054: import org.netbeans.jemmy.Outputable;
055: import org.netbeans.jemmy.TestOut;
056: import org.netbeans.jemmy.TimeoutExpiredException;
057: import org.netbeans.jemmy.Timeouts;
058:
059: import org.netbeans.jemmy.drivers.ListDriver;
060: import org.netbeans.jemmy.drivers.DriverManager;
061:
062: import java.awt.Choice;
063: import java.awt.Component;
064: import java.awt.Container;
065:
066: import java.awt.event.ItemListener;
067: import java.awt.event.KeyEvent;
068:
069: import java.util.Hashtable;
070:
071: /**
072: *
073: * <BR><BR>Timeouts used: <BR>
074: * ButtonOperator.PushButtonTimeout - time between choice pressing and releasing<BR>
075: * ComponentOperator.WaitComponentTimeout - time to wait choice displayed <BR>
076: * ComponentOperator.WaitComponentEnabledTimeout - time to wait choice enabled <BR>.
077: *
078: * @see org.netbeans.jemmy.Timeouts
079: *
080: * @author Alexandre Iline (alexandre.iline@sun.com)
081: *
082: */
083:
084: public class ChoiceOperator extends ComponentOperator implements
085: Outputable {
086:
087: /**
088: * Identifier for a selected item property.
089: * @see #getDump
090: */
091: public static final String SELECTED_ITEM_DPROP = "Selected item";
092:
093: /**
094: * Identifier for a items properties.
095: * @see #getDump
096: */
097: public static final String ITEM_PREFIX_DPROP = "Item";
098:
099: private TestOut output;
100: private ListDriver driver;
101:
102: /**
103: * Constructor.
104: * @param b a component
105: */
106: public ChoiceOperator(Choice b) {
107: super (b);
108: driver = DriverManager.getListDriver(getClass());
109: }
110:
111: /**
112: * Constructs a ChoiceOperator object.
113: * @param cont container
114: * @param chooser a component chooser specifying searching criteria.
115: * @param index an index between appropriate ones.
116: */
117: public ChoiceOperator(ContainerOperator cont,
118: ComponentChooser chooser, int index) {
119: this ((Choice) cont.waitSubComponent(new ChoiceFinder(chooser),
120: index));
121: copyEnvironment(cont);
122: }
123:
124: /**
125: * Constructs a ChoiceOperator object.
126: * @param cont container
127: * @param chooser a component chooser specifying searching criteria.
128: */
129: public ChoiceOperator(ContainerOperator cont,
130: ComponentChooser chooser) {
131: this (cont, chooser, 0);
132: }
133:
134: /**
135: * Constructor.
136: * Waits component in container first.
137: * Uses cont's timeout and output for waiting and to init operator.
138: * @param cont container
139: * @param text Choice text.
140: * @param index Ordinal component index.
141: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
142: * @throws TimeoutExpiredException
143: */
144: public ChoiceOperator(ContainerOperator cont, String text, int index) {
145: this ((Choice) waitComponent(cont,
146: new ChoiceBySelectedItemFinder(text, cont
147: .getComparator()), index));
148: copyEnvironment(cont);
149: }
150:
151: /**
152: * Constructor.
153: * Waits component in container first.
154: * Uses cont's timeout and output for waiting and to init operator.
155: * @param cont container
156: * @param text Choice text.
157: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
158: * @throws TimeoutExpiredException
159: */
160: public ChoiceOperator(ContainerOperator cont, String text) {
161: this (cont, text, 0);
162: }
163:
164: /**
165: * Constructor.
166: * Waits component in container first.
167: * Uses cont's timeout and output for waiting and to init operator.
168: * @param cont container
169: * @param index Ordinal component index.
170: * @throws TimeoutExpiredException
171: */
172: public ChoiceOperator(ContainerOperator cont, int index) {
173: this ((Choice) waitComponent(cont, new ChoiceFinder(), index));
174: copyEnvironment(cont);
175: }
176:
177: /**
178: * Constructor.
179: * Waits component in container first.
180: * Uses cont's timeout and output for waiting and to init operator.
181: * @param cont container
182: * @throws TimeoutExpiredException
183: */
184: public ChoiceOperator(ContainerOperator cont) {
185: this (cont, 0);
186: }
187:
188: /**
189: * Searches Choice in container.
190: * @param cont Container to search component in.
191: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
192: * @param index Ordinal component index.
193: * @return Choice instance or null if component was not found.
194: */
195: public static Choice findChoice(Container cont,
196: ComponentChooser chooser, int index) {
197: return ((Choice) findComponent(cont, new ChoiceFinder(chooser),
198: index));
199: }
200:
201: /**
202: * Searches 0'th Choice in container.
203: * @param cont Container to search component in.
204: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
205: * @return Choice instance or null if component was not found.
206: */
207: public static Choice findChoice(Container cont,
208: ComponentChooser chooser) {
209: return (findChoice(cont, chooser, 0));
210: }
211:
212: /**
213: * Searches Choice by text.
214: * @param cont Container to search component in.
215: * @param text Choice text. If null, contents is not checked.
216: * @param ce Compare text exactly.
217: * @param ccs Compare text case sensitively.
218: * @param index Ordinal component index.
219: * @return Choice instance or null if component was not found.
220: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
221: */
222: public static Choice findChoice(Container cont, String text,
223: boolean ce, boolean ccs, int index) {
224: return (findChoice(cont, new ChoiceBySelectedItemFinder(text,
225: new DefaultStringComparator(ce, ccs)), index));
226: }
227:
228: /**
229: * Searches Choice by text.
230: * @param cont Container to search component in.
231: * @param text Choice text. If null, contents is not checked.
232: * @param ce Compare text exactly.
233: * @param ccs Compare text case sensitively.
234: * @return Choice instance or null if component was not found.
235: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
236: */
237: public static Choice findChoice(Container cont, String text,
238: boolean ce, boolean ccs) {
239: return (findChoice(cont, text, ce, ccs, 0));
240: }
241:
242: /**
243: * Waits Choice in container.
244: * @param cont Container to search component in.
245: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
246: * @param index Ordinal component index.
247: * @return Choice instance.
248: * @throws TimeoutExpiredException
249: */
250: public static Choice waitChoice(Container cont,
251: ComponentChooser chooser, int index) {
252: return ((Choice) waitComponent(cont, new ChoiceFinder(chooser),
253: index));
254: }
255:
256: /**
257: * Waits 0'th Choice in container.
258: * @param cont Container to search component in.
259: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
260: * @return Choice instance.
261: * @throws TimeoutExpiredException
262: */
263: public static Choice waitChoice(Container cont,
264: ComponentChooser chooser) {
265: return (waitChoice(cont, chooser, 0));
266: }
267:
268: /**
269: * Waits Choice by text.
270: * @param cont Container to search component in.
271: * @param text Choice text. If null, contents is not checked.
272: * @param ce Compare text exactly.
273: * @param ccs Compare text case sensitively.
274: * @param index Ordinal component index.
275: * @return Choice instance.
276: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
277: * @throws TimeoutExpiredException
278: */
279: public static Choice waitChoice(Container cont, String text,
280: boolean ce, boolean ccs, int index) {
281: return (waitChoice(cont, new ChoiceBySelectedItemFinder(text,
282: new DefaultStringComparator(ce, ccs)), index));
283: }
284:
285: /**
286: * Waits Choice by text.
287: * @param cont Container to search component in.
288: * @param text Choice text. If null, contents is not checked.
289: * @param ce Compare text exactly.
290: * @param ccs Compare text case sensitively.
291: * @return Choice instance.
292: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
293: * @throws TimeoutExpiredException
294: */
295: public static Choice waitChoice(Container cont, String text,
296: boolean ce, boolean ccs) {
297: return (waitChoice(cont, text, ce, ccs, 0));
298: }
299:
300: public void setOutput(TestOut out) {
301: output = out;
302: super .setOutput(output.createErrorOutput());
303: }
304:
305: public TestOut getOutput() {
306: return (output);
307: }
308:
309: public void copyEnvironment(Operator anotherOperator) {
310: super .copyEnvironment(anotherOperator);
311: driver = (ListDriver) DriverManager.getDriver(
312: DriverManager.LIST_DRIVER_ID, getClass(),
313: anotherOperator.getProperties());
314: }
315:
316: /**
317: * Finds an item between choice items.
318: * @param item a text pattern.
319: * @param index an ordinal index between appropriate items.
320: * @return an item index.
321: */
322: public int findItemIndex(String item, int index) {
323: return (findItemIndex(item, getComparator(), index));
324: }
325:
326: /**
327: * Finds an item between choice items.
328: * @param item a text pattern.
329: * @return an item index.
330: */
331: public int findItemIndex(String item) {
332: return (findItemIndex(item, 0));
333: }
334:
335: /**
336: * Selects an item by text.
337: * @param item a text pattern.
338: * @param index an ordinal index between appropriate items.
339: */
340: public void selectItem(String item, int index) {
341: selectItem(item, getComparator(), index);
342: }
343:
344: /**
345: * Selects an item by text.
346: * @param item a text pattern.
347: */
348: public void selectItem(String item) {
349: selectItem(item, 0);
350: }
351:
352: /**
353: * Selects an item by index.
354: * @param index an item index.
355: */
356: public void selectItem(int index) {
357: output.printLine("Select " + Integer.toString(index)
358: + "`th item in combobox\n : " + toStringSource());
359: output.printGolden("Select " + Integer.toString(index)
360: + "`th item in combobox");
361: makeComponentVisible();
362: try {
363: waitComponentEnabled();
364: } catch (InterruptedException e) {
365: throw (new JemmyException("Interrupted!", e));
366: }
367: driver.selectItem(this , index);
368: if (getVerification()) {
369: waitItemSelected(index);
370: }
371: }
372:
373: /**
374: * Waits for item to be selected.
375: * @param index Item index.
376: */
377: public void waitItemSelected(final int index) {
378: getOutput()
379: .printLine(
380: "Wait "
381: + Integer.toString(index)
382: + "'th item to be selected in component \n : "
383: + toStringSource());
384: getOutput().printGolden(
385: "Wait " + Integer.toString(index)
386: + "'th item to be selected");
387: waitState(new ComponentChooser() {
388: public boolean checkComponent(Component comp) {
389: return (getSelectedIndex() == index);
390: }
391:
392: public String getDescription() {
393: return ("Has " + Integer.toString(index) + "'th item selected");
394: }
395: });
396: }
397:
398: /**
399: * Returns information about component.
400: */
401: public Hashtable getDump() {
402: Hashtable result = super .getDump();
403: if (((Choice) getSource()).getSelectedItem() != null) {
404: result.put(SELECTED_ITEM_DPROP, ((Choice) getSource())
405: .getSelectedItem());
406: }
407: String[] items = new String[((Choice) getSource())
408: .getItemCount()];
409: for (int i = 0; i < ((Choice) getSource()).getItemCount(); i++) {
410: items[i] = ((Choice) getSource()).getItem(i);
411: }
412: addToDump(result, ITEM_PREFIX_DPROP, items);
413: return (result);
414: }
415:
416: ////////////////////////////////////////////////////////
417: //Mapping //
418:
419: /**Maps <code>Choice.add(String)</code> through queue*/
420: public void add(final String item) {
421: runMapping(new MapVoidAction("add") {
422: public void map() {
423: ((Choice) getSource()).add(item);
424: }
425: });
426: }
427:
428: /**Maps <code>Choice.addItemListener(ItemListener)</code> through queue*/
429: public void addItemListener(final ItemListener itemListener) {
430: runMapping(new MapVoidAction("addItemListener") {
431: public void map() {
432: ((Choice) getSource()).addItemListener(itemListener);
433: }
434: });
435: }
436:
437: /**Maps <code>Choice.addNotify()</code> through queue*/
438: public void addNotify() {
439: runMapping(new MapVoidAction("addNotify") {
440: public void map() {
441: ((Choice) getSource()).addNotify();
442: }
443: });
444: }
445:
446: /**Maps <code>Choice.getItem(int)</code> through queue*/
447: public String getItem(final int index) {
448: return ((String) runMapping(new MapAction("getItem") {
449: public Object map() {
450: return (((Choice) getSource()).getItem(index));
451: }
452: }));
453: }
454:
455: /**Maps <code>Choice.getItemCount()</code> through queue*/
456: public int getItemCount() {
457: return (runMapping(new MapIntegerAction("getItemCount") {
458: public int map() {
459: return (((Choice) getSource()).getItemCount());
460: }
461: }));
462: }
463:
464: /**Maps <code>Choice.getSelectedIndex()</code> through queue*/
465: public int getSelectedIndex() {
466: return (runMapping(new MapIntegerAction("getSelectedIndex") {
467: public int map() {
468: return (((Choice) getSource()).getSelectedIndex());
469: }
470: }));
471: }
472:
473: /**Maps <code>Choice.getSelectedItem()</code> through queue*/
474: public String getSelectedItem() {
475: return ((String) runMapping(new MapAction("getSelectedItem") {
476: public Object map() {
477: return (((Choice) getSource()).getSelectedItem());
478: }
479: }));
480: }
481:
482: /**Maps <code>Choice.insert(String)</code> through queue*/
483: public void insert(final String item, final int index) {
484: runMapping(new MapVoidAction("insert") {
485: public void map() {
486: ((Choice) getSource()).insert(item, index);
487: }
488: });
489: }
490:
491: /**Maps <code>Choice.remove(int)</code> through queue*/
492: public void remove(final int position) {
493: runMapping(new MapVoidAction("remove") {
494: public void map() {
495: ((Choice) getSource()).remove(position);
496: }
497: });
498: }
499:
500: /**Maps <code>Choice.remove(String)</code> through queue*/
501: public void remove(final String item) {
502: runMapping(new MapVoidAction("remove") {
503: public void map() {
504: ((Choice) getSource()).remove(item);
505: }
506: });
507: }
508:
509: /**Maps <code>Choice.removeAll()</code> through queue*/
510: public void removeAll() {
511: runMapping(new MapVoidAction("removeAll") {
512: public void map() {
513: ((Choice) getSource()).removeAll();
514: }
515: });
516: }
517:
518: /**Maps <code>Choice.removeItemListener(ItemListener)</code> through queue*/
519: public void removeItemListener(final ItemListener itemListener) {
520: runMapping(new MapVoidAction("removeItemListener") {
521: public void map() {
522: ((Choice) getSource()).removeItemListener(itemListener);
523: }
524: });
525: }
526:
527: /**Maps <code>Choice.select(int)</code> through queue*/
528: public void select(final int pos) {
529: runMapping(new MapVoidAction("select") {
530: public void map() {
531: ((Choice) getSource()).select(pos);
532: }
533: });
534: }
535:
536: /**Maps <code>Choice.select(String)</code> through queue*/
537: public void setState(final String str) {
538: runMapping(new MapVoidAction("select") {
539: public void map() {
540: ((Choice) getSource()).select(str);
541: }
542: });
543: }
544:
545: //End of mapping //
546: ////////////////////////////////////////////////////////
547:
548: private int findItemIndex(String item, StringComparator comparator,
549: int index) {
550: int count = 0;
551: for (int i = 0; i < getItemCount(); i++) {
552: if (comparator.equals(getItem(i), item)) {
553: if (count == index) {
554: return (i);
555: } else {
556: count++;
557: }
558: }
559: }
560: return (-1);
561: }
562:
563: private void selectItem(String item, StringComparator comparator,
564: int index) {
565: selectItem(findItemIndex(item, comparator, index));
566: }
567:
568: /**
569: * Allows to find component by label.
570: */
571: public static class ChoiceBySelectedItemFinder implements
572: ComponentChooser {
573: String label;
574: StringComparator comparator;
575:
576: /**
577: * Constructs ChoiceBySelectedItemFinder.
578: * @param lb a text pattern
579: * @param comparator specifies string comparision algorithm.
580: */
581: public ChoiceBySelectedItemFinder(String lb,
582: StringComparator comparator) {
583: label = lb;
584: this .comparator = comparator;
585: }
586:
587: /**
588: * Constructs ChoiceBySelectedItemFinder.
589: * @param lb a text pattern
590: */
591: public ChoiceBySelectedItemFinder(String lb) {
592: this (lb, Operator.getDefaultStringComparator());
593: }
594:
595: public boolean checkComponent(Component comp) {
596: if (comp instanceof Choice) {
597: if (((Choice) comp).getSelectedItem() != null) {
598: return (comparator.equals(((Choice) comp)
599: .getSelectedItem(), label));
600: }
601: }
602: return (false);
603: }
604:
605: public String getDescription() {
606: return ("Choice with label \"" + label + "\"");
607: }
608: }
609:
610: /**
611: * Checks component type.
612: */
613: public static class ChoiceFinder extends Finder {
614: /**
615: * Constructs ChoiceFinder.
616: * @param sf other searching criteria.
617: */
618: public ChoiceFinder(ComponentChooser sf) {
619: super (Choice.class, sf);
620: }
621:
622: /**
623: * Constructs ChoiceFinder.
624: */
625: public ChoiceFinder() {
626: super (Choice.class);
627: }
628: }
629: }
|