Path: blob/master/test/jdk/javax/swing/JTable/8005019/bug8005019.java
41153 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 800501925* @summary JTable passes row index instead of length when inserts selection interval26* @author Alexander Scherbatiy27* @run main bug800501928*/2930import java.util.*;31import javax.swing.*;32import javax.swing.table.*;3334public class bug8005019 {3536public static void main(String[] args) throws Exception {37SwingUtilities.invokeAndWait(new Runnable() {3839@Override40public void run() {41testSelectionWithFilterTable();42}43});44}4546private static void testSelectionWithFilterTable() {47DefaultTableModel model = new DefaultTableModel(0, 1);48// a model with 3 elements is the minimum to demonstrate49// the bug50int last = 2;51for (int i = 0; i <= last; i++) {52model.addRow(new Object[]{i});53}54JTable table = new JTable(model);55table.setAutoCreateRowSorter(true);56// set selection at the end57table.setRowSelectionInterval(last, last);58// exclude rows based on identifier59RowFilter filter = new GeneralFilter(new int[]{0});60((DefaultRowSorter) table.getRowSorter()).setRowFilter(filter);61// insertRow _before or at_ selected model index, such that62// endIndex (in event) > 163model.insertRow(2, new Object[]{"x"});64}6566private static class GeneralFilter extends RowFilter<Object, Object> {6768private int[] columns;69List excludes = Arrays.asList(0);7071GeneralFilter(int[] columns) {72this.columns = columns;73}7475public boolean include(RowFilter.Entry<? extends Object, ? extends Object> value) {76int count = value.getValueCount();77if (columns.length > 0) {78for (int i = columns.length - 1; i >= 0; i--) {79int index = columns[i];80if (index < count) {81if (include(value, index)) {82return true;83}84}85}86} else {87while (--count >= 0) {88if (include(value, count)) {89return true;90}91}92}93return false;94}9596protected boolean include(97Entry<? extends Object, ? extends Object> entry,98int index) {99return !excludes.contains(entry.getIdentifier());100}101}102}103104105