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. Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package org.netbeans.jemmy.operators;
26 
27 import java.awt.Component;
28 
29 import javax.swing.JRadioButtonMenuItem;
30 
31 import org.netbeans.jemmy.ComponentChooser;
32 import org.netbeans.jemmy.JemmyProperties;
33 
34 /**
35  *
36  * <BR><BR>Timeouts used: <BR>
37  * JMenuItemOperator.PushMenuTimeout - time between button pressing and
38  * releasing<BR>
39  * ComponentOperator.WaitComponentTimeout - time to wait button displayed <BR>
40  * ComponentOperator.WaitComponentEnabledTimeout - time to wait button enabled
41  * <BR>.
42  *
43  * @see org.netbeans.jemmy.Timeouts
44  *
45  * @author Alexandre Iline (alexandre.iline@oracle.com)
46  *
47  */
48 public class JRadioButtonMenuItemOperator extends JMenuItemOperator {
49 
50     /**
51      * Constructor.
52      *
53      * @param item a component
54      */
JRadioButtonMenuItemOperator(JRadioButtonMenuItem item)55     public JRadioButtonMenuItemOperator(JRadioButtonMenuItem item) {
56         super(item);
57         setTimeouts(JemmyProperties.getProperties().getTimeouts());
58         setOutput(JemmyProperties.getProperties().getOutput());
59     }
60 
61     /**
62      * Constructs a JRadioButtonMenuItemOperator object.
63      *
64      * @param cont a container
65      * @param chooser a component chooser specifying searching criteria.
66      * @param index an index between appropriate ones.
67      */
JRadioButtonMenuItemOperator(ContainerOperator<?> cont, ComponentChooser chooser, int index)68     public JRadioButtonMenuItemOperator(ContainerOperator<?> cont, ComponentChooser chooser, int index) {
69         this((JRadioButtonMenuItem) cont.
70                 waitSubComponent(new JRadioButtonMenuItemFinder(chooser),
71                         index));
72         copyEnvironment(cont);
73     }
74 
75     /**
76      * Constructs a JRadioButtonMenuItemOperator object.
77      *
78      * @param cont a container
79      * @param chooser a component chooser specifying searching criteria.
80      */
JRadioButtonMenuItemOperator(ContainerOperator<?> cont, ComponentChooser chooser)81     public JRadioButtonMenuItemOperator(ContainerOperator<?> cont, ComponentChooser chooser) {
82         this(cont, chooser, 0);
83     }
84 
85     /**
86      * Constructor. Waits component in container first. Uses cont's timeout and
87      * output for waiting and to init operator.
88      *
89      * @param cont a container
90      * @param text Button text.
91      * @param index Ordinal component index.
92      * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
93      */
JRadioButtonMenuItemOperator(ContainerOperator<?> cont, String text, int index)94     public JRadioButtonMenuItemOperator(ContainerOperator<?> cont, String text, int index) {
95         this((JRadioButtonMenuItem) waitComponent(cont,
96                 new JRadioButtonMenuItemByLabelFinder(text,
97                         cont.getComparator()),
98                 index));
99         setTimeouts(cont.getTimeouts());
100         setOutput(cont.getOutput());
101     }
102 
103     /**
104      * Constructor. Waits component in container first. Uses cont's timeout and
105      * output for waiting and to init operator.
106      *
107      * @param cont a container
108      * @param text Button text.
109      * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
110      */
JRadioButtonMenuItemOperator(ContainerOperator<?> cont, String text)111     public JRadioButtonMenuItemOperator(ContainerOperator<?> cont, String text) {
112         this(cont, text, 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 index Ordinal component index.
121      */
JRadioButtonMenuItemOperator(ContainerOperator<?> cont, int index)122     public JRadioButtonMenuItemOperator(ContainerOperator<?> cont, int index) {
123         this((JRadioButtonMenuItem) waitComponent(cont,
124                 new JRadioButtonMenuItemFinder(),
125                 index));
126         copyEnvironment(cont);
127     }
128 
129     /**
130      * Constructor. Waits component in container first. Uses cont's timeout and
131      * output for waiting and to init operator.
132      *
133      * @param cont a container
134      */
JRadioButtonMenuItemOperator(ContainerOperator<?> cont)135     public JRadioButtonMenuItemOperator(ContainerOperator<?> cont) {
136         this(cont, 0);
137     }
138 
139     ////////////////////////////////////////////////////////
140     //Mapping                                             //
141     //End of mapping                                      //
142     ////////////////////////////////////////////////////////
143     /**
144      * Allows to find component by text.
145      */
146     public static class JRadioButtonMenuItemByLabelFinder implements ComponentChooser {
147 
148         String label;
149         StringComparator comparator;
150 
151         /**
152          * Constructs JRadioButtonMenuItemByLabelFinder.
153          *
154          * @param lb a text pattern
155          * @param comparator specifies string comparision algorithm.
156          */
JRadioButtonMenuItemByLabelFinder(String lb, StringComparator comparator)157         public JRadioButtonMenuItemByLabelFinder(String lb, StringComparator comparator) {
158             label = lb;
159             this.comparator = comparator;
160         }
161 
162         /**
163          * Constructs JRadioButtonMenuItemByLabelFinder.
164          *
165          * @param lb a text pattern
166          */
JRadioButtonMenuItemByLabelFinder(String lb)167         public JRadioButtonMenuItemByLabelFinder(String lb) {
168             this(lb, Operator.getDefaultStringComparator());
169         }
170 
171         @Override
checkComponent(Component comp)172         public boolean checkComponent(Component comp) {
173             if (comp instanceof JRadioButtonMenuItem) {
174                 if (((JRadioButtonMenuItem) comp).getText() != null) {
175                     return (comparator.equals(((JRadioButtonMenuItem) comp).getText(),
176                             label));
177                 }
178             }
179             return false;
180         }
181 
182         @Override
getDescription()183         public String getDescription() {
184             return "JRadioButtonMenuItem with text \"" + label + "\"";
185         }
186 
187         @Override
toString()188         public String toString() {
189             return "JRadioButtonMenuItemByLabelFinder{" + "label=" + label + ", comparator=" + comparator + '}';
190         }
191     }
192 
193     /**
194      * Checks component type.
195      */
196     public static class JRadioButtonMenuItemFinder extends Finder {
197 
198         /**
199          * Constructs JRadioButtonMenuItemFinder.
200          *
201          * @param sf other searching criteria.
202          */
JRadioButtonMenuItemFinder(ComponentChooser sf)203         public JRadioButtonMenuItemFinder(ComponentChooser sf) {
204             super(JRadioButtonMenuItem.class, sf);
205         }
206 
207         /**
208          * Constructs JRadioButtonMenuItemFinder.
209          */
JRadioButtonMenuItemFinder()210         public JRadioButtonMenuItemFinder() {
211             super(JRadioButtonMenuItem.class);
212         }
213     }
214 }
215