Debian Codegraph @

src/dr/app/mapper/application/MapperFrame.java

raw symbols SCIP
1 /*
2 * MapperFrame.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 /*
27 * BeautiFrame.java
28 *
29 * (c) 2002-2005 BEAST Development Core Team
30 *
31 * This package may be distributed under the
32 * Lesser Gnu Public Licence (LGPL)
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 * @author Andrew Rambaut
58 * @version $Id: BeautiFrame.java,v 1.22 2006/09/09 16:07:06 rambaut Exp $
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 // private AnalysisPanel analysisPanel;
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 // Prevent the application to close in requestClose()
83 // after a user cancel or a failure in beast file generation
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 // analysisPanel = new AnalysisPanel(this, trees.get(0));
108
109 tabbedPane.addTab("Measurements", measurementsPanel);
110 tabbedPane.addTab("Strains", strainsPanel);
111 tabbedPane.addTab("Locations", locationsPanel);
112 // tabbedPane.addTab("Analysis", analysisPanel);
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 // analysisPanel.timeScaleChanged();
130 setStatusMessage();
131 }
132
133 public void measurementsChanged() {
134 //To change body of created methods use File | Settings | File Templates.
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 // if (isNexus) {
154 // NexusImporter importer = new NexusImporter(reader);
155 // tree = importer.importTree(taxa);
156 // } else {
157 // NewickImporter importer = new NewickImporter(reader);
158 // tree = importer.importTree(taxa);
159 // }
160
161 // } catch (Importer.ImportException ime) {
162 // JOptionPane.showMessageDialog(this, "Error parsing imported file: " + ime,
163 // "Error reading file",
164 // JOptionPane.ERROR_MESSAGE);
165 // ime.printStackTrace();
166 // return false;
167 // } catch (IOException ioex) {
168 // JOptionPane.showMessageDialog(this, "File I/O Error: " + ioex,
169 // "File I/O Error",
170 // JOptionPane.ERROR_MESSAGE);
171 // ioex.printStackTrace();
172 // return false;
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 // if (taxa == null) {
193 // taxa = binaryTree;
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 // final Reader reader = new InputStreamReader(in);
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 // analyseTraceList(traces);
289 // addTraceList(traces);
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 // The cancel dialog button was pressed - do nothing
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 // } catch (final Exception ex) {
314 // EventQueue.invokeLater (
315 // new Runnable () {
316 // public void run () {
317 // JOptionPane.showMessageDialog(frame, "Fatal exception: " + ex.getMessage(),
318 // "Error reading file",
319 // JOptionPane.ERROR_MESSAGE);
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 // final Reader reader = new FileReader(traces.getFile());
348 traces.loadTraces();
349
350 EventQueue.invokeLater(
351 new Runnable() {
352 public void run() {
353 // analyseTraceList(traces);
354 // addTraceList(traces);
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 // The cancel dialog button was pressed - do nothing
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 // } catch (final Exception ex) {
380 // EventQueue.invokeLater (
381 // new Runnable () {
382 // public void run () {
383 // JOptionPane.showMessageDialog(frame, "Fatal exception: " + ex.getMessage(),
384 // "Error reading file",
385 // JOptionPane.ERROR_MESSAGE);
386 // }
387 // });
388 }
389
390 }
391 };
392 readThread.start();
393
394 }
395 }
396
397 protected boolean writeToFile(File file) throws IOException {
398 return false; //To change body of implemented methods use File | Settings | File Templates.
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 // analysisPanel.writeDataFile(writer);
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 // Tree tree = treesPanel.getTree();
426 // if (tree != null) {
427 // String message = "";
428 // message += "Tree loaded, " + tree.getTaxonCount() + " taxa";
429 //
430 // TemporalRooting tr = treesPanel.getTemporalRooting();
431 // if (tr.isContemporaneous()) {
432 // message += ", contemporaneous tips";
433 // } else {
434 // NumberFormatter nf = new NumberFormatter(3);
435 // message += ", dated tips with range " + nf.format(tr.getDateRange());
436 // }
437 // statusLabel.setText(message);
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 // for (String tip : treesPanel.getSelectedTips()) {
461 // pwriter.println(tip);
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 // doImportLocations();
517 }
518 };
519
520 protected AbstractAction importTreesAction = new AbstractAction("Import Trees...") {
521 public void actionPerformed(ActionEvent ae) {
522 // doImportTrees();
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 // doExportPDF();
535 }
536 };
537
538 }