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