bug4514858.java revision 8224:172405287fde
1/*
2 * Copyright (c) 2013, 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/* @test
24   @bug 4514858 4164779
25   @summary F6, F8 Ctrl-TAB and Ctrl-Shift-TAB in JSplitPane
26   @author Andrey Pikalev
27   @run main/manual bug4514858
28*/
29
30import javax.swing.*;
31import javax.swing.border.TitledBorder;
32import java.awt.*;
33import java.awt.event.*;
34
35
36public class bug4514858  implements ActionListener {
37
38    static String intructions = "Test the F6, F8, Ctrl-TAB and Ctrl-Shift-TAB keybinding functionality in JSplitPane\n" +
39            "with different LookAndFeels (switch LookAndFeel with the buttoms at the bottom of the\n" +
40            "frame \"Test\"):\n\n" +
41            "1. Move focus to the button \"Button 1\" in the frame \"Test\". Then press F6 several times.\n" +
42            "The focus should cycle between five buttons in order from 1 to 5.\n\n" +
43            "2. Move focus to the button \"Button 2\" in the frame \"Test\". Then press F8 three times.\n" +
44            "The splitters of the splitpanes should be highlited in order:\n" +
45            "\"JSplitPane 3\", \"JSplitPane 2\", \"JSplitPane 1\".\n\n" +
46            "3. Move focus to the button \"Button 2\" in the frame \"Test\". Press Ctrl-TAB.\n" +
47            "The focus should go to the \"Button 4\". Then press Ctrl-TAB again.\n" +
48            "The focus should go to the first enabled button at the bottom of frame.\n\n" +
49            "4. Move focus to the button \"Button 4\" in the frame \"Test\". Press Ctrl-Shift-TAB three times.\n" +
50            "The focus should go through the button \"Button 3\", then \"Button 1\", then to the last\n" +
51            "enabled button at the bottom of frame.";
52    static Test test = new Test();
53    JFrame fr;
54    public static void main(String[] argv) throws Exception {
55        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
56        SwingUtilities.invokeAndWait(new Runnable() {
57            public void run() {
58                new bug4514858().createAndShowGUI();
59            }
60        });
61        test.waitTestResult();
62    }
63    public void createAndShowGUI() {
64        fr = new JFrame("Test");
65
66        //-------------------------------------------------------------
67        JButton left2 = new JButton("Button 1");
68
69        JButton left3 = new JButton("Button 2");
70        JButton right3 = new JButton("Button 3");
71
72        JSplitPane right2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, left3, right3);
73        right2.setBorder(new TitledBorder("JSplitPane 3"));
74
75        JSplitPane left1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left2, right2);
76        left1.setBorder(new TitledBorder("JSplitPane 2"));
77
78        JButton left4 = new JButton("Button 4");
79        JButton right4 = new JButton("Button 5");
80
81        JSplitPane right1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left4, right4);
82        right1.setBorder(new TitledBorder("JSplitPane 4"));
83
84        JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, left1, right1);
85        sp.setBorder(new TitledBorder("JSplitPane 1"));
86        fr.getContentPane().add(sp);
87
88        //-------------------------------------------------------------
89        JPanel p = new JPanel();
90
91        JButton metal = new JButton("Metal");
92        metal.setActionCommand("Metal");
93        metal.setEnabled(isSupportedLAF("javax.swing.plaf.metal.MetalLookAndFeel"));
94        metal.addActionListener(this);
95        p.add(metal);
96
97        JButton motif = new JButton("Motif");
98        motif.setActionCommand("Motif");
99        motif.setEnabled(isSupportedLAF("com.sun.java.swing.plaf.motif.MotifLookAndFeel"));
100        motif.addActionListener(this);
101        p.add(motif);
102
103        JButton windows = new JButton("Windows");
104        windows.setActionCommand("Windows");
105        windows.setEnabled(isSupportedLAF("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"));
106        windows.addActionListener(this);
107        p.add(windows);
108
109        fr.getContentPane().add(p, BorderLayout.SOUTH);
110
111        fr.pack();
112        fr.setVisible(true);
113
114        JFrame instrFrame = test.createTestFrame("bug4514858 instructions", null, intructions, 250);
115        instrFrame.setBounds(fr.getWidth() + 50, fr.getHeight(), 600, 400);
116        instrFrame.setVisible(true);
117    }
118
119    private boolean isSupportedLAF(String str) {
120        try {
121            Class c = Class.forName(str);
122            LookAndFeel laf = (LookAndFeel)c.newInstance();
123            return laf.isSupportedLookAndFeel();
124        } catch (Exception e) {
125            return false;
126        }
127    }
128
129    public void actionPerformed(ActionEvent e) {
130        String s = e.getActionCommand();
131        if (s.equals("Metal")) {
132            s = "javax.swing.plaf.metal.MetalLookAndFeel";
133        } else if (s.equals("Motif")) {
134            s = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
135        } else {
136            s = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
137        }
138        try {
139            UIManager.setLookAndFeel(s);
140            SwingUtilities.updateComponentTreeUI(fr);
141            fr.pack();
142        } catch(Exception ex) {
143            ex.printStackTrace();
144            throw new RuntimeException(ex);
145        }
146    }
147   static class Test {
148        private boolean pass;
149        JFrame createTestFrame(String name, Component topComponent, String instructions, int instrHeight) {
150            final String PASS = "Pass";
151            final String FAIL = "Fail";
152            JFrame frame = new JFrame(name);
153            frame.setLayout(new BorderLayout());
154
155            JPanel testButtonsPanel = new JPanel();
156            testButtonsPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 20));
157
158            ActionListener btnAL = new ActionListener() {
159                public void actionPerformed(ActionEvent event) {
160                    switch (event.getActionCommand()) {
161                        case PASS:
162                            pass();
163                            break;
164                        default:
165                            throw new RuntimeException("Test failed.");
166                    }
167                }
168            };
169            JButton passBtn = new JButton(PASS);
170            passBtn.addActionListener(btnAL);
171            passBtn.setActionCommand(PASS);
172
173            JButton failBtn = new JButton(FAIL);
174            failBtn.addActionListener(btnAL);
175            failBtn.setActionCommand(FAIL);
176
177            testButtonsPanel.add(BorderLayout.WEST, passBtn);
178            testButtonsPanel.add(BorderLayout.EAST, failBtn);
179
180            JTextArea instrText = new JTextArea();
181            instrText.setLineWrap(true);
182            instrText.setEditable(false);
183            JScrollPane instrScrollPane = new JScrollPane(instrText);
184            instrScrollPane.setMaximumSize(new Dimension(Integer.MAX_VALUE, instrHeight));
185            instrText.append(instructions);
186
187            JPanel servicePanel = new JPanel();
188            servicePanel.setLayout(new BorderLayout());
189            if (topComponent == null) {
190                frame.add(BorderLayout.CENTER, instrScrollPane);
191            } else {
192                servicePanel.add(BorderLayout.CENTER, instrScrollPane);
193                frame.add(BorderLayout.CENTER, topComponent);
194            }
195            servicePanel.add(BorderLayout.SOUTH, testButtonsPanel);
196
197            frame.add(BorderLayout.SOUTH, servicePanel);
198            return frame;
199        }
200        synchronized void pass() {
201            pass = true;
202            notifyAll();
203        }
204        synchronized void waitTestResult() throws InterruptedException {
205            while (!pass) {
206                wait();
207            }
208        }
209    }
210}
211