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