JFrameOperator.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.accessibility.AccessibleContext;
29import javax.swing.JFrame;
30import javax.swing.JLayeredPane;
31import javax.swing.JMenuBar;
32import javax.swing.JRootPane;
33
34import org.netbeans.jemmy.ComponentChooser;
35import org.netbeans.jemmy.FrameWaiter;
36import org.netbeans.jemmy.JemmyProperties;
37
38/**
39 * <BR><BR>Timeouts used: <BR>
40 * FrameWaiter.WaitFrameTimeout - time to wait frame displayed <BR>
41 * FrameWaiter.AfterFrameTimeout - time to sleep after frame has been dispayed
42 * <BR>.
43 *
44 * @see org.netbeans.jemmy.Timeouts
45 *
46 * @author Alexandre Iline (alexandre.iline@oracle.com)
47 *
48 */
49public class JFrameOperator extends FrameOperator {
50
51    /**
52     * Constructor.
53     *
54     * @param w window
55     */
56    public JFrameOperator(JFrame w) {
57        super(w);
58    }
59
60    /**
61     * Constructs a JFrameOperator object.
62     *
63     * @param chooser a component chooser specifying searching criteria.
64     * @param index an index between appropriate ones.
65     * @param env an operator to copy environment from.
66     */
67    public JFrameOperator(ComponentChooser chooser, int index, Operator env) {
68        this((JFrame) waitFrame(new JFrameFinder(chooser),
69                index,
70                env.getTimeouts(),
71                env.getOutput()));
72        copyEnvironment(env);
73    }
74
75    /**
76     * Constructs a JFrameOperator object.
77     *
78     * @param chooser a component chooser specifying searching criteria.
79     * @param index an index between appropriate ones.
80     */
81    public JFrameOperator(ComponentChooser chooser, int index) {
82        this(chooser, index, Operator.getEnvironmentOperator());
83    }
84
85    /**
86     * Constructs a JFrameOperator object.
87     *
88     * @param chooser a component chooser specifying searching criteria.
89     */
90    public JFrameOperator(ComponentChooser chooser) {
91        this(chooser, 0);
92    }
93
94    /**
95     * Constructor. Waits for the frame with "title" subtitle. Constructor can
96     * be used in complicated cases when output or timeouts should differ from
97     * default.
98     *
99     * @param title a window title
100     * @param index Ordinal component index.
101     * @param env an operator to copy environment from.
102     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
103     *
104     */
105    public JFrameOperator(String title, int index, Operator env) {
106        this(new JFrameFinder(new FrameByTitleFinder(title,
107                env.getComparator())),
108                index, env);
109    }
110
111    /**
112     * Constructor. Waits for the frame with "title" subtitle. Uses current
113     * timeouts and output values.
114     *
115     * @param title a window title
116     * @param index Ordinal component index.
117     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
118     * @see JemmyProperties#getCurrentTimeouts()
119     * @see JemmyProperties#getCurrentOutput()
120     *
121     */
122    public JFrameOperator(String title, int index) {
123        this(title, index,
124                ComponentOperator.getEnvironmentOperator());
125    }
126
127    /**
128     * Constructor. Waits for the frame with "title" subtitle. Uses current
129     * timeouts and output values.
130     *
131     * @param title a window title
132     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
133     * @see JemmyProperties#getCurrentTimeouts()
134     * @see JemmyProperties#getCurrentOutput()
135     *
136     */
137    public JFrameOperator(String title) {
138        this(title, 0);
139    }
140
141    /**
142     * Constructor. Waits for the index'th frame. Uses current timeout and
143     * output for waiting and to init operator.
144     *
145     * @param index Ordinal component index.
146     *
147     */
148    public JFrameOperator(int index) {
149        this((JFrame) waitFrame(new JFrameFinder(),
150                index,
151                ComponentOperator.getEnvironmentOperator().getTimeouts(),
152                ComponentOperator.getEnvironmentOperator().getOutput()));
153        copyEnvironment(ComponentOperator.getEnvironmentOperator());
154    }
155
156    /**
157     * Constructor. Waits for the first frame. Uses current timeout and output
158     * for waiting and to init operator.
159     *
160     */
161    public JFrameOperator() {
162        this(0);
163    }
164
165    /**
166     * Searches an index'th frame.
167     *
168     * @param chooser a component chooser specifying searching criteria.
169     * @param index an index between appropriate ones.
170     * @return JFrame instance or null if component was not found.
171     */
172    public static JFrame findJFrame(ComponentChooser chooser, int index) {
173        return (JFrame) FrameWaiter.getFrame(new JFrameFinder(chooser), index);
174    }
175
176    /**
177     * Searches a frame.
178     *
179     * @param chooser a component chooser specifying searching criteria.
180     * @return JFrame instance or null if component was not found.
181     */
182    public static JFrame findJFrame(ComponentChooser chooser) {
183        return findJFrame(chooser, 0);
184    }
185
186    /**
187     * Searches an index'th frame by title.
188     *
189     * @param title Frame title
190     * @param ce Compare exactly. If true, text can be a substring of caption.
191     * @param cc Compare case sensitively. If true, both text and caption are
192     * @param index an index between appropriate ones.
193     * @return JFrame instance or null if component was not found.
194     */
195    public static JFrame findJFrame(String title, boolean ce, boolean cc, int index) {
196        return ((JFrame) FrameWaiter.
197                getFrame(new JFrameFinder(new FrameByTitleFinder(title,
198                        new DefaultStringComparator(ce, cc))),
199                        index));
200    }
201
202    /**
203     * Searches a frame by title.
204     *
205     * @param title Frame title
206     * @param ce Compare exactly. If true, text can be a substring of caption.
207     * @param cc Compare case sensitively. If true, both text and caption are
208     * @return JFrame instance or null if component was not found.
209     */
210    public static JFrame findJFrame(String title, boolean ce, boolean cc) {
211        return findJFrame(title, ce, cc, 0);
212    }
213
214    /**
215     * Waits an index'th frame.
216     *
217     * @param chooser a component chooser specifying searching criteria.
218     * @param index an index between appropriate ones.
219     * @return JFrame instance or null if component was not found.
220     *
221     */
222    public static JFrame waitJFrame(ComponentChooser chooser, int index) {
223        return ((JFrame) waitFrame(new JFrameFinder(chooser), index,
224                JemmyProperties.getCurrentTimeouts(),
225                JemmyProperties.getCurrentOutput()));
226    }
227
228    /**
229     * Waits a frame.
230     *
231     * @param chooser a component chooser specifying searching criteria.
232     * @return JFrame instance or null if component was not found.
233     *
234     */
235    public static JFrame waitJFrame(ComponentChooser chooser) {
236        return waitJFrame(chooser, 0);
237    }
238
239    /**
240     * Waits an index'th frame by title.
241     *
242     * @param title Frame title
243     * @param ce Compare exactly. If true, text can be a substring of caption.
244     * @param cc Compare case sensitively. If true, both text and caption are
245     * @param index an index between appropriate ones.
246     * @return JFrame instance or null if component was not found.
247     *
248     */
249    public static JFrame waitJFrame(String title, boolean ce, boolean cc, int index) {
250        try {
251            return ((JFrame) (new FrameWaiter()).
252                    waitFrame(new JFrameFinder(new FrameByTitleFinder(title,
253                            new DefaultStringComparator(ce, cc))),
254                            index));
255        } catch (InterruptedException e) {
256            JemmyProperties.getCurrentOutput().printStackTrace(e);
257            return null;
258        }
259    }
260
261    /**
262     * Waits a frame by title.
263     *
264     * @param title Frame title
265     * @param ce Compare exactly. If true, text can be a substring of caption.
266     * @param cc Compare case sensitively. If true, both text and caption are
267     * @return JFrame instance or null if component was not found.
268     *
269     */
270    public static JFrame waitJFrame(String title, boolean ce, boolean cc) {
271        return waitJFrame(title, ce, cc, 0);
272    }
273
274    ////////////////////////////////////////////////////////
275    //Mapping                                             //
276    /**
277     * Maps {@code JFrame.getAccessibleContext()} through queue
278     */
279    public AccessibleContext getAccessibleContext() {
280        return (runMapping(new MapAction<AccessibleContext>("getAccessibleContext") {
281            @Override
282            public AccessibleContext map() {
283                return getSource().getAccessibleContext();
284            }
285        }));
286    }
287
288    /**
289     * Maps {@code JFrame.getContentPane()} through queue
290     */
291    public Container getContentPane() {
292        return (runMapping(new MapAction<Container>("getContentPane") {
293            @Override
294            public Container map() {
295                return ((JFrame) getSource()).getContentPane();
296            }
297        }));
298    }
299
300    /**
301     * Maps {@code JFrame.getDefaultCloseOperation()} through queue
302     */
303    public int getDefaultCloseOperation() {
304        return (runMapping(new MapIntegerAction("getDefaultCloseOperation") {
305            @Override
306            public int map() {
307                return ((JFrame) getSource()).getDefaultCloseOperation();
308            }
309        }));
310    }
311
312    /**
313     * Maps {@code JFrame.getGlassPane()} through queue
314     */
315    public Component getGlassPane() {
316        return (runMapping(new MapAction<Component>("getGlassPane") {
317            @Override
318            public Component map() {
319                return ((JFrame) getSource()).getGlassPane();
320            }
321        }));
322    }
323
324    /**
325     * Maps {@code JFrame.getJMenuBar()} through queue
326     */
327    public JMenuBar getJMenuBar() {
328        return (runMapping(new MapAction<JMenuBar>("getJMenuBar") {
329            @Override
330            public JMenuBar map() {
331                return ((JFrame) getSource()).getJMenuBar();
332            }
333        }));
334    }
335
336    /**
337     * Maps {@code JFrame.getLayeredPane()} through queue
338     */
339    public JLayeredPane getLayeredPane() {
340        return (runMapping(new MapAction<JLayeredPane>("getLayeredPane") {
341            @Override
342            public JLayeredPane map() {
343                return ((JFrame) getSource()).getLayeredPane();
344            }
345        }));
346    }
347
348    /**
349     * Maps {@code JFrame.getRootPane()} through queue
350     */
351    public JRootPane getRootPane() {
352        return (runMapping(new MapAction<JRootPane>("getRootPane") {
353            @Override
354            public JRootPane map() {
355                return ((JFrame) getSource()).getRootPane();
356            }
357        }));
358    }
359
360    /**
361     * Maps {@code JFrame.setContentPane(Container)} through queue
362     */
363    public void setContentPane(final Container container) {
364        runMapping(new MapVoidAction("setContentPane") {
365            @Override
366            public void map() {
367                ((JFrame) getSource()).setContentPane(container);
368            }
369        });
370    }
371
372    /**
373     * Maps {@code JFrame.setDefaultCloseOperation(int)} through queue
374     */
375    public void setDefaultCloseOperation(final int i) {
376        runMapping(new MapVoidAction("setDefaultCloseOperation") {
377            @Override
378            public void map() {
379                ((JFrame) getSource()).setDefaultCloseOperation(i);
380            }
381        });
382    }
383
384    /**
385     * Maps {@code JFrame.setGlassPane(Component)} through queue
386     */
387    public void setGlassPane(final Component component) {
388        runMapping(new MapVoidAction("setGlassPane") {
389            @Override
390            public void map() {
391                ((JFrame) getSource()).setGlassPane(component);
392            }
393        });
394    }
395
396    /**
397     * Maps {@code JFrame.setJMenuBar(JMenuBar)} through queue
398     */
399    public void setJMenuBar(final JMenuBar jMenuBar) {
400        runMapping(new MapVoidAction("setJMenuBar") {
401            @Override
402            public void map() {
403                ((JFrame) getSource()).setJMenuBar(jMenuBar);
404            }
405        });
406    }
407
408    /**
409     * Maps {@code JFrame.setLayeredPane(JLayeredPane)} through queue
410     */
411    public void setLayeredPane(final JLayeredPane jLayeredPane) {
412        runMapping(new MapVoidAction("setLayeredPane") {
413            @Override
414            public void map() {
415                ((JFrame) getSource()).setLayeredPane(jLayeredPane);
416            }
417        });
418    }
419
420    //End of mapping                                      //
421    ////////////////////////////////////////////////////////
422    /**
423     * Checks component type.
424     */
425    public static class JFrameFinder extends Finder {
426
427        /**
428         * Constructs JFrameFinder.
429         *
430         * @param sf other searching criteria.
431         */
432        public JFrameFinder(ComponentChooser sf) {
433            super(JFrame.class, sf);
434        }
435
436        /**
437         * Constructs JFrameFinder.
438         */
439        public JFrameFinder() {
440            super(JFrame.class);
441        }
442    }
443}
444