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;
26
27import javax.swing.JCheckBoxMenuItem;
28
29import org.netbeans.jemmy.ComponentChooser;
30import org.netbeans.jemmy.JemmyProperties;
31
32/**
33 *
34 * <BR><BR>Timeouts used: <BR>
35 * JMenuItemOperator.PushMenuTimeout - 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 JCheckBoxMenuItemOperator extends JMenuItemOperator {
47
48    /**
49     * Constructor.
50     *
51     * @param item a component.
52     */
53    public JCheckBoxMenuItemOperator(JCheckBoxMenuItem item) {
54        super(item);
55        setTimeouts(JemmyProperties.getProperties().getTimeouts());
56        setOutput(JemmyProperties.getProperties().getOutput());
57    }
58
59    /**
60     * Constructs a JCheckBoxMenuItemOperator object.
61     *
62     * @param cont a container
63     * @param chooser a component chooser specifying searching criteria.
64     * @param index an index between appropriate ones.
65     */
66    public JCheckBoxMenuItemOperator(ContainerOperator<?> cont, ComponentChooser chooser, int index) {
67        this((JCheckBoxMenuItem) cont.
68                waitSubComponent(new JCheckBoxMenuItemFinder(chooser),
69                        index));
70        copyEnvironment(cont);
71    }
72
73    /**
74     * Constructs a JCheckBoxMenuItemOperator object.
75     *
76     * @param cont a container
77     * @param chooser a component chooser specifying searching criteria.
78     */
79    public JCheckBoxMenuItemOperator(ContainerOperator<?> cont, ComponentChooser chooser) {
80        this(cont, chooser, 0);
81    }
82
83    /**
84     * Constructor. Waits component in container first. Uses cont's timeout and
85     * output for waiting and to init operator.
86     *
87     * @param cont a container
88     * @param text Button text.
89     * @param index Ordinal component index.
90     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
91     */
92    public JCheckBoxMenuItemOperator(ContainerOperator<?> cont, String text, int index) {
93        this((JCheckBoxMenuItem) waitComponent(cont,
94                new JCheckBoxMenuItemByLabelFinder(text,
95                        cont.getComparator()),
96                index));
97        setTimeouts(cont.getTimeouts());
98        setOutput(cont.getOutput());
99    }
100
101    /**
102     * Constructor. Waits component in container first. Uses cont's timeout and
103     * output for waiting and to init operator.
104     *
105     * @param cont a container
106     * @param text Button text.
107     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
108     */
109    public JCheckBoxMenuItemOperator(ContainerOperator<?> cont, String text) {
110        this(cont, text, 0);
111    }
112
113    /**
114     * Constructor. Waits component in container first. Uses cont's timeout and
115     * output for waiting and to init operator.
116     *
117     * @param cont a container
118     * @param index Ordinal component index.
119     */
120    public JCheckBoxMenuItemOperator(ContainerOperator<?> cont, int index) {
121        this((JCheckBoxMenuItem) waitComponent(cont,
122                new JCheckBoxMenuItemFinder(),
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     */
133    public JCheckBoxMenuItemOperator(ContainerOperator<?> cont) {
134        this(cont, 0);
135    }
136
137    ////////////////////////////////////////////////////////
138    //Mapping                                             //
139    /**
140     * Maps {@code JCheckBoxMenuItem.getState()} through queue
141     */
142    public boolean getState() {
143        return (runMapping(new MapBooleanAction("getState") {
144            @Override
145            public boolean map() {
146                return ((JCheckBoxMenuItem) getSource()).getState();
147            }
148        }));
149    }
150
151    /**
152     * Maps {@code JCheckBoxMenuItem.setState(boolean)} through queue
153     */
154    public void setState(final boolean b) {
155        runMapping(new MapVoidAction("setState") {
156            @Override
157            public void map() {
158                ((JCheckBoxMenuItem) getSource()).setState(b);
159            }
160        });
161    }
162
163    //End of mapping                                      //
164    ////////////////////////////////////////////////////////
165    /**
166     * Allows to find component by text.
167     */
168    public static class JCheckBoxMenuItemByLabelFinder implements ComponentChooser {
169
170        String label;
171        StringComparator comparator;
172
173        /**
174         * Constructs JCheckBoxMenuItemByLabelFinder.
175         *
176         * @param lb a text pattern
177         * @param comparator specifies string comparision algorithm.
178         */
179        public JCheckBoxMenuItemByLabelFinder(String lb, StringComparator comparator) {
180            label = lb;
181            this.comparator = comparator;
182        }
183
184        /**
185         * Constructs JCheckBoxMenuItemByLabelFinder.
186         *
187         * @param lb a text pattern
188         */
189        public JCheckBoxMenuItemByLabelFinder(String lb) {
190            this(lb, Operator.getDefaultStringComparator());
191        }
192
193        @Override
194        public boolean checkComponent(Component comp) {
195            if (comp instanceof JCheckBoxMenuItem) {
196                if (((JCheckBoxMenuItem) comp).getText() != null) {
197                    return (comparator.equals(((JCheckBoxMenuItem) comp).getText(),
198                            label));
199                }
200            }
201            return false;
202        }
203
204        @Override
205        public String getDescription() {
206            return "JCheckBoxMenuItem with text \"" + label + "\"";
207        }
208
209        @Override
210        public String toString() {
211            return "JCheckBoxMenuItemByLabelFinder{" + "label=" + label + ", comparator=" + comparator + '}';
212        }
213    }
214
215    /**
216     * Checks component type.
217     */
218    public static class JCheckBoxMenuItemFinder extends Finder {
219
220        /**
221         * Constructs JCheckBoxMenuItemFinder.
222         *
223         * @param sf other searching criteria.
224         */
225        public JCheckBoxMenuItemFinder(ComponentChooser sf) {
226            super(JCheckBoxMenuItem.class, sf);
227        }
228
229        /**
230         * Constructs JCheckBoxMenuItemFinder.
231         */
232        public JCheckBoxMenuItemFinder() {
233            super(JCheckBoxMenuItem.class);
234        }
235    }
236}
237