| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
package dr.app.mapper.application; |
| 35 |
|
| 36 |
import dr.app.mapper.application.menus.MapperFileMenuHandler; |
| 37 |
import dr.evolution.tree.FlexibleTree; |
| 38 |
import dr.evolution.tree.Tree; |
| 39 |
import dr.inference.trace.LogFileTraces; |
| 40 |
import dr.inference.trace.TraceException; |
| 41 |
import dr.util.DataTable; |
| 42 |
import jam.framework.DocumentFrame; |
| 43 |
import jam.framework.Exportable; |
| 44 |
|
| 45 |
import javax.swing.*; |
| 46 |
import javax.swing.filechooser.FileNameExtensionFilter; |
| 47 |
import javax.swing.plaf.BorderUIResource; |
| 48 |
import java.awt.*; |
| 49 |
import java.awt.datatransfer.Clipboard; |
| 50 |
import java.awt.datatransfer.StringSelection; |
| 51 |
import java.awt.event.ActionEvent; |
| 52 |
import java.io.*; |
| 53 |
import java.util.ArrayList; |
| 54 |
import java.util.List; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public class MapperFrame extends DocumentFrame implements MapperFileMenuHandler { |
| 61 |
|
| 62 |
private final JTabbedPane tabbedPane = new JTabbedPane(); |
| 63 |
private final JLabel statusLabel = new JLabel("No data loaded"); |
| 64 |
|
| 65 |
private StrainsPanel strainsPanel; |
| 66 |
private MeasurementsPanel measurementsPanel; |
| 67 |
private LocationsPanel locationsPanel; |
| 68 |
|
| 69 |
|
| 70 |
MapperDocument document = new MapperDocument(); |
| 71 |
|
| 72 |
List<Tree> trees = new ArrayList<Tree>(); |
| 73 |
|
| 74 |
public MapperFrame(String title) { |
| 75 |
super(); |
| 76 |
|
| 77 |
setTitle(title); |
| 78 |
|
| 79 |
getExportDataAction().setEnabled(false); |
| 80 |
getExportPDFAction().setEnabled(false); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); |
| 85 |
|
| 86 |
getOpenAction().setEnabled(true); |
| 87 |
getSaveAction().setEnabled(false); |
| 88 |
getSaveAsAction().setEnabled(false); |
| 89 |
|
| 90 |
getFindAction().setEnabled(false); |
| 91 |
|
| 92 |
getCutAction().setEnabled(false); |
| 93 |
getPasteAction().setEnabled(false); |
| 94 |
getDeleteAction().setEnabled(false); |
| 95 |
getSelectAllAction().setEnabled(false); |
| 96 |
|
| 97 |
getCopyAction().setEnabled(false); |
| 98 |
|
| 99 |
getZoomWindowAction().setEnabled(false); |
| 100 |
} |
| 101 |
|
| 102 |
public void initializeComponents() { |
| 103 |
|
| 104 |
strainsPanel = new StrainsPanel(this, document); |
| 105 |
measurementsPanel = new MeasurementsPanel(this, document); |
| 106 |
locationsPanel = new LocationsPanel(this, document); |
| 107 |
|
| 108 |
|
| 109 |
tabbedPane.addTab("Measurements", measurementsPanel); |
| 110 |
tabbedPane.addTab("Strains", strainsPanel); |
| 111 |
tabbedPane.addTab("Locations", locationsPanel); |
| 112 |
|
| 113 |
|
| 114 |
JPanel panel = new JPanel(new BorderLayout(6, 6)); |
| 115 |
panel.setBorder(new BorderUIResource.EmptyBorderUIResource(new Insets(12, 12, 12, 12))); |
| 116 |
panel.add(tabbedPane, BorderLayout.CENTER); |
| 117 |
|
| 118 |
panel.add(statusLabel, BorderLayout.SOUTH); |
| 119 |
|
| 120 |
getContentPane().setLayout(new BorderLayout(0, 0)); |
| 121 |
getContentPane().add(panel, BorderLayout.CENTER); |
| 122 |
|
| 123 |
setSize(new Dimension(1024, 768)); |
| 124 |
|
| 125 |
setStatusMessage(); |
| 126 |
} |
| 127 |
|
| 128 |
public void timeScaleChanged() { |
| 129 |
|
| 130 |
setStatusMessage(); |
| 131 |
} |
| 132 |
|
| 133 |
public void measurementsChanged() { |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
protected boolean readFromFile(File file) throws IOException { |
| 139 |
Reader reader = new FileReader(file); |
| 140 |
|
| 141 |
BufferedReader bufferedReader = new BufferedReader(reader); |
| 142 |
String line = bufferedReader.readLine(); |
| 143 |
while (line != null && line.length() == 0) { |
| 144 |
line = bufferedReader.readLine(); |
| 145 |
} |
| 146 |
|
| 147 |
boolean isNexus = (line != null && line.toUpperCase().contains("#NEXUS")); |
| 148 |
|
| 149 |
reader = new FileReader(file); |
| 150 |
|
| 151 |
Tree tree = null; |
| 152 |
try { |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
} catch (Exception ex) { |
| 174 |
JOptionPane.showMessageDialog(this, "Fatal exception: " + ex, |
| 175 |
"Error reading file", |
| 176 |
JOptionPane.ERROR_MESSAGE); |
| 177 |
ex.printStackTrace(); |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
if (tree == null) { |
| 183 |
JOptionPane.showMessageDialog(this, "The file is not in a suitable format or contains no trees.", |
| 184 |
"Error reading file", |
| 185 |
JOptionPane.ERROR_MESSAGE); |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
FlexibleTree binaryTree = new FlexibleTree(tree, true); |
| 190 |
binaryTree.resolveTree(); |
| 191 |
trees.add(binaryTree); |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
getExportDataAction().setEnabled(true); |
| 197 |
|
| 198 |
return true; |
| 199 |
} |
| 200 |
|
| 201 |
public final void doImport() { |
| 202 |
final JFileChooser chooser = new JFileChooser(); |
| 203 |
chooser.setMultiSelectionEnabled(true); |
| 204 |
|
| 205 |
FileNameExtensionFilter filter = new FileNameExtensionFilter("Tab or Comma delimited tables", "csv", "txt"); |
| 206 |
chooser.setFileFilter(filter); |
| 207 |
|
| 208 |
final int returnVal = chooser.showOpenDialog(this); |
| 209 |
if (returnVal == JFileChooser.APPROVE_OPTION) { |
| 210 |
File[] files = chooser.getSelectedFiles(); |
| 211 |
importMeasurementFiles(files); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
void importMeasurementFiles(File[] files) { |
| 216 |
List<DataTable<String[]>> dataTables = new ArrayList<DataTable<String[]>>(); |
| 217 |
|
| 218 |
for (int i = 0; i < files.length; i++) { |
| 219 |
String fileName = files[i].getName(); |
| 220 |
|
| 221 |
DataTable<String[]> dataTable; |
| 222 |
try { |
| 223 |
dataTable = DataTable.Text.parse(new FileReader(files[i]), true, false); |
| 224 |
dataTables.add(dataTable); |
| 225 |
} catch (IOException e) { |
| 226 |
JOptionPane.showMessageDialog(this, "Unable to read measurements from file, " + fileName, |
| 227 |
"Error reading file", |
| 228 |
JOptionPane.ERROR_MESSAGE); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
for (DataTable<String[]> dataTable : dataTables) { |
| 233 |
document.addTable(dataTable); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
public final void doImportLocations() { |
| 239 |
final JFileChooser chooser = new JFileChooser(); |
| 240 |
chooser.setMultiSelectionEnabled(true); |
| 241 |
|
| 242 |
FileNameExtensionFilter filter = new FileNameExtensionFilter("BEAST log (*.log) Files", "log", "txt"); |
| 243 |
chooser.setFileFilter(filter); |
| 244 |
|
| 245 |
final int returnVal = chooser.showOpenDialog(this); |
| 246 |
if (returnVal == JFileChooser.APPROVE_OPTION) { |
| 247 |
File[] files = chooser.getSelectedFiles(); |
| 248 |
importLocationFiles(files); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
void importLocationFiles(File[] files) { |
| 253 |
LogFileTraces[] traces = new LogFileTraces[files.length]; |
| 254 |
|
| 255 |
for (int i = 0; i < files.length; i++) { |
| 256 |
traces[i] = new LogFileTraces(files[i].getName(), files[i]); |
| 257 |
} |
| 258 |
|
| 259 |
processTraces(traces); |
| 260 |
} |
| 261 |
|
| 262 |
protected void processTraces(final LogFileTraces[] tracesArray) { |
| 263 |
|
| 264 |
final JFrame frame = this; |
| 265 |
|
| 266 |
if (tracesArray.length == 1) { |
| 267 |
try { |
| 268 |
final LogFileTraces traces = tracesArray[0]; |
| 269 |
|
| 270 |
final String fileName = traces.getName(); |
| 271 |
final ProgressMonitorInputStream in = new ProgressMonitorInputStream( |
| 272 |
this, |
| 273 |
"Reading " + fileName, |
| 274 |
new FileInputStream(traces.getFile())); |
| 275 |
in.getProgressMonitor().setMillisToDecideToPopup(0); |
| 276 |
in.getProgressMonitor().setMillisToPopup(0); |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
Thread readThread = new Thread() { |
| 281 |
public void run() { |
| 282 |
try { |
| 283 |
traces.loadTraces(in); |
| 284 |
|
| 285 |
EventQueue.invokeLater( |
| 286 |
new Runnable() { |
| 287 |
public void run() { |
| 288 |
|
| 289 |
|
| 290 |
} |
| 291 |
}); |
| 292 |
|
| 293 |
} catch (final TraceException te) { |
| 294 |
EventQueue.invokeLater( |
| 295 |
new Runnable() { |
| 296 |
public void run() { |
| 297 |
JOptionPane.showMessageDialog(frame, "Problem with trace file: " + te.getMessage(), |
| 298 |
"Problem with tree file", |
| 299 |
JOptionPane.ERROR_MESSAGE); |
| 300 |
} |
| 301 |
}); |
| 302 |
} catch (final InterruptedIOException iioex) { |
| 303 |
|
| 304 |
} catch (final IOException ioex) { |
| 305 |
EventQueue.invokeLater( |
| 306 |
new Runnable() { |
| 307 |
public void run() { |
| 308 |
JOptionPane.showMessageDialog(frame, "File I/O Error: " + ioex.getMessage(), |
| 309 |
"File I/O Error", |
| 310 |
JOptionPane.ERROR_MESSAGE); |
| 311 |
} |
| 312 |
}); |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
} |
| 323 |
|
| 324 |
} |
| 325 |
}; |
| 326 |
readThread.start(); |
| 327 |
|
| 328 |
} catch (FileNotFoundException fnfe) { |
| 329 |
JOptionPane.showMessageDialog(this, "Unable to open file: File not found", |
| 330 |
"Unable to open file", |
| 331 |
JOptionPane.ERROR_MESSAGE); |
| 332 |
} catch (IOException ioex) { |
| 333 |
JOptionPane.showMessageDialog(this, "File I/O Error: " + ioex, |
| 334 |
"File I/O Error", |
| 335 |
JOptionPane.ERROR_MESSAGE); |
| 336 |
} catch (Exception ex) { |
| 337 |
JOptionPane.showMessageDialog(this, "Fatal exception: " + ex, |
| 338 |
"Error reading file", |
| 339 |
JOptionPane.ERROR_MESSAGE); |
| 340 |
} |
| 341 |
|
| 342 |
} else { |
| 343 |
Thread readThread = new Thread() { |
| 344 |
public void run() { |
| 345 |
try { |
| 346 |
for (final LogFileTraces traces : tracesArray) { |
| 347 |
|
| 348 |
traces.loadTraces(); |
| 349 |
|
| 350 |
EventQueue.invokeLater( |
| 351 |
new Runnable() { |
| 352 |
public void run() { |
| 353 |
|
| 354 |
|
| 355 |
} |
| 356 |
}); |
| 357 |
} |
| 358 |
|
| 359 |
} catch (final TraceException te) { |
| 360 |
EventQueue.invokeLater( |
| 361 |
new Runnable() { |
| 362 |
public void run() { |
| 363 |
JOptionPane.showMessageDialog(frame, "Problem with trace file: " + te.getMessage(), |
| 364 |
"Problem with tree file", |
| 365 |
JOptionPane.ERROR_MESSAGE); |
| 366 |
} |
| 367 |
}); |
| 368 |
} catch (final InterruptedIOException iioex) { |
| 369 |
|
| 370 |
} catch (final IOException ioex) { |
| 371 |
EventQueue.invokeLater( |
| 372 |
new Runnable() { |
| 373 |
public void run() { |
| 374 |
JOptionPane.showMessageDialog(frame, "File I/O Error: " + ioex.getMessage(), |
| 375 |
"File I/O Error", |
| 376 |
JOptionPane.ERROR_MESSAGE); |
| 377 |
} |
| 378 |
}); |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
} |
| 389 |
|
| 390 |
} |
| 391 |
}; |
| 392 |
readThread.start(); |
| 393 |
|
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
protected boolean writeToFile(File file) throws IOException { |
| 398 |
return false; |
| 399 |
} |
| 400 |
|
| 401 |
protected void doExportData() { |
| 402 |
FileDialog dialog = new FileDialog(this, |
| 403 |
"Export Data File...", |
| 404 |
FileDialog.SAVE); |
| 405 |
|
| 406 |
dialog.setVisible(true); |
| 407 |
if (dialog.getFile() != null) { |
| 408 |
File file = new File(dialog.getDirectory(), dialog.getFile()); |
| 409 |
|
| 410 |
Writer writer = null; |
| 411 |
try { |
| 412 |
writer = new PrintWriter(file); |
| 413 |
|
| 414 |
writer.close(); |
| 415 |
} catch (IOException ioe) { |
| 416 |
JOptionPane.showMessageDialog(this, "Error writing data file: " + ioe.getMessage(), |
| 417 |
"Export Error", |
| 418 |
JOptionPane.ERROR_MESSAGE); |
| 419 |
} |
| 420 |
|
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
private void setStatusMessage() { |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
} |
| 440 |
|
| 441 |
public JComponent getExportableComponent() { |
| 442 |
|
| 443 |
JComponent exportable = null; |
| 444 |
Component comp = tabbedPane.getSelectedComponent(); |
| 445 |
|
| 446 |
if (comp instanceof Exportable) { |
| 447 |
exportable = ((Exportable) comp).getExportableComponent(); |
| 448 |
} else if (comp instanceof JComponent) { |
| 449 |
exportable = (JComponent) comp; |
| 450 |
} |
| 451 |
|
| 452 |
return exportable; |
| 453 |
} |
| 454 |
|
| 455 |
@Override |
| 456 |
public void doCopy() { |
| 457 |
StringWriter writer = new StringWriter(); |
| 458 |
PrintWriter pwriter = new PrintWriter(writer); |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); |
| 465 |
StringSelection selection = new StringSelection(writer.toString()); |
| 466 |
clipboard.setContents(selection, selection); |
| 467 |
} |
| 468 |
|
| 469 |
@Override |
| 470 |
public Action getImportAction() { |
| 471 |
return getImportMeasurementsAction(); |
| 472 |
} |
| 473 |
|
| 474 |
@Override |
| 475 |
public Action getImportMeasurementsAction() { |
| 476 |
return importMeasurementsAction; |
| 477 |
} |
| 478 |
|
| 479 |
@Override |
| 480 |
public Action getImportLocationsAction() { |
| 481 |
return importLocationsAction; |
| 482 |
} |
| 483 |
|
| 484 |
@Override |
| 485 |
public Action getImportTreesAction() { |
| 486 |
return importTreesAction; |
| 487 |
} |
| 488 |
|
| 489 |
public Action getDeleteItemAction() { |
| 490 |
return getDeleteAction(); |
| 491 |
} |
| 492 |
|
| 493 |
@Override |
| 494 |
public Action getExportAction() { |
| 495 |
return getExportDataAction(); |
| 496 |
} |
| 497 |
|
| 498 |
@Override |
| 499 |
public Action getExportDataAction() { |
| 500 |
return exportDataAction; |
| 501 |
} |
| 502 |
|
| 503 |
@Override |
| 504 |
public Action getExportPDFAction() { |
| 505 |
return exportPDFAction; |
| 506 |
} |
| 507 |
|
| 508 |
protected AbstractAction importMeasurementsAction = new AbstractAction("Import Measurements...") { |
| 509 |
public void actionPerformed(ActionEvent ae) { |
| 510 |
doImport(); |
| 511 |
} |
| 512 |
}; |
| 513 |
|
| 514 |
protected AbstractAction importLocationsAction = new AbstractAction("Import Locations...") { |
| 515 |
public void actionPerformed(ActionEvent ae) { |
| 516 |
|
| 517 |
} |
| 518 |
}; |
| 519 |
|
| 520 |
protected AbstractAction importTreesAction = new AbstractAction("Import Trees...") { |
| 521 |
public void actionPerformed(ActionEvent ae) { |
| 522 |
|
| 523 |
} |
| 524 |
}; |
| 525 |
|
| 526 |
protected AbstractAction exportDataAction = new AbstractAction("Export Data...") { |
| 527 |
public void actionPerformed(ActionEvent ae) { |
| 528 |
doExportData(); |
| 529 |
} |
| 530 |
}; |
| 531 |
|
| 532 |
protected AbstractAction exportPDFAction = new AbstractAction("Export PDF...") { |
| 533 |
public void actionPerformed(ActionEvent ae) { |
| 534 |
|
| 535 |
} |
| 536 |
}; |
| 537 |
|
| 538 |
} |