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 /*
  25    @test
  26   @key headful
  27    @bug 6884066
  28    @summary JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
  29    @author Alexander Potochkin
  30    @run main bug6884066
  31 */
  32 
  33 import sun.awt.SunToolkit;
  34 
  35 import javax.swing.*;
  36 import javax.swing.table.JTableHeader;
  37 import javax.swing.table.TableColumnModel;
  38 import javax.swing.table.TableColumn;
  39 import java.awt.*;
  40 import java.awt.event.InputEvent;
  41 
  42 import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
  43 
  44 public class bug6884066 {
  45     private static JTableHeader header;
  46 
  47     public static void main(String[] args) throws Exception {
  48 
  49         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  50 
  51         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  52         Robot robot = new Robot();
  53         robot.setAutoDelay(20);
  54         SwingUtilities.invokeAndWait(new Runnable() {
  55             public void run() {
  56                 // just to quickly grab a column model
  57                 JTable table = new JTable(10, 5);
  58                 header = new JTableHeader(table.getColumnModel());
  59                 checkColumn(0, "A");
  60                 JFrame frame = new JFrame("standalone header");
  61                 frame.add(header);
  62                 frame.pack();
  63                 frame.setVisible(true);
  64                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  65             }
  66         });
  67         toolkit.realSync();
  68         Point point = header.getLocationOnScreen();
  69         robot.mouseMove(point.x + 3, point.y + 3);
  70         robot.mousePress(InputEvent.BUTTON1_MASK);
  71         for (int i = 0; i < header.getWidth() - 3; i++) {
  72             robot.mouseMove(point.x + i, point.y + 3);
  73         }
  74         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  75         SwingUtilities.invokeAndWait(new Runnable() {
  76             public void run() {
  77                 TableColumnModel model = header.getColumnModel();
  78                 checkColumn(model.getColumnCount() - 1, "A");
  79             }
  80         });
  81     }
  82 
  83     private static void checkColumn(int index, String str) {
  84         TableColumnModel model = header.getColumnModel();
  85         Object value = model.getColumn(index).getHeaderValue();
  86         if (!str.equals(value)) {
  87             throw new RuntimeException("Unexpected header's value; " +
  88                     "index = " + index + " value = " + value);
  89         }
  90     }
  91 }