5314N/A/*
5314N/A * Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
5314N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5314N/A *
5314N/A * This code is free software; you can redistribute it and/or modify it
5314N/A * under the terms of the GNU General Public License version 2 only, as
5314N/A * published by the Free Software Foundation.
5314N/A *
5314N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5314N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5314N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5314N/A * version 2 for more details (a copy is included in the LICENSE file that
5314N/A * accompanied this code).
5314N/A *
5314N/A * You should have received a copy of the GNU General Public License version
5314N/A * 2 along with this work; if not, write to the Free Software Foundation,
5314N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5314N/A *
5314N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5314N/A * or visit www.oracle.com if you need additional information or have any
5314N/A * questions.
5314N/A */
5314N/A
5314N/A/*
5314N/A * Portions Copyright (c) 2012 IBM Corporation
5314N/A */
5314N/A
5314N/A/* @test 1.1 2012/04/19
5314N/A * @bug 7055065
5314N/A * @summary NullPointerException when sorting JTable with empty cell
5314N/A * @author Jonathan Lu
5314N/A * @library ../../regtesthelpers/
5314N/A * @build Util
5314N/A * @run main bug7055065
5314N/A */
5314N/A
5314N/Aimport java.awt.Dimension;
5314N/Aimport java.awt.Point;
5314N/Aimport java.awt.Rectangle;
5314N/Aimport java.awt.Robot;
5314N/Aimport java.awt.Toolkit;
5314N/Aimport java.awt.event.InputEvent;
5314N/Aimport java.awt.event.KeyEvent;
5314N/Aimport javax.swing.JFrame;
5314N/Aimport javax.swing.JPanel;
5314N/Aimport javax.swing.JScrollPane;
5314N/Aimport javax.swing.JTable;
5314N/Aimport javax.swing.SwingUtilities;
5314N/Aimport javax.swing.table.AbstractTableModel;
5314N/Aimport javax.swing.table.TableModel;
5314N/Aimport javax.swing.table.TableRowSorter;
5314N/Aimport sun.awt.SunToolkit;
5314N/Aimport java.util.concurrent.Callable;
5314N/A
5314N/Apublic class bug7055065 {
5314N/A
5314N/A private static JTable table;
5314N/A
5314N/A public static void main(String[] args) throws Exception {
5314N/A SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
5314N/A Robot robot = new Robot();
5314N/A
5314N/A SwingUtilities.invokeAndWait(new Runnable() {
5314N/A
5314N/A public void run() {
5314N/A createAndShowUI();
5314N/A }
5314N/A });
5314N/A
5314N/A toolkit.realSync();
5314N/A clickCell(robot, 1, 1);
5314N/A Util.hitKeys(robot, KeyEvent.VK_BACK_SPACE, KeyEvent.VK_BACK_SPACE,
5314N/A KeyEvent.VK_BACK_SPACE);
5314N/A
5314N/A toolkit.realSync();
5314N/A clickColumnHeader(robot, 1);
5314N/A
5314N/A toolkit.realSync();
5314N/A clickColumnHeader(robot, 1);
5314N/A }
5314N/A
5314N/A private static void clickCell(Robot robot, final int row, final int column)
5314N/A throws Exception {
5314N/A Point point = Util.invokeOnEDT(new Callable<Point>() {
5314N/A @Override
5314N/A public Point call() throws Exception {
5314N/A Rectangle rect = table.getCellRect(row, column, false);
5314N/A Point point = new Point(rect.x + rect.width / 2, rect.y
5314N/A + rect.height / 2);
5314N/A SwingUtilities.convertPointToScreen(point, table);
5314N/A return point;
5314N/A }
5314N/A });
5314N/A
5314N/A robot.mouseMove(point.x, point.y);
5314N/A robot.mousePress(InputEvent.BUTTON1_MASK);
5314N/A robot.mouseRelease(InputEvent.BUTTON1_MASK);
5314N/A }
5314N/A
5314N/A private static void clickColumnHeader(Robot robot, final int column)
5314N/A throws Exception {
5314N/A Point point = Util.invokeOnEDT(new Callable<Point>() {
5314N/A @Override
5314N/A public Point call() throws Exception {
5314N/A Rectangle rect = table.getCellRect(0, column, false);
5314N/A int headerHeight = table.getTableHeader().getHeight();
5314N/A Point point = new Point(rect.x + rect.width / 2, rect.y
5314N/A - headerHeight / 2);
5314N/A SwingUtilities.convertPointToScreen(point, table);
5314N/A return point;
5314N/A }
5314N/A });
5314N/A
5314N/A robot.mouseMove(point.x, point.y);
5314N/A robot.mousePress(InputEvent.BUTTON1_MASK);
5314N/A robot.mouseRelease(InputEvent.BUTTON1_MASK);
5314N/A }
5314N/A
5314N/A private static void createAndShowUI() {
5314N/A JFrame frame = new JFrame("SimpleTableDemo");
5314N/A frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
5314N/A
5314N/A JPanel newContentPane = new JPanel();
5314N/A newContentPane.setOpaque(true);
5314N/A frame.setContentPane(newContentPane);
5314N/A
5314N/A final String[] columnNames = { "String", "Number" };
5314N/A final Object[][] data = { { "aaaa", new Integer(1) },
5314N/A { "bbbb", new Integer(3) }, { "cccc", new Integer(2) },
5314N/A { "dddd", new Integer(4) }, { "eeee", new Integer(5) } };
5314N/A table = new JTable(data, columnNames);
5314N/A
5314N/A table.setPreferredScrollableViewportSize(new Dimension(500, 400));
5314N/A table.setFillsViewportHeight(true);
5314N/A
5314N/A TableModel dataModel = new AbstractTableModel() {
5314N/A
5314N/A public int getColumnCount() {
5314N/A return columnNames.length;
5314N/A }
5314N/A
5314N/A public int getRowCount() {
5314N/A return data.length;
5314N/A }
5314N/A
5314N/A public Object getValueAt(int row, int col) {
5314N/A return data[row][col];
5314N/A }
5314N/A
5314N/A public String getColumnName(int column) {
5314N/A return columnNames[column];
5314N/A }
5314N/A
5314N/A public Class<?> getColumnClass(int c) {
5314N/A return getValueAt(0, c).getClass();
5314N/A }
5314N/A
5314N/A public boolean isCellEditable(int row, int col) {
5314N/A return col != 5;
5314N/A }
5314N/A
5314N/A public void setValueAt(Object aValue, int row, int column) {
5314N/A data[row][column] = aValue;
5314N/A }
5314N/A };
5314N/A table.setModel(dataModel);
5314N/A TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
5314N/A dataModel);
5314N/A table.setRowSorter(sorter);
5314N/A
5314N/A JScrollPane scrollPane = new JScrollPane(table);
5314N/A newContentPane.add(scrollPane);
5314N/A
5314N/A frame.pack();
5314N/A frame.setLocation(0, 0);
5314N/A frame.setVisible(true);
5314N/A }
5314N/A}