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: JTextAreaOperator.java,v 1.10 2007/10/05 11:35:34 jskrivanek Exp $ $Revision: 1.10 $ $Date: 2007/10/05 11:35:34 $
044: *
045: */
046:
047: package org.netbeans.jemmy.operators;
048:
049: import org.netbeans.jemmy.Action;
050: import org.netbeans.jemmy.ActionProducer;
051: import org.netbeans.jemmy.ComponentChooser;
052: import org.netbeans.jemmy.ComponentSearcher;
053: import org.netbeans.jemmy.JemmyException;
054: import org.netbeans.jemmy.JemmyProperties;
055: import org.netbeans.jemmy.Outputable;
056: import org.netbeans.jemmy.TestOut;
057: import org.netbeans.jemmy.Timeoutable;
058: import org.netbeans.jemmy.TimeoutExpiredException;
059: import org.netbeans.jemmy.Timeouts;
060:
061: import java.awt.Component;
062: import java.awt.Container;
063:
064: import java.awt.event.InputEvent;
065: import java.awt.event.KeyEvent;
066:
067: import java.util.Hashtable;
068:
069: import javax.swing.JTextArea;
070:
071: import javax.swing.text.BadLocationException;
072:
073: /**
074: *
075: * Class provides basic functions to operate with JTextArea
076: * (selection, typing, deleting)
077: *
078: * <BR><BR>Timeouts used: <BR>
079: * JTextComponentOperator.PushKeyTimeout - time between key pressing and releasing during text typing <BR>
080: * JTextComponentOperator.BetweenKeysTimeout - time to sleep between two chars typing <BR>
081: * JTextComponentOperator.ChangeCaretPositionTimeout - maximum time to chenge caret position <BR>
082: * JTextComponentOperator.TypeTextTimeout - maximum time to type text <BR>
083: * ComponentOperator.WaitComponentTimeout - time to wait component displayed <BR>
084: * ComponentOperator.WaitFocusTimeout - time to wait component focus <BR>
085: * JScrollBarOperator.OneScrollClickTimeout - time for one scroll click <BR>
086: * JScrollBarOperator.WholeScrollTimeout - time for the whole scrolling <BR>.
087: *
088: * @see org.netbeans.jemmy.Timeouts
089: *
090: * @author Alexandre Iline (alexandre.iline@sun.com)
091: */
092:
093: public class JTextAreaOperator extends JTextComponentOperator implements
094: Timeoutable, Outputable {
095:
096: /**
097: * Identifier for a "column count" property.
098: * @see #getDump
099: */
100: public static final String COLUMN_COUNT_DPROP = "Column count";
101:
102: /**
103: * Identifier for a "row count" property.
104: * @see #getDump
105: */
106: public static final String ROW_COUNT_DPROP = "Row count";
107:
108: private Timeouts timeouts;
109: private TestOut output;
110: private boolean pageNavigation = false;
111:
112: /**
113: * Constructor.
114: * @param b a component
115: */
116: public JTextAreaOperator(JTextArea b) {
117: super (b);
118: }
119:
120: /**
121: * Constructs a JTextAreaOperator object.
122: * @param cont a container
123: * @param chooser a component chooser specifying searching criteria.
124: * @param index an index between appropriate ones.
125: */
126: public JTextAreaOperator(ContainerOperator cont,
127: ComponentChooser chooser, int index) {
128: this ((JTextArea) cont.waitSubComponent(new JTextAreaFinder(
129: chooser), index));
130: copyEnvironment(cont);
131: }
132:
133: /**
134: * Constructs a JTextAreaOperator object.
135: * @param cont a container
136: * @param chooser a component chooser specifying searching criteria.
137: */
138: public JTextAreaOperator(ContainerOperator cont,
139: ComponentChooser chooser) {
140: this (cont, chooser, 0);
141: }
142:
143: /**
144: * Constructor.
145: * Waits component in container first.
146: * Uses cont's timeout and output for waiting and to init operator.
147: * @param cont a container
148: * @param text Button text.
149: * @param index Ordinal component index.
150: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
151: * @throws TimeoutExpiredException
152: */
153: public JTextAreaOperator(ContainerOperator cont, String text,
154: int index) {
155: this ((JTextArea) waitComponent(cont, new JTextAreaFinder(
156: new JTextComponentOperator.JTextComponentByTextFinder(
157: text, cont.getComparator())), index));
158: copyEnvironment(cont);
159: }
160:
161: /**
162: * Constructor.
163: * Waits component in container first.
164: * Uses cont's timeout and output for waiting and to init operator.
165: * @param cont a container
166: * @param text Button text.
167: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
168: * @throws TimeoutExpiredException
169: */
170: public JTextAreaOperator(ContainerOperator cont, String text) {
171: this (cont, text, 0);
172: }
173:
174: /**
175: * Constructor.
176: * Waits component in container first.
177: * Uses cont's timeout and output for waiting and to init operator.
178: * @param cont a container
179: * @param index Ordinal component index.
180: * @throws TimeoutExpiredException
181: */
182: public JTextAreaOperator(ContainerOperator cont, int index) {
183: this ((JTextArea) waitComponent(cont, new JTextAreaFinder(),
184: index));
185: copyEnvironment(cont);
186: }
187:
188: /**
189: * Constructor.
190: * Waits component in container first.
191: * Uses cont's timeout and output for waiting and to init operator.
192: * @param cont a container
193: * @throws TimeoutExpiredException
194: */
195: public JTextAreaOperator(ContainerOperator cont) {
196: this (cont, 0);
197: }
198:
199: /**
200: * Searches JTextArea in container.
201: * @param cont Container to search component in.
202: * @param chooser a component chooser specifying searching criteria.
203: * @param index Ordinal component index.
204: * @return JTextArea instance or null if component was not found.
205: */
206: public static JTextArea findJTextArea(Container cont,
207: ComponentChooser chooser, int index) {
208: return ((JTextArea) findJTextComponent(cont,
209: new JTextAreaFinder(chooser), index));
210: }
211:
212: /**
213: * Searches JTextArea in container.
214: * @param cont Container to search component in.
215: * @param chooser a component chooser specifying searching criteria.
216: * @return JTextArea instance or null if component was not found.
217: */
218: public static JTextArea findJTextArea(Container cont,
219: ComponentChooser chooser) {
220: return (findJTextArea(cont, chooser, 0));
221: }
222:
223: /**
224: * Searches JTextArea by text.
225: * @param cont Container to search component in.
226: * @param text Component text.
227: * @param ce Compare text exactly.
228: * @param ccs Compare text case sensitively.
229: * @param index Ordinal component index.
230: * @return JTextArea instance or null if component was not found.
231: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
232: */
233: public static JTextArea findJTextArea(Container cont, String text,
234: boolean ce, boolean ccs, int index) {
235: return (findJTextArea(cont, new JTextAreaFinder(
236: new JTextComponentOperator.JTextComponentByTextFinder(
237: text, new DefaultStringComparator(ce, ccs))),
238: index));
239: }
240:
241: /**
242: * Searches JTextArea by text.
243: * @param cont Container to search component in.
244: * @param text Component text.
245: * @param ce Compare text exactly.
246: * @param ccs Compare text case sensitively.
247: * @return JTextArea instance or null if component was not found.
248: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
249: */
250: public static JTextArea findJTextArea(Container cont, String text,
251: boolean ce, boolean ccs) {
252: return (findJTextArea(cont, text, ce, ccs, 0));
253: }
254:
255: /**
256: * Waits JTextArea in container.
257: * @param cont Container to search component in.
258: * @param chooser a component chooser specifying searching criteria.
259: * @param index Ordinal component index.
260: * @return JTextArea instance.
261: * @throws TimeoutExpiredException
262: */
263: public static JTextArea waitJTextArea(Container cont,
264: ComponentChooser chooser, int index) {
265: return ((JTextArea) waitJTextComponent(cont,
266: new JTextAreaFinder(chooser), index));
267: }
268:
269: /**
270: * Waits JTextArea in container.
271: * @param cont Container to search component in.
272: * @param chooser a component chooser specifying searching criteria.
273: * @return JTextArea instance.
274: * @throws TimeoutExpiredException
275: */
276: public static JTextArea waitJTextArea(Container cont,
277: ComponentChooser chooser) {
278: return (waitJTextArea(cont, chooser, 0));
279: }
280:
281: /**
282: * Waits JTextArea by text.
283: * @param cont Container to search component in.
284: * @param text Component text.
285: * @param ce Compare text exactly.
286: * @param ccs Compare text case sensitively.
287: * @param index Ordinal component index.
288: * @return JTextArea instance.
289: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
290: * @throws TimeoutExpiredException
291: */
292: public static JTextArea waitJTextArea(Container cont, String text,
293: boolean ce, boolean ccs, int index) {
294: return (waitJTextArea(cont, new JTextAreaFinder(
295: new JTextComponentOperator.JTextComponentByTextFinder(
296: text, new DefaultStringComparator(ce, ccs))),
297: index));
298: }
299:
300: /**
301: * Waits JTextArea by text.
302: * @param cont Container to search component in.
303: * @param text Component text.
304: * @param ce Compare text exactly.
305: * @param ccs Compare text case sensitively.
306: * @return JTextArea instance.
307: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
308: * @throws TimeoutExpiredException
309: */
310: public static JTextArea waitJTextArea(Container cont, String text,
311: boolean ce, boolean ccs) {
312: return (waitJTextArea(cont, text, ce, ccs, 0));
313: }
314:
315: public void setTimeouts(Timeouts times) {
316: timeouts = times;
317: super .setTimeouts(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: /**
334: * Notifies whether "PageUp" and "PageDown" should be used
335: * to change caret position. If can be useful if text takes
336: * some pages.
337: * @param yesOrNo if page navigation keys need to be used.
338: * @deprecated All text operations are performed by TextDriver regitered for this operator type.
339: */
340: public void usePageNavigationKeys(boolean yesOrNo) {
341: pageNavigation = yesOrNo;
342: }
343:
344: /**
345: * Moves caret to line.
346: * @param row Line to move caret to.
347: * @see JTextComponentOperator#changeCaretPosition(int)
348: * @see #changeCaretPosition(int)
349: * @see #changeCaretPosition(int, int)
350: * @throws TimeoutExpiredException
351: */
352: public void changeCaretRow(int row) {
353: changeCaretPosition(
354: row,
355: getCaretPosition()
356: - getLineStartOffset(getLineOfOffset(getCaretPosition())));
357: }
358:
359: /**
360: * Moves caret.
361: * @param row Line to move caret to.
362: * @param column Column to move caret to.
363: * @see JTextComponentOperator#changeCaretPosition(int)
364: * @see #changeCaretRow(int)
365: * @see #changeCaretPosition(int, int)
366: * @throws TimeoutExpiredException
367: */
368: public void changeCaretPosition(int row, int column) {
369: int startOffset = getLineStartOffset(row);
370: int endOffset = getLineEndOffset(row);
371: super .changeCaretPosition(getLineStartOffset(row)
372: + ((column <= (endOffset - startOffset)) ? column
373: : (endOffset - startOffset)));
374: }
375:
376: /**
377: * Types text.
378: * @param text Text to be typed.
379: * @param row Line to type text in.
380: * @param column Column to type text from.
381: * @see JTextComponentOperator#typeText(String, int)
382: * @throws TimeoutExpiredException
383: */
384: public void typeText(String text, int row, int column) {
385: if (!hasFocus()) {
386: makeComponentVisible();
387: }
388: changeCaretPosition(row, column);
389: typeText(text);
390: }
391:
392: /**
393: * Select a part of text.
394: * @param startRow Start position row.
395: * @param startColumn Start position column.
396: * @param endRow End position row.
397: * @param endColumn End position column.
398: * @see JTextComponentOperator#selectText(int, int)
399: * @see #selectLines(int, int)
400: * @throws TimeoutExpiredException
401: */
402: public void selectText(int startRow, int startColumn, int endRow,
403: int endColumn) {
404: int startPos = 0;
405: try {
406: startPos = getLineStartOffset(startRow) + startColumn;
407: } catch (JemmyException e) {
408: if (!(e.getInnerException() instanceof BadLocationException)) {
409: throw (e);
410: }
411: }
412: int endPos = getText().length();
413: try {
414: endPos = getLineStartOffset(endRow) + endColumn;
415: } catch (JemmyException e) {
416: if (!(e.getInnerException() instanceof BadLocationException)) {
417: throw (e);
418: }
419: }
420: selectText(startPos, endPos);
421: }
422:
423: /**
424: * Select some text lines.
425: * @param startLine start selection
426: * @param endLine end selection
427: * @see JTextComponentOperator#selectText(int, int)
428: * @see #selectText(int, int, int, int)
429: * @throws TimeoutExpiredException
430: */
431: public void selectLines(int startLine, int endLine) {
432: if (!hasFocus()) {
433: makeComponentVisible();
434: }
435: selectText(startLine, 0, endLine + 1, 0);
436: }
437:
438: /**
439: * Returns information about component.
440: */
441: public Hashtable getDump() {
442: Hashtable result = super .getDump();
443: result.put(COLUMN_COUNT_DPROP, Integer
444: .toString(((JTextArea) getSource()).getRows()));
445: result.put(ROW_COUNT_DPROP, Integer
446: .toString(((JTextArea) getSource()).getColumns()));
447: return (result);
448: }
449:
450: ////////////////////////////////////////////////////////
451: //Mapping //
452:
453: /**Maps <code>JTextArea.append(String)</code> through queue*/
454: public void append(final String string) {
455: runMapping(new MapVoidAction("append") {
456: public void map() {
457: ((JTextArea) getSource()).append(string);
458: }
459: });
460: }
461:
462: /**Maps <code>JTextArea.getColumns()</code> through queue*/
463: public int getColumns() {
464: return (runMapping(new MapIntegerAction("getColumns") {
465: public int map() {
466: return (((JTextArea) getSource()).getColumns());
467: }
468: }));
469: }
470:
471: /**Maps <code>JTextArea.getLineCount()</code> through queue*/
472: public int getLineCount() {
473: return (runMapping(new MapIntegerAction("getLineCount") {
474: public int map() {
475: return (((JTextArea) getSource()).getLineCount());
476: }
477: }));
478: }
479:
480: /**Maps <code>JTextArea.getLineEndOffset(int)</code> through queue*/
481: public int getLineEndOffset(final int i) {
482: return (runMapping(new MapIntegerAction("getLineEndOffset") {
483: public int map() throws BadLocationException {
484: return (((JTextArea) getSource()).getLineEndOffset(i));
485: }
486: }));
487: }
488:
489: /**Maps <code>JTextArea.getLineOfOffset(int)</code> through queue*/
490: public int getLineOfOffset(final int i) {
491: return (runMapping(new MapIntegerAction("getLineOfOffset") {
492: public int map() throws BadLocationException {
493: return (((JTextArea) getSource()).getLineOfOffset(i));
494: }
495: }));
496: }
497:
498: /**Maps <code>JTextArea.getLineStartOffset(int)</code> through queue*/
499: public int getLineStartOffset(final int i) {
500: return (runMapping(new MapIntegerAction("getLineStartOffset") {
501: public int map() throws BadLocationException {
502: return (((JTextArea) getSource()).getLineStartOffset(i));
503: }
504: }));
505: }
506:
507: /**Maps <code>JTextArea.getLineWrap()</code> through queue*/
508: public boolean getLineWrap() {
509: return (runMapping(new MapBooleanAction("getLineWrap") {
510: public boolean map() {
511: return (((JTextArea) getSource()).getLineWrap());
512: }
513: }));
514: }
515:
516: /**Maps <code>JTextArea.getRows()</code> through queue*/
517: public int getRows() {
518: return (runMapping(new MapIntegerAction("getRows") {
519: public int map() {
520: return (((JTextArea) getSource()).getRows());
521: }
522: }));
523: }
524:
525: /**Maps <code>JTextArea.getTabSize()</code> through queue*/
526: public int getTabSize() {
527: return (runMapping(new MapIntegerAction("getTabSize") {
528: public int map() {
529: return (((JTextArea) getSource()).getTabSize());
530: }
531: }));
532: }
533:
534: /**Maps <code>JTextArea.getWrapStyleWord()</code> through queue*/
535: public boolean getWrapStyleWord() {
536: return (runMapping(new MapBooleanAction("getWrapStyleWord") {
537: public boolean map() {
538: return (((JTextArea) getSource()).getWrapStyleWord());
539: }
540: }));
541: }
542:
543: /**Maps <code>JTextArea.insert(String, int)</code> through queue*/
544: public void insert(final String string, final int i) {
545: runMapping(new MapVoidAction("insert") {
546: public void map() {
547: ((JTextArea) getSource()).insert(string, i);
548: }
549: });
550: }
551:
552: /**Maps <code>JTextArea.replaceRange(String, int, int)</code> through queue*/
553: public void replaceRange(final String string, final int i,
554: final int i1) {
555: runMapping(new MapVoidAction("replaceRange") {
556: public void map() {
557: ((JTextArea) getSource()).replaceRange(string, i, i1);
558: }
559: });
560: }
561:
562: /**Maps <code>JTextArea.setColumns(int)</code> through queue*/
563: public void setColumns(final int i) {
564: runMapping(new MapVoidAction("setColumns") {
565: public void map() {
566: ((JTextArea) getSource()).setColumns(i);
567: }
568: });
569: }
570:
571: /**Maps <code>JTextArea.setLineWrap(boolean)</code> through queue*/
572: public void setLineWrap(final boolean b) {
573: runMapping(new MapVoidAction("setLineWrap") {
574: public void map() {
575: ((JTextArea) getSource()).setLineWrap(b);
576: }
577: });
578: }
579:
580: /**Maps <code>JTextArea.setRows(int)</code> through queue*/
581: public void setRows(final int i) {
582: runMapping(new MapVoidAction("setRows") {
583: public void map() {
584: ((JTextArea) getSource()).setRows(i);
585: }
586: });
587: }
588:
589: /**Maps <code>JTextArea.setTabSize(int)</code> through queue*/
590: public void setTabSize(final int i) {
591: runMapping(new MapVoidAction("setTabSize") {
592: public void map() {
593: ((JTextArea) getSource()).setTabSize(i);
594: }
595: });
596: }
597:
598: /**Maps <code>JTextArea.setWrapStyleWord(boolean)</code> through queue*/
599: public void setWrapStyleWord(final boolean b) {
600: runMapping(new MapVoidAction("setWrapStyleWord") {
601: public void map() {
602: ((JTextArea) getSource()).setWrapStyleWord(b);
603: }
604: });
605: }
606:
607: //End of mapping //
608: ////////////////////////////////////////////////////////
609:
610: /**
611: * Checks component type.
612: */
613: public static class JTextAreaFinder extends Finder {
614: /**
615: * Constructs JTextAreaFinder.
616: * @param sf other searching criteria.
617: */
618: public JTextAreaFinder(ComponentChooser sf) {
619: super (JTextArea.class, sf);
620: }
621:
622: /**
623: * Constructs JTextAreaFinder.
624: */
625: public JTextAreaFinder() {
626: super (JTextArea.class);
627: }
628: }
629: }
|