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 /* @test
  25    @bug 6889007
  26    @summary No resize cursor during hovering mouse over JTable
  27    @author Alexander Potochkin
  28 */
  29 
  30 import javax.swing.*;
  31 import javax.swing.plaf.basic.BasicTableHeaderUI;
  32 import javax.swing.table.JTableHeader;
  33 import java.awt.*;
  34 
  35 public class bug6889007 {
  36 
  37     public static void main(String[] args) throws Exception {
  38         Robot robot = new Robot();
  39         robot.setAutoDelay(20);
  40 
  41         final JFrame frame = new JFrame();
  42         frame.setUndecorated(true);
  43 
  44         SwingUtilities.invokeAndWait(new Runnable() {
  45             public void run() {
  46                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  47 
  48                 JTableHeader th = new JTableHeader();
  49                 th.setColumnModel(new JTable(20, 5).getColumnModel());
  50 
  51                 th.setUI(new MyTableHeaderUI());
  52 
  53                 frame.add(th);
  54                 frame.pack();
  55                 frame.setLocationRelativeTo(null);
  56                 frame.setVisible(true);
  57             }
  58         });
  59         robot.waitForIdle();
  60         Point point = frame.getLocationOnScreen();
  61         int shift = 10;
  62         int x = point.x;
  63         int y = point.y + frame.getHeight()/2;
  64         for(int i = -shift; i < frame.getWidth() + 2*shift; i++) {
  65             robot.mouseMove(x++, y);
  66         }
  67         robot.waitForIdle();
  68         // 9 is a magic test number
  69         if (MyTableHeaderUI.getTestValue() != 9) {
  70             throw new RuntimeException("Unexpected test number "
  71                     + MyTableHeaderUI.getTestValue());
  72         }
  73         System.out.println("ok");
  74     }
  75 
  76     static class MyTableHeaderUI extends BasicTableHeaderUI {
  77         private static int testValue;
  78 
  79         protected void rolloverColumnUpdated(int oldColumn, int newColumn) {
  80             increaseTestValue(newColumn);
  81             Cursor cursor = Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR);
  82             if (oldColumn != -1 && newColumn != -1 &&
  83                     header.getCursor() != cursor) {
  84                 throw new RuntimeException("Wrong type of cursor!");
  85             }
  86         }
  87 
  88         private static synchronized void increaseTestValue(int increment) {
  89             testValue += increment;
  90         }
  91 
  92         public static synchronized int getTestValue() {
  93             return testValue;
  94         }
  95     }
  96 }