0001: /*
0002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003: *
0004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
0005: *
0006: * The contents of this file are subject to the terms of either the GNU
0007: * General Public License Version 2 only ("GPL") or the Common
0008: * Development and Distribution License("CDDL") (collectively, the
0009: * "License"). You may not use this file except in compliance with the
0010: * License. You can obtain a copy of the License at
0011: * http://www.netbeans.org/cddl-gplv2.html
0012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
0013: * specific language governing permissions and limitations under the
0014: * License. When distributing the software, include this License Header
0015: * Notice in each file and include the License file at
0016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
0017: * particular file as subject to the "Classpath" exception as provided
0018: * by Sun in the GPL Version 2 section of the License file that
0019: * accompanied this code. If applicable, add the following below the
0020: * License Header, with the fields enclosed by brackets [] replaced by
0021: * your own identifying information:
0022: * "Portions Copyrighted [year] [name of copyright owner]"
0023: *
0024: * Contributor(s): Alexandre Iline.
0025: *
0026: * The Original Software is the Jemmy library.
0027: * The Initial Developer of the Original Software is Alexandre Iline.
0028: * All Rights Reserved.
0029: *
0030: * If you wish your version of this file to be governed by only the CDDL
0031: * or only the GPL Version 2, indicate your decision by adding
0032: * "[Contributor] elects to include this software in this distribution
0033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
0034: * single choice of license, a recipient has the option to distribute
0035: * your version of this file under either the CDDL, the GPL Version 2 or
0036: * to extend the choice of license to its licensees as provided above.
0037: * However, if you add GPL Version 2 code and therefore, elected the GPL
0038: * Version 2 license, then the option applies only if the new code is
0039: * made subject to such option by the copyright holder.
0040: *
0041: *
0042: *
0043: * $Id: JComboBoxOperator.java,v 1.15 2007/10/05 11:35:37 jskrivanek Exp $ $Revision: 1.15 $ $Date: 2007/10/05 11:35:37 $
0044: *
0045: */
0046:
0047: package org.netbeans.jemmy.operators;
0048:
0049: import org.netbeans.jemmy.ComponentChooser;
0050: import org.netbeans.jemmy.ComponentSearcher;
0051: import org.netbeans.jemmy.JemmyException;
0052: import org.netbeans.jemmy.Outputable;
0053: import org.netbeans.jemmy.TestOut;
0054: import org.netbeans.jemmy.Timeoutable;
0055: import org.netbeans.jemmy.Timeouts;
0056: import org.netbeans.jemmy.Waiter;
0057: import org.netbeans.jemmy.WindowWaiter;
0058: import org.netbeans.jemmy.util.EmptyVisualizer;
0059: import org.netbeans.jemmy.drivers.ListDriver;
0060: import org.netbeans.jemmy.drivers.DriverManager;
0061:
0062: import java.awt.Component;
0063: import java.awt.Container;
0064: import java.awt.Window;
0065:
0066: import java.awt.event.ActionEvent;
0067: import java.awt.event.ActionListener;
0068: import java.awt.event.ItemListener;
0069: import java.awt.event.KeyEvent;
0070:
0071: import java.util.Hashtable;
0072:
0073: import javax.swing.ComboBoxEditor;
0074: import javax.swing.ComboBoxModel;
0075: import javax.swing.JComboBox.KeySelectionManager;
0076: import javax.swing.JButton;
0077: import javax.swing.JComboBox;
0078: import javax.swing.JList;
0079: import javax.swing.JTextField;
0080: import javax.swing.ListCellRenderer;
0081:
0082: import javax.swing.event.ListDataEvent;
0083:
0084: import javax.swing.plaf.ComboBoxUI;
0085:
0086: import javax.swing.plaf.basic.ComboPopup;
0087:
0088: /**
0089: * <BR><BR>Timeouts used: <BR>
0090: * JComboBoxOperator.BeforeSelectingTimeout - time to sleep after list opened and before item selected <BR>
0091: * JComboBoxOperator.WaitListTimeout - time to wait list opened <BR>
0092: * ComponentOperator.WaitComponentTimeout - time to wait component displayed <BR>
0093: * ComponentOperator.WaitComponentEnabledTimeout - time to wait component enabled <BR>
0094: * ComponentOperator.WaitStateTimeout - time to wait for item to be selected <BR>
0095: * AbstractButtonOperator.PushButtonTimeout - time between combo button pressing and releasing<BR>
0096: * ComponentOperator.MouseClickTimeout - time between mouse pressing and releasing during item selecting<BR>
0097: * JTextComponentOperator.PushKeyTimeout - time between key pressing and releasing during text typing <BR>
0098: * JTextComponentOperator.BetweenKeysTimeout - time to sleep between two chars typing <BR>
0099: * JTextComponentOperator.ChangeCaretPositionTimeout - maximum time to chenge caret position <BR>
0100: * JTextComponentOperator.TypeTextTimeout - maximum time to type text <BR>.
0101: *
0102: * @see Timeouts
0103: *
0104: * @author Alexandre Iline (alexandre.iline@sun.com)
0105: *
0106: */
0107:
0108: public class JComboBoxOperator extends JComponentOperator implements
0109: Timeoutable, Outputable {
0110:
0111: /**
0112: * Identifier for a "text" property.
0113: * @see #getDump
0114: */
0115: public static final String TEXT_DPROP = "Text";
0116:
0117: /**
0118: * Identifier for a "item" property values.
0119: * @see #getDump
0120: */
0121: public static final String ITEM_PREFIX_DPROP = "Item";
0122:
0123: private final static long BEFORE_SELECTING_TIMEOUT = 0;
0124: private final static long WAIT_LIST_TIMEOUT = 60000;
0125:
0126: private TestOut output;
0127: private Timeouts timeouts;
0128: private ComponentSearcher searcher;
0129:
0130: private JButtonOperator button;
0131: private JTextFieldOperator text;
0132:
0133: ListDriver driver;
0134:
0135: /**
0136: * Constructs a JComboBoxOperator object.
0137: * @param b a component
0138: */
0139: public JComboBoxOperator(JComboBox b) {
0140: super (b);
0141: driver = DriverManager.getListDriver(getClass());
0142: }
0143:
0144: /**
0145: * Constructs a JComboBoxOperator object.
0146: * @param cont a container
0147: * @param chooser a component chooser specifying searching criteria.
0148: * @param index an index between appropriate ones.
0149: */
0150: public JComboBoxOperator(ContainerOperator cont,
0151: ComponentChooser chooser, int index) {
0152: this ((JComboBox) cont.waitSubComponent(new JComboBoxFinder(
0153: chooser), index));
0154: copyEnvironment(cont);
0155: }
0156:
0157: /**
0158: * Constructs a JComboBoxOperator object.
0159: * @param cont a container
0160: * @param chooser a component chooser specifying searching criteria.
0161: */
0162: public JComboBoxOperator(ContainerOperator cont,
0163: ComponentChooser chooser) {
0164: this (cont, chooser, 0);
0165: }
0166:
0167: /**
0168: * Constructor.
0169: * Waits component in container first.
0170: * Uses cont's timeout and output for waiting and to init operator.
0171: * @param cont a container
0172: * @param text Text of item which is currently selected.
0173: * @param index Ordinal component index.
0174: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
0175: * @throws TimeoutExpiredException
0176: */
0177: public JComboBoxOperator(ContainerOperator cont, String text,
0178: int index) {
0179: this ((JComboBox) waitComponent(cont, new JComboBoxByItemFinder(
0180: text, -1, cont.getComparator()), index));
0181: copyEnvironment(cont);
0182: }
0183:
0184: /**
0185: * Constructor.
0186: * Waits component in container first.
0187: * Uses cont's timeout and output for waiting and to init operator.
0188: * @param cont a container
0189: * @param text Text of item which is currently selected.
0190: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
0191: * @throws TimeoutExpiredException
0192: */
0193: public JComboBoxOperator(ContainerOperator cont, String text) {
0194: this (cont, text, 0);
0195: }
0196:
0197: /**
0198: * Constructor.
0199: * Waits component in container first.
0200: * Uses cont's timeout and output for waiting and to init operator.
0201: * @param cont a container
0202: * @param index Ordinal component index.
0203: * @throws TimeoutExpiredException
0204: */
0205: public JComboBoxOperator(ContainerOperator cont, int index) {
0206: this ((JComboBox) waitComponent(cont, new JComboBoxFinder(),
0207: index));
0208: copyEnvironment(cont);
0209: }
0210:
0211: /**
0212: * Constructor.
0213: * Waits component in container first.
0214: * Uses cont's timeout and output for waiting and to init operator.
0215: * @param cont a container
0216: * @throws TimeoutExpiredException
0217: */
0218: public JComboBoxOperator(ContainerOperator cont) {
0219: this (cont, 0);
0220: }
0221:
0222: /**
0223: * Searches JComboBox in container.
0224: * @param cont Container to search component in.
0225: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
0226: * @param index Ordinal component index.
0227: * @return JComboBox instance or null if component was not found.
0228: */
0229: public static JComboBox findJComboBox(Container cont,
0230: ComponentChooser chooser, int index) {
0231: return ((JComboBox) findComponent(cont, new JComboBoxFinder(
0232: chooser), index));
0233: }
0234:
0235: /**
0236: * Searches 0'th JComboBox in container.
0237: * @param cont Container to search component in.
0238: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
0239: * @return JComboBox instance or null if component was not found.
0240: */
0241: public static JComboBox findJComboBox(Container cont,
0242: ComponentChooser chooser) {
0243: return (findJComboBox(cont, chooser, 0));
0244: }
0245:
0246: /**
0247: * Searches JComboBox by item.
0248: * @param cont Container to search component in.
0249: * @param text Item text. If null, contents is not checked.
0250: * @param ce Compare text exactly.
0251: * @param ccs Compare text case sensitively.
0252: * @param itemIndex Index of item to compare text. If -1, selected item is checked.
0253: * @param index Ordinal component index.
0254: * @return JComboBox instance or null if component was not found.
0255: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
0256: */
0257: public static JComboBox findJComboBox(Container cont, String text,
0258: boolean ce, boolean ccs, int itemIndex, int index) {
0259: return (findJComboBox(cont, new JComboBoxByItemFinder(text,
0260: itemIndex, new DefaultStringComparator(ce, ccs)), index));
0261: }
0262:
0263: /**
0264: * Searches JComboBox by item.
0265: * @param cont Container to search component in.
0266: * @param text Item text. If null, contents is not checked.
0267: * @param ce Compare text exactly.
0268: * @param ccs Compare text case sensitively.
0269: * @param itemIndex Index of item to compare text. If -1, selected item is checked.
0270: * @return JComboBox instance or null if component was not found.
0271: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
0272: */
0273: public static JComboBox findJComboBox(Container cont, String text,
0274: boolean ce, boolean ccs, int itemIndex) {
0275: return (findJComboBox(cont, text, ce, ccs, itemIndex, 0));
0276: }
0277:
0278: /**
0279: * Waits JComboBox in container.
0280: * @param cont Container to search component in.
0281: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
0282: * @param index Ordinal component index.
0283: * @return JComboBox instance or null if component was not found.
0284: * @throws TimeoutExpiredException
0285: */
0286: public static JComboBox waitJComboBox(Container cont,
0287: ComponentChooser chooser, int index) {
0288: return ((JComboBox) waitComponent(cont, new JComboBoxFinder(
0289: chooser), index));
0290: }
0291:
0292: /**
0293: * Waits 0'th JComboBox in container.
0294: * @param cont Container to search component in.
0295: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
0296: * @return JComboBox instance or null if component was not found.
0297: * @throws TimeoutExpiredException
0298: */
0299: public static JComboBox waitJComboBox(Container cont,
0300: ComponentChooser chooser) {
0301: return (waitJComboBox(cont, chooser, 0));
0302: }
0303:
0304: /**
0305: * Waits JComboBox by item.
0306: * @param cont Container to search component in.
0307: * @param text Item text. If null, contents is not checked.
0308: * @param ce Compare text exactly.
0309: * @param ccs Compare text case sensitively.
0310: * @param itemIndex Index of item to compare text. If -1, selected item is checked.
0311: * @param index Ordinal component index.
0312: * @return JComboBox instance or null if component was not found.
0313: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
0314: * @throws TimeoutExpiredException
0315: */
0316: public static JComboBox waitJComboBox(Container cont, String text,
0317: boolean ce, boolean ccs, int itemIndex, int index) {
0318: return (waitJComboBox(cont, new JComboBoxByItemFinder(text,
0319: itemIndex, new DefaultStringComparator(ce, ccs)), index));
0320: }
0321:
0322: /**
0323: * Waits JComboBox by item.
0324: * @param cont Container to search component in.
0325: * @param text Item text. If null, contents is not checked.
0326: * @param ce Compare text exactly.
0327: * @param ccs Compare text case sensitively.
0328: * @param itemIndex Index of item to compare text. If -1, selected item is checked.
0329: * @return JComboBox instance or null if component was not found.
0330: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
0331: * @throws TimeoutExpiredException
0332: */
0333: public static JComboBox waitJComboBox(Container cont, String text,
0334: boolean ce, boolean ccs, int itemIndex) {
0335: return (waitJComboBox(cont, text, ce, ccs, itemIndex, 0));
0336: }
0337:
0338: static {
0339: Timeouts.initDefault(
0340: "JComboBoxOperator.BeforeSelectingTimeout",
0341: BEFORE_SELECTING_TIMEOUT);
0342: Timeouts.initDefault("JComboBoxOperator.WaitListTimeout",
0343: WAIT_LIST_TIMEOUT);
0344: }
0345:
0346: public void setTimeouts(Timeouts timeouts) {
0347: super .setTimeouts(timeouts);
0348: this .timeouts = timeouts;
0349: }
0350:
0351: public Timeouts getTimeouts() {
0352: return (timeouts);
0353: }
0354:
0355: public void setOutput(TestOut output) {
0356: super .setOutput(output);
0357: this .output = output;
0358: }
0359:
0360: public TestOut getOutput() {
0361: return (output);
0362: }
0363:
0364: public void copyEnvironment(Operator anotherOperator) {
0365: super .copyEnvironment(anotherOperator);
0366: driver = (ListDriver) DriverManager.getDriver(
0367: DriverManager.LIST_DRIVER_ID, getClass(),
0368: anotherOperator.getProperties());
0369: }
0370:
0371: /**
0372: * Searches JButton inside component.
0373: * @return JButton which is used to expand this JComboBox.
0374: */
0375: public JButton findJButton() {
0376: return ((JButton) waitSubComponent(new ComponentChooser() {
0377: public boolean checkComponent(Component comp) {
0378: return (comp instanceof JButton);
0379: }
0380:
0381: public String getDescription() {
0382: return ("Button for combobox popup menu opening");
0383: }
0384: }));
0385: }
0386:
0387: /**
0388: * Searches JTextField inside component.
0389: * @return JTextField if JComboBox is editable, null otherwise.
0390: */
0391: public JTextField findJTextField() {
0392: return ((JTextField) waitSubComponent(new ComponentChooser() {
0393: public boolean checkComponent(Component comp) {
0394: return (comp instanceof JTextField);
0395: }
0396:
0397: public String getDescription() {
0398: return ("ComboBox's text field");
0399: }
0400: }));
0401: }
0402:
0403: /**
0404: * Creates an operator for button returned by
0405: * <code>findJButton()</code> method.
0406: * @return new JButtonOperator instance.
0407: */
0408: public JButtonOperator getButton() {
0409: if (button == null) {
0410: button = new JButtonOperator(findJButton());
0411: button.copyEnvironment(this );
0412: button.setOutput(getOutput().createErrorOutput());
0413: }
0414: return (button);
0415: }
0416:
0417: /**
0418: * Creates an operator for button returned by
0419: * <code>findJTextField()</code> method.
0420: * @return new JTextField instance.
0421: */
0422: public JTextFieldOperator getTextField() {
0423: if (((JComboBox) getSource()).isEditable()) {
0424: text = new JTextFieldOperator(findJTextField());
0425: text.copyEnvironment(this );
0426: text.setOutput(getOutput().createErrorOutput());
0427: }
0428: return (text);
0429: }
0430:
0431: /**
0432: * Waits combobox's list to be displayed.
0433: * @return JList object if it was displayed in JComboBoxOperator.WaitListTimeout millisecont,
0434: * null otherwise.
0435: * @throws TimeoutExpiredException
0436: */
0437: public JList waitList() {
0438: Waiter pw = new ListWater();
0439: pw.setOutput(output.createErrorOutput());
0440: Timeouts times = timeouts.cloneThis();
0441: times.setTimeout("Waiter.WaitingTime", times
0442: .getTimeout("JComboBoxOperator.WaitListTimeout"));
0443: pw.setTimeouts(times);
0444: try {
0445: return ((JList) pw.waitAction(null));
0446: } catch (InterruptedException e) {
0447: output.printStackTrace(e);
0448: }
0449: return (null);
0450: }
0451:
0452: /**
0453: * Push combobox's button to expand or collapse combobox.
0454: * @throws TimeoutExpiredException
0455: */
0456: public void pushComboButton() {
0457: makeComponentVisible();
0458: getButton().push();
0459: }
0460:
0461: /**
0462: * Finds an item between list items.
0463: * @param item a text pattern.
0464: * @param comparator a searching criteria.
0465: * @return an item index.
0466: */
0467: public int findItemIndex(String item, StringComparator comparator) {
0468: ComboBoxModel model = getModel();
0469: for (int i = 0; i < model.getSize(); i++) {
0470: if (comparator.equals(model.getElementAt(i).toString(),
0471: item)) {
0472: return (i);
0473: }
0474: }
0475: return (-1);
0476: }
0477:
0478: /**
0479: * Waits for an item available between list items.
0480: * @param item a text pattern.
0481: * @param comparator a searching criteria.
0482: * @return an item index or throws TimeoutExpiredException if item not found.
0483: */
0484: public int waitItem(final String item,
0485: final StringComparator comparator) {
0486: getOutput().printLine(
0487: "Wait item \"" + item
0488: + "\" available in combo box \n : "
0489: + toStringSource());
0490: getOutput().printGolden(
0491: "Wait item \"" + item + "\" available in combo box.");
0492: waitState(new ComponentChooser() {
0493: public boolean checkComponent(Component comp) {
0494: return findItemIndex(item, comparator) > -1;
0495: }
0496:
0497: public String getDescription() {
0498: return "Item \"" + item + "\" available in combo box.";
0499: }
0500: });
0501: return findItemIndex(item, comparator);
0502: }
0503:
0504: /**
0505: * Waits for an item of given index available between list items.
0506: * @param itemIndex index of desired item
0507: * @return an item index or throws TimeoutExpiredException if item not found.
0508: */
0509: public int waitItem(final int itemIndex) {
0510: getOutput().printLine(
0511: "Wait item of index \"" + itemIndex
0512: + "\" available in combo box \n : "
0513: + toStringSource());
0514: getOutput().printGolden(
0515: "Wait item of index \"" + itemIndex
0516: + "\" available in combo box.");
0517: waitState(new ComponentChooser() {
0518: public boolean checkComponent(Component comp) {
0519: // given itemIndex is within size of combo box
0520: return getModel().getSize() > itemIndex;
0521: }
0522:
0523: public String getDescription() {
0524: return "Item \"" + itemIndex
0525: + "\" available in combo box.";
0526: }
0527: });
0528: return itemIndex;
0529: }
0530:
0531: /**
0532: * Selects an item by text.
0533: * @param item a text pattern.
0534: * @param comparator a searching criteria.
0535: */
0536: public void selectItem(String item, StringComparator comparator) {
0537: output.printLine("Select \"" + item
0538: + "\" item in combobox\n : " + toStringSource());
0539: output.printGolden("Select \"" + item + "\" item in combobox");
0540: selectItem(waitItem(item, comparator));
0541: }
0542:
0543: /**
0544: * Selects combobox item.
0545: * @param item Item text.
0546: * @param ce Compare exactly.
0547: * @param cc Compare case sensitivelly.
0548: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
0549: * @throws TimeoutExpiredException
0550: * @deprecated Use selectItem(String) or selectItem(String, StringComparator)
0551: */
0552: public void selectItem(String item, boolean ce, boolean cc) {
0553: selectItem(item, new DefaultStringComparator(ce, cc));
0554: }
0555:
0556: /**
0557: * Selects combobox item.
0558: * Uses StringComparator assigned to this object.
0559: * @param item Item text.
0560: * @throws TimeoutExpiredException
0561: */
0562: public void selectItem(String item) {
0563: selectItem(item, getComparator());
0564: }
0565:
0566: /**
0567: * Selects combobox item.
0568: * If verification mode is on, checks that right item has been selected.
0569: * @param index Item index.
0570: * @throws TimeoutExpiredException
0571: */
0572: public void selectItem(int index) {
0573: output.printLine("Select " + Integer.toString(index)
0574: + "\'th item in combobox\n : " + toStringSource());
0575: output.printGolden("Select " + Integer.toString(index)
0576: + "\'th item in combobox");
0577: try {
0578: waitComponentEnabled();
0579: } catch (InterruptedException e) {
0580: throw new JemmyException("Interrupted", e);
0581: }
0582:
0583: driver.selectItem(this , waitItem(index));
0584:
0585: if (getVerification()) {
0586: waitItemSelected(index);
0587: }
0588: }
0589:
0590: /**
0591: * Types text in the editable combobox.
0592: * If combobox has no focus, does simple mouse click on it first.
0593: * @param text text to type.
0594: * @throws TimeoutExpiredException
0595: */
0596: public void typeText(String text) {
0597: makeComponentVisible();
0598: JTextFieldOperator tfo = getTextField();
0599: tfo.copyEnvironment(this );
0600: tfo.setVisualizer(new EmptyVisualizer());
0601: tfo.typeText(text);
0602: }
0603:
0604: /**
0605: * Clears text in the editable combobox using left-arrow and delete keys.
0606: * If combobox has no focus, does simple mouse click on it first.
0607: * @throws TimeoutExpiredException
0608: */
0609: public void clearText() {
0610: makeComponentVisible();
0611: JTextFieldOperator tfo = getTextField();
0612: tfo.copyEnvironment(this );
0613: tfo.setVisualizer(new EmptyVisualizer());
0614: tfo.clearText();
0615: }
0616:
0617: /**
0618: * Requests a focus, clears text, types new one and pushes Enter.
0619: * @param text New text value. Shouln't include final '\n'.
0620: * @throws TimeoutExpiredException
0621: */
0622: public void enterText(String text) {
0623: makeComponentVisible();
0624: JTextFieldOperator tfo = getTextField();
0625: tfo.copyEnvironment(this );
0626: tfo.setVisualizer(new EmptyVisualizer());
0627: tfo.enterText(text);
0628: }
0629:
0630: /**
0631: * Waits for item to be selected.
0632: * @param index Item index.
0633: */
0634: public void waitItemSelected(final int index) {
0635: getOutput()
0636: .printLine(
0637: "Wait "
0638: + Integer.toString(index)
0639: + "'th item to be selected in component \n : "
0640: + toStringSource());
0641: getOutput().printGolden(
0642: "Wait " + Integer.toString(index)
0643: + "'th item to be selected");
0644: waitState(new ComponentChooser() {
0645: public boolean checkComponent(Component comp) {
0646: return (getSelectedIndex() == index);
0647: }
0648:
0649: public String getDescription() {
0650: return ("Has " + Integer.toString(index) + "'th item selected");
0651: }
0652: });
0653: }
0654:
0655: /**
0656: * Waits for item to be selected. Uses getComparator() comparator.
0657: * @param item wait an item to be selected.
0658: */
0659: public void waitItemSelected(final String item) {
0660: getOutput()
0661: .printLine(
0662: "Wait \""
0663: + item
0664: + "\" item to be selected in component \n : "
0665: + toStringSource());
0666: getOutput().printGolden(
0667: "WaitWait \"" + item + "\" item to be selected");
0668: waitState(new JComboBoxByItemFinder(item, -1, getComparator()));
0669:
0670: }
0671:
0672: /**
0673: * Returns information about component.
0674: */
0675: public Hashtable getDump() {
0676: Hashtable result = super .getDump();
0677: if (((JComboBox) getSource()).getSelectedItem() != null
0678: && ((JComboBox) getSource()).getSelectedItem()
0679: .toString() != null) {
0680: result.put(TEXT_DPROP, ((JComboBox) getSource())
0681: .getSelectedItem().toString());
0682: }
0683: String[] items = new String[((JComboBox) getSource())
0684: .getItemCount()];
0685: for (int i = 0; i < ((JComboBox) getSource()).getItemCount(); i++) {
0686: if (((JComboBox) getSource()).getItemAt(i) != null
0687: && ((JComboBox) getSource()).getItemAt(i)
0688: .toString() != null) {
0689: items[i] = ((JComboBox) getSource()).getItemAt(i)
0690: .toString();
0691: }
0692: }
0693: addToDump(result, ITEM_PREFIX_DPROP, items);
0694: return (result);
0695: }
0696:
0697: ////////////////////////////////////////////////////////
0698: //Mapping //
0699:
0700: /**Maps <code>JComboBox.actionPerformed(ActionEvent)</code> through queue*/
0701: public void actionPerformed(final ActionEvent actionEvent) {
0702: runMapping(new MapVoidAction("actionPerformed") {
0703: public void map() {
0704: ((JComboBox) getSource()).actionPerformed(actionEvent);
0705: }
0706: });
0707: }
0708:
0709: /**Maps <code>JComboBox.addActionListener(ActionListener)</code> through queue*/
0710: public void addActionListener(final ActionListener actionListener) {
0711: runMapping(new MapVoidAction("addActionListener") {
0712: public void map() {
0713: ((JComboBox) getSource())
0714: .addActionListener(actionListener);
0715: }
0716: });
0717: }
0718:
0719: /**Maps <code>JComboBox.addItem(Object)</code> through queue*/
0720: public void addItem(final Object object) {
0721: runMapping(new MapVoidAction("addItem") {
0722: public void map() {
0723: ((JComboBox) getSource()).addItem(object);
0724: }
0725: });
0726: }
0727:
0728: /**Maps <code>JComboBox.addItemListener(ItemListener)</code> through queue*/
0729: public void addItemListener(final ItemListener itemListener) {
0730: runMapping(new MapVoidAction("addItemListener") {
0731: public void map() {
0732: ((JComboBox) getSource()).addItemListener(itemListener);
0733: }
0734: });
0735: }
0736:
0737: /**Maps <code>JComboBox.configureEditor(ComboBoxEditor, Object)</code> through queue*/
0738: public void configureEditor(final ComboBoxEditor comboBoxEditor,
0739: final Object object) {
0740: runMapping(new MapVoidAction("configureEditor") {
0741: public void map() {
0742: ((JComboBox) getSource()).configureEditor(
0743: comboBoxEditor, object);
0744: }
0745: });
0746: }
0747:
0748: /**Maps <code>JComboBox.contentsChanged(ListDataEvent)</code> through queue*/
0749: public void contentsChanged(final ListDataEvent listDataEvent) {
0750: runMapping(new MapVoidAction("contentsChanged") {
0751: public void map() {
0752: ((JComboBox) getSource())
0753: .contentsChanged(listDataEvent);
0754: }
0755: });
0756: }
0757:
0758: /**Maps <code>JComboBox.getActionCommand()</code> through queue*/
0759: public String getActionCommand() {
0760: return ((String) runMapping(new MapAction("getActionCommand") {
0761: public Object map() {
0762: return (((JComboBox) getSource()).getActionCommand());
0763: }
0764: }));
0765: }
0766:
0767: /**Maps <code>JComboBox.getEditor()</code> through queue*/
0768: public ComboBoxEditor getEditor() {
0769: return ((ComboBoxEditor) runMapping(new MapAction("getEditor") {
0770: public Object map() {
0771: return (((JComboBox) getSource()).getEditor());
0772: }
0773: }));
0774: }
0775:
0776: /**Maps <code>JComboBox.getItemAt(int)</code> through queue*/
0777: public Object getItemAt(final int i) {
0778: return ((Object) runMapping(new MapAction("getItemAt") {
0779: public Object map() {
0780: return (((JComboBox) getSource()).getItemAt(i));
0781: }
0782: }));
0783: }
0784:
0785: /**Maps <code>JComboBox.getItemCount()</code> through queue*/
0786: public int getItemCount() {
0787: return (runMapping(new MapIntegerAction("getItemCount") {
0788: public int map() {
0789: return (((JComboBox) getSource()).getItemCount());
0790: }
0791: }));
0792: }
0793:
0794: /**Maps <code>JComboBox.getKeySelectionManager()</code> through queue*/
0795: public KeySelectionManager getKeySelectionManager() {
0796: return ((KeySelectionManager) runMapping(new MapAction(
0797: "getKeySelectionManager") {
0798: public Object map() {
0799: return (((JComboBox) getSource())
0800: .getKeySelectionManager());
0801: }
0802: }));
0803: }
0804:
0805: /**Maps <code>JComboBox.getMaximumRowCount()</code> through queue*/
0806: public int getMaximumRowCount() {
0807: return (runMapping(new MapIntegerAction("getMaximumRowCount") {
0808: public int map() {
0809: return (((JComboBox) getSource()).getMaximumRowCount());
0810: }
0811: }));
0812: }
0813:
0814: /**Maps <code>JComboBox.getModel()</code> through queue*/
0815: public ComboBoxModel getModel() {
0816: return ((ComboBoxModel) runMapping(new MapAction("getModel") {
0817: public Object map() {
0818: return (((JComboBox) getSource()).getModel());
0819: }
0820: }));
0821: }
0822:
0823: /**Maps <code>JComboBox.getRenderer()</code> through queue*/
0824: public ListCellRenderer getRenderer() {
0825: return ((ListCellRenderer) runMapping(new MapAction(
0826: "getRenderer") {
0827: public Object map() {
0828: return (((JComboBox) getSource()).getRenderer());
0829: }
0830: }));
0831: }
0832:
0833: /**Maps <code>JComboBox.getSelectedIndex()</code> through queue*/
0834: public int getSelectedIndex() {
0835: return (runMapping(new MapIntegerAction("getSelectedIndex") {
0836: public int map() {
0837: return (((JComboBox) getSource()).getSelectedIndex());
0838: }
0839: }));
0840: }
0841:
0842: /**Maps <code>JComboBox.getSelectedItem()</code> through queue*/
0843: public Object getSelectedItem() {
0844: return ((Object) runMapping(new MapAction("getSelectedItem") {
0845: public Object map() {
0846: return (((JComboBox) getSource()).getSelectedItem());
0847: }
0848: }));
0849: }
0850:
0851: /**Maps <code>JComboBox.getSelectedObjects()</code> through queue*/
0852: public Object[] getSelectedObjects() {
0853: return ((Object[]) runMapping(new MapAction(
0854: "getSelectedObjects") {
0855: public Object map() {
0856: return (((JComboBox) getSource()).getSelectedObjects());
0857: }
0858: }));
0859: }
0860:
0861: /**Maps <code>JComboBox.getUI()</code> through queue*/
0862: public ComboBoxUI getUI() {
0863: return ((ComboBoxUI) runMapping(new MapAction("getUI") {
0864: public Object map() {
0865: return (((JComboBox) getSource()).getUI());
0866: }
0867: }));
0868: }
0869:
0870: /**Maps <code>JComboBox.hidePopup()</code> through queue*/
0871: public void hidePopup() {
0872: runMapping(new MapVoidAction("hidePopup") {
0873: public void map() {
0874: ((JComboBox) getSource()).hidePopup();
0875: }
0876: });
0877: }
0878:
0879: /**Maps <code>JComboBox.insertItemAt(Object, int)</code> through queue*/
0880: public void insertItemAt(final Object object, final int i) {
0881: runMapping(new MapVoidAction("insertItemAt") {
0882: public void map() {
0883: ((JComboBox) getSource()).insertItemAt(object, i);
0884: }
0885: });
0886: }
0887:
0888: /**Maps <code>JComboBox.intervalAdded(ListDataEvent)</code> through queue*/
0889: public void intervalAdded(final ListDataEvent listDataEvent) {
0890: runMapping(new MapVoidAction("intervalAdded") {
0891: public void map() {
0892: ((JComboBox) getSource()).intervalAdded(listDataEvent);
0893: }
0894: });
0895: }
0896:
0897: /**Maps <code>JComboBox.intervalRemoved(ListDataEvent)</code> through queue*/
0898: public void intervalRemoved(final ListDataEvent listDataEvent) {
0899: runMapping(new MapVoidAction("intervalRemoved") {
0900: public void map() {
0901: ((JComboBox) getSource())
0902: .intervalRemoved(listDataEvent);
0903: }
0904: });
0905: }
0906:
0907: /**Maps <code>JComboBox.isEditable()</code> through queue*/
0908: public boolean isEditable() {
0909: return (runMapping(new MapBooleanAction("isEditable") {
0910: public boolean map() {
0911: return (((JComboBox) getSource()).isEditable());
0912: }
0913: }));
0914: }
0915:
0916: /**Maps <code>JComboBox.isLightWeightPopupEnabled()</code> through queue*/
0917: public boolean isLightWeightPopupEnabled() {
0918: return (runMapping(new MapBooleanAction(
0919: "isLightWeightPopupEnabled") {
0920: public boolean map() {
0921: return (((JComboBox) getSource())
0922: .isLightWeightPopupEnabled());
0923: }
0924: }));
0925: }
0926:
0927: /**Maps <code>JComboBox.isPopupVisible()</code> through queue*/
0928: public boolean isPopupVisible() {
0929: return (runMapping(new MapBooleanAction("isPopupVisible") {
0930: public boolean map() {
0931: return (((JComboBox) getSource()).isPopupVisible());
0932: }
0933: }));
0934: }
0935:
0936: /**Maps <code>JComboBox.processKeyEvent(KeyEvent)</code> through queue*/
0937: public void processKeyEvent(final KeyEvent keyEvent) {
0938: runMapping(new MapVoidAction("processKeyEvent") {
0939: public void map() {
0940: ((JComboBox) getSource()).processKeyEvent(keyEvent);
0941: }
0942: });
0943: }
0944:
0945: /**Maps <code>JComboBox.removeActionListener(ActionListener)</code> through queue*/
0946: public void removeActionListener(final ActionListener actionListener) {
0947: runMapping(new MapVoidAction("removeActionListener") {
0948: public void map() {
0949: ((JComboBox) getSource())
0950: .removeActionListener(actionListener);
0951: }
0952: });
0953: }
0954:
0955: /**Maps <code>JComboBox.removeAllItems()</code> through queue*/
0956: public void removeAllItems() {
0957: runMapping(new MapVoidAction("removeAllItems") {
0958: public void map() {
0959: ((JComboBox) getSource()).removeAllItems();
0960: }
0961: });
0962: }
0963:
0964: /**Maps <code>JComboBox.removeItem(Object)</code> through queue*/
0965: public void removeItem(final Object object) {
0966: runMapping(new MapVoidAction("removeItem") {
0967: public void map() {
0968: ((JComboBox) getSource()).removeItem(object);
0969: }
0970: });
0971: }
0972:
0973: /**Maps <code>JComboBox.removeItemAt(int)</code> through queue*/
0974: public void removeItemAt(final int i) {
0975: runMapping(new MapVoidAction("removeItemAt") {
0976: public void map() {
0977: ((JComboBox) getSource()).removeItemAt(i);
0978: }
0979: });
0980: }
0981:
0982: /**Maps <code>JComboBox.removeItemListener(ItemListener)</code> through queue*/
0983: public void removeItemListener(final ItemListener itemListener) {
0984: runMapping(new MapVoidAction("removeItemListener") {
0985: public void map() {
0986: ((JComboBox) getSource())
0987: .removeItemListener(itemListener);
0988: }
0989: });
0990: }
0991:
0992: /**Maps <code>JComboBox.selectWithKeyChar(char)</code> through queue*/
0993: public boolean selectWithKeyChar(final char c) {
0994: return (runMapping(new MapBooleanAction("selectWithKeyChar") {
0995: public boolean map() {
0996: return (((JComboBox) getSource()).selectWithKeyChar(c));
0997: }
0998: }));
0999: }
1000:
1001: /**Maps <code>JComboBox.setActionCommand(String)</code> through queue*/
1002: public void setActionCommand(final String string) {
1003: runMapping(new MapVoidAction("setActionCommand") {
1004: public void map() {
1005: ((JComboBox) getSource()).setActionCommand(string);
1006: }
1007: });
1008: }
1009:
1010: /**Maps <code>JComboBox.setEditable(boolean)</code> through queue*/
1011: public void setEditable(final boolean b) {
1012: runMapping(new MapVoidAction("setEditable") {
1013: public void map() {
1014: ((JComboBox) getSource()).setEditable(b);
1015: }
1016: });
1017: }
1018:
1019: /**Maps <code>JComboBox.setEditor(ComboBoxEditor)</code> through queue*/
1020: public void setEditor(final ComboBoxEditor comboBoxEditor) {
1021: runMapping(new MapVoidAction("setEditor") {
1022: public void map() {
1023: ((JComboBox) getSource()).setEditor(comboBoxEditor);
1024: }
1025: });
1026: }
1027:
1028: /**Maps <code>JComboBox.setKeySelectionManager(KeySelectionManager)</code> through queue*/
1029: public void setKeySelectionManager(
1030: final KeySelectionManager keySelectionManager) {
1031: runMapping(new MapVoidAction("setKeySelectionManager") {
1032: public void map() {
1033: ((JComboBox) getSource())
1034: .setKeySelectionManager(keySelectionManager);
1035: }
1036: });
1037: }
1038:
1039: /**Maps <code>JComboBox.setLightWeightPopupEnabled(boolean)</code> through queue*/
1040: public void setLightWeightPopupEnabled(final boolean b) {
1041: runMapping(new MapVoidAction("setLightWeightPopupEnabled") {
1042: public void map() {
1043: ((JComboBox) getSource()).setLightWeightPopupEnabled(b);
1044: }
1045: });
1046: }
1047:
1048: /**Maps <code>JComboBox.setMaximumRowCount(int)</code> through queue*/
1049: public void setMaximumRowCount(final int i) {
1050: runMapping(new MapVoidAction("setMaximumRowCount") {
1051: public void map() {
1052: ((JComboBox) getSource()).setMaximumRowCount(i);
1053: }
1054: });
1055: }
1056:
1057: /**Maps <code>JComboBox.setModel(ComboBoxModel)</code> through queue*/
1058: public void setModel(final ComboBoxModel comboBoxModel) {
1059: runMapping(new MapVoidAction("setModel") {
1060: public void map() {
1061: ((JComboBox) getSource()).setModel(comboBoxModel);
1062: }
1063: });
1064: }
1065:
1066: /**Maps <code>JComboBox.setPopupVisible(boolean)</code> through queue*/
1067: public void setPopupVisible(final boolean b) {
1068: runMapping(new MapVoidAction("setPopupVisible") {
1069: public void map() {
1070: ((JComboBox) getSource()).setPopupVisible(b);
1071: }
1072: });
1073: }
1074:
1075: /**Maps <code>JComboBox.setRenderer(ListCellRenderer)</code> through queue*/
1076: public void setRenderer(final ListCellRenderer listCellRenderer) {
1077: runMapping(new MapVoidAction("setRenderer") {
1078: public void map() {
1079: ((JComboBox) getSource()).setRenderer(listCellRenderer);
1080: }
1081: });
1082: }
1083:
1084: /**Maps <code>JComboBox.setSelectedIndex(int)</code> through queue*/
1085: public void setSelectedIndex(final int i) {
1086: runMapping(new MapVoidAction("setSelectedIndex") {
1087: public void map() {
1088: ((JComboBox) getSource()).setSelectedIndex(i);
1089: }
1090: });
1091: }
1092:
1093: /**Maps <code>JComboBox.setSelectedItem(Object)</code> through queue*/
1094: public void setSelectedItem(final Object object) {
1095: runMapping(new MapVoidAction("setSelectedItem") {
1096: public void map() {
1097: ((JComboBox) getSource()).setSelectedItem(object);
1098: }
1099: });
1100: }
1101:
1102: /**Maps <code>JComboBox.setUI(ComboBoxUI)</code> through queue*/
1103: public void setUI(final ComboBoxUI comboBoxUI) {
1104: runMapping(new MapVoidAction("setUI") {
1105: public void map() {
1106: ((JComboBox) getSource()).setUI(comboBoxUI);
1107: }
1108: });
1109: }
1110:
1111: /**Maps <code>JComboBox.showPopup()</code> through queue*/
1112: public void showPopup() {
1113: runMapping(new MapVoidAction("showPopup") {
1114: public void map() {
1115: ((JComboBox) getSource()).showPopup();
1116: }
1117: });
1118: }
1119:
1120: //End of mapping //
1121: ////////////////////////////////////////////////////////
1122:
1123: private void init() {
1124: if (button == null) {
1125: button = new JButtonOperator(findJButton());
1126: button.copyEnvironment(this );
1127: button.setOutput(getOutput().createErrorOutput());
1128: if (((JComboBox) getSource()).isEditable()) {
1129: text = new JTextFieldOperator(findJTextField());
1130: text.copyEnvironment(this );
1131: text.setOutput(getOutput().createErrorOutput());
1132: }
1133: }
1134: }
1135:
1136: /**
1137: * Allows to find component by an item.
1138: */
1139: public static class JComboBoxByItemFinder implements
1140: ComponentChooser {
1141: String label;
1142: int itemIndex;
1143: StringComparator comparator;
1144:
1145: /**
1146: * Constructs JComboBoxByItemFinder.
1147: * @param lb a text pattern
1148: * @param ii item index to check. If equal to -1, selected item is checked.
1149: * @param comparator specifies string comparision algorithm.
1150: */
1151: public JComboBoxByItemFinder(String lb, int ii,
1152: StringComparator comparator) {
1153: label = lb;
1154: itemIndex = ii;
1155: this .comparator = comparator;
1156: }
1157:
1158: /**
1159: * Constructs JComboBoxByItemFinder.
1160: * @param lb a text pattern
1161: * @param ii item index to check. If equal to -1, selected item is checked.
1162: */
1163: public JComboBoxByItemFinder(String lb, int ii) {
1164: this (lb, ii, Operator.getDefaultStringComparator());
1165: }
1166:
1167: public boolean checkComponent(Component comp) {
1168: if (comp instanceof JComboBox) {
1169: if (label == null) {
1170: return (true);
1171: }
1172: if (((JComboBox) comp).getModel().getSize() > itemIndex) {
1173: int ii = itemIndex;
1174: if (ii == -1) {
1175: ii = ((JComboBox) comp).getSelectedIndex();
1176: if (ii == -1) {
1177: return (false);
1178: }
1179: }
1180: return (comparator.equals(((JComboBox) comp)
1181: .getModel().getElementAt(ii).toString(),
1182: label));
1183: }
1184: }
1185: return (false);
1186: }
1187:
1188: public String getDescription() {
1189: return ("JComboBox with text \"" + label + "\" in "
1190: + (new Integer(itemIndex)).toString() + "'th item");
1191: }
1192: }
1193:
1194: /**
1195: * Checks component type.
1196: */
1197: public static class JComboBoxFinder extends Finder {
1198: /**
1199: * Constructs JComboBoxFinder.
1200: * @param sf other searching criteria.
1201: */
1202: public JComboBoxFinder(ComponentChooser sf) {
1203: super (JComboBox.class, sf);
1204: }
1205:
1206: /**
1207: * Constructs JComboBoxFinder.
1208: */
1209: public JComboBoxFinder() {
1210: super (JComboBox.class);
1211: }
1212: }
1213:
1214: private class PopupWindowChooser implements ComponentChooser {
1215: ComponentChooser pChooser;
1216:
1217: public PopupWindowChooser(ComponentChooser pChooser) {
1218: this .pChooser = pChooser;
1219: }
1220:
1221: public boolean checkComponent(Component comp) {
1222: ComponentSearcher cs = new ComponentSearcher(
1223: (Container) comp);
1224: cs.setOutput(TestOut.getNullOutput());
1225: return (cs.findComponent(pChooser) != null);
1226: }
1227:
1228: public String getDescription() {
1229: return ("Popup window");
1230: }
1231: }
1232:
1233: private class ListWater extends Waiter {
1234: ComponentChooser cChooser;
1235: ComponentChooser pChooser;
1236:
1237: public ListWater() {
1238: super ();
1239: cChooser = new ComponentChooser() {
1240: public boolean checkComponent(Component comp) {
1241: if (comp instanceof JList) {
1242: Container cont = (Container) comp;
1243: while ((cont = cont.getParent()) != null) {
1244: if (cont instanceof ComboPopup) {
1245: return (true);
1246: }
1247: }
1248: }
1249: return (false);
1250: }
1251:
1252: public String getDescription() {
1253: return ("Popup menu");
1254: }
1255: };
1256: pChooser = new PopupWindowChooser(cChooser);
1257: }
1258:
1259: public Object actionProduced(Object obj) {
1260: Window popupWindow = null;
1261: if (pChooser.checkComponent(getWindow())) {
1262: popupWindow = getWindow();
1263: } else {
1264: popupWindow = WindowWaiter.getWindow(getWindow(),
1265: pChooser);
1266: }
1267: if (popupWindow != null) {
1268: ComponentSearcher sc = new ComponentSearcher(
1269: popupWindow);
1270: sc.setOutput(TestOut.getNullOutput());
1271: return (sc.findComponent(cChooser));
1272: } else {
1273: return (null);
1274: }
1275: }
1276:
1277: public String getDescription() {
1278: return ("Wait popup expanded");
1279: }
1280: }
1281: }
|