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