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     * @(#)TreeTableModel.java
022     */
023    
024    package edu.upenn.crimson;
025    
026    import java.util.ArrayList;
027    import javax.swing.table.*;
028    
029    /**
030     * TreeTableModel. This is a TableModel that is used by TreeManager to
031     * list the trees. It is maintained here so that it can be updated
032     * easily whenever the tree list is rebuilt.
033     *
034     * @author  Stephen Fisher
035     * @version $Id: TreeTableModel.java,v 1.12 2007/05/16 18:55:58 fisher Exp $
036     */
037    
038    public class TreeTableModel extends AbstractTableModel {
039    
040             /** Stores the Trees that are in the table. */
041             private ArrayList trees = new ArrayList();
042             
043             private String[] columnNames = {"Tree ID", "Partitions", "Num Leaves", "Binary", "Ultrametric"};
044             
045             //-----------------------------------------
046             // Methods added to handle trees ArrayList
047             
048             /** Append a new row to the end of the table. */
049             public void addRow(Tree tree) { addRowAt(tree, -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(Tree tree, int row) { 
056                      if (row == -1) trees.add(tree);
057                      else trees.add(row, tree);
058                      
059                      fireTableDataChanged();
060             }
061             
062             /** 
063              * Returns the Tree that represents the specified row in the
064              * table.
065              */
066             public Tree getRow(int row) { 
067                      if (trees.isEmpty()) return null;
068                      else return (Tree) trees.get(row); 
069             }
070             
071             /** 
072              * Removes the Tree that represents the specified row in the
073              * table.
074              */
075             public void removeRow(int row) { removeRows(row, row); }
076             
077             /** Removes the Trees 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 Trees 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 (trees.isEmpty()) return;
088                      
089                      if (minRow == maxRow) {
090                                    if (minRow == -1) trees.clear();  // remove all rows
091                                    else trees.remove(minRow); // remove one row
092                      } else {
093                                    // can't use removeRange() because protected method
094                                    while (maxRow >= minRow) {
095                                             trees.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 (trees.isEmpty()) return 0;
112                      else return trees.size(); 
113             }
114             
115             public String getColumnName(int col) { return columnNames[col]; }
116             
117             /** "ID", "Partitions", "Species", "maxTempDepth", "Tot Seq Length" */
118             public Object getValueAt(int row, int col) {
119                      Tree tree = getRow(row);
120                      switch (col) {
121                      case 0: return "  " + tree.getID();
122                      case 1: return "  " + tree.getNumPartitions();
123                      case 2: return "  " + tree.getNumLeaves();
124                      case 3: return "  " + (tree.isBinary() ? "yes" : "no");
125                      case 4: return "  " + (tree.isUltrametric() ? "yes" : "no");
126                      default: return tree.toString();
127                      }
128             }
129    } // TreeTableModel.java