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