jdk/test/javax/swing/JTable/8005019/bug8005019.java
changeset 14992 e9db4be29324
child 23010 6dadb192ad81
equal deleted inserted replaced
14968:a41ada2ed4ef 14992:e9db4be29324
       
     1 /*
       
     2  * Copyright (c) 2010, 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 /* @test
       
    25  * @bug 8005019
       
    26  * @summary JTable passes row index instead of length when inserts selection interval
       
    27  * @author Alexander Scherbatiy
       
    28  * @run main bug8005019
       
    29  */
       
    30 
       
    31 import java.util.*;
       
    32 import javax.swing.*;
       
    33 import javax.swing.table.*;
       
    34 
       
    35 public class bug8005019 {
       
    36 
       
    37     public static void main(String[] args) throws Exception {
       
    38         SwingUtilities.invokeAndWait(new Runnable() {
       
    39 
       
    40             @Override
       
    41             public void run() {
       
    42                 testSelectionWithFilterTable();
       
    43             }
       
    44         });
       
    45     }
       
    46 
       
    47     private static void testSelectionWithFilterTable() {
       
    48         DefaultTableModel model = new DefaultTableModel(0, 1);
       
    49         // a model with 3 elements is the minimum to demonstrate
       
    50         // the bug
       
    51         int last = 2;
       
    52         for (int i = 0; i <= last; i++) {
       
    53             model.addRow(new Object[]{i});
       
    54         }
       
    55         JTable table = new JTable(model);
       
    56         table.setAutoCreateRowSorter(true);
       
    57         // set selection at the end
       
    58         table.setRowSelectionInterval(last, last);
       
    59         // exclude rows based on identifier
       
    60         RowFilter filter = new GeneralFilter(new int[]{0});
       
    61         ((DefaultRowSorter) table.getRowSorter()).setRowFilter(filter);
       
    62         // insertRow _before or at_ selected model index, such that
       
    63         // endIndex (in event) > 1
       
    64         model.insertRow(2, new Object[]{"x"});
       
    65     }
       
    66 
       
    67     private static class GeneralFilter extends RowFilter<Object, Object> {
       
    68 
       
    69         private int[] columns;
       
    70         List excludes = Arrays.asList(0);
       
    71 
       
    72         GeneralFilter(int[] columns) {
       
    73             this.columns = columns;
       
    74         }
       
    75 
       
    76         public boolean include(RowFilter.Entry<? extends Object, ? extends Object> value) {
       
    77             int count = value.getValueCount();
       
    78             if (columns.length > 0) {
       
    79                 for (int i = columns.length - 1; i >= 0; i--) {
       
    80                     int index = columns[i];
       
    81                     if (index < count) {
       
    82                         if (include(value, index)) {
       
    83                             return true;
       
    84                         }
       
    85                     }
       
    86                 }
       
    87             } else {
       
    88                 while (--count >= 0) {
       
    89                     if (include(value, count)) {
       
    90                         return true;
       
    91                     }
       
    92                 }
       
    93             }
       
    94             return false;
       
    95         }
       
    96 
       
    97         protected boolean include(
       
    98                 Entry<? extends Object, ? extends Object> entry,
       
    99                 int index) {
       
   100             return !excludes.contains(entry.getIdentifier());
       
   101         }
       
   102     }
       
   103 }