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
24/*
25 * @test
26 * @key headful
27 * @bug 7068740
28 * @summary JTable wrapped in JLayer can't use PGUP/PGDOWN keys
29 * @author Vladislav Karnaukhov
30 * @run main bug7068740
31 */
32
33import javax.swing.*;
34import javax.swing.plaf.LayerUI;
35import javax.swing.plaf.metal.MetalLookAndFeel;
36import javax.swing.table.DefaultTableModel;
37import java.awt.*;
38import java.awt.event.KeyEvent;
39import java.lang.reflect.InvocationTargetException;
40import java.util.concurrent.atomic.AtomicInteger;
41
42public class bug7068740 extends JFrame {
43
44    private static Robot robot = null;
45    private static JTable table = null;
46
47    bug7068740() {
48        super();
49        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
50
51        DefaultTableModel model = new DefaultTableModel() {
52            @Override
53            public int getRowCount() {
54                return 20;
55            }
56
57            @Override
58            public int getColumnCount() {
59                return 2;
60            }
61
62            @Override
63            public Object getValueAt(int row, int column) {
64                return "(" + row + "," + column + ")";
65            }
66        };
67
68        table = new JTable(model);
69        table.setRowSelectionInterval(0, 0);
70        LayerUI<JComponent> layerUI = new LayerUI<>();
71        JLayer<JComponent> layer = new JLayer<>(table, layerUI);
72        JScrollPane scrollPane = new JScrollPane(layer);
73        add(scrollPane);
74        pack();
75        setLocationRelativeTo(null);
76    }
77
78    private static void setUp() {
79        try {
80            if (robot == null) {
81                robot = new Robot();
82                robot.setAutoDelay(50);
83            }
84
85            SwingUtilities.invokeAndWait(new Runnable() {
86                @Override
87                public void run() {
88                    bug7068740 test = new bug7068740();
89                    test.setVisible(true);
90                }
91            });
92        } catch (InterruptedException e) {
93            e.printStackTrace();
94            throw new RuntimeException("Test failed");
95        } catch (InvocationTargetException e) {
96            e.printStackTrace();
97            throw new RuntimeException("Test failed");
98        } catch (AWTException e) {
99            e.printStackTrace();
100            throw new RuntimeException("Test failed");
101        }
102    }
103
104    private static int getSelectedRow() throws Exception {
105        final AtomicInteger row = new AtomicInteger(-1);
106        SwingUtilities.invokeAndWait(new Runnable() {
107            @Override
108            public void run() {
109                row.set(table.getSelectedRow());
110            }
111        });
112        return row.intValue();
113    }
114
115    private static void doTest() throws Exception {
116        robot.waitForIdle();
117
118        robot.keyPress(KeyEvent.VK_PAGE_DOWN);
119        robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
120        robot.waitForIdle();
121
122        if (getSelectedRow() != 19) {
123            throw new RuntimeException("Test failed");
124        }
125
126        robot.keyPress(KeyEvent.VK_PAGE_UP);
127        robot.keyRelease(KeyEvent.VK_PAGE_UP);
128        robot.waitForIdle();
129        if (getSelectedRow() != 0) {
130            throw new RuntimeException("Test failed");
131        }
132    }
133
134    public static void main(String[] args) throws Exception {
135        try {
136            UIManager.setLookAndFeel(new MetalLookAndFeel());
137            setUp();
138            doTest();
139        } catch (UnsupportedLookAndFeelException e) {
140            e.printStackTrace();
141            throw new RuntimeException("Test failed");
142        }
143    }
144}
145