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.Color;
26import java.awt.Container;
27import java.util.Hashtable;
28
29import javax.swing.JColorChooser;
30import javax.swing.JComponent;
31import javax.swing.JTabbedPane;
32import javax.swing.colorchooser.AbstractColorChooserPanel;
33import javax.swing.colorchooser.ColorSelectionModel;
34import javax.swing.plaf.ColorChooserUI;
35
36import org.netbeans.jemmy.ComponentChooser;
37import org.netbeans.jemmy.ComponentSearcher;
38import org.netbeans.jemmy.JemmyProperties;
39import org.netbeans.jemmy.Outputable;
40import org.netbeans.jemmy.TestOut;
41
42/**
43 *
44 * Class provides methods to cover main JColorChooser component functionality.
45 *
46 * @author Alexandre Iline (alexandre.iline@oracle.com)
47 *
48 */
49public class JColorChooserOperator extends JComponentOperator
50        implements Outputable {
51
52    /**
53     * Identifier for a "color" property.
54     *
55     * @see #getDump
56     */
57    public static final String COLOR_DPROP = "Color";
58
59    /**
60     * Identifier for a "selected page" property.
61     *
62     * @see #getDump
63     */
64    public static final String SELECTED_PAGE_DPROP = "Selected page";
65
66    private static final String RGB_TITLE = "RGB";
67
68    private TestOut output;
69    private JTabbedPaneOperator tabbed;
70    private JTextFieldOperator red;
71    private JTextFieldOperator green;
72    private JTextFieldOperator blue;
73
74    /**
75     * Constructor.
76     *
77     * @param comp a component
78     */
79    public JColorChooserOperator(JColorChooser comp) {
80        super(comp);
81        setTimeouts(JemmyProperties.getProperties().getTimeouts());
82        setOutput(JemmyProperties.getProperties().getOutput());
83        tabbed = new JTabbedPaneOperator(this);
84    }
85
86    /**
87     * Constructs a JColorChooserOperator object.
88     *
89     * @param cont a container
90     * @param chooser a component chooser specifying searching criteria.
91     * @param index an index between appropriate ones.
92     */
93    public JColorChooserOperator(ContainerOperator<?> cont, ComponentChooser chooser, int index) {
94        this((JColorChooser) cont.
95                waitSubComponent(new JColorChooserFinder(chooser),
96                        index));
97        copyEnvironment(cont);
98    }
99
100    /**
101     * Constructs a JColorChooserOperator object.
102     *
103     * @param cont a container
104     * @param chooser a component chooser specifying searching criteria.
105     */
106    public JColorChooserOperator(ContainerOperator<?> cont, ComponentChooser chooser) {
107        this(cont, chooser, 0);
108    }
109
110    /**
111     * Constructor. Waits component in container first. Uses cont's timeout and
112     * output for waiting and to init operator.
113     *
114     * @param cont Operator pointing a container to search component in.
115     * @param index Ordinal component index.
116     *
117     */
118    public JColorChooserOperator(ContainerOperator<?> cont, int index) {
119        this((JColorChooser) waitComponent(cont,
120                new JColorChooserFinder(),
121                index));
122        copyEnvironment(cont);
123    }
124
125    /**
126     * Constructor. Waits component in container first. Uses cont's timeout and
127     * output for waiting and to init operator.
128     *
129     * @param cont Operator pointing a container to search component in.
130     *
131     */
132    public JColorChooserOperator(ContainerOperator<?> cont) {
133        this(cont, 0);
134    }
135
136    /**
137     * Searches JColorChooser in container.
138     *
139     * @param cont Container to search component in.
140     * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
141     * @param index Ordinal component index.
142     * @return JColorChooser instance or null if component was not found.
143     */
144    public static JColorChooser findJColorChooser(Container cont, ComponentChooser chooser, int index) {
145        return (JColorChooser) findComponent(cont, new JColorChooserFinder(chooser), index);
146    }
147
148    /**
149     * Searches 0'th JColorChooser in container.
150     *
151     * @param cont Container to search component in.
152     * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
153     * @return JColorChooser instance or null if component was not found.
154     */
155    public static JColorChooser findJColorChooser(Container cont, ComponentChooser chooser) {
156        return findJColorChooser(cont, chooser, 0);
157    }
158
159    /**
160     * Searches JColorChooser in container.
161     *
162     * @param cont Container to search component in.
163     * @param index Ordinal component index.
164     * @return JColorChooser instance or null if component was not found.
165     */
166    public static JColorChooser findJColorChooser(Container cont, int index) {
167        return findJColorChooser(cont, ComponentSearcher.getTrueChooser(Integer.toString(index) + "'th JColorChooser instance"), index);
168    }
169
170    /**
171     * Searches 0'th JColorChooser in container.
172     *
173     * @param cont Container to search component in.
174     * @return JColorChooser instance or null if component was not found.
175     */
176    public static JColorChooser findJColorChooser(Container cont) {
177        return findJColorChooser(cont, 0);
178    }
179
180    /**
181     * Waits JColorChooser in container.
182     *
183     * @param cont Container to search component in.
184     * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
185     * @param index Ordinal component index.
186     * @return JColorChooser instance or null if component was not displayed.
187     *
188     */
189    public static JColorChooser waitJColorChooser(Container cont, ComponentChooser chooser, int index) {
190        return (JColorChooser) waitComponent(cont, new JColorChooserFinder(chooser), index);
191    }
192
193    /**
194     * Waits 0'th JColorChooser in container.
195     *
196     * @param cont Container to search component in.
197     * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
198     * @return JColorChooser instance or null if component was not displayed.
199     *
200     */
201    public static JColorChooser waitJColorChooser(Container cont, ComponentChooser chooser) {
202        return waitJColorChooser(cont, chooser, 0);
203    }
204
205    /**
206     * Waits JColorChooser in container.
207     *
208     * @param cont Container to search component in.
209     * @param index Ordinal component index.
210     * @return JColorChooser instance or null if component was not displayed.
211     *
212     */
213    public static JColorChooser waitJColorChooser(Container cont, int index) {
214        return waitJColorChooser(cont, ComponentSearcher.getTrueChooser(Integer.toString(index) + "'th JColorChooser instance"), index);
215    }
216
217    /**
218     * Waits 0'th JColorChooser in container.
219     *
220     * @param cont Container to search component in.
221     * @return JColorChooser instance or null if component was not displayed.
222     *
223     */
224    public static JColorChooser waitJColorChooser(Container cont) {
225        return waitJColorChooser(cont, 0);
226    }
227
228    @Override
229    public void setOutput(TestOut out) {
230        output = out;
231        super.setOutput(output.createErrorOutput());
232    }
233
234    @Override
235    public TestOut getOutput() {
236        return output;
237    }
238
239    /**
240     * Switches tab to "RGB" page.
241     */
242    public void switchToRGB() {
243        if (!tabbed.getTitleAt(tabbed.getSelectedIndex()).
244                equals(RGB_TITLE)) {
245            tabbed.selectPage(RGB_TITLE);
246        }
247        blue = new JTextFieldOperator(this, 2);
248        green = new JTextFieldOperator(this, 1);
249        red = new JTextFieldOperator(this, 0);
250    }
251
252    /**
253     * Enters red color component value. Switches to "RGB" page first.
254     *
255     * @param value red color component
256     * @see #switchToRGB()
257     * @see #enterColor(int, int, int)
258     * @see #enterColor(java.awt.Color)
259     * @see #enterColor(int)
260     */
261    public void enterRed(int value) {
262        switchToRGB();
263        red.setText(Integer.toString(value));
264    }
265
266    /**
267     * Enters green color component value. Switches to "RGB" page first.
268     *
269     * @param value green color component
270     * @see #switchToRGB()
271     * @see #enterColor(int, int, int)
272     * @see #enterColor(java.awt.Color)
273     * @see #enterColor(int)
274     */
275    public void enterGreen(int value) {
276        switchToRGB();
277        green.setText(Integer.toString(value));
278    }
279
280    /**
281     * Enters blue color component value. Switches to "RGB" page first.
282     *
283     * @param value blue color component
284     * @see #switchToRGB()
285     * @see #enterColor(int, int, int)
286     * @see #enterColor(java.awt.Color)
287     * @see #enterColor(int)
288     */
289    public void enterBlue(int value) {
290        switchToRGB();
291        blue.setText(Integer.toString(value));
292    }
293
294    /**
295     * Enters all color components values. Switches to "RGB" page first.
296     *
297     * @param red red color component
298     * @param green green color component
299     * @param blue blue color component
300     * @see #switchToRGB()
301     * @see #enterColor(java.awt.Color)
302     * @see #enterColor(int)
303     */
304    public void enterColor(int red, int green, int blue) {
305        switchToRGB();
306        enterRed(red);
307        enterGreen(green);
308        enterBlue(blue);
309    }
310
311    /**
312     * Enters color. Switches to "RGB" page first.
313     *
314     * @param color a color
315     * @see #switchToRGB()
316     * @see #enterColor(int, int, int)
317     * @see #enterColor(int)
318     */
319    public void enterColor(Color color) {
320        enterColor(color.getRed(), color.getGreen(), color.getBlue());
321    }
322
323    /**
324     * Enters color. Switches to "RGB" page first.
325     *
326     * @param color a color
327     * @see #switchToRGB()
328     * @see #enterColor(int, int, int)
329     * @see #enterColor(java.awt.Color)
330     */
331    public void enterColor(int color) {
332        enterColor(new Color(color));
333    }
334
335    /**
336     * Returns information about component.
337     */
338    @Override
339    public Hashtable<String, Object> getDump() {
340        Hashtable<String, Object> result = super.getDump();
341        result.put(COLOR_DPROP, ((JColorChooser) getSource()).getColor().toString());
342        JTabbedPane tb = (JTabbedPane) tabbed.getSource();
343        result.put(SELECTED_PAGE_DPROP, tb.getTitleAt(tb.getSelectedIndex()));
344        return result;
345    }
346
347    ////////////////////////////////////////////////////////
348    //Mapping                                             //
349    /**
350     * Maps
351     * {@code JColorChooser.addChooserPanel(AbstractColorChooserPanel)}
352     * through queue
353     */
354    public void addChooserPanel(final AbstractColorChooserPanel abstractColorChooserPanel) {
355        runMapping(new MapVoidAction("addChooserPanel") {
356            @Override
357            public void map() {
358                ((JColorChooser) getSource()).addChooserPanel(abstractColorChooserPanel);
359            }
360        });
361    }
362
363    /**
364     * Maps {@code JColorChooser.getChooserPanels()} through queue
365     */
366    public AbstractColorChooserPanel[] getChooserPanels() {
367        return ((AbstractColorChooserPanel[]) runMapping(new MapAction<Object>("getChooserPanels") {
368            @Override
369            public Object map() {
370                return ((JColorChooser) getSource()).getChooserPanels();
371            }
372        }));
373    }
374
375    /**
376     * Maps {@code JColorChooser.getColor()} through queue
377     */
378    public Color getColor() {
379        return (runMapping(new MapAction<Color>("getColor") {
380            @Override
381            public Color map() {
382                return ((JColorChooser) getSource()).getColor();
383            }
384        }));
385    }
386
387    /**
388     * Maps {@code JColorChooser.getPreviewPanel()} through queue
389     */
390    public JComponent getPreviewPanel() {
391        return (runMapping(new MapAction<JComponent>("getPreviewPanel") {
392            @Override
393            public JComponent map() {
394                return ((JColorChooser) getSource()).getPreviewPanel();
395            }
396        }));
397    }
398
399    /**
400     * Maps {@code JColorChooser.getSelectionModel()} through queue
401     */
402    public ColorSelectionModel getSelectionModel() {
403        return (runMapping(new MapAction<ColorSelectionModel>("getSelectionModel") {
404            @Override
405            public ColorSelectionModel map() {
406                return ((JColorChooser) getSource()).getSelectionModel();
407            }
408        }));
409    }
410
411    /**
412     * Maps {@code JColorChooser.getUI()} through queue
413     */
414    public ColorChooserUI getUI() {
415        return (runMapping(new MapAction<ColorChooserUI>("getUI") {
416            @Override
417            public ColorChooserUI map() {
418                return ((JColorChooser) getSource()).getUI();
419            }
420        }));
421    }
422
423    /**
424     * Maps
425     * {@code JColorChooser.removeChooserPanel(AbstractColorChooserPanel)}
426     * through queue
427     */
428    public AbstractColorChooserPanel removeChooserPanel(final AbstractColorChooserPanel abstractColorChooserPanel) {
429        return (runMapping(new MapAction<AbstractColorChooserPanel>("removeChooserPanel") {
430            @Override
431            public AbstractColorChooserPanel map() {
432                return ((JColorChooser) getSource()).removeChooserPanel(abstractColorChooserPanel);
433            }
434        }));
435    }
436
437    /**
438     * Maps
439     * {@code JColorChooser.setChooserPanels(AbstractColorChooserPanel[])}
440     * through queue
441     */
442    public void setChooserPanels(final AbstractColorChooserPanel[] abstractColorChooserPanel) {
443        runMapping(new MapVoidAction("setChooserPanels") {
444            @Override
445            public void map() {
446                ((JColorChooser) getSource()).setChooserPanels(abstractColorChooserPanel);
447            }
448        });
449    }
450
451    /**
452     * Maps {@code JColorChooser.setColor(int)} through queue
453     */
454    public void setColor(final int i) {
455        runMapping(new MapVoidAction("setColor") {
456            @Override
457            public void map() {
458                ((JColorChooser) getSource()).setColor(i);
459            }
460        });
461    }
462
463    /**
464     * Maps {@code JColorChooser.setColor(int, int, int)} through queue
465     */
466    public void setColor(final int i, final int i1, final int i2) {
467        runMapping(new MapVoidAction("setColor") {
468            @Override
469            public void map() {
470                ((JColorChooser) getSource()).setColor(i, i1, i2);
471            }
472        });
473    }
474
475    /**
476     * Maps {@code JColorChooser.setColor(Color)} through queue
477     */
478    public void setColor(final Color color) {
479        runMapping(new MapVoidAction("setColor") {
480            @Override
481            public void map() {
482                ((JColorChooser) getSource()).setColor(color);
483            }
484        });
485    }
486
487    /**
488     * Maps {@code JColorChooser.setPreviewPanel(JComponent)} through queue
489     */
490    public void setPreviewPanel(final JComponent jComponent) {
491        runMapping(new MapVoidAction("setPreviewPanel") {
492            @Override
493            public void map() {
494                ((JColorChooser) getSource()).setPreviewPanel(jComponent);
495            }
496        });
497    }
498
499    /**
500     * Maps {@code JColorChooser.setSelectionModel(ColorSelectionModel)}
501     * through queue
502     */
503    public void setSelectionModel(final ColorSelectionModel colorSelectionModel) {
504        runMapping(new MapVoidAction("setSelectionModel") {
505            @Override
506            public void map() {
507                ((JColorChooser) getSource()).setSelectionModel(colorSelectionModel);
508            }
509        });
510    }
511
512    /**
513     * Maps {@code JColorChooser.setUI(ColorChooserUI)} through queue
514     */
515    public void setUI(final ColorChooserUI colorChooserUI) {
516        runMapping(new MapVoidAction("setUI") {
517            @Override
518            public void map() {
519                ((JColorChooser) getSource()).setUI(colorChooserUI);
520            }
521        });
522    }
523
524    //End of mapping                                      //
525    ////////////////////////////////////////////////////////
526    /**
527     * Checks component type.
528     */
529    public static class JColorChooserFinder extends Finder {
530
531        /**
532         * Constructs JColorChooserFinder.
533         *
534         * @param sf other searching criteria.
535         */
536        public JColorChooserFinder(ComponentChooser sf) {
537            super(JColorChooser.class, sf);
538        }
539
540        /**
541         * Constructs JColorChooserFinder.
542         */
543        public JColorChooserFinder() {
544            super(JColorChooser.class);
545        }
546    }
547
548}
549