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: TextAreaOperator.java,v 1.8 2007/10/05 11:35:51 jskrivanek Exp $ $Revision: 1.8 $ $Date: 2007/10/05 11:35:51 $
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.TextArea;
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: * This operator type covers java.awt.textArea component.
072: *
073: * @see org.netbeans.jemmy.Timeouts
074: *
075: * @author Alexandre Iline (alexandre.iline@sun.com)
076: *
077: */
078:
079: public class TextAreaOperator 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.TextArea</code> managed by
099: * this instance.
100: */
101: public TextAreaOperator(TextArea b) {
102: super (b);
103: }
104:
105: /**
106: * Constructs a TextAreaOperator 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 TextAreaOperator(ContainerOperator cont,
112: ComponentChooser chooser, int index) {
113: this ((TextArea) cont.waitSubComponent(new TextAreaFinder(
114: chooser), index));
115: copyEnvironment(cont);
116: }
117:
118: /**
119: * Constructs a TextAreaOperator object.
120: * @param cont a container
121: * @param chooser a component chooser specifying searching criteria.
122: */
123: public TextAreaOperator(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.TextArea</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 textArea.
137: * @param text TextArea 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 TextAreaOperator(ContainerOperator cont, String text,
142: int index) {
143: this ((TextArea) waitComponent(cont, new TextAreaByTextFinder(
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.TextArea</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 textArea.
157: * @param text TextArea text.
158: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
159: */
160: public TextAreaOperator(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 textArea.
169: * @param index Ordinal component index.
170: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
171: */
172: public TextAreaOperator(ContainerOperator cont, int index) {
173: this (
174: (TextArea) waitComponent(cont, new TextAreaFinder(),
175: index));
176: copyEnvironment(cont);
177: }
178:
179: /**
180: * Constructor.
181: * Waits component in container first.
182: * Uses cont's timeout and output for waiting and to init operator.
183: * @param cont The operator for a container containing the sought for textArea.
184: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
185: */
186: public TextAreaOperator(ContainerOperator cont) {
187: this (cont, 0);
188: }
189:
190: /**
191: * Searches TextArea in a container.
192: * @param cont Container in which to search for the component. The container
193: * lies above the component in the display containment hierarchy. The containment
194: * need not be direct.
195: * @param chooser org.netbeans.jemmy.ComponentChooser implementation, defining and
196: * applying search criteria.
197: * @param index Ordinal component index. The first <code>index</code> is 0.
198: * @return TextArea instance or null if component was not found.
199: */
200: public static TextArea findTextArea(Container cont,
201: ComponentChooser chooser, int index) {
202: return ((TextArea) findComponent(cont, new TextAreaFinder(
203: chooser), index));
204: }
205:
206: /**
207: * Searches for the first TextArea in a container.
208: * @param cont Container in which to search for the component. The container
209: * lies above the component in the display containment hierarchy. The containment
210: * need not be direct.
211: * @param chooser org.netbeans.jemmy.ComponentChooser implementation, defining and
212: * applying search criteria.
213: * @return TextArea instance or null if component was not found.
214: */
215: public static TextArea findTextArea(Container cont,
216: ComponentChooser chooser) {
217: return (findTextArea(cont, chooser, 0));
218: }
219:
220: /**
221: * Searches TextArea by text.
222: * @param cont Container to search component in.
223: * @param text TextArea text. If null, contents is not checked.
224: * @param ce Compare text exactly.
225: * @param ccs Compare text case sensitively.
226: * @param index Ordinal component index.
227: * @return TextArea instance or null if component was not found.
228: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
229: */
230: public static TextArea findTextArea(Container cont, String text,
231: boolean ce, boolean ccs, int index) {
232: return (findTextArea(cont, new TextAreaByTextFinder(text,
233: new DefaultStringComparator(ce, ccs)), index));
234: }
235:
236: /**
237: * Searches TextArea by text.
238: * @param cont Container to search component in.
239: * @param text TextArea text. If null, contents is not checked.
240: * @param ce Compare text exactly.
241: * @param ccs Compare text case sensitively.
242: * @return TextArea instance or null if component was not found.
243: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
244: */
245: public static TextArea findTextArea(Container cont, String text,
246: boolean ce, boolean ccs) {
247: return (findTextArea(cont, text, ce, ccs, 0));
248: }
249:
250: /**
251: * Waits TextArea in container.
252: * @param cont Container to search component in.
253: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
254: * @param index Ordinal component index.
255: * @return TextArea instance.
256: */
257: public static TextArea waitTextArea(Container cont,
258: ComponentChooser chooser, int index) {
259: return ((TextArea) waitComponent(cont, new TextAreaFinder(
260: chooser), index));
261: }
262:
263: /**
264: * Waits 0'th TextArea in container.
265: * @param cont Container to search component in.
266: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
267: * @return TextArea instance.
268: */
269: public static TextArea waitTextArea(Container cont,
270: ComponentChooser chooser) {
271: return (waitTextArea(cont, chooser, 0));
272: }
273:
274: /**
275: * Waits TextArea by text.
276: * @param cont Container to search component in.
277: * @param text TextArea text. If null, contents is not checked.
278: * @param ce Compare text exactly.
279: * @param ccs Compare text case sensitively.
280: * @param index Ordinal component index.
281: * @return TextArea instance.
282: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
283: */
284: public static TextArea waitTextArea(Container cont, String text,
285: boolean ce, boolean ccs, int index) {
286: return (waitTextArea(cont, new TextAreaByTextFinder(text,
287: new DefaultStringComparator(ce, ccs)), index));
288: }
289:
290: /**
291: * Waits TextArea by text.
292: * @param cont Container to search component in.
293: * @param text TextArea text. If null, contents is not checked.
294: * @param ce Compare text exactly.
295: * @param ccs Compare text case sensitively.
296: * @return TextArea instance.
297: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
298: */
299: public static TextArea waitTextArea(Container cont, String text,
300: boolean ce, boolean ccs) {
301: return (waitTextArea(cont, text, ce, ccs, 0));
302: }
303:
304: static {
305: Timeouts.initDefault("TextAreaOperator.PushKeyTimeout",
306: PUSH_KEY_TIMEOUT);
307: Timeouts.initDefault("TextAreaOperator.BetweenKeysTimeout",
308: BETWEEN_KEYS_TIMEOUT);
309: Timeouts.initDefault(
310: "TextAreaOperator.ChangeCaretPositionTimeout",
311: CHANGE_CARET_POSITION_TIMEOUT);
312: Timeouts.initDefault("TextAreaOperator.TypeTextTimeout",
313: TYPE_TEXT_TIMEOUT);
314: }
315:
316: public void setTimeouts(Timeouts timeouts) {
317: super .setTimeouts(timeouts);
318: this .timeouts = timeouts;
319: }
320:
321: public Timeouts getTimeouts() {
322: return (timeouts);
323: }
324:
325: public void setOutput(TestOut out) {
326: output = out;
327: super .setOutput(output.createErrorOutput());
328: }
329:
330: public TestOut getOutput() {
331: return (output);
332: }
333:
334: public Hashtable getDump() {
335: Hashtable result = super .getDump();
336: result.put(TEXT_DPROP, ((TextArea) getSource()).getText());
337: return (result);
338: }
339:
340: ////////////////////////////////////////////////////////
341: //Mapping //
342:
343: /**Maps <code>TextArea.getColumns()</code> through queue*/
344: public int getColumns() {
345: return (runMapping(new MapIntegerAction("getColumns") {
346: public int map() {
347: return (((TextArea) getSource()).getColumns());
348: }
349: }));
350: }
351:
352: /**Maps <code>TextArea.getMinimumSize(int, int)</code> through queue*/
353: public Dimension getMinimumSize(final int i, final int i1) {
354: return ((Dimension) runMapping(new MapAction("getMinimumSize") {
355: public Object map() {
356: return (((TextArea) getSource()).getMinimumSize(i, i1));
357: }
358: }));
359: }
360:
361: /**Maps <code>TextArea.getPreferredSize(int, int)</code> through queue*/
362: public Dimension getPreferredSize(final int i, final int i1) {
363: return ((Dimension) runMapping(new MapAction("getPreferredSize") {
364: public Object map() {
365: return (((TextArea) getSource())
366: .getPreferredSize(i, i1));
367: }
368: }));
369: }
370:
371: /**Maps <code>TextArea.getRows()</code> through queue*/
372: public int getRows() {
373: return (runMapping(new MapIntegerAction("getRows") {
374: public int map() {
375: return (((TextArea) getSource()).getRows());
376: }
377: }));
378: }
379:
380: /**Maps <code>TextArea.getScrollbarVisibility()</code> through queue*/
381: public int getScrollbarVisibility() {
382: return (runMapping(new MapIntegerAction(
383: "getScrollbarVisibility") {
384: public int map() {
385: return (((TextArea) getSource())
386: .getScrollbarVisibility());
387: }
388: }));
389: }
390:
391: /**Maps <code>TextArea.replaceRange(String, int, int)</code> through queue*/
392: public void replaceRange(final String string, final int i,
393: final int i1) {
394: runMapping(new MapVoidAction("replaceRange") {
395: public void map() {
396: ((TextArea) getSource()).replaceRange(string, i, i1);
397: }
398: });
399: }
400:
401: /**Maps <code>TextArea.setColumns(int)</code> through queue*/
402: public void setColumns(final int i) {
403: runMapping(new MapVoidAction("setColumns") {
404: public void map() {
405: ((TextArea) getSource()).setColumns(i);
406: }
407: });
408: }
409:
410: /**Maps <code>TextArea.setRows(int)</code> through queue*/
411: public void setRows(final int i) {
412: runMapping(new MapVoidAction("setRows") {
413: public void map() {
414: ((TextArea) getSource()).setRows(i);
415: }
416: });
417: }
418:
419: //End of mapping //
420: ////////////////////////////////////////////////////////
421:
422: /**
423: * Allows to find component by text.
424: */
425: public static class TextAreaByTextFinder implements
426: ComponentChooser {
427: String label;
428: StringComparator comparator;
429:
430: /**
431: * Constructs TextAreaByTextFinder.
432: * @param lb a text pattern
433: * @param comparator specifies string comparision algorithm.
434: */
435: public TextAreaByTextFinder(String lb,
436: StringComparator comparator) {
437: label = lb;
438: this .comparator = comparator;
439: }
440:
441: /**
442: * Constructs TextAreaByTextFinder.
443: * @param lb a text pattern
444: */
445: public TextAreaByTextFinder(String lb) {
446: this (lb, Operator.getDefaultStringComparator());
447: }
448:
449: public boolean checkComponent(Component comp) {
450: if (comp instanceof TextArea) {
451: if (((TextArea) comp).getText() != null) {
452: return (comparator.equals(((TextArea) comp)
453: .getText(), label));
454: }
455: }
456: return (false);
457: }
458:
459: public String getDescription() {
460: return ("TextArea with text \"" + label + "\"");
461: }
462: }
463:
464: /**
465: * Checks component type.
466: */
467: public static class TextAreaFinder extends Finder {
468: /**
469: * Constructs TextAreaFinder.
470: * @param sf other searching criteria.
471: */
472: public TextAreaFinder(ComponentChooser sf) {
473: super (TextArea.class, sf);
474: }
475:
476: /**
477: * Constructs TextAreaFinder.
478: */
479: public TextAreaFinder() {
480: super (TextArea.class);
481: }
482: }
483: }
|