1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.commons.gui.table;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import javax.swing.table.AbstractTableModel;
25
26 public abstract class AbstractSimpleTableModel<T> extends AbstractTableModel {
27
28 private Class[] columnClasses;
29
30 private List<T> data = new ArrayList<T>(0);
31
32 public AbstractSimpleTableModel(Class[] columnClasses)
33 {
34 this.columnClasses = columnClasses;
35 }
36
37 public void add(int index, T item)
38 {
39 data.add(index, item);
40 fireTableRowsInserted(index, index);
41 }
42
43 public void add(int index, T[] items)
44 {
45 for (T item : items) {
46 data.add(index, item);
47 }
48 fireTableRowsInserted(index, index + items.length - 1);
49 }
50
51 public void add(T item)
52 {
53 add(data.size(), item);
54 }
55
56 public Class<?> getColumnClass(int column)
57 {
58 return columnClasses[column];
59 }
60
61 public int getColumnCount()
62 {
63 return columnClasses.length;
64 }
65
66 public List<T> getData()
67 {
68 return data;
69 }
70
71 public T getItem(int row)
72 {
73 return data.get(row);
74 }
75
76 public int getRowCount()
77 {
78 return data.size();
79 }
80
81 public abstract Object getValueAt(int row, int column);
82
83 public void remove(int index)
84 {
85 data.remove(index);
86 fireTableRowsDeleted(index, index);
87 }
88
89 public void remove(int firstRow, int lastRow)
90 {
91 for (int i = firstRow; i <= lastRow; i++) {
92 data.remove(firstRow);
93 }
94 fireTableRowsDeleted(firstRow, lastRow);
95 }
96
97 public boolean remove(T item)
98 {
99 int index = data.indexOf(item);
100 if (index != -1) {
101 data.remove(index);
102 fireTableRowsDeleted(index, index);
103 return true;
104 }
105 return false;
106 }
107
108 public void setData(List<T> data)
109 {
110 this.data = data;
111 fireTableDataChanged();
112 }
113
114 }