1/*
2 * Copyright (c) 2010, 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 */
23
24/*
25 * @test
26 * @bug 6199676
27 * @summary Tests preview panel after L&F changing
28 * @author Sergey Malenkov
29 */
30
31import java.awt.Component;
32import java.awt.Container;
33import javax.swing.JColorChooser;
34import javax.swing.JFrame;
35import javax.swing.JPanel;
36import javax.swing.SwingUtilities;
37import javax.swing.UIManager;
38import javax.swing.UIManager.LookAndFeelInfo;
39
40public class Test6199676 implements Runnable {
41    public static void main(String[] args) {
42        SwingUtilities.invokeLater(new Test6199676());
43    }
44
45    private static void exit(String error) {
46        if (error != null) {
47            System.err.println(error);
48            System.exit(1);
49        }
50        else {
51            System.exit(0);
52        }
53    }
54
55    private static Component getPreview(Container container) {
56        String name = "ColorChooser.previewPanelHolder";
57        for (Component component : container.getComponents()) {
58            if (!name.equals(component.getName())) {
59                component = (component instanceof Container)
60                        ? getPreview((Container) component)
61                        : null;
62            }
63            if (component instanceof Container) {
64                container = (Container) component;
65                return 1 == container.getComponentCount()
66                        ? container.getComponent(0)
67                        : null;
68            }
69        }
70        return null;
71    }
72
73    private static boolean isShowing(Component component) {
74        return (component != null) && component.isShowing();
75    }
76
77    private int index;
78    private boolean updated;
79    private JColorChooser chooser;
80
81    public synchronized void run() {
82        if (this.chooser == null) {
83            this.chooser = new JColorChooser();
84
85            JFrame frame = new JFrame(getClass().getName());
86            frame.add(this.chooser);
87            frame.setVisible(true);
88        }
89        else if (this.updated) {
90            if (isShowing(this.chooser.getPreviewPanel())) {
91                exit("custom preview panel is showing");
92            }
93            exit(null);
94        }
95        else {
96            Component component = this.chooser.getPreviewPanel();
97            if (component == null) {
98                component = getPreview(this.chooser);
99            }
100            if (!isShowing(component)) {
101                exit("default preview panel is not showing");
102            }
103            this.updated = true;
104            this.chooser.setPreviewPanel(new JPanel());
105        }
106        LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
107        LookAndFeelInfo info = infos[++this.index % infos.length];
108        try {
109            UIManager.setLookAndFeel(info.getClassName());
110        }
111        catch (Exception exception) {
112            exit("could not change L&F");
113        }
114        SwingUtilities.updateComponentTreeUI(this.chooser);
115        SwingUtilities.invokeLater(this);
116    }
117}
118