Debian Codegraph @

src/dr/app/gui/table/TaxonListTable.java

raw symbols SCIP
1 /*
2 * TaxonListTable.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 // Create a model of the data.
60 taxaTableModel = new TaxaTableModel(names, taxonList);
61
62 // Create the table
63 //JTable tableView = new JTable(dataModel);
64 TableSorter sorter = new TableSorter(taxaTableModel); //ADDED THIS
65 setModel(sorter);
66 sorter.addTableModelListener(this); //ADDED THIS
67
68 // Turn off auto-resizing so that we can set column sizes programmatically.
69 // In this mode, all columns will get their preferred widths, as set below.
70 setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
71 setColumnSelectionAllowed(true);
72
73 // Set the widths of the columns.
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 // MutableTaxonListListener interface
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 // TableModel inner class
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 // These methods always need to be implemented.
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 // The default implementations of these methods in
166 // AbstractTableModel would work, but we can refine them.
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 }