1 /*
   2  * Copyright (c) 2014, 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 8032874
  28  * @summary Test whether ArrayIndexOutOfBoundsException is thrown or not,
  29  *          once selected row is removed from JTable with Sorter and Filter
  30  * @author Dmitry Markov
  31  * @run main bug8032874
  32  */
  33 
  34 import java.awt.*;
  35 import java.util.ArrayList;
  36 import java.util.List;
  37 
  38 import javax.swing.*;
  39 import javax.swing.table.AbstractTableModel;
  40 import javax.swing.table.TableRowSorter;
  41 
  42 public class bug8032874 {
  43     private static final int ROW_COUNT = 5;
  44     private static JTable table;
  45     private static TestTableModel tableModel;
  46 
  47     public static void main(String args[]) throws Exception {
  48         Robot robot = new Robot();
  49 
  50         SwingUtilities.invokeAndWait(new Runnable() {
  51             @Override
  52             public void run() {
  53                 createAndShowUI();
  54             }
  55         });
  56         robot.waitForIdle();
  57 
  58         SwingUtilities.invokeAndWait(new Runnable() {
  59             @Override
  60             public void run() {
  61                 table.getRowSorter().toggleSortOrder(0);
  62                 table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
  63                 table.setRowSelectionInterval(1, 2);
  64             }
  65         });
  66         robot.waitForIdle();
  67 
  68         SwingUtilities.invokeAndWait(new Runnable() {
  69             @Override
  70             public void run() {
  71                 for (int i = 0; i < ROW_COUNT; i++) {
  72                     tableModel.remove(0);
  73                     table.getRowSorter().toggleSortOrder(0);
  74                 }
  75             }
  76         });
  77     }
  78 
  79     public static void createAndShowUI() {
  80         try {
  81             UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  82         } catch (Exception e) {
  83             throw new RuntimeException(e);
  84         }
  85 
  86         JFrame frame = new JFrame("bug8032874");
  87         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  88 
  89         JPanel panel = new JPanel();
  90         panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  91 
  92         tableModel = new TestTableModel();
  93         table = new JTable(tableModel);
  94         table.setSurrendersFocusOnKeystroke(true);
  95 
  96         final TableRowSorter<TestTableModel> rowSorter = new TableRowSorter<TestTableModel>(tableModel);
  97         rowSorter.setRowFilter(new RowFilter<TestTableModel, Integer>() {
  98             @Override
  99             public boolean include(Entry<? extends TestTableModel, ? extends Integer> entry) {
 100                 return entry.getIdentifier() % 2 == 0;
 101             }
 102         });
 103         table.setRowSorter(rowSorter);
 104 
 105         JScrollPane jScrollPane = new JScrollPane(table);
 106         panel.add(jScrollPane);
 107 
 108         frame.setContentPane(panel);
 109         frame.setSize(new Dimension(800, 600));
 110         frame.setVisible(true);
 111     }
 112 
 113     private static class TestTableModel extends AbstractTableModel {
 114         private final List<Integer> data;
 115 
 116         public TestTableModel() {
 117             data = new ArrayList<Integer>();
 118 
 119             for (int i = 0; i < ROW_COUNT; i++) {
 120                 data.add(i);
 121             }
 122         }
 123 
 124         @Override
 125         public int getRowCount() {
 126             return data.size();
 127         }
 128 
 129         @Override
 130         public int getColumnCount() {
 131             return 1;
 132         }
 133 
 134         @Override
 135         public Object getValueAt(int rowIndex, int columnIndex) {
 136             return data.get(rowIndex);
 137         }
 138 
 139         public void remove(int row) {
 140             data.remove(row);
 141             fireTableRowsDeleted(row, row);
 142         }
 143     }
 144 }
 145