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;
26
27import javax.swing.JToggleButton;
28
29import org.netbeans.jemmy.ComponentChooser;
30import org.netbeans.jemmy.TimeoutExpiredException;
31
32/**
33 *
34 * <BR><BR>Timeouts used: <BR>
35 * AbstractButtonOperator.PushButtonTimeout - time between button pressing and
36 * releasing<BR>
37 * ComponentOperator.WaitComponentTimeout - time to wait button displayed <BR>
38 * ComponentOperator.WaitComponentEnabledTimeout - time to wait button enabled
39 * <BR>.
40 *
41 * @see org.netbeans.jemmy.Timeouts
42 *
43 * @author Alexandre Iline (alexandre.iline@oracle.com)
44 *
45 */
46public class JToggleButtonOperator extends AbstractButtonOperator {
47
48    /**
49     * Constructor.
50     *
51     * @param b a component
52     */
53    public JToggleButtonOperator(JToggleButton b) {
54        super(b);
55    }
56
57    /**
58     * Constructs a JToggleButtonOperator object.
59     *
60     * @param cont a container
61     * @param chooser a component chooser specifying searching criteria.
62     * @param index an index between appropriate ones.
63     */
64    public JToggleButtonOperator(ContainerOperator<?> cont, ComponentChooser chooser, int index) {
65        this((JToggleButton) cont.
66                waitSubComponent(new JToggleButtonFinder(chooser),
67                        index));
68        copyEnvironment(cont);
69    }
70
71    /**
72     * Constructs a JToggleButtonOperator object.
73     *
74     * @param cont a container
75     * @param chooser a component chooser specifying searching criteria.
76     */
77    public JToggleButtonOperator(ContainerOperator<?> cont, ComponentChooser chooser) {
78        this(cont, chooser, 0);
79    }
80
81    /**
82     * Constructor. Waits component in container first. Uses cont's timeout and
83     * output for waiting and to init operator.
84     *
85     * @param cont a container
86     * @param text Button text.
87     * @param index Ordinal component index.
88     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
89     * @throws TimeoutExpiredException
90     */
91    public JToggleButtonOperator(ContainerOperator<?> cont, String text, int index) {
92        this((JToggleButton) waitComponent(cont,
93                new JToggleButtonFinder(new AbstractButtonOperator.AbstractButtonByLabelFinder(text,
94                        cont.getComparator())),
95                index));
96        copyEnvironment(cont);
97    }
98
99    /**
100     * Constructor. Waits component in container first. Uses cont's timeout and
101     * output for waiting and to init operator.
102     *
103     * @param cont a container
104     * @param text Button text.
105     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
106     * @throws TimeoutExpiredException
107     */
108    public JToggleButtonOperator(ContainerOperator<?> cont, String text) {
109        this(cont, text, 0);
110    }
111
112    /**
113     * Constructor. Waits component in container first. Uses cont's timeout and
114     * output for waiting and to init operator.
115     *
116     * @param cont a container
117     * @param index Ordinal component index.
118     * @throws TimeoutExpiredException
119     */
120    public JToggleButtonOperator(ContainerOperator<?> cont, int index) {
121        this((JToggleButton) waitComponent(cont,
122                new JToggleButtonFinder(),
123                index));
124        copyEnvironment(cont);
125    }
126
127    /**
128     * Constructor. Waits component in container first. Uses cont's timeout and
129     * output for waiting and to init operator.
130     *
131     * @param cont a container
132     * @throws TimeoutExpiredException
133     */
134    public JToggleButtonOperator(ContainerOperator<?> cont) {
135        this(cont, 0);
136    }
137
138    /**
139     * Searches JToggleButton in container.
140     *
141     * @param cont Container to search component in.
142     * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
143     * @param index Ordinal component index.
144     * @return JToggleButton instance or null if component was not found.
145     */
146    public static JToggleButton findJToggleButton(Container cont, ComponentChooser chooser, int index) {
147        return (JToggleButton) findAbstractButton(cont, new JToggleButtonFinder(chooser), index);
148    }
149
150    /**
151     * Searches 0'th JToggleButton in container.
152     *
153     * @param cont Container to search component in.
154     * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
155     * @return JToggleButton instance or null if component was not found.
156     */
157    public static JToggleButton findJToggleButton(Container cont, ComponentChooser chooser) {
158        return findJToggleButton(cont, chooser, 0);
159    }
160
161    /**
162     * Searches JToggleButton by text.
163     *
164     * @param cont Container to search component in.
165     * @param text Button text. If null, contents is not checked.
166     * @param ce Compare text exactly.
167     * @param ccs Compare text case sensitively.
168     * @param index Ordinal component index.
169     * @return JToggleButton instance or null if component was not found.
170     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
171     */
172    public static JToggleButton findJToggleButton(Container cont, String text, boolean ce, boolean ccs, int index) {
173        return (findJToggleButton(cont,
174                new JToggleButtonFinder(new AbstractButtonOperator.AbstractButtonByLabelFinder(text,
175                        new DefaultStringComparator(ce, ccs))),
176                index));
177    }
178
179    /**
180     * Searches JToggleButton by text.
181     *
182     * @param cont Container to search component in.
183     * @param text Button text. If null, contents is not checked.
184     * @param ce Compare text exactly.
185     * @param ccs Compare text case sensitively.
186     * @return JToggleButton instance or null if component was not found.
187     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
188     */
189    public static JToggleButton findJToggleButton(Container cont, String text, boolean ce, boolean ccs) {
190        return findJToggleButton(cont, text, ce, ccs, 0);
191    }
192
193    /**
194     * Waits JToggleButton in container.
195     *
196     * @param cont Container to search component in.
197     * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
198     * @param index Ordinal component index.
199     * @return JToggleButton instance.
200     * @throws TimeoutExpiredException
201     */
202    public static JToggleButton waitJToggleButton(Container cont, ComponentChooser chooser, int index) {
203        return (JToggleButton) waitAbstractButton(cont, new JToggleButtonFinder(chooser), index);
204    }
205
206    /**
207     * Waits 0'th JToggleButton in container.
208     *
209     * @param cont Container to search component in.
210     * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
211     * @return JToggleButton instance.
212     * @throws TimeoutExpiredException
213     */
214    public static JToggleButton waitJToggleButton(Container cont, ComponentChooser chooser) {
215        return waitJToggleButton(cont, chooser, 0);
216    }
217
218    /**
219     * Waits JToggleButton by text.
220     *
221     * @param cont Container to search component in.
222     * @param text Button text. If null, contents is not checked.
223     * @param ce Compare text exactly.
224     * @param ccs Compare text case sensitively.
225     * @param index Ordinal component index.
226     * @return JToggleButton instance.
227     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
228     * @throws TimeoutExpiredException
229     */
230    public static JToggleButton waitJToggleButton(Container cont, String text, boolean ce, boolean ccs, int index) {
231        return (waitJToggleButton(cont,
232                new JToggleButtonFinder(new AbstractButtonOperator.AbstractButtonByLabelFinder(text,
233                        new DefaultStringComparator(ce, ccs))),
234                index));
235    }
236
237    /**
238     * Waits JToggleButton by text.
239     *
240     * @param cont Container to search component in.
241     * @param text Button text. If null, contents is not checked.
242     * @param ce Compare text exactly.
243     * @param ccs Compare text case sensitively.
244     * @return JToggleButton instance.
245     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
246     * @throws TimeoutExpiredException
247     */
248    public static JToggleButton waitJToggleButton(Container cont, String text, boolean ce, boolean ccs) {
249        return waitJToggleButton(cont, text, ce, ccs, 0);
250    }
251
252    /**
253     * Prepares the button to click.
254     */
255    protected void prepareToClick() {
256        makeComponentVisible();
257    }
258
259    /**
260     * Checks component type.
261     */
262    public static class JToggleButtonFinder extends Finder {
263
264        /**
265         * Constructs JToggleButtonFinder.
266         *
267         * @param sf other searching criteria.
268         */
269        public JToggleButtonFinder(ComponentChooser sf) {
270            super(JToggleButton.class, sf);
271        }
272
273        /**
274         * Constructs JToggleButtonFinder.
275         */
276        public JToggleButtonFinder() {
277            super(JToggleButton.class);
278        }
279    }
280}
281