cae75d9fd121b2f126f296a83d003b320ee62929
[idea/community.git] / platform / lang-api / src / com / intellij / codeInspection / ui / ListWrappingTableModel.java
1 /*
2  * Copyright 2007-2013 Bas Leijdekkers
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.intellij.codeInspection.ui;
17
18 import com.intellij.util.containers.ContainerUtil;
19 import org.jetbrains.annotations.NotNull;
20
21 import javax.swing.table.AbstractTableModel;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25
26 public class ListWrappingTableModel extends AbstractTableModel {
27
28   private final List<List<String>> list;
29   private final List<String> columnNames = new ArrayList<>();
30
31   public ListWrappingTableModel(@NotNull List<List<String>> list,
32                                 @NotNull String... columnNames) {
33     this.list = list;
34     ContainerUtil.addAll(this.columnNames, columnNames);
35   }
36
37   /**
38    * Constructor to create a single column model.
39    *
40    * @param list       the rows of the table
41    * @param columnName the name in the column header
42    */
43   public ListWrappingTableModel(@NotNull List<String> list, @NotNull String columnName) {
44     this.list = new ArrayList<>();
45     this.list.add(list);
46     columnNames.add(columnName);
47   }
48
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);
54     }
55     int i = 0;
56     for (; i < values.length; i++) {
57       final String value = values[i];
58       list.get(i).add(value);
59     }
60     for (int max = list.size();i < max; i++) {
61       list.get(i).add("");
62     }
63     final int index = list.get(0).size() - 1;
64     fireTableRowsInserted(index, index);
65   }
66
67   public void addRow() {
68     final int columnCount = list.size();
69     final String[] strings = new String[columnCount];
70     Arrays.fill(strings, "");
71     addRow(strings);
72   }
73
74   @Override
75   public Class<String> getColumnClass(int columnIndex) {
76     return String.class;
77   }
78
79   @Override
80   public int getColumnCount() {
81     return columnNames.size();
82   }
83
84   @Override
85   public String getColumnName(int columnIndex) {
86     if (columnIndex < columnNames.size()) {
87       return columnNames.get(columnIndex);
88     }
89     return null;
90   }
91
92   @Override
93   public int getRowCount() {
94     final List<String> column0 = list.get(0);
95     if (column0 == null) {
96       return 0;
97     }
98     return column0.size();
99   }
100
101   @Override
102   public Object getValueAt(int rowIndex, int columnIndex) {
103     return list.get(columnIndex).get(rowIndex);
104   }
105
106   public int indexOf(String value, int columnIndex) {
107     return list.get(columnIndex).indexOf(value);
108   }
109
110   @Override
111   public boolean isCellEditable(int rowIndex, int columnIndex) {
112     return true;
113   }
114
115   public void removeRow(int rowIndex) {
116     for (List<String> column : list) {
117       column.remove(rowIndex);
118     }
119     fireTableRowsDeleted(rowIndex, rowIndex);
120   }
121
122   @Override
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);
128     }
129   }
130 }