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: TextFieldOperator.java,v 1.8 2007/10/05 11:35:41 jskrivanek Exp $ $Revision: 1.8 $ $Date: 2007/10/05 11:35:41 $
044: *
045: */
046:
047: package org.netbeans.jemmy.operators;
048:
049: import org.netbeans.jemmy.ActionProducer;
050: import org.netbeans.jemmy.Action;
051: import org.netbeans.jemmy.ComponentChooser;
052: import org.netbeans.jemmy.ComponentSearcher;
053: import org.netbeans.jemmy.JemmyProperties;
054: import org.netbeans.jemmy.Outputable;
055: import org.netbeans.jemmy.TestOut;
056: import org.netbeans.jemmy.Timeoutable;
057: import org.netbeans.jemmy.Timeouts;
058:
059: import java.awt.Component;
060: import java.awt.Container;
061: import java.awt.Dimension;
062: import java.awt.TextField;
063:
064: import java.awt.event.ActionListener;
065: import java.awt.event.KeyEvent;
066: import java.awt.event.TextListener;
067:
068: import java.util.Hashtable;
069:
070: /**
071: *
072: * This operator type covers java.awt.TextField component.
073: *
074: * @see org.netbeans.jemmy.Timeouts
075: *
076: * @author Alexandre Iline (alexandre.iline@sun.com)
077: *
078: */
079: public class TextFieldOperator extends TextComponentOperator implements
080: Timeoutable, Outputable {
081:
082: /**
083: * Identifier for a "text" property.
084: * @see #getDump
085: */
086: public static final String TEXT_DPROP = "Text";
087:
088: private final static long PUSH_KEY_TIMEOUT = 0;
089: private final static long BETWEEN_KEYS_TIMEOUT = 0;
090: private final static long CHANGE_CARET_POSITION_TIMEOUT = 60000;
091: private final static long TYPE_TEXT_TIMEOUT = 60000;
092:
093: private Timeouts timeouts;
094: private TestOut output;
095:
096: /**
097: * Constructor.
098: * @param b The <code>java.awt.TextField</code> managed by
099: * this instance.
100: */
101: public TextFieldOperator(TextField b) {
102: super (b);
103: }
104:
105: /**
106: * Constructs a TextFieldOperator object.
107: * @param cont a container
108: * @param chooser a component chooser specifying searching criteria.
109: * @param index an index between appropriate ones.
110: */
111: public TextFieldOperator(ContainerOperator cont,
112: ComponentChooser chooser, int index) {
113: this ((TextField) cont.waitSubComponent(new TextFieldFinder(
114: chooser), index));
115: copyEnvironment(cont);
116: }
117:
118: /**
119: * Constructs a TextFieldOperator object.
120: * @param cont a container
121: * @param chooser a component chooser specifying searching criteria.
122: */
123: public TextFieldOperator(ContainerOperator cont,
124: ComponentChooser chooser) {
125: this (cont, chooser, 0);
126: }
127:
128: /**
129: * Constructor.
130: * Waits for a component in a container to show. The component is
131: * identified as the <code>index+1</code>'th
132: * <code>java.awt.TextField</code> that shows, lies below
133: * the container in the display containment hierarchy,
134: * and that has the desired text. Uses cont's timeout and output
135: * for waiting and to init this operator.
136: * @param cont The operator for a container containing the sought for textField.
137: * @param text TextField text.
138: * @param index Ordinal component index. The first component has <code>index</code> 0.
139: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
140: */
141: public TextFieldOperator(ContainerOperator cont, String text,
142: int index) {
143: this ((TextField) waitComponent(cont, new TextFieldByTextFinder(
144: text, cont.getComparator()), index));
145: copyEnvironment(cont);
146: }
147:
148: /**
149: * Constructor.
150: * Waits for a component in a container to show. The component is
151: * identified as the first
152: * <code>java.awt.TextField</code> that shows, lies below
153: * the container in the display containment hierarchy,
154: * and that has the desired text. Uses cont's timeout and output
155: * for waiting and to init this operator.
156: * @param cont The operator for a container containing the sought for textField.
157: * @param text TextField text.
158: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
159: */
160: public TextFieldOperator(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 The operator for a container containing the sought for textField.
169: * @param index Ordinal component index.
170: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
171: */
172: public TextFieldOperator(ContainerOperator cont, int index) {
173: this ((TextField) waitComponent(cont, new TextFieldFinder(),
174: index));
175: copyEnvironment(cont);
176: }
177:
178: /**
179: * Constructor.
180: * Waits component in container first.
181: * Uses cont's timeout and output for waiting and to init operator.
182: * @param cont The operator for a container containing the sought for textField.
183: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
184: */
185: public TextFieldOperator(ContainerOperator cont) {
186: this (cont, 0);
187: }
188:
189: /**
190: * Searches TextField in a container.
191: * @param cont Container in which to search for the component. The container
192: * lies above the component in the display containment hierarchy. The containment
193: * need not be direct.
194: * @param chooser org.netbeans.jemmy.ComponentChooser implementation, defining and
195: * applying search criteria.
196: * @param index Ordinal component index. The first <code>index</code> is 0.
197: * @return TextField instance or null if component was not found.
198: */
199: public static TextField findTextField(Container cont,
200: ComponentChooser chooser, int index) {
201: return ((TextField) findComponent(cont, new TextFieldFinder(
202: chooser), index));
203: }
204:
205: /**
206: * Searches for the first TextField in a container.
207: * @param cont Container in which to search for the component. The container
208: * lies above the component in the display containment hierarchy. The containment
209: * need not be direct.
210: * @param chooser org.netbeans.jemmy.ComponentChooser implementation, defining and
211: * applying search criteria.
212: * @return TextField instance or null if component was not found.
213: */
214: public static TextField findTextField(Container cont,
215: ComponentChooser chooser) {
216: return (findTextField(cont, chooser, 0));
217: }
218:
219: /**
220: * Searches TextField by text.
221: * @param cont Container to search component in.
222: * @param text TextField text. If null, contents is not checked.
223: * @param ce Compare text exactly.
224: * @param ccs Compare text case sensitively.
225: * @param index Ordinal component index.
226: * @return TextField instance or null if component was not found.
227: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
228: */
229: public static TextField findTextField(Container cont, String text,
230: boolean ce, boolean ccs, int index) {
231: return (findTextField(cont, new TextFieldByTextFinder(text,
232: new DefaultStringComparator(ce, ccs)), index));
233: }
234:
235: /**
236: * Searches TextField by text.
237: * @param cont Container to search component in.
238: * @param text TextField text. If null, contents is not checked.
239: * @param ce Compare text exactly.
240: * @param ccs Compare text case sensitively.
241: * @return TextField instance or null if component was not found.
242: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
243: */
244: public static TextField findTextField(Container cont, String text,
245: boolean ce, boolean ccs) {
246: return (findTextField(cont, text, ce, ccs, 0));
247: }
248:
249: /**
250: * Waits TextField in container.
251: * @param cont Container to search component in.
252: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
253: * @param index Ordinal component index.
254: * @return TextField instance.
255: */
256: public static TextField waitTextField(Container cont,
257: ComponentChooser chooser, int index) {
258: return ((TextField) waitComponent(cont, new TextFieldFinder(
259: chooser), index));
260: }
261:
262: /**
263: * Waits 0'th TextField in container.
264: * @param cont Container to search component in.
265: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
266: * @return TextField instance.
267: */
268: public static TextField waitTextField(Container cont,
269: ComponentChooser chooser) {
270: return (waitTextField(cont, chooser, 0));
271: }
272:
273: /**
274: * Waits TextField by text.
275: * @param cont Container to search component in.
276: * @param text TextField text. If null, contents is not checked.
277: * @param ce Compare text exactly.
278: * @param ccs Compare text case sensitively.
279: * @param index Ordinal component index.
280: * @return TextField instance.
281: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
282: */
283: public static TextField waitTextField(Container cont, String text,
284: boolean ce, boolean ccs, int index) {
285: return (waitTextField(cont, new TextFieldByTextFinder(text,
286: new DefaultStringComparator(ce, ccs)), index));
287: }
288:
289: /**
290: * Waits TextField by text.
291: * @param cont Container to search component in.
292: * @param text TextField text. If null, contents is not checked.
293: * @param ce Compare text exactly.
294: * @param ccs Compare text case sensitively.
295: * @return TextField instance.
296: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
297: */
298: public static TextField waitTextField(Container cont, String text,
299: boolean ce, boolean ccs) {
300: return (waitTextField(cont, text, ce, ccs, 0));
301: }
302:
303: static {
304: Timeouts.initDefault("TextFieldOperator.PushKeyTimeout",
305: PUSH_KEY_TIMEOUT);
306: Timeouts.initDefault("TextFieldOperator.BetweenKeysTimeout",
307: BETWEEN_KEYS_TIMEOUT);
308: Timeouts.initDefault(
309: "TextFieldOperator.ChangeCaretPositionTimeout",
310: CHANGE_CARET_POSITION_TIMEOUT);
311: Timeouts.initDefault("TextFieldOperator.TypeTextTimeout",
312: TYPE_TEXT_TIMEOUT);
313: }
314:
315: public void setTimeouts(Timeouts timeouts) {
316: super .setTimeouts(timeouts);
317: this .timeouts = timeouts;
318: }
319:
320: public Timeouts getTimeouts() {
321: return (timeouts);
322: }
323:
324: public void setOutput(TestOut out) {
325: output = out;
326: super .setOutput(output.createErrorOutput());
327: }
328:
329: public TestOut getOutput() {
330: return (output);
331: }
332:
333: public Hashtable getDump() {
334: Hashtable result = super .getDump();
335: result.put(TEXT_DPROP, ((TextField) getSource()).getText());
336: return (result);
337: }
338:
339: ////////////////////////////////////////////////////////
340: //Mapping //
341:
342: /**Maps <code>TextField.addActionListener(ActionListener)</code> through queue*/
343: public void addActionListener(final ActionListener actionListener) {
344: runMapping(new MapVoidAction("addActionListener") {
345: public void map() {
346: ((TextField) getSource())
347: .addActionListener(actionListener);
348: }
349: });
350: }
351:
352: /**Maps <code>TextField.echoCharIsSet()</code> through queue*/
353: public boolean echoCharIsSet() {
354: return (runMapping(new MapBooleanAction("echoCharIsSet") {
355: public boolean map() {
356: return (((TextField) getSource()).echoCharIsSet());
357: }
358: }));
359: }
360:
361: /**Maps <code>TextField.getColumns()</code> through queue*/
362: public int getColumns() {
363: return (runMapping(new MapIntegerAction("getColumns") {
364: public int map() {
365: return (((TextField) getSource()).getColumns());
366: }
367: }));
368: }
369:
370: /**Maps <code>TextField.getEchoChar()</code> through queue*/
371: public char getEchoChar() {
372: return (runMapping(new MapCharacterAction("getEchoChar") {
373: public char map() {
374: return (((TextField) getSource()).getEchoChar());
375: }
376: }));
377: }
378:
379: /**Maps <code>TextField.getMinimumSize(int)</code> through queue*/
380: public Dimension getMinimumSize(final int i) {
381: return ((Dimension) runMapping(new MapAction("getMinimumSize") {
382: public Object map() {
383: return (((TextField) getSource()).getMinimumSize(i));
384: }
385: }));
386: }
387:
388: /**Maps <code>TextField.getPreferredSize(int)</code> through queue*/
389: public Dimension getPreferredSize(final int i) {
390: return ((Dimension) runMapping(new MapAction("getPreferredSize") {
391: public Object map() {
392: return (((TextField) getSource()).getPreferredSize(i));
393: }
394: }));
395: }
396:
397: /**Maps <code>TextField.removeActionListener(ActionListener)</code> through queue*/
398: public void removeActionListener(final ActionListener actionListener) {
399: runMapping(new MapVoidAction("removeActionListener") {
400: public void map() {
401: ((TextField) getSource())
402: .removeActionListener(actionListener);
403: }
404: });
405: }
406:
407: /**Maps <code>TextField.setColumns(int)</code> through queue*/
408: public void setColumns(final int i) {
409: runMapping(new MapVoidAction("setColumns") {
410: public void map() {
411: ((TextField) getSource()).setColumns(i);
412: }
413: });
414: }
415:
416: //End of mapping //
417: ////////////////////////////////////////////////////////
418:
419: /**
420: * Allows to find component by text.
421: */
422: public static class TextFieldByTextFinder implements
423: ComponentChooser {
424: String label;
425: StringComparator comparator;
426:
427: /**
428: * Constructs TextFieldByTextFinder.
429: * @param lb a text pattern
430: * @param comparator specifies string comparision algorithm.
431: */
432: public TextFieldByTextFinder(String lb,
433: StringComparator comparator) {
434: label = lb;
435: this .comparator = comparator;
436: }
437:
438: /**
439: * Constructs TextFieldByTextFinder.
440: * @param lb a text pattern
441: */
442: public TextFieldByTextFinder(String lb) {
443: this (lb, Operator.getDefaultStringComparator());
444: }
445:
446: public boolean checkComponent(Component comp) {
447: if (comp instanceof TextField) {
448: if (((TextField) comp).getText() != null) {
449: return (comparator.equals(((TextField) comp)
450: .getText(), label));
451: }
452: }
453: return (false);
454: }
455:
456: public String getDescription() {
457: return ("TextField with text \"" + label + "\"");
458: }
459: }
460:
461: /**
462: * Checks component type.
463: */
464: public static class TextFieldFinder extends Finder {
465: /**
466: * Constructs TextFieldFinder.
467: * @param sf other searching criteria.
468: */
469: public TextFieldFinder(ComponentChooser sf) {
470: super (TextField.class, sf);
471: }
472:
473: /**
474: * Constructs TextFieldFinder.
475: */
476: public TextFieldFinder() {
477: super (TextField.class);
478: }
479: }
480: }
|