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