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     * @(#)QueryTableModel.java
022     */
023    
024    package edu.upenn.crimson;
025    
026    import java.util.ArrayList;
027    import javax.swing.table.*;
028    
029    /**
030     * QueryTableModel.  This is a TableModel that is used by QueryManager
031     * to list the queries. It is maintained here so that it can be
032     * updated easily whenever the query list is rebuilt.
033     *
034     * @author  Stephen Fisher
035     * @version $Id: QueryTableModel.java,v 1.11 2007/06/14 19:48:42 fisher Exp $
036     */
037    
038    public class QueryTableModel extends AbstractTableModel {
039             /** Stores the Queries that are in the table. */
040             private ArrayList queries = new ArrayList();
041             
042             private String[] columnNames = {"Query ID", "Tree", "Leaf Selection", "Seq Selection", "Saved"};
043             
044             //-----------------------------------------
045             // Methods added to handle queries ArrayList
046             
047             /** Append a new row to the end of the table. */
048             public void addRow(Query query) { addRowAt(query, -1); }
049             
050             /** 
051              * Add new row to table.  If row index is '-1' then add to end
052              * of table.
053              */
054             public void addRowAt(Query query, int row) { 
055                      if (row == -1) queries.add(query);
056                      else queries.add(row, query);
057                      
058                      fireTableDataChanged();
059             }
060             
061             /** 
062              * Returns the Query that represents the specified row in the
063              * table.
064              */
065             public Query getRow(int row) { 
066                      if (queries.isEmpty()) return null;
067                      else return (Query) queries.get(row); 
068             }
069             
070             /** 
071              * Removes the Query that represents the specified row in the
072              * table.
073              */
074             public void removeRow(int row) { removeRows(row, row); }
075             
076             /** Removes the Queries that represent the rows specified. */
077             public void removeRows(int[] rows) {
078                      removeRows(rows[0], rows[rows.length-1]);
079             }
080             
081             /** 
082              * Removes the Queries that represent the specified row in the
083              * table.  If the row is -1 then will clear the entire table.
084              */
085             public void removeRows(int minRow, int maxRow) {
086                      if (queries.isEmpty() && (maxRow > -1)) {
087                                    // XXX this should throw an exception, as this
088                                    // condition should never occur
089                                    CrimsonUtils.printWarning("No queries in table.");
090                                    return;
091                      }
092                      
093                      if (minRow == maxRow) {
094                                    if (minRow == -1) queries.clear();  // remove all rows
095                                    else queries.remove(minRow); // remove one row
096                      } else {
097                                    // can't use removeRange() because protected method
098                                    while (maxRow >= minRow) {
099                                             queries.remove(maxRow--);
100                                    }
101                      }
102                      
103                      fireTableDataChanged();
104             }
105             
106             /** Removes all rows from the table. */
107             public void removeAllRows() { removeRow(-1); }
108             
109             //----------------------------------------
110             // Standard AbstractTableModel methods
111             
112             public int getColumnCount() { return columnNames.length; }
113             
114             public int getRowCount() { 
115                      if (queries.isEmpty()) return 0;
116                      else return queries.size(); 
117             }
118             
119             public String getColumnName(int col) { return columnNames[col]; }
120             
121             /** "ID", "Tree", "Leaf Selection", "Seq Selection" */
122             public Object getValueAt(int row, int col) {
123                      Query query = getRow(row);
124                      switch (col) {
125                      case 0: return " " + query.getID();
126                      case 1: return " " + query.getTreeID();
127                      case 2: return " " + query.getLeafSelectionString();
128                      case 3: return " " + query.getSequenceSelectionString();
129                      case 4: return " " + (query.isSaved() ? "  yes" : "  no");
130                                    /*
131                      case 3: 
132                                    if (query.getLeafSelection() == 1) {
133                                             return " -";
134                                    } else {
135                                             return " " + Integer.toString(query.getNumLeaves());
136                                    }
137                      case 4: return " " + query.getSequenceSelectionString();
138                      case 5: 
139                                    if (query.getSequenceSelection() == 1) {
140                                             return " -";
141                                    } else {
142                                             return " " + Integer.toString(query.getNumBasePairs());
143                                    }
144                                    */
145                      default: return query.toString();
146                      }
147             }
148    } // QueryTableModel.java