JTextFieldOperator.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.awt.event.ActionListener;
27
28import javax.swing.BoundedRangeModel;
29import javax.swing.JTextField;
30
31import org.netbeans.jemmy.ComponentChooser;
32import org.netbeans.jemmy.TimeoutExpiredException;
33
34/**
35 * <BR><BR>Timeouts used: <BR>
36 * JTextComponentOperator.PushKeyTimeout - time between key pressing and
37 * releasing during text typing <BR>
38 * JTextComponentOperator.BetweenKeysTimeout - time to sleep between two chars
39 * typing <BR>
40 * JTextComponentOperator.ChangeCaretPositionTimeout - maximum time to chenge
41 * caret position <BR>
42 * JTextComponentOperator.TypeTextTimeout - maximum time to type text <BR>
43 * ComponentOperator.WaitComponentTimeout - time to wait component displayed
44 * <BR>
45 * ComponentOperator.WaitFocusTimeout - time to wait component focus <BR>
46 * JScrollBarOperator.OneScrollClickTimeout - time for one scroll click <BR>
47 * JScrollBarOperator.WholeScrollTimeout - time for the whole scrolling <BR>.
48 *
49 * @see org.netbeans.jemmy.Timeouts
50 *
51 * @author Alexandre Iline (alexandre.iline@oracle.com)
52 *
53 */
54public class JTextFieldOperator extends JTextComponentOperator {
55
56    /**
57     * Constructor.
58     *
59     * @param b a component
60     */
61    public JTextFieldOperator(JTextField b) {
62        super(b);
63    }
64
65    /**
66     * Constructs a JTextFieldOperator object.
67     *
68     * @param cont a container
69     * @param chooser a component chooser specifying searching criteria.
70     * @param index an index between appropriate ones.
71     */
72    public JTextFieldOperator(ContainerOperator<?> cont, ComponentChooser chooser, int index) {
73        this((JTextField) cont.
74                waitSubComponent(new JTextFieldFinder(chooser),
75                        index));
76        copyEnvironment(cont);
77    }
78
79    /**
80     * Constructs a JTextFieldOperator object.
81     *
82     * @param cont a container
83     * @param chooser a component chooser specifying searching criteria.
84     */
85    public JTextFieldOperator(ContainerOperator<?> cont, ComponentChooser chooser) {
86        this(cont, chooser, 0);
87    }
88
89    /**
90     * Constructor. Waits component in container first. Uses cont's timeout and
91     * output for waiting and to init operator.
92     *
93     * @param cont a container
94     * @param text Button text.
95     * @param index Ordinal component index.
96     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
97     * @throws TimeoutExpiredException
98     */
99    public JTextFieldOperator(ContainerOperator<?> cont, String text, int index) {
100        this((JTextField) waitComponent(cont,
101                new JTextFieldFinder(new JTextComponentOperator.JTextComponentByTextFinder(text,
102                        cont.getComparator())),
103                index));
104        copyEnvironment(cont);
105    }
106
107    /**
108     * Constructor. Waits component in container first. Uses cont's timeout and
109     * output for waiting and to init operator.
110     *
111     * @param cont a container
112     * @param text Button text.
113     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
114     * @throws TimeoutExpiredException
115     */
116    public JTextFieldOperator(ContainerOperator<?> cont, String text) {
117        this(cont, text, 0);
118    }
119
120    /**
121     * Constructor. Waits component in container first. Uses cont's timeout and
122     * output for waiting and to init operator.
123     *
124     * @param cont a container
125     * @param index Ordinal component index.
126     * @throws TimeoutExpiredException
127     */
128    public JTextFieldOperator(ContainerOperator<?> cont, int index) {
129        this((JTextField) waitComponent(cont,
130                new JTextFieldFinder(),
131                index));
132        copyEnvironment(cont);
133    }
134
135    /**
136     * Constructor. Waits component in container first. Uses cont's timeout and
137     * output for waiting and to init operator.
138     *
139     * @param cont a container
140     * @throws TimeoutExpiredException
141     */
142    public JTextFieldOperator(ContainerOperator<?> cont) {
143        this(cont, 0);
144    }
145
146    /**
147     * Searches JTextField in container.
148     *
149     * @param cont Container to search component in.
150     * @param chooser a component chooser specifying searching criteria.
151     * @param index Ordinal component index.
152     * @return JTextField instance or null if component was not found.
153     */
154    public static JTextField findJTextField(Container cont, ComponentChooser chooser, int index) {
155        return (JTextField) findJTextComponent(cont, new JTextFieldFinder(chooser), index);
156    }
157
158    /**
159     * Searches JTextField in container.
160     *
161     * @param cont Container to search component in.
162     * @param chooser a component chooser specifying searching criteria.
163     * @return JTextField instance or null if component was not found.
164     */
165    public static JTextField findJTextField(Container cont, ComponentChooser chooser) {
166        return findJTextField(cont, chooser, 0);
167    }
168
169    /**
170     * Searches JTextField by text.
171     *
172     * @param cont Container to search component in.
173     * @param text Component text.
174     * @param ce Compare text exactly.
175     * @param ccs Compare text case sensitively.
176     * @param index Ordinal component index.
177     * @return JTextField instance or null if component was not found.
178     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
179     */
180    public static JTextField findJTextField(Container cont, String text, boolean ce, boolean ccs, int index) {
181        return (findJTextField(cont,
182                new JTextFieldFinder(new JTextComponentOperator.JTextComponentByTextFinder(text,
183                        new DefaultStringComparator(ce, ccs))),
184                index));
185    }
186
187    /**
188     * Searches JTextField by text.
189     *
190     * @param cont Container to search component in.
191     * @param text Component text.
192     * @param ce Compare text exactly.
193     * @param ccs Compare text case sensitively.
194     * @return JTextField instance or null if component was not found.
195     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
196     */
197    public static JTextField findJTextField(Container cont, String text, boolean ce, boolean ccs) {
198        return findJTextField(cont, text, ce, ccs, 0);
199    }
200
201    /**
202     * Waits JTextField in container.
203     *
204     * @param cont Container to search component in.
205     * @param chooser a component chooser specifying searching criteria.
206     * @param index Ordinal component index.
207     * @return JTextField instance.
208     * @throws TimeoutExpiredException
209     */
210    public static JTextField waitJTextField(Container cont, ComponentChooser chooser, int index) {
211        return (JTextField) waitJTextComponent(cont, new JTextFieldFinder(chooser), index);
212    }
213
214    /**
215     * Waits JTextField in container.
216     *
217     * @param cont Container to search component in.
218     * @param chooser a component chooser specifying searching criteria.
219     * @return JTextField instance.
220     * @throws TimeoutExpiredException
221     */
222    public static JTextField waitJTextField(Container cont, ComponentChooser chooser) {
223        return waitJTextField(cont, chooser, 0);
224    }
225
226    /**
227     * Waits JTextField by text.
228     *
229     * @param cont Container to search component in.
230     * @param text Component text.
231     * @param ce Compare text exactly.
232     * @param ccs Compare text case sensitively.
233     * @param index Ordinal component index.
234     * @return JTextField instance.
235     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
236     * @throws TimeoutExpiredException
237     */
238    public static JTextField waitJTextField(Container cont, String text, boolean ce, boolean ccs, int index) {
239        return (waitJTextField(cont,
240                new JTextFieldFinder(new JTextComponentOperator.JTextComponentByTextFinder(text,
241                        new DefaultStringComparator(ce, ccs))),
242                index));
243    }
244
245    /**
246     * Waits JTextField by text.
247     *
248     * @param cont Container to search component in.
249     * @param text Component text.
250     * @param ce Compare text exactly.
251     * @param ccs Compare text case sensitively.
252     * @return JTextField instance.
253     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
254     * @throws TimeoutExpiredException
255     */
256    public static JTextField waitJTextField(Container cont, String text, boolean ce, boolean ccs) {
257        return waitJTextField(cont, text, ce, ccs, 0);
258    }
259
260    /**
261     * Wait some text to be displayed starting from certain position.
262     *
263     * @param text a text to wait.
264     * @param position start position.
265     */
266    @Override
267    public void waitText(String text, int position) {
268        super.waitText(removeNewLines(text), position);
269    }
270
271    /**
272     * Wait some text to be displayed.
273     *
274     * @param text a text to wait.
275     */
276    @Override
277    public void waitText(String text) {
278        super.waitText(removeNewLines(text));
279    }
280
281    ////////////////////////////////////////////////////////
282    //Mapping                                             //
283    /**
284     * Maps {@code JTextField.addActionListener(ActionListener)} through queue
285     */
286    public void addActionListener(final ActionListener actionListener) {
287        runMapping(new MapVoidAction("addActionListener") {
288            @Override
289            public void map() {
290                ((JTextField) getSource()).addActionListener(actionListener);
291            }
292        });
293    }
294
295    /**
296     * Maps {@code JTextField.getColumns()} through queue
297     */
298    public int getColumns() {
299        return (runMapping(new MapIntegerAction("getColumns") {
300            @Override
301            public int map() {
302                return ((JTextField) getSource()).getColumns();
303            }
304        }));
305    }
306
307    /**
308     * Maps {@code JTextField.getHorizontalAlignment()} through queue
309     */
310    public int getHorizontalAlignment() {
311        return (runMapping(new MapIntegerAction("getHorizontalAlignment") {
312            @Override
313            public int map() {
314                return ((JTextField) getSource()).getHorizontalAlignment();
315            }
316        }));
317    }
318
319    /**
320     * Maps {@code JTextField.getHorizontalVisibility()} through queue
321     */
322    public BoundedRangeModel getHorizontalVisibility() {
323        return (runMapping(new MapAction<BoundedRangeModel>("getHorizontalVisibility") {
324            @Override
325            public BoundedRangeModel map() {
326                return ((JTextField) getSource()).getHorizontalVisibility();
327            }
328        }));
329    }
330
331    /**
332     * Maps {@code JTextField.getScrollOffset()} through queue
333     */
334    public int getScrollOffset() {
335        return (runMapping(new MapIntegerAction("getScrollOffset") {
336            @Override
337            public int map() {
338                return ((JTextField) getSource()).getScrollOffset();
339            }
340        }));
341    }
342
343    /**
344     * Maps {@code JTextField.postActionEvent()} through queue
345     */
346    public void postActionEvent() {
347        runMapping(new MapVoidAction("postActionEvent") {
348            @Override
349            public void map() {
350                ((JTextField) getSource()).postActionEvent();
351            }
352        });
353    }
354
355    /**
356     * Maps {@code JTextField.removeActionListener(ActionListener)} through queue
357     */
358    public void removeActionListener(final ActionListener actionListener) {
359        runMapping(new MapVoidAction("removeActionListener") {
360            @Override
361            public void map() {
362                ((JTextField) getSource()).removeActionListener(actionListener);
363            }
364        });
365    }
366
367    /**
368     * Maps {@code JTextField.setActionCommand(String)} through queue
369     */
370    public void setActionCommand(final String string) {
371        runMapping(new MapVoidAction("setActionCommand") {
372            @Override
373            public void map() {
374                ((JTextField) getSource()).setActionCommand(string);
375            }
376        });
377    }
378
379    /**
380     * Maps {@code JTextField.setColumns(int)} through queue
381     */
382    public void setColumns(final int i) {
383        runMapping(new MapVoidAction("setColumns") {
384            @Override
385            public void map() {
386                ((JTextField) getSource()).setColumns(i);
387            }
388        });
389    }
390
391    /**
392     * Maps {@code JTextField.setHorizontalAlignment(int)} through queue
393     */
394    public void setHorizontalAlignment(final int i) {
395        runMapping(new MapVoidAction("setHorizontalAlignment") {
396            @Override
397            public void map() {
398                ((JTextField) getSource()).setHorizontalAlignment(i);
399            }
400        });
401    }
402
403    /**
404     * Maps {@code JTextField.setScrollOffset(int)} through queue
405     */
406    public void setScrollOffset(final int i) {
407        runMapping(new MapVoidAction("setScrollOffset") {
408            @Override
409            public void map() {
410                ((JTextField) getSource()).setScrollOffset(i);
411            }
412        });
413    }
414
415    //End of mapping                                      //
416    ////////////////////////////////////////////////////////
417    private String removeNewLines(String text) {
418        StringBuffer buff = new StringBuffer(text);
419        int i = 0;
420        while (i < buff.length()) {
421            if (buff.charAt(i) != '\n') {
422                i++;
423            } else {
424                buff.deleteCharAt(i);
425            }
426        }
427        return buff.toString();
428    }
429
430    /**
431     * Checks component type.
432     */
433    public static class JTextFieldFinder extends Finder {
434
435        /**
436         * Constructs JTextFieldFinder.
437         *
438         * @param sf other searching criteria.
439         */
440        public JTextFieldFinder(ComponentChooser sf) {
441            super(JTextField.class, sf);
442        }
443
444        /**
445         * Constructs JTextFieldFinder.
446         */
447        public JTextFieldFinder() {
448            super(JTextField.class);
449        }
450    }
451}
452