| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.app.gui.table; |
| 27 |
|
| 28 |
import dr.evolution.util.*; |
| 29 |
import jam.table.*; |
| 30 |
|
| 31 |
import javax.swing.*; |
| 32 |
import javax.swing.table.AbstractTableModel; |
| 33 |
import java.awt.*; |
| 34 |
import java.util.Iterator; |
| 35 |
import java.util.TreeSet; |
| 36 |
|
| 37 |
public class TaxonListTable extends JTable implements MutableTaxonListListener { |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
private static final long serialVersionUID = -5706027520036473157L; |
| 43 |
|
| 44 |
final static String TAXON_NAME = "Taxon"; |
| 45 |
|
| 46 |
TaxaTableModel taxaTableModel = null; |
| 47 |
TaxonList taxonList = null; |
| 48 |
|
| 49 |
public TaxonListTable(TaxonList taxonList) { |
| 50 |
|
| 51 |
this.taxonList = taxonList; |
| 52 |
|
| 53 |
if (taxonList instanceof MutableTaxonList) { |
| 54 |
((MutableTaxonList)taxonList).addMutableTaxonListListener(this); |
| 55 |
} |
| 56 |
|
| 57 |
String[] names = getColumnNames(); |
| 58 |
|
| 59 |
|
| 60 |
taxaTableModel = new TaxaTableModel(names, taxonList); |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
TableSorter sorter = new TableSorter(taxaTableModel); |
| 65 |
setModel(sorter); |
| 66 |
sorter.addTableModelListener(this); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
setAutoResizeMode(JTable.AUTO_RESIZE_OFF); |
| 71 |
setColumnSelectionAllowed(true); |
| 72 |
|
| 73 |
|
| 74 |
getColumn(TAXON_NAME).setPreferredWidth(120); |
| 75 |
for (int i = 0; i < getColumnCount(); i++) { |
| 76 |
getColumnModel().getColumn(i).setCellRenderer( |
| 77 |
new TableRenderer(true, SwingConstants.LEFT, new Insets(0, 4, 0, 0))); |
| 78 |
} |
| 79 |
|
| 80 |
setDefaultEditor(dr.evolution.util.Date.class, new DateCellEditor()); |
| 81 |
} |
| 82 |
|
| 83 |
public void updateColumns() { |
| 84 |
taxaTableModel.setColumnNames(getColumnNames()); |
| 85 |
} |
| 86 |
|
| 87 |
private String[] getColumnNames() { |
| 88 |
TreeSet attributes = new TreeSet(); |
| 89 |
|
| 90 |
for (int i = 0; i < taxonList.getTaxonCount(); i++) { |
| 91 |
Taxon taxon = taxonList.getTaxon(i); |
| 92 |
Iterator iter = taxon.getAttributeNames(); |
| 93 |
while (iter.hasNext()) { |
| 94 |
Object attribute = iter.next(); |
| 95 |
attributes.add(attribute); |
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
String[] names = new String[attributes.size() + 1]; |
| 101 |
names[0] = "Taxon"; |
| 102 |
Iterator iter = attributes.iterator(); |
| 103 |
int i = 1; |
| 104 |
while (iter.hasNext()) { |
| 105 |
names[i] = iter.next().toString(); |
| 106 |
i++; |
| 107 |
} |
| 108 |
|
| 109 |
return names; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public void taxonAdded(TaxonList taxonList, Taxon taxon) { |
| 117 |
|
| 118 |
taxaTableModel.fireTableDataChanged(); |
| 119 |
} |
| 120 |
|
| 121 |
public void taxonRemoved(TaxonList taxonList, Taxon taxon) { |
| 122 |
taxaTableModel.fireTableDataChanged(); |
| 123 |
} |
| 124 |
|
| 125 |
public void taxaChanged(TaxonList taxonList) { |
| 126 |
updateColumns(); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
class TaxaTableModel extends AbstractTableModel { |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
private static final long serialVersionUID = 367162013042095775L; |
| 139 |
String[] names = null; |
| 140 |
TaxonList taxonList; |
| 141 |
|
| 142 |
public TaxaTableModel(String[] names, TaxonList taxonList) { |
| 143 |
this.names = names; |
| 144 |
this.taxonList = taxonList; |
| 145 |
} |
| 146 |
|
| 147 |
public void setColumnNames(String[] names) { |
| 148 |
this.names = names; |
| 149 |
fireTableStructureChanged(); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
public int getColumnCount() { return names.length; } |
| 154 |
public int getRowCount() { return taxonList.getTaxonCount();} |
| 155 |
|
| 156 |
public Object getValueAt(int row, int column) { |
| 157 |
|
| 158 |
if (column == 0) { |
| 159 |
return taxonList.getTaxon(row).getId(); |
| 160 |
} else { |
| 161 |
return taxonList.getTaxonAttribute(row, names[column]); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public String getColumnName(int column) {return names[column];} |
| 168 |
public Class getColumnClass(int c) {return getValueAt(0, c).getClass();} |
| 169 |
public boolean isCellEditable(int row, int column) { |
| 170 |
|
| 171 |
if (taxonList instanceof MutableTaxonList) { |
| 172 |
if (column == 0) return true; |
| 173 |
|
| 174 |
Object item = taxonList.getTaxonAttribute(row, names[column]); |
| 175 |
if (item instanceof String) return true; |
| 176 |
if (item instanceof dr.evolution.util.Date) return true; |
| 177 |
} |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
public void setValueAt(Object aValue, int row, int column) { |
| 182 |
|
| 183 |
if (taxonList instanceof MutableTaxonList) { |
| 184 |
|
| 185 |
if (column == 0) { |
| 186 |
((MutableTaxonList)taxonList).setTaxonId(row, (String)aValue); |
| 187 |
} else { |
| 188 |
((MutableTaxonList)taxonList).setTaxonAttribute(row, names[column], aValue); |
| 189 |
} |
| 190 |
|
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
} |
| 195 |
} |