001 /* 002 * CRIMSON 003 * Copyright (c) 2006, Stephen Fisher, Susan Davidson, and Junhyong Kim, 004 * University of Pennsylvania. 005 * 006 * This program is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU General Public License as 008 * published by the Free Software Foundation; either version 2 of the 009 * License, or (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, but 012 * WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 * 02110-1301 USA. 020 * 021 * @(#)ModelTableModel.java 022 */ 023 024 package edu.upenn.crimson; 025 026 import java.util.ArrayList; 027 import javax.swing.table.*; 028 029 /** 030 * ModelTableModel. This is a TableModel that is used by ModelManager to 031 * list the models. It is maintained here so that it can be updated 032 * easily whenever the model list is rebuilt. 033 * 034 * @author Stephen Fisher 035 * @version $Id: ModelTableModel.java,v 1.3 2007/05/16 18:55:58 fisher Exp $ 036 */ 037 038 public class ModelTableModel extends AbstractTableModel { 039 040 /** Stores the Models that are in the table. */ 041 private ArrayList models = new ArrayList(); 042 043 private String[] columnNames = {"Model Name", "Notes"}; 044 045 //----------------------------------------- 046 // Methods added to handle models ArrayList 047 048 /** Append a new row to the end of the table. */ 049 public void addRow(Model model) { addRowAt(model, -1); } 050 051 /** 052 * Add new row to table. If row index is '-1' then add to end 053 * of table. 054 */ 055 public void addRowAt(Model model, int row) { 056 if (row == -1) models.add(model); 057 else models.add(row, model); 058 059 fireTableDataChanged(); 060 } 061 062 /** 063 * Returns the Model that represents the specified row in the 064 * table. 065 */ 066 public Model getRow(int row) { 067 if (models.isEmpty()) return null; 068 else return (Model) models.get(row); 069 } 070 071 /** 072 * Removes the Model that represents the specified row in the 073 * table. 074 */ 075 public void removeRow(int row) { removeRows(row, row); } 076 077 /** Removes the Models that represent the rows specified. */ 078 public void removeRows(int[] rows) { 079 removeRows(rows[0], rows[rows.length-1]); 080 } 081 082 /** 083 * Removes the Models that represent the specified row in the 084 * table. If the row is -1 then will clear the entire table. 085 */ 086 public void removeRows(int minRow, int maxRow) { 087 if (models.isEmpty()) return; 088 089 if (minRow == maxRow) { 090 if (minRow == -1) models.clear(); // remove all rows 091 else models.remove(minRow); // remove one row 092 } else { 093 // can't use removeRange() because protected method 094 while (maxRow >= minRow) { 095 models.remove(maxRow--); 096 } 097 } 098 099 fireTableDataChanged(); 100 } 101 102 /** Removes all rows from the table. */ 103 public void removeAllRows() { removeRow(-1); } 104 105 //---------------------------------------- 106 // Standard AbstractTableModel methods 107 108 public int getColumnCount() { return columnNames.length; } 109 110 public int getRowCount() { 111 if (models.isEmpty()) return 0; 112 else return models.size(); 113 } 114 115 public String getColumnName(int col) { return columnNames[col]; } 116 117 /** "ID", "Notes" */ 118 public Object getValueAt(int row, int col) { 119 Model model = getRow(row); 120 switch (col) { 121 case 0: return " " + model.getID(); 122 case 1: return " " + model.getNotes(); 123 default: return model.toString(); 124 } 125 } 126 } // ModelTableModel.java