JCheckBoxMenuItemOperator.java revision 13978:1993af50385d
128257Smsmith/*
238061Smsmith * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
328257Smsmith * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
428257Smsmith *
528257Smsmith * This code is free software; you can redistribute it and/or modify it
628257Smsmith * under the terms of the GNU General Public License version 2 only, as
728257Smsmith * published by the Free Software Foundation.
828257Smsmith *
928257Smsmith * This code is distributed in the hope that it will be useful, but WITHOUT
1028257Smsmith * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1128257Smsmith * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1228257Smsmith * version 2 for more details (a copy is included in the LICENSE file that
1328257Smsmith * accompanied this code).
1428257Smsmith *
1528257Smsmith * You should have received a copy of the GNU General Public License version
1628257Smsmith * 2 along with this work; if not, write to the Free Software Foundation,
1728257Smsmith * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1828257Smsmith *
1928257Smsmith * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2028257Smsmith * or visit www.oracle.com if you need additional information or have any
2128257Smsmith * questions.
2228257Smsmith */
2328257Smsmithpackage org.netbeans.jemmy.operators;
2428257Smsmith
2528257Smsmithimport java.awt.Component;
2638061Smsmith
2728257Smsmithimport javax.swing.JCheckBoxMenuItem;
2828257Smsmith
2928257Smsmithimport org.netbeans.jemmy.ComponentChooser;
3028257Smsmithimport org.netbeans.jemmy.JemmyProperties;
3128257Smsmith
3228257Smsmith/**
3328257Smsmith *
3428257Smsmith * <BR><BR>Timeouts used: <BR>
3528257Smsmith * JMenuItemOperator.PushMenuTimeout - time between button pressing and
3628257Smsmith * releasing<BR>
3728257Smsmith * ComponentOperator.WaitComponentTimeout - time to wait button displayed <BR>
3828257Smsmith * ComponentOperator.WaitComponentEnabledTimeout - time to wait button enabled
3928257Smsmith * <BR>.
4028257Smsmith *
4128257Smsmith * @see org.netbeans.jemmy.Timeouts
4228257Smsmith *
4328257Smsmith * @author Alexandre Iline (alexandre.iline@oracle.com)
4428257Smsmith *
4528257Smsmith */
4628257Smsmithpublic class JCheckBoxMenuItemOperator extends JMenuItemOperator {
4728257Smsmith
4828257Smsmith    /**
4928257Smsmith     * Constructor.
5028257Smsmith     *
5128257Smsmith     * @param item a component.
5228257Smsmith     */
5328257Smsmith    public JCheckBoxMenuItemOperator(JCheckBoxMenuItem item) {
5428257Smsmith        super(item);
5528257Smsmith        setTimeouts(JemmyProperties.getProperties().getTimeouts());
5628257Smsmith        setOutput(JemmyProperties.getProperties().getOutput());
5728257Smsmith    }
5828257Smsmith
5928257Smsmith    /**
6028257Smsmith     * Constructs a JCheckBoxMenuItemOperator object.
6128257Smsmith     *
6228257Smsmith     * @param cont a container
6328257Smsmith     * @param chooser a component chooser specifying searching criteria.
6428257Smsmith     * @param index an index between appropriate ones.
6528257Smsmith     */
6628257Smsmith    public JCheckBoxMenuItemOperator(ContainerOperator<?> cont, ComponentChooser chooser, int index) {
6728257Smsmith        this((JCheckBoxMenuItem) cont.
6828257Smsmith                waitSubComponent(new JCheckBoxMenuItemFinder(chooser),
6928257Smsmith                        index));
7028257Smsmith        copyEnvironment(cont);
7128257Smsmith    }
7228257Smsmith
7328257Smsmith    /**
7428257Smsmith     * Constructs a JCheckBoxMenuItemOperator object.
7528257Smsmith     *
7628257Smsmith     * @param cont a container
7728257Smsmith     * @param chooser a component chooser specifying searching criteria.
7828257Smsmith     */
7928257Smsmith    public JCheckBoxMenuItemOperator(ContainerOperator<?> cont, ComponentChooser chooser) {
8028257Smsmith        this(cont, chooser, 0);
8128257Smsmith    }
8228257Smsmith
8328257Smsmith    /**
8428257Smsmith     * Constructor. Waits component in container first. Uses cont's timeout and
8528257Smsmith     * output for waiting and to init operator.
8628257Smsmith     *
8728257Smsmith     * @param cont a container
8828257Smsmith     * @param text Button text.
8928257Smsmith     * @param index Ordinal component index.
9028257Smsmith     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
9128257Smsmith     */
9228257Smsmith    public JCheckBoxMenuItemOperator(ContainerOperator<?> cont, String text, int index) {
9328257Smsmith        this((JCheckBoxMenuItem) waitComponent(cont,
9428257Smsmith                new JCheckBoxMenuItemByLabelFinder(text,
9528257Smsmith                        cont.getComparator()),
9628257Smsmith                index));
9728257Smsmith        setTimeouts(cont.getTimeouts());
9828257Smsmith        setOutput(cont.getOutput());
9928257Smsmith    }
10028257Smsmith
10138061Smsmith    /**
10238061Smsmith     * Constructor. Waits component in container first. Uses cont's timeout and
10338061Smsmith     * output for waiting and to init operator.
10438061Smsmith     *
10538061Smsmith     * @param cont a container
10638061Smsmith     * @param text Button text.
10738061Smsmith     * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
10838061Smsmith     */
10938061Smsmith    public JCheckBoxMenuItemOperator(ContainerOperator<?> cont, String text) {
11038061Smsmith        this(cont, text, 0);
11138061Smsmith    }
11238061Smsmith
11338061Smsmith    /**
11438061Smsmith     * Constructor. Waits component in container first. Uses cont's timeout and
11538061Smsmith     * output for waiting and to init operator.
11638061Smsmith     *
11738061Smsmith     * @param cont a container
11838061Smsmith     * @param index Ordinal component index.
11938061Smsmith     */
12038061Smsmith    public JCheckBoxMenuItemOperator(ContainerOperator<?> cont, int index) {
12128257Smsmith        this((JCheckBoxMenuItem) waitComponent(cont,
12228257Smsmith                new JCheckBoxMenuItemFinder(),
12338061Smsmith                index));
12428257Smsmith        copyEnvironment(cont);
12528257Smsmith    }
12628257Smsmith
12728257Smsmith    /**
12828257Smsmith     * Constructor. Waits component in container first. Uses cont's timeout and
12928257Smsmith     * output for waiting and to init operator.
13028257Smsmith     *
13128257Smsmith     * @param cont a container
13228257Smsmith     */
13328257Smsmith    public JCheckBoxMenuItemOperator(ContainerOperator<?> cont) {
13428257Smsmith        this(cont, 0);
13528257Smsmith    }
13628257Smsmith
13728257Smsmith    ////////////////////////////////////////////////////////
13828257Smsmith    //Mapping                                             //
13928257Smsmith    /**
14028257Smsmith     * Maps {@code JCheckBoxMenuItem.getState()} through queue
14138061Smsmith     */
14228257Smsmith    public boolean getState() {
14328257Smsmith        return (runMapping(new MapBooleanAction("getState") {
14428257Smsmith            @Override
14528257Smsmith            public boolean map() {
14628257Smsmith                return ((JCheckBoxMenuItem) getSource()).getState();
14728257Smsmith            }
14828257Smsmith        }));
14928257Smsmith    }
15028257Smsmith
15128257Smsmith    /**
15228257Smsmith     * Maps {@code JCheckBoxMenuItem.setState(boolean)} through queue
15328257Smsmith     */
15428257Smsmith    public void setState(final boolean b) {
15528257Smsmith        runMapping(new MapVoidAction("setState") {
15628257Smsmith            @Override
15728257Smsmith            public void map() {
15828257Smsmith                ((JCheckBoxMenuItem) getSource()).setState(b);
15938061Smsmith            }
16028257Smsmith        });
16128257Smsmith    }
16228257Smsmith
16328257Smsmith    //End of mapping                                      //
16428257Smsmith    ////////////////////////////////////////////////////////
16528257Smsmith    /**
16628257Smsmith     * Allows to find component by text.
16728257Smsmith     */
16828257Smsmith    public static class JCheckBoxMenuItemByLabelFinder implements ComponentChooser {
16928257Smsmith
17028257Smsmith        String label;
17128257Smsmith        StringComparator comparator;
17228257Smsmith
17328257Smsmith        /**
17428257Smsmith         * Constructs JCheckBoxMenuItemByLabelFinder.
17528257Smsmith         *
17628257Smsmith         * @param lb a text pattern
17728257Smsmith         * @param comparator specifies string comparision algorithm.
17828257Smsmith         */
17928257Smsmith        public JCheckBoxMenuItemByLabelFinder(String lb, StringComparator comparator) {
18028257Smsmith            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