Debian Codegraph @

src/dr/app/bss/TreesTableModel.java

raw symbols SCIP
1 /*
2 * TreesTableModel.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.bss;
27
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.io.File;
31
32 import javax.swing.JButton;
33 import javax.swing.JFileChooser;
34 import javax.swing.SwingWorker;
35 import javax.swing.table.AbstractTableModel;
36
37 import dr.evolution.tree.Tree;
38 import dr.evolution.util.Taxa;
39 import dr.evolution.util.Taxon;
40
41 /**
42 * @author Filip Bielejec
43 * @version $Id$
44 */
45 @SuppressWarnings("serial")
46 public class TreesTableModel extends AbstractTableModel {
47
48 private PartitionDataList dataList;
49 private MainFrame frame;
50
51 public final static int TREE_FILE_INDEX = 0;
52 public final static int TAXA_SET_INDEX = 1;
53 public final static int TAXA_COUNT_INDEX = 2;
54
55 public static String[] COLUMN_NAMES = { "Tree File", "Taxa Set",
56 "Taxa count" };
57
58 private static final Class<?>[] COLUMN_TYPES = new Class<?>[] {
59 JButton.class, // Tree File
60 JButton.class, // Taxa Set
61 Integer.class // Taxa
62 };
63
64 private TaxaEditor taxaEditor;
65
66 public TreesTableModel(PartitionDataList dataList, MainFrame frame) {
67 this.dataList = dataList;
68 this.frame = frame;
69
70 addDefaultRow();
71
72 }// END: Constructor
73
74 public void addDefaultRow() {
75 dataList.recordsList.add(new TreesTableRecord());
76 fireTableDataChanged();
77 }// END: addDefaultRow
78
79 public void deleteRow(int row) {
80
81 // remove taxa connected to this row from Taxa panel
82 String value = dataList.recordsList.get(row).getName();
83
84 Utils.removeTaxaWithAttributeValue(dataList, Utils.TREE_FILENAME, value);
85
86 dataList.recordsList.remove(row);
87 fireTableDataChanged();
88 }// END: deleteRow
89
90 public void setRow(int row, TreesTableRecord record) {
91
92 // remove taxa connected to this row from Taxa panel
93 String value = dataList.recordsList.get(row).getName();
94 Utils.removeTaxaWithAttributeValue(dataList, Utils.TREE_FILENAME, value);
95
96 // add taxa with attributes
97 if(record.isTreeSet()) {
98 applyTaxa(record.getTree());
99 }
100
101 dataList.recordsList.set(row, record);
102 fireTableDataChanged();
103
104 }// END: setRow
105
106 public void applyTaxa(Tree tree) {
107
108 // set attributes to be parsed later in Taxa panel
109 Taxa taxa = new Taxa();
110 for (Taxon taxon : tree.asList()) {
111
112 double absoluteHeight = Utils.getAbsoluteTaxonHeight(taxon, tree);
113 taxon.setAttribute(Utils.ABSOLUTE_HEIGHT, absoluteHeight);
114 taxon.setAttribute(Utils.TREE_FILENAME, tree.getId());
115 taxa.addTaxon(taxon);
116 dataList.allTaxa.addTaxon(taxon);
117
118 }// END: taxon loop
119
120 }// END: applyTaxa
121
122 @Override
123 public int getColumnCount() {
124 return COLUMN_NAMES.length;
125 }
126
127 @Override
128 public int getRowCount() {
129 return dataList.recordsList.size();
130 }
131
132 @Override
133 public Class<?> getColumnClass(int columnIndex) {
134 return COLUMN_TYPES[columnIndex];
135 }// END: getColumnClass
136
137 public boolean isCellEditable(int row, int column) {
138 switch (column) {
139 case TREE_FILE_INDEX:
140 return false;
141 case TAXA_COUNT_INDEX:
142 return false;
143 case TAXA_SET_INDEX:
144 return false;
145 default:
146 return false;
147 }
148 }// END: isCellEditable
149
150 public String getColumnName(int column) {
151 return COLUMN_NAMES[column];
152 }// END: getColumnName
153
154 @Override
155 public Object getValueAt(int row, int column) {
156 switch (column) {
157
158 case TREE_FILE_INDEX:
159 JButton treeFileButton = new JButton(Utils.CHOOSE_FILE);
160 treeFileButton.addActionListener(new ListenLoadTreeFile(row));
161 return treeFileButton;
162
163 case TAXA_SET_INDEX:
164
165 JButton taxaEditorButton = new JButton(Utils.EDIT_TAXA_SET);
166 taxaEditorButton.addActionListener(new ListenOpenTaxaEditor(row));
167 return taxaEditorButton;
168
169 case TAXA_COUNT_INDEX:
170 return dataList.recordsList.get(row).getTaxaCount();
171
172 default:
173 return "Error";
174 }
175 }// END: getValueAt
176
177 private class ListenOpenTaxaEditor implements ActionListener {
178 private int row;
179
180 public ListenOpenTaxaEditor(int row) {
181 this.row = row;
182 }
183
184 public void actionPerformed(ActionEvent ev) {
185
186 try {
187
188 taxaEditor = new TaxaEditor(frame, dataList, row);
189 taxaEditor.launch();
190
191 } catch (Exception e) {
192 Utils.handleException(e);
193 }
194
195 }// END: actionPerformed
196 }// END: ListenOpenSiteRateModelEditor
197
198 private class ListenLoadTreeFile implements ActionListener {
199
200 private int row;
201
202 public ListenLoadTreeFile(int row) {
203 this.row = row;
204 }// END: Constructor
205
206 public void actionPerformed(ActionEvent ev) {
207
208 doLoadTreeFile(row);
209
210 }// END: actionPerformed
211 }// END: ListenLoadTreeFile
212
213 private void doLoadTreeFile(int row) {
214
215 try {
216
217 JFileChooser chooser = new JFileChooser();
218 chooser.setDialogTitle("Select trees file...");
219 chooser.setMultiSelectionEnabled(false);
220 chooser.setCurrentDirectory(frame.getWorkingDirectory());
221
222 int returnValue = chooser.showOpenDialog(Utils.getActiveFrame());
223
224 if (returnValue == JFileChooser.APPROVE_OPTION) {
225
226 File file = chooser.getSelectedFile();
227
228 if (file != null) {
229
230 loadTreeFile(file, row);
231
232 File tmpDir = chooser.getCurrentDirectory();
233 if (tmpDir != null) {
234 frame.setWorkingDirectory(tmpDir);
235 }
236
237 }// END: file opened check
238 }// END: dialog cancelled check
239
240 } catch (Exception e) {
241 Utils.handleException(e);
242 }// END: try-catch block
243
244 }// END: doLoadTreeFile
245
246
247 private void loadTreeFile(final File file, final int row) {
248
249 frame.setBusy();
250 SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
251
252 // Executed in background thread
253 public Void doInBackground() {
254
255 try {
256
257 Tree tree = Utils.importTreeFromFile(file);
258 tree.setId(file.getName());
259
260 setRow(row, new TreesTableRecord(tree.getId(), tree));
261
262 } catch (Exception e) {
263 Utils.handleException(e);
264 }// END: try-catch block
265
266 return null;
267 }// END: doInBackground()
268
269 // Executed in event dispatch thread
270 public void done() {
271 frame.setIdle();
272 frame.fireTaxaChanged();
273 }// END: done
274 };
275
276 worker.execute();
277
278 }// END: loadTreeFile
279
280 public void setDataList(PartitionDataList dataList) {
281 this.dataList = dataList;
282 }
283
284 }// END: class