Debian Codegraph @

src/dr/app/pathogen/SamplesPanel.java

raw symbols SCIP
1 /*
2 * SamplesPanel.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.pathogen;
27
28 import dr.app.beauti.options.DateGuesser;
29 import dr.app.beauti.tipdatepanel.GuessDatesDialog;
30 import dr.app.beauti.util.PanelUtils;
31 import dr.evolution.util.Date;
32 import dr.evolution.util.TimeScale;
33 import dr.evolution.util.Units;
34 import dr.evolution.util.TaxonList;
35 import dr.app.gui.table.*;
36 import jam.framework.Exportable;
37 import jam.table.HeaderRenderer;
38 import dr.app.gui.table.TableEditorStopper;
39 import jam.table.TableRenderer;
40
41 import javax.swing.*;
42 import javax.swing.event.ListSelectionEvent;
43 import javax.swing.event.ListSelectionListener;
44 import javax.swing.plaf.BorderUIResource;
45 import javax.swing.table.AbstractTableModel;
46 import java.awt.*;
47 import java.awt.event.ActionEvent;
48 import java.awt.event.ItemEvent;
49 import java.awt.event.ItemListener;
50
51 /**
52 * @author Andrew Rambaut
53 * @author Alexei Drummond
54 * @version $Id: DataPanel.java,v 1.17 2006/09/05 13:29:34 rambaut Exp $
55 */
56 public class SamplesPanel extends JPanel implements Exportable {
57
58 /**
59 *
60 */
61 private static final long serialVersionUID = 5283922195494563924L;
62 JScrollPane scrollPane = new JScrollPane();
63 JTable dataTable = null;
64 DataTableModel dataTableModel = null;
65
66 ClearDatesAction clearDatesAction = new ClearDatesAction();
67 GuessDatesAction guessDatesAction = new GuessDatesAction();
68
69 JComboBox unitsCombo = new JComboBox(new String[]{"Years", "Months", "Days"});
70 JComboBox directionCombo = new JComboBox(new String[]{"Since some time in the past", "Before the present"});
71
72 PathogenFrame frame = null;
73
74 TaxonList taxonList = null;
75
76 int datesUnits;
77 int datesDirection;
78 double maximumTipHeight = 0.0;
79
80 DateGuesser guesser = new DateGuesser();
81
82 double[] heights = null;
83
84 GuessDatesDialog guessDatesDialog = null;
85
86 public SamplesPanel(PathogenFrame parent, TaxonList taxonList) {
87
88 this.frame = parent;
89
90 dataTableModel = new DataTableModel();
91 TableSorter sorter = new TableSorter(dataTableModel);
92 dataTable = new JTable(sorter);
93
94 sorter.setTableHeader(dataTable.getTableHeader());
95
96 dataTable.getTableHeader().setReorderingAllowed(false);
97 dataTable.getTableHeader().setDefaultRenderer(
98 new HeaderRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
99
100 dataTable.getColumnModel().getColumn(0).setCellRenderer(
101 new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
102 dataTable.getColumnModel().getColumn(0).setPreferredWidth(80);
103
104 dataTable.getColumnModel().getColumn(1).setCellRenderer(
105 new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
106 dataTable.getColumnModel().getColumn(1).setPreferredWidth(80);
107 dataTable.getColumnModel().getColumn(1).setCellEditor(
108 new DateCellEditor());
109
110 dataTable.getColumnModel().getColumn(2).setCellRenderer(
111 new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
112 dataTable.getColumnModel().getColumn(2).setPreferredWidth(80);
113 dataTable.getColumnModel().getColumn(2).setCellEditor(
114 new DateCellEditor());
115
116 dataTable.getColumnModel().getColumn(3).setCellRenderer(
117 new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
118 dataTable.getColumnModel().getColumn(3).setPreferredWidth(80);
119
120 TableEditorStopper.ensureEditingStopWhenTableLosesFocus(dataTable);
121
122 dataTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
123 public void valueChanged(ListSelectionEvent evt) {
124 selectionChanged();
125 }
126 });
127
128 scrollPane = new JScrollPane(dataTable,
129 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
130 JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
131 scrollPane.setOpaque(false);
132
133 PanelUtils.setupComponent(unitsCombo);
134 PanelUtils.setupComponent(directionCombo);
135
136 JToolBar toolBar1 = new JToolBar();
137 toolBar1.setFloatable(false);
138 toolBar1.setOpaque(false);
139
140 toolBar1.setLayout(new FlowLayout(java.awt.FlowLayout.LEFT, 0, 0));
141 JButton button = new JButton(clearDatesAction);
142 PanelUtils.setupComponent(button);
143 toolBar1.add(button);
144 button = new JButton(guessDatesAction);
145 PanelUtils.setupComponent(button);
146 toolBar1.add(button);
147 toolBar1.add(new JToolBar.Separator(new Dimension(12, 12)));
148 final JLabel unitsLabel = new JLabel("Dates specified as ");
149 toolBar1.add(unitsLabel);
150 toolBar1.add(unitsCombo);
151 toolBar1.add(directionCombo);
152
153 setOpaque(false);
154 setBorder(new BorderUIResource.EmptyBorderUIResource(new java.awt.Insets(12, 12, 12, 12)));
155 setLayout(new BorderLayout(0, 0));
156
157 add(toolBar1, "North");
158 add(scrollPane, "Center");
159
160 ItemListener listener = new ItemListener() {
161 public void itemStateChanged(ItemEvent ev) {
162 timeScaleChanged();
163 }
164 };
165 unitsCombo.addItemListener(listener);
166 directionCombo.addItemListener(listener);
167
168 setTaxonList(taxonList);
169 }
170
171 public final void timeScaleChanged() {
172 Units.Type units = Units.Type.YEARS;
173 switch (unitsCombo.getSelectedIndex()) {
174 case 0:
175 units = Units.Type.YEARS;
176 break;
177 case 1:
178 units = Units.Type.MONTHS;
179 break;
180 case 2:
181 units = Units.Type.DAYS;
182 break;
183 }
184
185 boolean backwards = directionCombo.getSelectedIndex() == 1;
186
187 for (int i = 0; i < taxonList.getTaxonCount(); i++) {
188 Date date = taxonList.getTaxon(i).getDate();
189 double d = date.getTimeValue();
190
191 Date newDate = createDate(d, units, backwards, 0.0);
192
193 newDate.setUncertainty(date.getUncertainty());
194
195 taxonList.getTaxon(i).setDate(newDate);
196 }
197
198 calculateHeights();
199
200 dataTableModel.fireTableDataChanged();
201 frame.timeScaleChanged();
202 }
203
204 private Date createDate(double timeValue, Units.Type units, boolean backwards, double origin) {
205 if (backwards) {
206 return Date.createTimeAgoFromOrigin(timeValue, units, origin);
207 } else {
208 return Date.createTimeSinceOrigin(timeValue, units, origin);
209 }
210 }
211
212 private void setTaxonList(TaxonList taxonList) {
213 this.taxonList = taxonList;
214
215 setupTable();
216
217 unitsCombo.setSelectedIndex(datesUnits);
218 directionCombo.setSelectedIndex(datesDirection);
219
220 calculateHeights();
221
222 dataTableModel.fireTableDataChanged();
223 }
224
225 private void setupTable() {
226 dataTableModel.fireTableDataChanged();
227 }
228
229 public void getOptions() {
230 datesUnits = unitsCombo.getSelectedIndex();
231 datesDirection = directionCombo.getSelectedIndex();
232 }
233
234 public JComponent getExportableComponent() {
235 return dataTable;
236 }
237
238 public void selectionChanged() {
239 // nothing to do
240 }
241
242 public void clearDates() {
243 for (int i = 0; i < taxonList.getTaxonCount(); i++) {
244 java.util.Date origin = new java.util.Date(0);
245
246 double d = 0.0;
247
248 Date date = Date.createTimeSinceOrigin(d, Units.Type.YEARS, origin);
249 taxonList.getTaxon(i).setAttribute("date", date);
250 }
251
252 // adjust the dates to the current timescale...
253 timeScaleChanged();
254
255 dataTableModel.fireTableDataChanged();
256 }
257
258 public void guessDates() {
259
260 if (guessDatesDialog == null) {
261 guessDatesDialog = new GuessDatesDialog(frame);
262 }
263
264 int result = guessDatesDialog.showDialog();
265
266 if (result == -1 || result == JOptionPane.CANCEL_OPTION) {
267 return;
268 }
269
270 guesser.guessDates = true;
271 guessDatesDialog.setupGuesser(guesser);
272
273 String warningMessage = null;
274
275 guesser.guessDates(taxonList);
276
277 if (warningMessage != null) {
278 JOptionPane.showMessageDialog(this, "Warning: some dates may not be set correctly - \n" + warningMessage,
279 "Error guessing dates",
280 JOptionPane.WARNING_MESSAGE);
281 }
282
283 // adjust the dates to the current timescale...
284 timeScaleChanged();
285
286 dataTableModel.fireTableDataChanged();
287 }
288
289 public class ClearDatesAction extends AbstractAction {
290 /**
291 *
292 */
293 private static final long serialVersionUID = -7281309694753868635L;
294
295 public ClearDatesAction() {
296 super("Clear Dates");
297 setToolTipText("Use this tool to remove sampling dates from each taxon");
298 }
299
300 public void actionPerformed(ActionEvent ae) {
301 clearDates();
302 }
303 }
304
305 public class GuessDatesAction extends AbstractAction {
306 /**
307 *
308 */
309 private static final long serialVersionUID = 8514706149822252033L;
310
311 public GuessDatesAction() {
312 super("Guess Dates");
313 setToolTipText("Use this tool to guess the sampling dates from the taxon labels");
314 }
315
316 public void actionPerformed(ActionEvent ae) {
317 guessDates();
318 }
319 }
320
321 private void calculateHeights() {
322
323 maximumTipHeight = 0.0;
324 if (taxonList == null || taxonList.getTaxonCount() == 0) return;
325
326 heights = null;
327
328 dr.evolution.util.Date mostRecent = null;
329 for (int i = 0; i < taxonList.getTaxonCount(); i++) {
330 Date date = taxonList.getTaxon(i).getDate();
331 if ((date != null) && (mostRecent == null || date.after(mostRecent))) {
332 mostRecent = date;
333 }
334 }
335
336 if (mostRecent != null) {
337 heights = new double[taxonList.getTaxonCount()];
338
339 TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue());
340 double time0 = timeScale.convertTime(mostRecent.getTimeValue(), mostRecent);
341
342 for (int i = 0; i < taxonList.getTaxonCount(); i++) {
343 Date date = taxonList.getTaxon(i).getDate();
344 if (date != null) {
345 heights[i] = timeScale.convertTime(date.getTimeValue(), date) - time0;
346 if (heights[i] > maximumTipHeight) maximumTipHeight = heights[i];
347 }
348 }
349 }
350 }
351
352 class DataTableModel extends AbstractTableModel {
353
354 /**
355 *
356 */
357 private static final long serialVersionUID = -6707994233020715574L;
358 String[] columnNames = {"Name", "Date", "Precision", "Height"};
359
360 public DataTableModel() {
361 }
362
363 public int getColumnCount() {
364 return columnNames.length;
365 }
366
367 public int getRowCount() {
368 if (taxonList == null) return 0;
369
370 return taxonList.getTaxonCount();
371 }
372
373 public Object getValueAt(int row, int col) {
374 Date date = taxonList.getTaxon(row).getDate();
375 switch (col) {
376 case 0:
377 return taxonList.getTaxonId(row);
378 case 1:
379 if (date != null) {
380 return date.getTimeValue();
381 } else {
382 return "-";
383 }
384 case 2:
385 if (date != null) {
386 return date.getUncertainty();
387 } else {
388 return "-";
389 }
390 case 3:
391 if (heights != null) {
392 return heights[row];
393 } else {
394 return "0.0";
395 }
396 }
397 return null;
398 }
399
400 public void setValueAt(Object aValue, int row, int col) {
401 if (col == 0) {
402 taxonList.getTaxon(row).setId(aValue.toString());
403 } else if (col == 1) {
404 Date date = taxonList.getTaxon(row).getDate();
405 if (date != null) {
406 double d = (Double) aValue;
407 Date newDate = createDate(d, date.getUnits(), date.isBackwards(), date.getOrigin());
408 taxonList.getTaxon(row).setDate(newDate);
409 }
410 } else if (col == 2) {
411 Date date = taxonList.getTaxon(row).getDate();
412 if (date != null) {
413 double d = (Double) aValue;
414 if (d >= 0.0) {
415 date.setUncertainty(d);
416 }
417 }
418 }
419
420 timeScaleChanged();
421 }
422
423 public boolean isCellEditable(int row, int col) {
424 if (col == 0) return true;
425 if (col == 1 || col == 2) {
426 Date date = taxonList.getTaxon(row).getDate();
427 return (date != null);
428 }
429 return false;
430 }
431
432 public String getColumnName(int column) {
433 return columnNames[column];
434 }
435
436 public Class getColumnClass(int c) {
437 return getValueAt(0, c).getClass();
438 }
439
440 public String toString() {
441 StringBuffer buffer = new StringBuffer();
442
443 buffer.append(getColumnName(0));
444 for (int j = 1; j < getColumnCount(); j++) {
445 buffer.append("\t");
446 buffer.append(getColumnName(j));
447 }
448 buffer.append("\n");
449
450 for (int i = 0; i < getRowCount(); i++) {
451 buffer.append(getValueAt(i, 0));
452 for (int j = 1; j < getColumnCount(); j++) {
453 buffer.append("\t");
454 buffer.append(getValueAt(i, j));
455 }
456 buffer.append("\n");
457 }
458
459 return buffer.toString();
460 }
461 }
462 }