Path: blob/master/test/jdk/javax/swing/JSplitPane/4514858/bug4514858.java
41153 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/* @test23@bug 4514858 416477924@summary F6, F8 Ctrl-TAB and Ctrl-Shift-TAB in JSplitPane25@author Andrey Pikalev26@run main/manual bug451485827*/2829import javax.swing.*;30import javax.swing.border.TitledBorder;31import java.awt.*;32import java.awt.event.*;333435public class bug4514858 implements ActionListener {3637static String intructions = "Test the F6, F8, Ctrl-TAB and Ctrl-Shift-TAB keybinding functionality in JSplitPane\n" +38"with different LookAndFeels (switch LookAndFeel with the buttoms at the bottom of the\n" +39"frame \"Test\"):\n\n" +40"1. Move focus to the button \"Button 1\" in the frame \"Test\". Then press F6 several times.\n" +41"The focus should cycle between five buttons in order from 1 to 5.\n\n" +42"2. Move focus to the button \"Button 2\" in the frame \"Test\". Then press F8 three times.\n" +43"The splitters of the splitpanes should be highlited in order:\n" +44"\"JSplitPane 3\", \"JSplitPane 2\", \"JSplitPane 1\".\n\n" +45"3. Move focus to the button \"Button 2\" in the frame \"Test\". Press Ctrl-TAB.\n" +46"The focus should go to the \"Button 4\". Then press Ctrl-TAB again.\n" +47"The focus should go to the first enabled button at the bottom of frame.\n\n" +48"4. Move focus to the button \"Button 4\" in the frame \"Test\". Press Ctrl-Shift-TAB three times.\n" +49"The focus should go through the button \"Button 3\", then \"Button 1\", then to the last\n" +50"enabled button at the bottom of frame.";51static Test test = new Test();52JFrame fr;53public static void main(String[] argv) throws Exception {54UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());55SwingUtilities.invokeAndWait(new Runnable() {56public void run() {57new bug4514858().createAndShowGUI();58}59});60test.waitTestResult();61}62public void createAndShowGUI() {63fr = new JFrame("Test");6465//-------------------------------------------------------------66JButton left2 = new JButton("Button 1");6768JButton left3 = new JButton("Button 2");69JButton right3 = new JButton("Button 3");7071JSplitPane right2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, left3, right3);72right2.setBorder(new TitledBorder("JSplitPane 3"));7374JSplitPane left1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left2, right2);75left1.setBorder(new TitledBorder("JSplitPane 2"));7677JButton left4 = new JButton("Button 4");78JButton right4 = new JButton("Button 5");7980JSplitPane right1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left4, right4);81right1.setBorder(new TitledBorder("JSplitPane 4"));8283JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, left1, right1);84sp.setBorder(new TitledBorder("JSplitPane 1"));85fr.getContentPane().add(sp);8687//-------------------------------------------------------------88JPanel p = new JPanel();8990JButton metal = new JButton("Metal");91metal.setActionCommand("Metal");92metal.setEnabled(isSupportedLAF("javax.swing.plaf.metal.MetalLookAndFeel"));93metal.addActionListener(this);94p.add(metal);9596JButton motif = new JButton("Motif");97motif.setActionCommand("Motif");98motif.setEnabled(isSupportedLAF("com.sun.java.swing.plaf.motif.MotifLookAndFeel"));99motif.addActionListener(this);100p.add(motif);101102JButton windows = new JButton("Windows");103windows.setActionCommand("Windows");104windows.setEnabled(isSupportedLAF("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"));105windows.addActionListener(this);106p.add(windows);107108fr.getContentPane().add(p, BorderLayout.SOUTH);109110fr.pack();111fr.setVisible(true);112113JFrame instrFrame = test.createTestFrame("bug4514858 instructions", null, intructions, 250);114instrFrame.setBounds(fr.getWidth() + 50, fr.getHeight(), 600, 400);115instrFrame.setVisible(true);116}117118private boolean isSupportedLAF(String str) {119try {120Class c = Class.forName(str);121LookAndFeel laf = (LookAndFeel)c.newInstance();122return laf.isSupportedLookAndFeel();123} catch (Exception e) {124return false;125}126}127128public void actionPerformed(ActionEvent e) {129String s = e.getActionCommand();130if (s.equals("Metal")) {131s = "javax.swing.plaf.metal.MetalLookAndFeel";132} else if (s.equals("Motif")) {133s = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";134} else {135s = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";136}137try {138UIManager.setLookAndFeel(s);139SwingUtilities.updateComponentTreeUI(fr);140fr.pack();141} catch(Exception ex) {142ex.printStackTrace();143throw new RuntimeException(ex);144}145}146static class Test {147private boolean pass;148JFrame createTestFrame(String name, Component topComponent, String instructions, int instrHeight) {149final String PASS = "Pass";150final String FAIL = "Fail";151JFrame frame = new JFrame(name);152frame.setLayout(new BorderLayout());153154JPanel testButtonsPanel = new JPanel();155testButtonsPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 20));156157ActionListener btnAL = new ActionListener() {158public void actionPerformed(ActionEvent event) {159switch (event.getActionCommand()) {160case PASS:161pass();162break;163default:164throw new RuntimeException("Test failed.");165}166}167};168JButton passBtn = new JButton(PASS);169passBtn.addActionListener(btnAL);170passBtn.setActionCommand(PASS);171172JButton failBtn = new JButton(FAIL);173failBtn.addActionListener(btnAL);174failBtn.setActionCommand(FAIL);175176testButtonsPanel.add(BorderLayout.WEST, passBtn);177testButtonsPanel.add(BorderLayout.EAST, failBtn);178179JTextArea instrText = new JTextArea();180instrText.setLineWrap(true);181instrText.setEditable(false);182JScrollPane instrScrollPane = new JScrollPane(instrText);183instrScrollPane.setMaximumSize(new Dimension(Integer.MAX_VALUE, instrHeight));184instrText.append(instructions);185186JPanel servicePanel = new JPanel();187servicePanel.setLayout(new BorderLayout());188if (topComponent == null) {189frame.add(BorderLayout.CENTER, instrScrollPane);190} else {191servicePanel.add(BorderLayout.CENTER, instrScrollPane);192frame.add(BorderLayout.CENTER, topComponent);193}194servicePanel.add(BorderLayout.SOUTH, testButtonsPanel);195196frame.add(BorderLayout.SOUTH, servicePanel);197return frame;198}199synchronized void pass() {200pass = true;201notifyAll();202}203synchronized void waitTestResult() throws InterruptedException {204while (!pass) {205wait();206}207}208}209}210211212