JTextAreaOperator.java revision 13978:1993af50385d
1/*
2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.netbeans.jemmy.operators;
24
25import java.awt.Container;
26import java.util.Hashtable;
27
28import javax.swing.JTextArea;
29import javax.swing.text.BadLocationException;
30
31import org.netbeans.jemmy.ComponentChooser;
32import org.netbeans.jemmy.JemmyException;
33import org.netbeans.jemmy.Outputable;
34import org.netbeans.jemmy.TestOut;
35import org.netbeans.jemmy.TimeoutExpiredException;
36import org.netbeans.jemmy.Timeoutable;
37import org.netbeans.jemmy.Timeouts;
38
39/**
40 *
41 * Class provides basic functions to operate with JTextArea (selection, typing,
42 * deleting)
43 *
44 * <BR><BR>Timeouts used: <BR>
45 * JTextComponentOperator.PushKeyTimeout - time between key pressing and
46 * releasing during text typing <BR>
47 * JTextComponentOperator.BetweenKeysTimeout - time to sleep between two chars
48 * typing <BR>
49 * JTextComponentOperator.ChangeCaretPositionTimeout - maximum time to chenge
50 * caret position <BR>
51 * JTextComponentOperator.TypeTextTimeout - maximum time to type text <BR>
52 * ComponentOperator.WaitComponentTimeout - time to wait component displayed
53 * <BR>
54 * ComponentOperator.WaitFocusTimeout - time to wait component focus <BR>
55 * JScrollBarOperator.OneScrollClickTimeout - time for one scroll click <BR>
56 * JScrollBarOperator.WholeScrollTimeout - time for the whole scrolling <BR>.
57 *
58 * @see org.netbeans.jemmy.Timeouts
59 *
60 * @author Alexandre Iline (alexandre.iline@oracle.com)
61 */
62public class JTextAreaOperator extends JTextComponentOperator
63        implements Timeoutable, Outputable {
64
65    /**
66     * Identifier for a "column count" property.
67     *
68     * @see #getDump
69     */
70    public static final String COLUMN_COUNT_DPROP = "Column count";
71
72    /**
73     * Identifier for a "row count" property.
74     *
75     * @see #getDump
76     */
77    public static final String ROW_COUNT_DPROP = "Row count";
78
79    private Timeouts timeouts;
80    private TestOut output;
81
82    /**
83     * Constructor.
84     *
85     * @param b a component
86     */
87    public JTextAreaOperator(JTextArea b) {
88        super(b);
89    }
90
91    /**
92     * Constructs a JTextAreaOperator object.
93     *
94     * @param cont a container
95     * @param chooser a component chooser specifying searching criteria.
96     * @param index an index between appropriate ones.
97     */
98    public JTextAreaOperator(ContainerOperator<?> cont, ComponentChooser chooser, int index) {
99        this((JTextArea) cont.
100                waitSubComponent(new JTextAreaFinder(chooser),
101                        index));
102        copyEnvironment(cont);
103    }
104
105    /**
106     * Constructs a JTextAreaOperator object.
107     *
108     * @param cont a container
109     * @param chooser a component chooser specifying searching criteria.
110     */
111    public JTextAreaOperator(ContainerOperator<?> cont, ComponentChooser chooser) {
112        this(cont, chooser, 0);
113    }
114
115    /**
116     * Constructor. Waits component in container first. Uses cont's timeout and
117     * output for waiting and to init operator.
118     *
119     * @param cont a container
120     * @param text Button text.
121     * @param index Ordinal component index.
122     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
123     * @throws TimeoutExpiredException
124     */
125    public JTextAreaOperator(ContainerOperator<?> cont, String text, int index) {
126        this((JTextArea) waitComponent(cont,
127                new JTextAreaFinder(new JTextComponentOperator.JTextComponentByTextFinder(text,
128                        cont.getComparator())),
129                index));
130        copyEnvironment(cont);
131    }
132
133    /**
134     * Constructor. Waits component in container first. Uses cont's timeout and
135     * output for waiting and to init operator.
136     *
137     * @param cont a container
138     * @param text Button text.
139     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
140     * @throws TimeoutExpiredException
141     */
142    public JTextAreaOperator(ContainerOperator<?> cont, String text) {
143        this(cont, text, 0);
144    }
145
146    /**
147     * Constructor. Waits component in container first. Uses cont's timeout and
148     * output for waiting and to init operator.
149     *
150     * @param cont a container
151     * @param index Ordinal component index.
152     * @throws TimeoutExpiredException
153     */
154    public JTextAreaOperator(ContainerOperator<?> cont, int index) {
155        this((JTextArea) waitComponent(cont,
156                new JTextAreaFinder(),
157                index));
158        copyEnvironment(cont);
159    }
160
161    /**
162     * Constructor. Waits component in container first. Uses cont's timeout and
163     * output for waiting and to init operator.
164     *
165     * @param cont a container
166     * @throws TimeoutExpiredException
167     */
168    public JTextAreaOperator(ContainerOperator<?> cont) {
169        this(cont, 0);
170    }
171
172    /**
173     * Searches JTextArea in container.
174     *
175     * @param cont Container to search component in.
176     * @param chooser a component chooser specifying searching criteria.
177     * @param index Ordinal component index.
178     * @return JTextArea instance or null if component was not found.
179     */
180    public static JTextArea findJTextArea(Container cont, ComponentChooser chooser, int index) {
181        return (JTextArea) findJTextComponent(cont, new JTextAreaFinder(chooser), index);
182    }
183
184    /**
185     * Searches JTextArea in container.
186     *
187     * @param cont Container to search component in.
188     * @param chooser a component chooser specifying searching criteria.
189     * @return JTextArea instance or null if component was not found.
190     */
191    public static JTextArea findJTextArea(Container cont, ComponentChooser chooser) {
192        return findJTextArea(cont, chooser, 0);
193    }
194
195    /**
196     * Searches JTextArea by text.
197     *
198     * @param cont Container to search component in.
199     * @param text Component text.
200     * @param ce Compare text exactly.
201     * @param ccs Compare text case sensitively.
202     * @param index Ordinal component index.
203     * @return JTextArea instance or null if component was not found.
204     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
205     */
206    public static JTextArea findJTextArea(Container cont, String text, boolean ce, boolean ccs, int index) {
207        return (findJTextArea(cont,
208                new JTextAreaFinder(new JTextComponentOperator.JTextComponentByTextFinder(text,
209                        new DefaultStringComparator(ce, ccs))),
210                index));
211    }
212
213    /**
214     * Searches JTextArea by text.
215     *
216     * @param cont Container to search component in.
217     * @param text Component text.
218     * @param ce Compare text exactly.
219     * @param ccs Compare text case sensitively.
220     * @return JTextArea instance or null if component was not found.
221     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
222     */
223    public static JTextArea findJTextArea(Container cont, String text, boolean ce, boolean ccs) {
224        return findJTextArea(cont, text, ce, ccs, 0);
225    }
226
227    /**
228     * Waits JTextArea in container.
229     *
230     * @param cont Container to search component in.
231     * @param chooser a component chooser specifying searching criteria.
232     * @param index Ordinal component index.
233     * @return JTextArea instance.
234     * @throws TimeoutExpiredException
235     */
236    public static JTextArea waitJTextArea(Container cont, ComponentChooser chooser, int index) {
237        return (JTextArea) waitJTextComponent(cont, new JTextAreaFinder(chooser), index);
238    }
239
240    /**
241     * Waits JTextArea in container.
242     *
243     * @param cont Container to search component in.
244     * @param chooser a component chooser specifying searching criteria.
245     * @return JTextArea instance.
246     * @throws TimeoutExpiredException
247     */
248    public static JTextArea waitJTextArea(Container cont, ComponentChooser chooser) {
249        return waitJTextArea(cont, chooser, 0);
250    }
251
252    /**
253     * Waits JTextArea by text.
254     *
255     * @param cont Container to search component in.
256     * @param text Component text.
257     * @param ce Compare text exactly.
258     * @param ccs Compare text case sensitively.
259     * @param index Ordinal component index.
260     * @return JTextArea instance.
261     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
262     * @throws TimeoutExpiredException
263     */
264    public static JTextArea waitJTextArea(Container cont, String text, boolean ce, boolean ccs, int index) {
265        return (waitJTextArea(cont,
266                new JTextAreaFinder(new JTextComponentOperator.JTextComponentByTextFinder(text,
267                        new DefaultStringComparator(ce, ccs))),
268                index));
269    }
270
271    /**
272     * Waits JTextArea by text.
273     *
274     * @param cont Container to search component in.
275     * @param text Component text.
276     * @param ce Compare text exactly.
277     * @param ccs Compare text case sensitively.
278     * @return JTextArea instance.
279     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
280     * @throws TimeoutExpiredException
281     */
282    public static JTextArea waitJTextArea(Container cont, String text, boolean ce, boolean ccs) {
283        return waitJTextArea(cont, text, ce, ccs, 0);
284    }
285
286    @Override
287    public void setTimeouts(Timeouts times) {
288        timeouts = times;
289        super.setTimeouts(timeouts);
290    }
291
292    @Override
293    public Timeouts getTimeouts() {
294        return timeouts;
295    }
296
297    @Override
298    public void setOutput(TestOut out) {
299        output = out;
300        super.setOutput(output.createErrorOutput());
301    }
302
303    @Override
304    public TestOut getOutput() {
305        return output;
306    }
307
308    /**
309     * Notifies whether "PageUp" and "PageDown" should be used to change caret
310     * position. If can be useful if text takes some pages.
311     *
312     * @param yesOrNo if page navigation keys need to be used.
313     * @deprecated All text operations are performed by TextDriver regitered for
314     * this operator type.
315     */
316    @Deprecated
317    public void usePageNavigationKeys(boolean yesOrNo) {
318    }
319
320    /**
321     * Moves caret to line.
322     *
323     * @param row Line to move caret to.
324     * @see JTextComponentOperator#changeCaretPosition(int)
325     * @see #changeCaretPosition(int)
326     * @see #changeCaretPosition(int, int)
327     * @throws TimeoutExpiredException
328     */
329    public void changeCaretRow(int row) {
330        changeCaretPosition(row, getCaretPosition()
331                - getLineStartOffset(getLineOfOffset(getCaretPosition())));
332    }
333
334    /**
335     * Moves caret.
336     *
337     * @param row Line to move caret to.
338     * @param column Column to move caret to.
339     * @see JTextComponentOperator#changeCaretPosition(int)
340     * @see #changeCaretRow(int)
341     * @see #changeCaretPosition(int, int)
342     * @throws TimeoutExpiredException
343     */
344    public void changeCaretPosition(int row, int column) {
345        int startOffset = getLineStartOffset(row);
346        int endOffset = getLineEndOffset(row);
347        super.changeCaretPosition(getLineStartOffset(row)
348                + ((column <= (endOffset - startOffset))
349                        ? column
350                        : (endOffset - startOffset)));
351    }
352
353    /**
354     * Types text.
355     *
356     * @param text Text to be typed.
357     * @param row Line to type text in.
358     * @param column Column to type text from.
359     * @see JTextComponentOperator#typeText(String, int)
360     * @throws TimeoutExpiredException
361     */
362    public void typeText(String text, int row, int column) {
363        if (!hasFocus()) {
364            makeComponentVisible();
365        }
366        changeCaretPosition(row, column);
367        typeText(text);
368    }
369
370    /**
371     * Select a part of text.
372     *
373     * @param startRow Start position row.
374     * @param startColumn Start position column.
375     * @param endRow End position row.
376     * @param endColumn End position column.
377     * @see JTextComponentOperator#selectText(int, int)
378     * @see #selectLines(int, int)
379     * @throws TimeoutExpiredException
380     */
381    public void selectText(int startRow, int startColumn,
382            int endRow, int endColumn) {
383        int startPos = 0;
384        try {
385            startPos = getLineStartOffset(startRow) + startColumn;
386        } catch (JemmyException e) {
387            if (!(e.getInnerThrowable() instanceof BadLocationException)) {
388                throw (e);
389            }
390        }
391        int endPos = getText().length();
392        try {
393            endPos = getLineStartOffset(endRow) + endColumn;
394        } catch (JemmyException e) {
395            if (!(e.getInnerThrowable() instanceof BadLocationException)) {
396                throw (e);
397            }
398        }
399        selectText(startPos, endPos);
400    }
401
402    /**
403     * Select some text lines.
404     *
405     * @param startLine start selection
406     * @param endLine end selection
407     * @see JTextComponentOperator#selectText(int, int)
408     * @see #selectText(int, int, int, int)
409     * @throws TimeoutExpiredException
410     */
411    public void selectLines(int startLine, int endLine) {
412        if (!hasFocus()) {
413            makeComponentVisible();
414        }
415        selectText(startLine, 0, endLine + 1, 0);
416    }
417
418    /**
419     * Returns information about component.
420     */
421    @Override
422    public Hashtable<String, Object> getDump() {
423        Hashtable<String, Object> result = super.getDump();
424        result.put(COLUMN_COUNT_DPROP, Integer.toString(((JTextArea) getSource()).getRows()));
425        result.put(ROW_COUNT_DPROP, Integer.toString(((JTextArea) getSource()).getColumns()));
426        return result;
427    }
428
429    ////////////////////////////////////////////////////////
430    //Mapping                                             //
431    /**
432     * Maps {@code JTextArea.append(String)} through queue
433     */
434    public void append(final String string) {
435        runMapping(new MapVoidAction("append") {
436            @Override
437            public void map() {
438                ((JTextArea) getSource()).append(string);
439            }
440        });
441    }
442
443    /**
444     * Maps {@code JTextArea.getColumns()} through queue
445     */
446    public int getColumns() {
447        return (runMapping(new MapIntegerAction("getColumns") {
448            @Override
449            public int map() {
450                return ((JTextArea) getSource()).getColumns();
451            }
452        }));
453    }
454
455    /**
456     * Maps {@code JTextArea.getLineCount()} through queue
457     */
458    public int getLineCount() {
459        return (runMapping(new MapIntegerAction("getLineCount") {
460            @Override
461            public int map() {
462                return ((JTextArea) getSource()).getLineCount();
463            }
464        }));
465    }
466
467    /**
468     * Maps {@code JTextArea.getLineEndOffset(int)} through queue
469     */
470    public int getLineEndOffset(final int i) {
471        return (runMapping(new MapIntegerAction("getLineEndOffset") {
472            @Override
473            public int map() throws BadLocationException {
474                return ((JTextArea) getSource()).getLineEndOffset(i);
475            }
476        }));
477    }
478
479    /**
480     * Maps {@code JTextArea.getLineOfOffset(int)} through queue
481     */
482    public int getLineOfOffset(final int i) {
483        return (runMapping(new MapIntegerAction("getLineOfOffset") {
484            @Override
485            public int map() throws BadLocationException {
486                return ((JTextArea) getSource()).getLineOfOffset(i);
487            }
488        }));
489    }
490
491    /**
492     * Maps {@code JTextArea.getLineStartOffset(int)} through queue
493     */
494    public int getLineStartOffset(final int i) {
495        return (runMapping(new MapIntegerAction("getLineStartOffset") {
496            @Override
497            public int map() throws BadLocationException {
498                return ((JTextArea) getSource()).getLineStartOffset(i);
499            }
500        }));
501    }
502
503    /**
504     * Maps {@code JTextArea.getLineWrap()} through queue
505     */
506    public boolean getLineWrap() {
507        return (runMapping(new MapBooleanAction("getLineWrap") {
508            @Override
509            public boolean map() {
510                return ((JTextArea) getSource()).getLineWrap();
511            }
512        }));
513    }
514
515    /**
516     * Maps {@code JTextArea.getRows()} through queue
517     */
518    public int getRows() {
519        return (runMapping(new MapIntegerAction("getRows") {
520            @Override
521            public int map() {
522                return ((JTextArea) getSource()).getRows();
523            }
524        }));
525    }
526
527    /**
528     * Maps {@code JTextArea.getTabSize()} through queue
529     */
530    public int getTabSize() {
531        return (runMapping(new MapIntegerAction("getTabSize") {
532            @Override
533            public int map() {
534                return ((JTextArea) getSource()).getTabSize();
535            }
536        }));
537    }
538
539    /**
540     * Maps {@code JTextArea.getWrapStyleWord()} through queue
541     */
542    public boolean getWrapStyleWord() {
543        return (runMapping(new MapBooleanAction("getWrapStyleWord") {
544            @Override
545            public boolean map() {
546                return ((JTextArea) getSource()).getWrapStyleWord();
547            }
548        }));
549    }
550
551    /**
552     * Maps {@code JTextArea.insert(String, int)} through queue
553     */
554    public void insert(final String string, final int i) {
555        runMapping(new MapVoidAction("insert") {
556            @Override
557            public void map() {
558                ((JTextArea) getSource()).insert(string, i);
559            }
560        });
561    }
562
563    /**
564     * Maps {@code JTextArea.replaceRange(String, int, int)} through queue
565     */
566    public void replaceRange(final String string, final int i, final int i1) {
567        runMapping(new MapVoidAction("replaceRange") {
568            @Override
569            public void map() {
570                ((JTextArea) getSource()).replaceRange(string, i, i1);
571            }
572        });
573    }
574
575    /**
576     * Maps {@code JTextArea.setColumns(int)} through queue
577     */
578    public void setColumns(final int i) {
579        runMapping(new MapVoidAction("setColumns") {
580            @Override
581            public void map() {
582                ((JTextArea) getSource()).setColumns(i);
583            }
584        });
585    }
586
587    /**
588     * Maps {@code JTextArea.setLineWrap(boolean)} through queue
589     */
590    public void setLineWrap(final boolean b) {
591        runMapping(new MapVoidAction("setLineWrap") {
592            @Override
593            public void map() {
594                ((JTextArea) getSource()).setLineWrap(b);
595            }
596        });
597    }
598
599    /**
600     * Maps {@code JTextArea.setRows(int)} through queue
601     */
602    public void setRows(final int i) {
603        runMapping(new MapVoidAction("setRows") {
604            @Override
605            public void map() {
606                ((JTextArea) getSource()).setRows(i);
607            }
608        });
609    }
610
611    /**
612     * Maps {@code JTextArea.setTabSize(int)} through queue
613     */
614    public void setTabSize(final int i) {
615        runMapping(new MapVoidAction("setTabSize") {
616            @Override
617            public void map() {
618                ((JTextArea) getSource()).setTabSize(i);
619            }
620        });
621    }
622
623    /**
624     * Maps {@code JTextArea.setWrapStyleWord(boolean)} through queue
625     */
626    public void setWrapStyleWord(final boolean b) {
627        runMapping(new MapVoidAction("setWrapStyleWord") {
628            @Override
629            public void map() {
630                ((JTextArea) getSource()).setWrapStyleWord(b);
631            }
632        });
633    }
634
635    //End of mapping                                      //
636    ////////////////////////////////////////////////////////
637    /**
638     * Checks component type.
639     */
640    public static class JTextAreaFinder extends Finder {
641
642        /**
643         * Constructs JTextAreaFinder.
644         *
645         * @param sf other searching criteria.
646         */
647        public JTextAreaFinder(ComponentChooser sf) {
648            super(JTextArea.class, sf);
649        }
650
651        /**
652         * Constructs JTextAreaFinder.
653         */
654        public JTextAreaFinder() {
655            super(JTextArea.class);
656        }
657    }
658}
659