1 /*
   2  * Copyright (c) 2012, 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  * Portions Copyright (c) 2012 IBM Corporation
  26  */
  27 
  28 /* @test 1.1 2012/04/19
  29  * @bug 7055065
  30  * @summary NullPointerException when sorting JTable with empty cell
  31  * @author Jonathan Lu
  32  * @library ../../regtesthelpers/
  33  * @build Util
  34  * @run main bug7055065
  35  */
  36 
  37 import java.awt.Dimension;
  38 import java.awt.Point;
  39 import java.awt.Rectangle;
  40 import java.awt.Robot;
  41 import java.awt.Toolkit;
  42 import java.awt.event.InputEvent;
  43 import java.awt.event.KeyEvent;
  44 import javax.swing.JFrame;
  45 import javax.swing.JPanel;
  46 import javax.swing.JScrollPane;
  47 import javax.swing.JTable;
  48 import javax.swing.SwingUtilities;
  49 import javax.swing.table.AbstractTableModel;
  50 import javax.swing.table.TableModel;
  51 import javax.swing.table.TableRowSorter;
  52 import sun.awt.SunToolkit;
  53 import java.util.concurrent.Callable;
  54 
  55 public class bug7055065 {
  56 
  57     private static JTable table;
  58 
  59     public static void main(String[] args) throws Exception {
  60         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  61         Robot robot = new Robot();
  62 
  63         SwingUtilities.invokeAndWait(new Runnable() {
  64 
  65             public void run() {
  66                 createAndShowUI();
  67             }
  68         });
  69 
  70         toolkit.realSync();
  71         clickCell(robot, 1, 1);
  72         Util.hitKeys(robot, KeyEvent.VK_BACK_SPACE, KeyEvent.VK_BACK_SPACE,
  73                 KeyEvent.VK_BACK_SPACE);
  74 
  75         toolkit.realSync();
  76         clickColumnHeader(robot, 1);
  77 
  78         toolkit.realSync();
  79         clickColumnHeader(robot, 1);
  80     }
  81 
  82     private static void clickCell(Robot robot, final int row, final int column)
  83         throws Exception {
  84         Point point = Util.invokeOnEDT(new Callable<Point>() {
  85             @Override
  86             public Point call() throws Exception {
  87                 Rectangle rect = table.getCellRect(row, column, false);
  88                 Point point = new Point(rect.x + rect.width / 2, rect.y
  89                     + rect.height / 2);
  90                 SwingUtilities.convertPointToScreen(point, table);
  91                 return point;
  92             }
  93         });
  94 
  95         robot.mouseMove(point.x, point.y);
  96         robot.mousePress(InputEvent.BUTTON1_MASK);
  97         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  98     }
  99 
 100     private static void clickColumnHeader(Robot robot, final int column)
 101         throws Exception {
 102         Point point = Util.invokeOnEDT(new Callable<Point>() {
 103             @Override
 104             public Point call() throws Exception {
 105                 Rectangle rect = table.getCellRect(0, column, false);
 106                 int headerHeight = table.getTableHeader().getHeight();
 107                 Point point = new Point(rect.x + rect.width / 2, rect.y
 108                     - headerHeight / 2);
 109                 SwingUtilities.convertPointToScreen(point, table);
 110                 return point;
 111             }
 112         });
 113 
 114         robot.mouseMove(point.x, point.y);
 115         robot.mousePress(InputEvent.BUTTON1_MASK);
 116         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 117     }
 118 
 119     private static void createAndShowUI() {
 120         JFrame frame = new JFrame("SimpleTableDemo");
 121         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 122 
 123         JPanel newContentPane = new JPanel();
 124         newContentPane.setOpaque(true);
 125         frame.setContentPane(newContentPane);
 126 
 127         final String[] columnNames = { "String", "Number" };
 128         final Object[][] data = { { "aaaa", new Integer(1) },
 129             { "bbbb", new Integer(3) }, { "cccc", new Integer(2) },
 130             { "dddd", new Integer(4) }, { "eeee", new Integer(5) } };
 131         table = new JTable(data, columnNames);
 132 
 133         table.setPreferredScrollableViewportSize(new Dimension(500, 400));
 134         table.setFillsViewportHeight(true);
 135 
 136         TableModel dataModel = new AbstractTableModel() {
 137 
 138             public int getColumnCount() {
 139                 return columnNames.length;
 140             }
 141 
 142             public int getRowCount() {
 143                 return data.length;
 144             }
 145 
 146             public Object getValueAt(int row, int col) {
 147                 return data[row][col];
 148             }
 149 
 150             public String getColumnName(int column) {
 151                 return columnNames[column];
 152             }
 153 
 154             public Class<?> getColumnClass(int c) {
 155                 return getValueAt(0, c).getClass();
 156             }
 157 
 158             public boolean isCellEditable(int row, int col) {
 159                 return col != 5;
 160             }
 161 
 162             public void setValueAt(Object aValue, int row, int column) {
 163                 data[row][column] = aValue;
 164             }
 165         };
 166         table.setModel(dataModel);
 167         TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
 168                 dataModel);
 169         table.setRowSorter(sorter);
 170 
 171         JScrollPane scrollPane = new JScrollPane(table);
 172         newContentPane.add(scrollPane);
 173 
 174         frame.pack();
 175         frame.setLocation(0, 0);
 176         frame.setVisible(true);
 177     }
 178 }