View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Steffen Pingel
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 }