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