1 /*** 2 * Copyright (c) 2003 held jointly by the individual authors. 3 * 4 * This library is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU Lesser General Public License as published 6 * by the Free Software Foundation; either version 2.1 of the License, or 7 * (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, but 10 * WITHOUT ANY WARRANTY; with out even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public License 15 * along with this library; if not, write to the Free Software Foundation, 16 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 17 * 18 * > http://www.gnu.org/copyleft/lesser.html 19 * > http://www.opensource.org/licenses/lgpl-license.php 20 */ 21 package net.mlw.vlh.swing.support; 22 23 import java.util.LinkedList; 24 import java.util.List; 25 26 import javax.swing.table.AbstractTableModel; 27 28 import net.mlw.vlh.ValueList; 29 import net.mlw.vlh.swing.ValueListTableModel; 30 31 /*** 32 * @author Matthew L. Wilson 33 * @version $Revision: 1.5 $ $Date: 2005/08/30 18:54:25 $ 34 */ 35 public abstract class AbstractValueListTableModel extends AbstractTableModel implements ValueListTableModel 36 { 37 protected List list = new LinkedList(); 38 39 protected int rowCount = 0; 40 41 /*** 42 * @see javax.swing.table.TableModel#getRowCount() 43 */ 44 public int getRowCount() 45 { 46 return rowCount; 47 } 48 49 public synchronized int addBean(Object bean) 50 { 51 list.add(bean); 52 return (rowCount = list.size()) - 1; 53 } 54 55 public Object getBean(int row) 56 { 57 if (row == -1 || list.size() < row) 58 { 59 return null; 60 } 61 return list.get(row); 62 } 63 64 public synchronized void setList(List list) 65 { 66 this.list = list; 67 this.rowCount = list.size(); 68 fireTableDataChanged(); 69 } 70 71 public synchronized void setValueList(ValueList valueList) 72 { 73 this.list = valueList.getList(); 74 this.rowCount = list.size(); 75 fireTableDataChanged(); 76 } 77 78 /*** 79 * @see net.mlw.vlh.swing.ValueListTableModel#trimFromBottomOfList() 80 */ 81 public synchronized int trimFromList(int index) 82 { 83 list.remove(index); 84 return (rowCount = list.size()) - 1; 85 } 86 87 /*** 88 * @see net.mlw.vlh.swing.ValueListTableModel#contains(Object) 89 */ 90 public boolean contains(Object bean) 91 { 92 return list.contains(bean); 93 } 94 95 public synchronized int removeBean(Object bean) 96 { 97 int index = list.indexOf(bean); 98 if (index >= 0) 99 { 100 list.remove(index); 101 this.rowCount = list.size(); 102 } 103 return index; 104 } 105 }