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: JTextFieldOperator.java,v 1.7 2007/10/05 11:35:25 jskrivanek Exp $ $Revision: 1.7 $ $Date: 2007/10/05 11:35:25 $
044: *
045: */
046:
047: package org.netbeans.jemmy.operators;
048:
049: import org.netbeans.jemmy.ComponentChooser;
050: import org.netbeans.jemmy.ComponentSearcher;
051: import org.netbeans.jemmy.TimeoutExpiredException;
052:
053: import java.awt.Component;
054: import java.awt.Container;
055:
056: import java.awt.event.ActionListener;
057: import java.awt.event.KeyEvent;
058:
059: import javax.swing.BoundedRangeModel;
060: import javax.swing.JTextField;
061:
062: /**
063: * <BR><BR>Timeouts used: <BR>
064: * JTextComponentOperator.PushKeyTimeout - time between key pressing and releasing during text typing <BR>
065: * JTextComponentOperator.BetweenKeysTimeout - time to sleep between two chars typing <BR>
066: * JTextComponentOperator.ChangeCaretPositionTimeout - maximum time to chenge caret position <BR>
067: * JTextComponentOperator.TypeTextTimeout - maximum time to type text <BR>
068: * ComponentOperator.WaitComponentTimeout - time to wait component displayed <BR>
069: * ComponentOperator.WaitFocusTimeout - time to wait component focus <BR>
070: * JScrollBarOperator.OneScrollClickTimeout - time for one scroll click <BR>
071: * JScrollBarOperator.WholeScrollTimeout - time for the whole scrolling <BR>.
072: *
073: * @see org.netbeans.jemmy.Timeouts
074: *
075: * @author Alexandre Iline (alexandre.iline@sun.com)
076: *
077: */
078: public class JTextFieldOperator extends JTextComponentOperator {
079:
080: /**
081: * Constructor.
082: * @param b a component
083: */
084: public JTextFieldOperator(JTextField b) {
085: super (b);
086: }
087:
088: /**
089: * Constructs a JTextFieldOperator object.
090: * @param cont a container
091: * @param chooser a component chooser specifying searching criteria.
092: * @param index an index between appropriate ones.
093: */
094: public JTextFieldOperator(ContainerOperator cont,
095: ComponentChooser chooser, int index) {
096: this ((JTextField) cont.waitSubComponent(new JTextFieldFinder(
097: chooser), index));
098: copyEnvironment(cont);
099: }
100:
101: /**
102: * Constructs a JTextFieldOperator object.
103: * @param cont a container
104: * @param chooser a component chooser specifying searching criteria.
105: */
106: public JTextFieldOperator(ContainerOperator cont,
107: ComponentChooser chooser) {
108: this (cont, chooser, 0);
109: }
110:
111: /**
112: * Constructor.
113: * Waits component in container first.
114: * Uses cont's timeout and output for waiting and to init operator.
115: * @param cont a container
116: * @param text Button text.
117: * @param index Ordinal component index.
118: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
119: * @throws TimeoutExpiredException
120: */
121: public JTextFieldOperator(ContainerOperator cont, String text,
122: int index) {
123: this ((JTextField) waitComponent(cont, new JTextFieldFinder(
124: new JTextComponentOperator.JTextComponentByTextFinder(
125: text, cont.getComparator())), index));
126: copyEnvironment(cont);
127: }
128:
129: /**
130: * Constructor.
131: * Waits component in container first.
132: * Uses cont's timeout and output for waiting and to init operator.
133: * @param cont a container
134: * @param text Button text.
135: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
136: * @throws TimeoutExpiredException
137: */
138: public JTextFieldOperator(ContainerOperator cont, String text) {
139: this (cont, text, 0);
140: }
141:
142: /**
143: * Constructor.
144: * Waits component in container first.
145: * Uses cont's timeout and output for waiting and to init operator.
146: * @param cont a container
147: * @param index Ordinal component index.
148: * @throws TimeoutExpiredException
149: */
150: public JTextFieldOperator(ContainerOperator cont, int index) {
151: this ((JTextField) waitComponent(cont, new JTextFieldFinder(),
152: index));
153: copyEnvironment(cont);
154: }
155:
156: /**
157: * Constructor.
158: * Waits component in container first.
159: * Uses cont's timeout and output for waiting and to init operator.
160: * @param cont a container
161: * @throws TimeoutExpiredException
162: */
163: public JTextFieldOperator(ContainerOperator cont) {
164: this (cont, 0);
165: }
166:
167: /**
168: * Searches JTextField in container.
169: * @param cont Container to search component in.
170: * @param chooser a component chooser specifying searching criteria.
171: * @param index Ordinal component index.
172: * @return JTextField instance or null if component was not found.
173: */
174: public static JTextField findJTextField(Container cont,
175: ComponentChooser chooser, int index) {
176: return ((JTextField) findJTextComponent(cont,
177: new JTextFieldFinder(chooser), index));
178: }
179:
180: /**
181: * Searches JTextField in container.
182: * @param cont Container to search component in.
183: * @param chooser a component chooser specifying searching criteria.
184: * @return JTextField instance or null if component was not found.
185: */
186: public static JTextField findJTextField(Container cont,
187: ComponentChooser chooser) {
188: return (findJTextField(cont, chooser, 0));
189: }
190:
191: /**
192: * Searches JTextField by text.
193: * @param cont Container to search component in.
194: * @param text Component text.
195: * @param ce Compare text exactly.
196: * @param ccs Compare text case sensitively.
197: * @param index Ordinal component index.
198: * @return JTextField instance or null if component was not found.
199: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
200: */
201: public static JTextField findJTextField(Container cont,
202: String text, boolean ce, boolean ccs, int index) {
203: return (findJTextField(cont, new JTextFieldFinder(
204: new JTextComponentOperator.JTextComponentByTextFinder(
205: text, new DefaultStringComparator(ce, ccs))),
206: index));
207: }
208:
209: /**
210: * Searches JTextField by text.
211: * @param cont Container to search component in.
212: * @param text Component text.
213: * @param ce Compare text exactly.
214: * @param ccs Compare text case sensitively.
215: * @return JTextField instance or null if component was not found.
216: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
217: */
218: public static JTextField findJTextField(Container cont,
219: String text, boolean ce, boolean ccs) {
220: return (findJTextField(cont, text, ce, ccs, 0));
221: }
222:
223: /**
224: * Waits JTextField in container.
225: * @param cont Container to search component in.
226: * @param chooser a component chooser specifying searching criteria.
227: * @param index Ordinal component index.
228: * @return JTextField instance.
229: * @throws TimeoutExpiredException
230: */
231: public static JTextField waitJTextField(Container cont,
232: ComponentChooser chooser, int index) {
233: return ((JTextField) waitJTextComponent(cont,
234: new JTextFieldFinder(chooser), index));
235: }
236:
237: /**
238: * Waits JTextField in container.
239: * @param cont Container to search component in.
240: * @param chooser a component chooser specifying searching criteria.
241: * @return JTextField instance.
242: * @throws TimeoutExpiredException
243: */
244: public static JTextField waitJTextField(Container cont,
245: ComponentChooser chooser) {
246: return (waitJTextField(cont, chooser, 0));
247: }
248:
249: /**
250: * Waits JTextField by text.
251: * @param cont Container to search component in.
252: * @param text Component text.
253: * @param ce Compare text exactly.
254: * @param ccs Compare text case sensitively.
255: * @param index Ordinal component index.
256: * @return JTextField instance.
257: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
258: * @throws TimeoutExpiredException
259: */
260: public static JTextField waitJTextField(Container cont,
261: String text, boolean ce, boolean ccs, int index) {
262: return (waitJTextField(cont, new JTextFieldFinder(
263: new JTextComponentOperator.JTextComponentByTextFinder(
264: text, new DefaultStringComparator(ce, ccs))),
265: index));
266: }
267:
268: /**
269: * Waits JTextField by text.
270: * @param cont Container to search component in.
271: * @param text Component text.
272: * @param ce Compare text exactly.
273: * @param ccs Compare text case sensitively.
274: * @return JTextField instance.
275: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
276: * @throws TimeoutExpiredException
277: */
278: public static JTextField waitJTextField(Container cont,
279: String text, boolean ce, boolean ccs) {
280: return (waitJTextField(cont, text, ce, ccs, 0));
281: }
282:
283: /**
284: * Wait some text to be displayed starting from certain position.
285: * @param text a text to wait.
286: * @param position start position.
287: */
288: public void waitText(String text, int position) {
289: super .waitText(removeNewLines(text), position);
290: }
291:
292: /**
293: * Wait some text to be displayed.
294: * @param text a text to wait.
295: */
296: public void waitText(String text) {
297: super .waitText(removeNewLines(text));
298: }
299:
300: ////////////////////////////////////////////////////////
301: //Mapping //
302:
303: /**Maps <code>JTextField.addActionListener(ActionListener)</code> through queue*/
304: public void addActionListener(final ActionListener actionListener) {
305: runMapping(new MapVoidAction("addActionListener") {
306: public void map() {
307: ((JTextField) getSource())
308: .addActionListener(actionListener);
309: }
310: });
311: }
312:
313: /**Maps <code>JTextField.getColumns()</code> through queue*/
314: public int getColumns() {
315: return (runMapping(new MapIntegerAction("getColumns") {
316: public int map() {
317: return (((JTextField) getSource()).getColumns());
318: }
319: }));
320: }
321:
322: /**Maps <code>JTextField.getHorizontalAlignment()</code> through queue*/
323: public int getHorizontalAlignment() {
324: return (runMapping(new MapIntegerAction(
325: "getHorizontalAlignment") {
326: public int map() {
327: return (((JTextField) getSource())
328: .getHorizontalAlignment());
329: }
330: }));
331: }
332:
333: /**Maps <code>JTextField.getHorizontalVisibility()</code> through queue*/
334: public BoundedRangeModel getHorizontalVisibility() {
335: return ((BoundedRangeModel) runMapping(new MapAction(
336: "getHorizontalVisibility") {
337: public Object map() {
338: return (((JTextField) getSource())
339: .getHorizontalVisibility());
340: }
341: }));
342: }
343:
344: /**Maps <code>JTextField.getScrollOffset()</code> through queue*/
345: public int getScrollOffset() {
346: return (runMapping(new MapIntegerAction("getScrollOffset") {
347: public int map() {
348: return (((JTextField) getSource()).getScrollOffset());
349: }
350: }));
351: }
352:
353: /**Maps <code>JTextField.postActionEvent()</code> through queue*/
354: public void postActionEvent() {
355: runMapping(new MapVoidAction("postActionEvent") {
356: public void map() {
357: ((JTextField) getSource()).postActionEvent();
358: }
359: });
360: }
361:
362: /**Maps <code>JTextField.removeActionListener(ActionListener)</code> through queue*/
363: public void removeActionListener(final ActionListener actionListener) {
364: runMapping(new MapVoidAction("removeActionListener") {
365: public void map() {
366: ((JTextField) getSource())
367: .removeActionListener(actionListener);
368: }
369: });
370: }
371:
372: /**Maps <code>JTextField.setActionCommand(String)</code> through queue*/
373: public void setActionCommand(final String string) {
374: runMapping(new MapVoidAction("setActionCommand") {
375: public void map() {
376: ((JTextField) getSource()).setActionCommand(string);
377: }
378: });
379: }
380:
381: /**Maps <code>JTextField.setColumns(int)</code> through queue*/
382: public void setColumns(final int i) {
383: runMapping(new MapVoidAction("setColumns") {
384: public void map() {
385: ((JTextField) getSource()).setColumns(i);
386: }
387: });
388: }
389:
390: /**Maps <code>JTextField.setHorizontalAlignment(int)</code> through queue*/
391: public void setHorizontalAlignment(final int i) {
392: runMapping(new MapVoidAction("setHorizontalAlignment") {
393: public void map() {
394: ((JTextField) getSource()).setHorizontalAlignment(i);
395: }
396: });
397: }
398:
399: /**Maps <code>JTextField.setScrollOffset(int)</code> through queue*/
400: public void setScrollOffset(final int i) {
401: runMapping(new MapVoidAction("setScrollOffset") {
402: public void map() {
403: ((JTextField) getSource()).setScrollOffset(i);
404: }
405: });
406: }
407:
408: //End of mapping //
409: ////////////////////////////////////////////////////////
410:
411: private String removeNewLines(String text) {
412: StringBuffer buff = new StringBuffer(text);
413: int i = 0;
414: while (i < buff.length()) {
415: if (buff.charAt(i) != '\n') {
416: i++;
417: } else {
418: buff.deleteCharAt(i);
419: }
420: }
421: return (buff.toString());
422: }
423:
424: /**
425: * Checks component type.
426: */
427: public static class JTextFieldFinder extends Finder {
428: /**
429: * Constructs JTextFieldFinder.
430: * @param sf other searching criteria.
431: */
432: public JTextFieldFinder(ComponentChooser sf) {
433: super (JTextField.class, sf);
434: }
435:
436: /**
437: * Constructs JTextFieldFinder.
438: */
439: public JTextFieldFinder() {
440: super (JTextField.class);
441: }
442: }
443: }
|