2 * Copyright 2007-2013 Bas Leijdekkers
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.codeInspection.ui;
18 import com.intellij.util.containers.ContainerUtil;
19 import org.jetbrains.annotations.NotNull;
21 import javax.swing.table.AbstractTableModel;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
26 public class ListWrappingTableModel extends AbstractTableModel {
28 private final List<List<String>> list;
29 private final List<String> columnNames = new ArrayList<>();
31 public ListWrappingTableModel(@NotNull List<List<String>> list,
32 @NotNull String... columnNames) {
34 ContainerUtil.addAll(this.columnNames, columnNames);
38 * Constructor to create a single column model.
40 * @param list the rows of the table
41 * @param columnName the name in the column header
43 public ListWrappingTableModel(@NotNull List<String> list, @NotNull String columnName) {
44 this.list = new ArrayList<>();
46 columnNames.add(columnName);
49 public void addRow(String... values) {
50 if (list.size() < values.length) {
51 throw new IllegalArgumentException("number of table columns: " +
52 list.size() + " does not match number of argument " +
53 "columns: " + values.length);
56 for (; i < values.length; i++) {
57 final String value = values[i];
58 list.get(i).add(value);
60 for (int max = list.size();i < max; i++) {
63 final int index = list.get(0).size() - 1;
64 fireTableRowsInserted(index, index);
67 public void addRow() {
68 final int columnCount = list.size();
69 final String[] strings = new String[columnCount];
70 Arrays.fill(strings, "");
75 public Class<String> getColumnClass(int columnIndex) {
80 public int getColumnCount() {
81 return columnNames.size();
85 public String getColumnName(int columnIndex) {
86 if (columnIndex < columnNames.size()) {
87 return columnNames.get(columnIndex);
93 public int getRowCount() {
94 final List<String> column0 = list.get(0);
95 if (column0 == null) {
98 return column0.size();
102 public Object getValueAt(int rowIndex, int columnIndex) {
103 return list.get(columnIndex).get(rowIndex);
106 public int indexOf(String value, int columnIndex) {
107 return list.get(columnIndex).indexOf(value);
111 public boolean isCellEditable(int rowIndex, int columnIndex) {
115 public void removeRow(int rowIndex) {
116 for (List<String> column : list) {
117 column.remove(rowIndex);
119 fireTableRowsDeleted(rowIndex, rowIndex);
123 public void setValueAt(Object value, int rowIndex, int columnIndex) {
124 final List<String> strings = list.get(columnIndex);
125 if (rowIndex >= 0 && rowIndex < strings.size()) {
126 strings.set(rowIndex, String.valueOf(value));
127 fireTableCellUpdated(rowIndex, columnIndex);