Debian Codegraph @

src/dr/app/beauti/tipdatepanel/TipDatesPanel.java

raw symbols SCIP
1 /*
2 * TipDatesPanel.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.beauti.tipdatepanel;
27
28 import dr.app.beauti.BeautiFrame;
29 import dr.app.beauti.BeautiPanel;
30 import dr.app.beauti.components.tipdatesampling.TipDateSamplingComponentOptions;
31 import dr.app.beauti.options.*;
32 import dr.app.beauti.types.TipDateSamplingType;
33 import dr.app.beauti.util.PanelUtils;
34 import dr.app.gui.table.DateCellEditor;
35 import dr.app.gui.table.TableEditorStopper;
36 import dr.app.gui.table.TableSorter;
37 import dr.evolution.util.*;
38 import dr.evolution.util.Date;
39 import dr.evoxml.util.DateUnitsType;
40 import dr.util.DataTable;
41 import jam.framework.Exportable;
42 import jam.table.HeaderRenderer;
43 import jam.table.TableRenderer;
44
45 import javax.swing.*;
46 import javax.swing.event.ListSelectionEvent;
47 import javax.swing.event.ListSelectionListener;
48 import javax.swing.filechooser.FileNameExtensionFilter;
49 import javax.swing.plaf.BorderUIResource;
50 import javax.swing.table.AbstractTableModel;
51 import java.awt.*;
52 import java.awt.event.*;
53 import java.io.File;
54 import java.io.FileNotFoundException;
55 import java.io.FileReader;
56 import java.io.IOException;
57 import java.util.*;
58 import java.util.List;
59
60 /**
61 * @author Andrew Rambaut
62 * @author Alexei Drummond
63 * @author Tommy Lam
64 * @version $Id: DataPanel.java,v 1.17 2006/09/05 13:29:34 rambaut Exp $
65 */
66 public class TipDatesPanel extends BeautiPanel implements Exportable {
67
68 /**
69 *
70 */
71 private static final long serialVersionUID = 5283922195494563924L;
72
73 private JScrollPane scrollPane = new JScrollPane();
74 private JTable dataTable = null;
75 private DataTableModel dataTableModel = null;
76
77 private SetDatesAction setDatesAction = new SetDatesAction();
78 private ClearDatesAction clearDatesAction = new ClearDatesAction();
79 private GuessDatesAction guessDatesAction = new GuessDatesAction();
80 private ImportDatesAction importDatesAction = new ImportDatesAction();
81
82 private SetUncertaintyAction setUncertaintyAction = new SetUncertaintyAction();
83
84 private JCheckBox usingTipDates = new JCheckBox("Use tip dates");
85 private JCheckBox specifyOriginDate = new JCheckBox("Specify origin date:");
86 private JTextField originDateText = new JTextField(20);
87 private JLabel originDateLabel = new JLabel("");
88
89 final JLabel unitsLabel;
90 final JLabel tipDateSamplingLabel;
91 final JLabel tipDateTaxonSetLabel;
92
93 private JComboBox unitsCombo = new JComboBox(EnumSet.range(DateUnitsType.YEARS, DateUnitsType.DAYS).toArray());
94 private JComboBox directionCombo = new JComboBox(EnumSet.range(DateUnitsType.FORWARDS, DateUnitsType.BACKWARDS).toArray());
95
96 // JComboBox tipDateSamplingCombo = new JComboBox( TipDateSamplingType.values() );
97 private JComboBox tipDateSamplingCombo = new JComboBox(new TipDateSamplingType[] {
98 TipDateSamplingType.NO_SAMPLING,
99 TipDateSamplingType.SAMPLE_INDIVIDUALLY,
100 // TipDateSamplingType.SAMPLE_JOINT,
101 TipDateSamplingType.SAMPLE_PRECISION
102 });
103 private JComboBox tipDateTaxonSetCombo = new JComboBox();
104
105 private BeautiFrame frame = null;
106
107 private BeautiOptions options = null;
108
109 private double[] heights = null;
110
111 private GuessDatesDialog guessDatesDialog = null;
112 private SetValueDialog dateValueDialog = null;
113 private SetValueDialog precisionValueDialog = null;
114
115 public TipDatesPanel(BeautiFrame parent) {
116
117 this.frame = parent;
118
119 dataTableModel = new DataTableModel();
120 TableSorter sorter = new TableSorter(dataTableModel);
121 dataTable = new JTable(sorter);
122
123 sorter.setTableHeader(dataTable.getTableHeader());
124
125 dataTable.getTableHeader().setReorderingAllowed(false);
126 // dataTable.getTableHeader().setDefaultRenderer(
127 // new HeaderRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
128
129 dataTable.getTableHeader().setReorderingAllowed(false);
130 dataTable.getTableHeader().setDefaultRenderer(
131 new HeaderRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
132
133 dataTable.getColumnModel().getColumn(0).setCellRenderer(
134 new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
135 dataTable.getColumnModel().getColumn(0).setPreferredWidth(80);
136
137 dataTable.getColumnModel().getColumn(1).setCellRenderer(
138 new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
139 dataTable.getColumnModel().getColumn(1).setPreferredWidth(80);
140 dataTable.getColumnModel().getColumn(1).setCellEditor(
141 new DateCellEditor());
142
143 dataTable.getColumnModel().getColumn(2).setCellRenderer(
144 new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
145 dataTable.getColumnModel().getColumn(2).setPreferredWidth(80);
146 dataTable.getColumnModel().getColumn(2).setCellEditor(
147 new DateCellEditor());
148
149 dataTable.getColumnModel().getColumn(3).setCellRenderer(
150 new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
151 dataTable.getColumnModel().getColumn(3).setPreferredWidth(80);
152
153 TableEditorStopper.ensureEditingStopWhenTableLosesFocus(dataTable);
154
155 dataTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
156 public void valueChanged(ListSelectionEvent evt) {
157 selectionChanged();
158 }
159 });
160
161 scrollPane = new JScrollPane(dataTable,
162 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
163 JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
164 scrollPane.setOpaque(false);
165
166 PanelUtils.setupComponent(unitsCombo);
167 PanelUtils.setupComponent(directionCombo);
168
169 JToolBar toolBar1 = new JToolBar();
170 toolBar1.setFloatable(false);
171 toolBar1.setOpaque(false);
172 toolBar1.setBorder(BorderFactory.createEmptyBorder());
173
174 toolBar1.setLayout(new FlowLayout(java.awt.FlowLayout.LEFT, 0, 0));
175 JButton button = new JButton(guessDatesAction);
176 PanelUtils.setupComponent(button);
177 toolBar1.add(button);
178
179 button = new JButton(importDatesAction);
180 PanelUtils.setupComponent(button);
181 toolBar1.add(button);
182
183 button = new JButton(setDatesAction);
184 PanelUtils.setupComponent(button);
185 toolBar1.add(button);
186
187 button = new JButton(clearDatesAction);
188 PanelUtils.setupComponent(button);
189 toolBar1.add(button);
190
191 button = new JButton(setUncertaintyAction);
192 PanelUtils.setupComponent(button);
193 toolBar1.add(button);
194
195 JToolBar toolBar3 = new JToolBar();
196 toolBar3.setFloatable(false);
197 toolBar3.setOpaque(false);
198 toolBar3.setBorder(BorderFactory.createEmptyBorder());
199
200 toolBar3.setLayout(new FlowLayout(java.awt.FlowLayout.LEFT, 0, 0));
201
202 unitsLabel = new JLabel("Dates as:");
203 toolBar3.add(unitsLabel);
204 toolBar3.add(unitsCombo);
205 toolBar3.add(directionCombo);
206
207 toolBar3.add(new JToolBar.Separator(new Dimension(12, 12)));
208
209 toolBar3.add(specifyOriginDate);
210 toolBar3.add(originDateText);
211 toolBar3.add(originDateLabel);
212
213 JPanel panel2 = new JPanel();
214 panel2.setLayout(new BoxLayout(panel2, BoxLayout.PAGE_AXIS));
215 panel2.setOpaque(false);
216 panel2.add(toolBar1);
217 panel2.add(toolBar3);
218
219
220 JPanel panel1 = new JPanel(new BorderLayout(0, 0));
221 panel1.setOpaque(false);
222 panel1.add(panel2, "North");
223 panel1.add(scrollPane, "Center");
224
225 JToolBar toolBar2 = new JToolBar();
226 toolBar2.setFloatable(false);
227 toolBar2.setOpaque(false);
228 toolBar2.setBorder(BorderFactory.createEmptyBorder());
229
230 toolBar2.setLayout(new FlowLayout(java.awt.FlowLayout.LEFT, 0, 0));
231
232 PanelUtils.setupComponent(tipDateSamplingCombo);
233 tipDateSamplingCombo.setToolTipText("<html>Select whether to allow sampling<br>" +
234 "of all or individual tip dates.</html>");
235
236 // substitutionRateField.setToolTipText("<html>Enter the substitution rate here.</html>");
237 // substitutionRateField.setEnabled(true);
238
239 tipDateSamplingLabel = new JLabel("Tip date sampling:");
240 toolBar2.add(tipDateSamplingLabel);
241 toolBar2.add(tipDateSamplingCombo);
242
243 tipDateTaxonSetLabel = new JLabel("Apply to taxon set:");
244 toolBar2.add(tipDateTaxonSetLabel);
245 toolBar2.add(tipDateTaxonSetCombo);
246
247 setOpaque(false);
248 setBorder(new BorderUIResource.EmptyBorderUIResource(new java.awt.Insets(12, 12, 12, 12)));
249 setLayout(new BorderLayout(0, 0));
250 add(usingTipDates, BorderLayout.NORTH);
251 add(panel1, BorderLayout.CENTER);
252 add(toolBar2, BorderLayout.SOUTH);
253
254 tipDateSamplingCombo.addItemListener(
255 new java.awt.event.ItemListener() {
256 public void itemStateChanged(java.awt.event.ItemEvent ev) {
257 boolean samplingOn = tipDateSamplingCombo.getSelectedItem() != TipDateSamplingType.NO_SAMPLING;
258 tipDateTaxonSetLabel.setEnabled(samplingOn);
259 tipDateTaxonSetCombo.setEnabled(samplingOn);
260 fireModelsChanged();
261 }
262 }
263 );
264
265 specifyOriginDate.addItemListener(
266 new java.awt.event.ItemListener() {
267 public void itemStateChanged(java.awt.event.ItemEvent ev) {
268 boolean enabled = usingTipDates.isSelected();
269 originDateText.setEnabled(enabled && specifyOriginDate.isSelected());
270 originDateLabel.setEnabled(enabled && specifyOriginDate.isSelected());
271 timeScaleChanged();
272 }
273 }
274 );
275
276 originDateText.addFocusListener(
277 new FocusAdapter() {
278 @Override
279 public void focusLost(FocusEvent focusEvent) {
280 timeScaleChanged();
281 }
282 }
283 );
284
285 usingTipDates.addItemListener(new ItemListener() {
286 @Override
287 public void itemStateChanged(ItemEvent e) {
288 boolean enabled = usingTipDates.isSelected();
289
290 options.useTipDates = enabled;
291
292 setEnableComponents(enabled);
293
294 if (options.taxonList != null) timeScaleChanged();
295 }
296 });
297
298 setEnableComponents(false);
299
300 ItemListener listener = new ItemListener() {
301 public void itemStateChanged(ItemEvent ev) {
302 timeScaleChanged();
303 }
304 };
305 unitsCombo.addItemListener(listener);
306 directionCombo.addItemListener(listener);
307 }
308
309 private void setEnableComponents(boolean enabled) {
310 clearDatesAction.setEnabled(enabled);
311 guessDatesAction.setEnabled(enabled);
312 importDatesAction.setEnabled(enabled);
313 setDatesAction.setEnabled(enabled);
314 setUncertaintyAction.setEnabled(enabled);
315 unitsLabel.setEnabled(enabled);
316 unitsCombo.setEnabled(enabled);
317 directionCombo.setEnabled(enabled);
318 scrollPane.setEnabled(enabled);
319 dataTable.setEnabled(enabled);
320 tipDateSamplingCombo.setEnabled(enabled);
321 tipDateSamplingLabel.setEnabled(enabled);
322 tipDateTaxonSetCombo.setEnabled(enabled);
323 tipDateTaxonSetLabel.setEnabled(enabled);
324
325 specifyOriginDate.setEnabled(enabled);
326 originDateText.setEnabled(enabled && specifyOriginDate.isSelected());
327 originDateLabel.setEnabled(enabled && specifyOriginDate.isSelected());
328 }
329
330 public final void timeScaleChanged() {
331 Units.Type units = Units.Type.YEARS;
332 switch ((DateUnitsType) unitsCombo.getSelectedItem()) {
333 case YEARS:
334 units = Units.Type.YEARS;
335 break;
336 case MONTHS:
337 units = Units.Type.MONTHS;
338 break;
339 case DAYS:
340 units = Units.Type.DAYS;
341 break;
342 }
343
344 boolean backwards = directionCombo.getSelectedItem() == DateUnitsType.BACKWARDS;
345
346 for (int i = 0; i < options.taxonList.getTaxonCount(); i++) {
347 Date date = options.taxonList.getTaxon(i).getDate();
348 double d = date.getTimeValue();
349
350 Date newDate = createDate(d, units, backwards, 0.0);
351
352 newDate.setUncertainty(date.getUncertainty());
353
354 options.taxonList.getTaxon(i).setDate(newDate);
355 }
356
357 if (specifyOriginDate.isSelected()) {
358 String text = originDateText.getText();
359 DateGuesser guesser = options.dateGuesser;
360 guessDatesDialog.setupGuesser(guesser);
361
362 try {
363 options.originDate = guesser.parseDate(text);
364 } catch (GuessDatesException e) {
365 options.originDate = null;
366 }
367 } else {
368 options.originDate = null;
369 }
370 if ( options.originDate != null) {
371 originDateLabel.setText(" date value: " + Double.toString(options.originDate.getTimeValue()));
372 } else {
373 originDateLabel.setText(" unable to parse date");
374 }
375
376 calculateHeights();
377
378 if (options.useTipDates) {
379 for (PartitionTreeModel treeModel : options.getPartitionTreeModels()) {
380 treeModel.setTipCalibrations(true);
381 }
382 }
383
384 dataTableModel.fireTableDataChanged();
385 frame.setDirty();
386 }
387
388 private Date createDate(double timeValue, Units.Type units, boolean backwards, double origin) {
389 if (backwards) {
390 return Date.createTimeAgoFromOrigin(timeValue, units, origin);
391 } else {
392 return Date.createTimeSinceOrigin(timeValue, units, origin);
393 }
394 }
395
396 public void setOptions(BeautiOptions options) {
397 this.options = options;
398
399 unitsCombo.setSelectedItem(options.datesUnits);
400 directionCombo.setSelectedItem(options.datesDirection);
401
402 calculateHeights();
403 usingTipDates.setSelected(options.useTipDates);
404
405 dataTableModel.fireTableDataChanged();
406
407 Object item = tipDateTaxonSetCombo.getSelectedItem();
408
409 tipDateTaxonSetCombo.removeAllItems();
410 tipDateTaxonSetCombo.addItem("All taxa");
411 for (TaxonList taxa : options.taxonSets) {
412 tipDateTaxonSetCombo.addItem(taxa);
413 }
414
415 if (item != null) {
416 tipDateTaxonSetCombo.setSelectedItem(item);
417 }
418
419 setupTable();
420 }
421
422 private void setupTable() {
423 dataTableModel.fireTableDataChanged();
424 }
425
426 public void getOptions(BeautiOptions options) {
427 options.datesUnits = (DateUnitsType) unitsCombo.getSelectedItem();
428 options.datesDirection = (DateUnitsType) directionCombo.getSelectedItem();
429
430 TipDateSamplingComponentOptions comp = (TipDateSamplingComponentOptions) options.getComponentOptions(TipDateSamplingComponentOptions.class);
431 comp.tipDateSamplingType = (TipDateSamplingType) tipDateSamplingCombo.getSelectedItem();
432 if (tipDateTaxonSetCombo.getSelectedItem() instanceof TaxonList) {
433 comp.tipDateSamplingTaxonSet = (TaxonList) tipDateTaxonSetCombo.getSelectedItem();
434 } else {
435 comp.tipDateSamplingTaxonSet = null;
436 }
437 }
438
439 private void fireModelsChanged() {
440 frame.setDirty();
441 }
442
443 public JComponent getExportableComponent() {
444 return dataTable;
445 }
446
447 public void selectionChanged() {
448 // nothing to do
449 }
450
451 private void setDates() {
452 if (options.taxonList == null) { // validation of check empty taxonList
453 return;
454 }
455
456 int result;
457 do {
458 if (dateValueDialog == null) {
459 dateValueDialog = new SetValueDialog(frame, "Set Date for Taxa");
460 }
461
462 int[] selRows = dataTable.getSelectedRows();
463
464 if (selRows.length == 1) {
465 precisionValueDialog.setDescription("Set date value for selected taxon");
466 } else if (selRows.length > 1) {
467 dateValueDialog.setDescription("Set date values for selected taxa");
468 } else {
469 dateValueDialog.setDescription("Set date values for all taxa");
470 }
471
472 result = dateValueDialog.showDialog();
473
474 if (result == -1 || result == JOptionPane.CANCEL_OPTION) {
475 return;
476 }
477
478 // currentTrait.guessTrait = true; // ?? no use?
479 String value = dateValueDialog.getValue();
480
481 java.util.Date origin = new java.util.Date(0);
482 double d = Double.parseDouble(value);
483 Date date = Date.createTimeSinceOrigin(d, Units.Type.YEARS, origin);
484
485 if (selRows.length > 0) {
486 for (int row : selRows) {
487 options.taxonList.getTaxon(row).setAttribute("date", date);
488 }
489 } else {
490 for (Taxon taxon : options.taxonList) {
491 taxon.setAttribute("date", date);
492 }
493 }
494
495 // adjust the dates to the current timescale...
496 timeScaleChanged();
497
498 dataTableModel.fireTableDataChanged();
499 } while (result < 0);
500 }
501
502 private void setPrecisions() {
503 if (options.taxonList == null) { // validation of check empty taxonList
504 return;
505 }
506
507 int result;
508 do {
509 if (precisionValueDialog == null) {
510 precisionValueDialog = new SetValueDialog(frame, "Set Precision for Taxa");
511 }
512
513 int[] selRows = dataTable.getSelectedRows();
514
515 if (selRows.length == 1) {
516 precisionValueDialog.setDescription("Set precision value for selected taxon");
517 } else if (selRows.length > 1) {
518 precisionValueDialog.setDescription("Set precision values for selected taxa");
519 } else {
520 precisionValueDialog.setDescription("Set precision values for all taxa");
521 }
522
523 result = precisionValueDialog.showDialog();
524
525 if (result == -1 || result == JOptionPane.CANCEL_OPTION) {
526 return;
527 }
528
529 double value;
530 try {
531 value = Double.parseDouble(precisionValueDialog.getValue());
532 } catch (NumberFormatException nfe) {
533 value = 0.0;
534 }
535 if (value < 0.0 || Double.isNaN(value) || Double.isInfinite(value)) {
536 value = 0.0;
537 }
538
539 if (selRows.length > 0) {
540 for (int row : selRows) {
541 options.taxonList.getTaxon(row).getDate().setUncertainty(value);
542 }
543 } else {
544 for (Taxon taxon : options.taxonList) {
545 taxon.setAttribute("precision", value);
546 }
547 }
548
549 dataTableModel.fireTableDataChanged();
550 } while (result < 0);
551 }
552
553 private void clearDates() {
554 for (int i = 0; i < options.taxonList.getTaxonCount(); i++) {
555 java.util.Date origin = new java.util.Date(0);
556
557 double d = 0.0;
558
559 Date date = Date.createTimeSinceOrigin(d, Units.Type.YEARS, origin);
560 options.taxonList.getTaxon(i).setAttribute("date", date);
561 }
562
563 // adjust the dates to the current timescale...
564 timeScaleChanged();
565
566 dataTableModel.fireTableDataChanged();
567 }
568
569 private void guessDates() {
570
571 if (guessDatesDialog == null) {
572 guessDatesDialog = new GuessDatesDialog(frame);
573 }
574
575 int[] selRows = dataTable.getSelectedRows();
576
577 if (selRows.length > 0) {
578 guessDatesDialog.setDescription("Parse date values for selected taxa");
579 } else {
580 guessDatesDialog.setDescription("Parse date values for all taxa");
581 }
582
583
584 int result = guessDatesDialog.showDialog();
585
586 if (result == -1 || result == JOptionPane.CANCEL_OPTION) {
587 return;
588 }
589
590 DateGuesser guesser = options.dateGuesser;
591
592 guesser.guessDates = true;
593 guessDatesDialog.setupGuesser(guesser);
594
595 if (selRows.length > 0) {
596 Taxa selectedTaxa = new Taxa();
597
598 for (int row : selRows) {
599 Taxon taxon = (Taxon) dataTable.getValueAt(row, 0);
600 selectedTaxa.addTaxon(taxon);
601 }
602 guesser.guessDates(selectedTaxa);
603 } else {
604 guesser.guessDates(options.taxonList);
605 }
606
607 // adjust the dates to the current timescale...
608 timeScaleChanged();
609
610 dataTableModel.fireTableDataChanged();
611 }
612
613 private void importDates() {
614
615 File[] files = frame.selectImportFiles("Import Dates File...", false, new FileNameExtensionFilter[]{
616 new FileNameExtensionFilter("Tab-delimited text files", "txt", "tab", "dat")});
617
618 DataTable<String[]> dataTable;
619
620 if (files != null && files.length != 0) {
621 try {
622 // Load the file as a table
623 dataTable = DataTable.Text.parse(new FileReader(files[0]), true, true, true, false);
624
625 } catch (FileNotFoundException fnfe) {
626 JOptionPane.showMessageDialog(this, "Unable to open file: File not found",
627 "Unable to open file",
628 JOptionPane.ERROR_MESSAGE);
629 return;
630 } catch (IOException ioe) {
631 JOptionPane.showMessageDialog(this, "Unable to read file: " + ioe.getMessage(),
632 "Unable to read file",
633 JOptionPane.ERROR_MESSAGE);
634 return;
635 } catch (Exception ex) {
636 ex.printStackTrace(System.err);
637 JOptionPane.showMessageDialog(this, "Fatal exception: " + ex,
638 "Error reading file",
639 JOptionPane.ERROR_MESSAGE);
640 ex.printStackTrace();
641 return;
642 }
643 } else {
644 return;
645 }
646
647 if (dataTable.getColumnCount() == 0) {
648 // expecting at least 2 columns - labels and dates
649 JOptionPane.showMessageDialog(frame,
650 "Expecting a tab delimited file with at\n" +
651 "least 2 columns (taxon labels and dates).",
652 "Incompatible values", JOptionPane.ERROR_MESSAGE);
653 return;
654 }
655
656 String[] columnLabels = dataTable.getColumnLabels();
657 String[] taxonNames = dataTable.getRowLabels();
658
659 if (columnLabels.length < 2) {
660 // only one column so leave it
661 return;
662 }
663
664 boolean hasColumnHeadings = options.taxonList.getTaxonIndex(columnLabels[0]) < 0;
665
666 // assume the second column contains the dates
667 int dateColumn = 0;
668
669 if (hasColumnHeadings && columnLabels.length > 1) {
670 List<Integer> dateColumns = new ArrayList<Integer>();
671
672 // see if there is a column labelled 'dates' or something
673 for (int i = 1; i < dataTable.getColumnCount(); i++) {
674 if (columnLabels[i].toLowerCase().contains("date")) {
675 dateColumns.add(i - 1);
676 }
677 }
678
679 if (dateColumns.size() > 0) {
680 // if there are multiple date column possibilities, take the first
681 // @todo - allow the user to select the column to use
682 dateColumn = dateColumns.get(0);
683 }
684 }
685
686 Map<Taxon, String> taxonDateMap = new HashMap<Taxon, String>();
687
688 String[] values = dataTable.getColumn(dateColumn);
689
690 if (!hasColumnHeadings) {
691 final int index = options.taxonList.getTaxonIndex(columnLabels[0]);
692 if (index >= 0) {
693 taxonDateMap.put(options.taxonList.getTaxon(index), columnLabels[dateColumn + 1]);
694 }
695 }
696
697 int j = 0;
698 for (final String taxonName : taxonNames) {
699
700 final int index = options.taxonList.getTaxonIndex(taxonName);
701 if (index >= 0) {
702 taxonDateMap.put(options.taxonList.getTaxon(index), values[j]);
703 }
704 j++;
705 }
706
707 if (guessDatesDialog == null) {
708 guessDatesDialog = new GuessDatesDialog(frame);
709 }
710
711 guessDatesDialog.setDescription("Parse date values from file");
712
713 int result = guessDatesDialog.showDialog(true);
714
715 if (result == -1 || result == JOptionPane.CANCEL_OPTION) {
716 return;
717 }
718
719 DateGuesser guesser = options.dateGuesser;
720
721 guesser.guessDates = true;
722 guessDatesDialog.setupGuesser(guesser);
723
724 guesser.guessDates(options.taxonList, taxonDateMap);
725
726 // adjust the dates to the current timescale...
727 timeScaleChanged();
728
729 dataTableModel.fireTableDataChanged();
730 }
731
732 public class SetDatesAction extends AbstractAction {
733 /**
734 *
735 */
736 private static final long serialVersionUID = -7281309694753868635L;
737
738 SetDatesAction() {
739 super("Set Dates");
740 setToolTipText("Use this tool to set sampling date values for the selected taxa");
741 }
742
743 public void actionPerformed(ActionEvent ae) {
744 setDates();
745 }
746 }
747
748 public class ClearDatesAction extends AbstractAction {
749 /**
750 *
751 */
752 private static final long serialVersionUID = -7281309694753868635L;
753
754 ClearDatesAction() {
755 super("Clear Dates");
756 setToolTipText("Use this tool to remove sampling dates from each taxon");
757 }
758
759 public void actionPerformed(ActionEvent ae) {
760 clearDates();
761 }
762 }
763
764 public class GuessDatesAction extends AbstractAction {
765 /**
766 *
767 */
768 private static final long serialVersionUID = 8514706149822252033L;
769
770 GuessDatesAction() {
771 super("Parse Dates");
772 setToolTipText("Use this tool to parse the sampling dates from the taxon labels");
773 }
774
775 public void actionPerformed(ActionEvent ae) {
776 guessDates();
777 }
778 }
779
780 public class ImportDatesAction extends AbstractAction {
781 /**
782 *
783 */
784 private static final long serialVersionUID = 8514706149822252033L;
785
786 ImportDatesAction() {
787 super("Import Dates");
788 setToolTipText("Use this tool to import the sampling dates from a file");
789 }
790
791 public void actionPerformed(ActionEvent ae) {
792 importDates();
793 }
794 }
795
796 public class SetUncertaintyAction extends AbstractAction {
797 /**
798 *
799 */
800 private static final long serialVersionUID = -7281309694753868639L;
801
802 SetUncertaintyAction() {
803 super("Set Uncertainty");
804 setToolTipText("Use this tool to set uncertainty in the date for the selected taxa");
805 }
806
807 public void actionPerformed(ActionEvent ae) {
808 setPrecisions();
809 }
810 }
811
812 private void calculateHeights() {
813
814 options.maximumTipHeight = 0.0;
815 if (options.taxonList == null || options.taxonList.getTaxonCount() == 0) return;
816
817 heights = null;
818
819 dr.evolution.util.Date mostRecent = options.originDate;
820 for (int i = 0; i < options.taxonList.getTaxonCount(); i++) {
821 Date date = options.taxonList.getTaxon(i).getDate();
822 if ((date != null) && (mostRecent == null || date.after(mostRecent))) {
823 mostRecent = date;
824 }
825 }
826
827 if (mostRecent != null) {
828 heights = new double[options.taxonList.getTaxonCount()];
829
830 TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue());
831 double time0 = timeScale.convertTime(mostRecent.getTimeValue(), mostRecent);
832
833 for (int i = 0; i < options.taxonList.getTaxonCount(); i++) {
834 Taxon taxon = options.taxonList.getTaxon(i);
835 Date date = taxon.getDate();
836 if (date != null) {
837 heights[i] = timeScale.convertTime(date.getTimeValue(), date) - time0;
838 taxon.setAttribute("height", heights[i]);
839 if (heights[i] > options.maximumTipHeight) options.maximumTipHeight = heights[i];
840 }
841 }
842 }
843
844 frame.setStatusMessage();
845 }
846
847 class DataTableModel extends AbstractTableModel {
848
849 /**
850 *
851 */
852 private static final long serialVersionUID = -6707994233020715574L;
853 String[] columnNames = {"Name", "Date", "Uncertainty", "Height"};
854
855 public DataTableModel() {
856 }
857
858 public int getColumnCount() {
859 return columnNames.length;
860 }
861
862 public int getRowCount() {
863 if (options == null) return 0;
864 if (options.taxonList == null) return 0;
865
866 return options.taxonList.getTaxonCount();
867 }
868
869 public Object getValueAt(int row, int col) {
870 Date date = options.taxonList.getTaxon(row).getDate();
871
872 switch (col) {
873 case 0:
874 return options.taxonList.getTaxon(row);
875 case 1:
876 if (date != null) {
877 return date.getTimeValue();
878 } else {
879 return "-";
880 }
881 case 2:
882 if (date != null) {
883 return date.getUncertainty();
884 } else {
885 return "-";
886 }
887 case 3:
888 if (heights != null) {
889 return heights[row];
890 } else {
891 return "0.0";
892 }
893 }
894 return null;
895 }
896
897 public void setValueAt(Object aValue, int row, int col) {
898 if (col == 0) {
899 options.taxonList.getTaxon(row).setId(aValue.toString());
900 } else if (col == 1) {
901 Date date = options.taxonList.getTaxon(row).getDate();
902 if (date != null) {
903 double d = (Double) aValue;
904 Date newDate = createDate(d, date.getUnits(), date.isBackwards(), date.getOrigin());
905 options.taxonList.getTaxon(row).setDate(newDate);
906 }
907 } else if (col == 2) {
908 Date date = options.taxonList.getTaxon(row).getDate();
909 if (date != null) {
910 double d = (Double) aValue;
911 if (d >= 0.0) {
912 date.setUncertainty(d);
913 }
914 }
915 }
916
917 timeScaleChanged();
918 }
919
920 public boolean isCellEditable(int row, int col) {
921 if (col == 0) return false;
922 if (col == 1 || col == 2) {
923 Date date = options.taxonList.getTaxon(row).getDate();
924 return (date != null);
925 }
926 return false;
927 }
928
929 public String getColumnName(int column) {
930 return columnNames[column];
931 }
932
933 public Class getColumnClass(int c) {
934 return getValueAt(0, c).getClass();
935 }
936
937 public String toString() {
938 StringBuilder sb = new StringBuilder();
939
940 sb.append(getColumnName(0));
941 for (int j = 1; j < getColumnCount(); j++) {
942 sb.append("\t");
943 sb.append(getColumnName(j));
944 }
945 sb.append("\n");
946
947 for (int i = 0; i < getRowCount(); i++) {
948 sb.append(getValueAt(i, 0));
949 for (int j = 1; j < getColumnCount(); j++) {
950 sb.append("\t");
951 sb.append(getValueAt(i, j));
952 }
953 sb.append("\n");
954 }
955
956 return sb.toString();
957 }
958 }
959 }