Debian Codegraph @

src/dr/app/beauti/BeautiFrame.java

raw symbols SCIP
1 /*
2 * BeautiFrame.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.beauti;
35
36 import dr.app.beauti.ancestralstatespanel.AncestralStatesPanel;
37 import dr.app.beauti.clockmodelspanel.ClockModelsPanel;
38 import dr.app.beauti.components.ComponentFactory;
39 import dr.app.beauti.components.ancestralstates.AncestralStatesComponentFactory;
40 import dr.app.beauti.components.continuous.ContinuousComponentFactory;
41 import dr.app.beauti.components.discrete.DiscreteTraitsComponentFactory;
42 import dr.app.beauti.components.dollo.DolloComponentFactory;
43 import dr.app.beauti.components.hpm.HierarchicalModelComponentFactory;
44 import dr.app.beauti.components.linkedparameters.LinkedParameterComponentFactory;
45 import dr.app.beauti.components.marginalLikelihoodEstimation.MarginalLikelihoodEstimationComponentFactory;
46 import dr.app.beauti.components.sequenceerror.SequenceErrorModelComponentFactory;
47 import dr.app.beauti.components.tipdatesampling.TipDateSamplingComponentFactory;
48 import dr.app.beauti.datapanel.DataPanel;
49 import dr.app.beauti.generator.BeastGenerator;
50 import dr.app.beauti.generator.Generator;
51 import dr.app.beauti.mcmcpanel.MCMCPanel;
52 import dr.app.beauti.operatorspanel.OperatorsPanel;
53 import dr.app.beauti.options.BeautiOptions;
54 import dr.app.beauti.options.PartitionTreePrior;
55 import dr.app.beauti.priorspanel.DefaultPriorTableDialog;
56 import dr.app.beauti.priorspanel.PriorsPanel;
57 import dr.app.beauti.sitemodelspanel.SiteModelsPanel;
58 import dr.app.beauti.taxonsetspanel.TaxonSetPanel;
59 import dr.app.beauti.tipdatepanel.TipDatesPanel;
60 import dr.app.beauti.traitspanel.TraitsPanel;
61 import dr.app.beauti.treespanel.TreesPanel;
62 import dr.app.beauti.util.BEAUTiImporter;
63 import dr.app.beauti.util.TextUtil;
64 import dr.app.gui.FileDrop;
65 import dr.app.util.OSType;
66 import dr.app.util.Utils;
67 import dr.evolution.io.Importer.ImportException;
68 import dr.evolution.io.NexusImporter.MissingBlockException;
69 import jam.framework.DocumentFrame;
70 import jam.framework.Exportable;
71 import jam.util.IconUtils;
72 import org.jdom.JDOMException;
73
74 import javax.swing.*;
75 import javax.swing.border.Border;
76 import javax.swing.event.ChangeEvent;
77 import javax.swing.event.ChangeListener;
78 import javax.swing.filechooser.FileNameExtensionFilter;
79 import javax.swing.plaf.BorderUIResource;
80 import java.awt.*;
81 import java.awt.event.HierarchyBoundsListener;
82 import java.awt.event.HierarchyEvent;
83 import java.io.*;
84 import java.util.HashMap;
85 import java.util.Map;
86
87 /**
88 * @author Andrew Rambaut
89 * @author Alexei Drummond
90 * @version $Id: BeautiFrame.java,v 1.22 2006/09/09 16:07:06 rambaut Exp $
91 */
92 public class BeautiFrame extends DocumentFrame {
93
94 private static final long serialVersionUID = 2114148696789612509L;
95
96 public final static String DATA_PARTITIONS = "Partitions";
97 public final static String TAXON_SETS = "Taxa";
98 public final static String TIP_DATES = "Tips";
99 public final static String TRAITS = "Traits";
100 public final static String SITE_MODELS = "Sites";
101 public final static String CLOCK_MODELS = "Clocks";
102 public final static String TREES = "Trees";
103 public final static String ANCESTRAL_STATES = "States";
104 public final static String PRIORS = "Priors";
105 public final static String OPERATORS = "Operators";
106 public final static String MCMC = "MCMC";
107
108 private BeautiOptions options;
109 private BeastGenerator generator;
110 private final ComponentFactory[] components;
111
112 public final JTabbedPane tabbedPane = new JTabbedPane();
113 public final JLabel statusLabel = new JLabel();
114
115 private DataPanel dataPanel;
116 private TipDatesPanel tipDatesPanel;
117 private TraitsPanel traitsPanel;
118 private TaxonSetPanel taxonSetPanel;
119 private SiteModelsPanel siteModelsPanel;
120 private AncestralStatesPanel ancestralStatesPanel;
121 private ClockModelsPanel clockModelsPanel;
122 private TreesPanel treesPanel;
123 private PriorsPanel priorsPanel;
124 private OperatorsPanel operatorsPanel;
125 private MCMCPanel mcmcPanel;
126
127 private BeautiPanel currentPanel;
128
129 private Map<String, FileDialog> fileDialogs = new HashMap<String, FileDialog>();
130 private Map<String, JFileChooser> fileChoosers = new HashMap<String, JFileChooser>();
131
132 final Icon gearIcon = IconUtils.getIcon(this.getClass(), "images/gear.png");
133
134 public BeautiFrame(String title) {
135 super();
136
137 setTitle(title);
138
139 // Prevent the application to close in requestClose()
140 // after a user cancel or a failure in beast file generation
141 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
142
143 // getOpenAction().setEnabled(false);
144 // getSaveAction().setEnabled(false);
145
146 getFindAction().setEnabled(false);
147
148 getZoomWindowAction().setEnabled(false);
149
150 components = new ComponentFactory[] {
151 AncestralStatesComponentFactory.INSTANCE,
152 ContinuousComponentFactory.INSTANCE,
153 DiscreteTraitsComponentFactory.INSTANCE,
154 // DnDsComponentFactory.INSTANCE,
155 DolloComponentFactory.INSTANCE,
156 LinkedParameterComponentFactory.INSTANCE,
157 HierarchicalModelComponentFactory.INSTANCE,
158 MarginalLikelihoodEstimationComponentFactory.INSTANCE,
159 SequenceErrorModelComponentFactory.INSTANCE,
160 TipDateSamplingComponentFactory.INSTANCE
161 };
162
163 options = new BeautiOptions(components);
164 generator = new BeastGenerator(options, components);
165
166 this.getContentPane().addHierarchyBoundsListener(new HierarchyBoundsListener() {
167 public void ancestorMoved(HierarchyEvent e) {
168 }
169
170 public void ancestorResized(HierarchyEvent e) {
171 setStatusMessage();
172 }
173 });
174 }
175
176 public void initializeComponents() {
177
178 dataPanel = new DataPanel(this, getImportAction(), getDeleteAction()/*, getImportTraitsAction()*/);
179 tipDatesPanel = new TipDatesPanel(this);
180 traitsPanel = new TraitsPanel(this, dataPanel, getImportTraitsAction());
181 taxonSetPanel = new TaxonSetPanel(this);
182 siteModelsPanel = new SiteModelsPanel(this, getDeleteAction());
183 ancestralStatesPanel = new AncestralStatesPanel(this);
184 clockModelsPanel = new ClockModelsPanel(this);
185 // clockmodelspanel = new OldClockModelsPanel(this);
186 // oldTreesPanel = new OldTreesPanel(this);
187 treesPanel = new TreesPanel(this, getDeleteAction());
188 // speciesTreesPanel = new SpeciesTreesPanel(this);
189 priorsPanel = new PriorsPanel(this, false);
190 operatorsPanel = new OperatorsPanel(this);
191 mcmcPanel = new MCMCPanel(this);
192
193 int index = 0;
194 tabbedPane.addTab(DATA_PARTITIONS, dataPanel);
195 tabbedPane.setToolTipTextAt(index++, "<html>" +
196 "Import sequence alignments, organize data partitions,<br>" +
197 "link models between partitions and select *BEAST</html>");
198
199 tabbedPane.addTab(TAXON_SETS, taxonSetPanel);
200 tabbedPane.setToolTipTextAt(index++, "<html>" +
201 "Create and edit sets of taxa which can be used to <br>" +
202 "define times of most recent common ancestors and <br>" +
203 "to keep groups monophyletic.</html>");
204 // tabbedPane.addTab("Species Sets", speciesSetPanel);
205 tabbedPane.addTab(TIP_DATES, tipDatesPanel);
206 tabbedPane.setToolTipTextAt(index++, "<html>" +
207 "Specify sampling dates of tips for use in temporal <br>" +
208 "analyses of measurably evolving populations.</html>");
209 tabbedPane.addTab(TRAITS, traitsPanel);
210 tabbedPane.setToolTipTextAt(index++, "<html>" +
211 "Import and organize continuous and discrete traits <br>" +
212 "for taxa, convert them into data partitions for evolutionary<br>" +
213 "analysis.</html>");
214 tabbedPane.addTab(SITE_MODELS, siteModelsPanel);
215 tabbedPane.setToolTipTextAt(index++, "<html>" +
216 "Select evolutionary models to be used for each data <br>" +
217 "partition including substitution models, codon partitioning<br>" +
218 "and trait evolution models.</html>");
219 tabbedPane.addTab(CLOCK_MODELS, clockModelsPanel);
220 tabbedPane.setToolTipTextAt(index++, "<html>" +
221 "Select relaxed molecular clock models to be used across <br>" +
222 "the tree. Specify sampling of rates.</html>");
223 tabbedPane.addTab(TREES, treesPanel);
224 tabbedPane.setToolTipTextAt(index++, "<html>" +
225 "Select the priors on trees including coalescent models<br>" +
226 "birth-death speciation models and the *BEAST gene tree,<br>" +
227 "species tree options.</html>");
228
229 tabbedPane.addTab(ANCESTRAL_STATES, ancestralStatesPanel);
230 tabbedPane.setToolTipTextAt(index++, "<html>" +
231 "Select options for sampling ancestral states at specific<br>" +
232 "or all common ancestors, models of counting state changes<br>" +
233 "and models of sequencing error for data partitions.</html>");
234
235 tabbedPane.addTab(PRIORS, priorsPanel);
236 tabbedPane.setToolTipTextAt(index++, "<html>" +
237 "Specify prior probability distributions on each and every<br>" +
238 "parameter of the current model.</html>");
239 tabbedPane.addTab(OPERATORS, operatorsPanel);
240 tabbedPane.setToolTipTextAt(index++, "<html>" +
241 "Select and adjust the menu of operators that will be used<br>" +
242 "to propose changes to the parameters. Switch off operators<br>" +
243 "on certain parameters to fix them to initial values.</html>");
244 tabbedPane.addTab(MCMC, mcmcPanel);
245 tabbedPane.setToolTipTextAt(index++, "<html>" +
246 "Specify the details of MCMC sampling. This includes chain<br>" +
247 "length, sampling frequencies, log file names and more.</html>");
248
249 for (int i = 1; i < tabbedPane.getTabCount(); i++) {
250 tabbedPane.setEnabledAt(i, false);
251 }
252
253 currentPanel = (BeautiPanel) tabbedPane.getSelectedComponent();
254
255 tabbedPane.addChangeListener(new ChangeListener() {
256 public void stateChanged(ChangeEvent e) {
257 BeautiPanel selectedPanel = (BeautiPanel) tabbedPane.getSelectedComponent();
258 if (selectedPanel == dataPanel) {
259 dataPanel.selectionChanged();
260 } else {
261 getDeleteAction().setEnabled(false);
262 }
263 currentPanel.getOptions(options);
264 setAllOptions();
265 currentPanel = selectedPanel;
266 }
267 });
268
269 JPanel basePanel = new JPanel(new BorderLayout(6, 6));
270 basePanel.setBorder(new BorderUIResource.EmptyBorderUIResource(new java.awt.Insets(12, 12, 12, 12)));
271 // basePanel.setPreferredSize(new java.awt.Dimension(800, 600));
272
273 getExportAction().setEnabled(false);
274 JButton generateButton = new JButton(getExportAction());
275 generateButton.putClientProperty("JButton.buttonType", "roundRect");
276
277 JPanel panel2 = new JPanel(new BorderLayout(6, 6));
278 panel2.add(statusLabel, BorderLayout.WEST);
279 panel2.add(generateButton, BorderLayout.EAST);
280 panel2.setMinimumSize(new java.awt.Dimension(10, 10));
281
282 basePanel.add(tabbedPane, BorderLayout.CENTER);
283 basePanel.add(panel2, BorderLayout.SOUTH);
284
285 add(basePanel, BorderLayout.CENTER);
286
287 Toolkit tk = Toolkit.getDefaultToolkit();
288 Dimension d = tk.getScreenSize();
289 // System.out.println("Screen width = " + d.width);
290 // System.out.println("Screen height = " + d.height);
291
292 if (d.width < 1000 || d.height < 700) {
293 setSize(new java.awt.Dimension(700, 550));
294 } else {
295 setSize(new java.awt.Dimension(1024, 768));
296 }
297
298 if (OSType.isMac()) {
299 setMinimumSize(new java.awt.Dimension(640, 480));
300 }
301
302 setAllOptions();
303
304 Color focusColor = UIManager.getColor("Focus.color");
305 Border focusBorder = BorderFactory.createMatteBorder(2, 2, 2, 2, focusColor);
306 dataPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
307 new FileDrop(null, dataPanel, focusBorder, new FileDrop.Listener() {
308 public void filesDropped(java.io.File[] files) {
309 importFiles(files);
310 } // end filesDropped
311 }); // end FileDrop.Listener
312
313
314 }
315
316 /**
317 * set all the options for all panels
318 */
319 public void setAllOptions() {
320 try {
321 dataPanel.setOptions(options);
322 tipDatesPanel.setOptions(options);
323 traitsPanel.setOptions(options);
324 taxonSetPanel.setOptions(options);
325 siteModelsPanel.setOptions(options);
326 clockModelsPanel.setOptions(options);
327 treesPanel.setOptions(options);
328 ancestralStatesPanel.setOptions(options);
329 priorsPanel.setOptions(options);
330 operatorsPanel.setOptions(options);
331 mcmcPanel.setOptions(options);
332
333 setStatusMessage();
334 } catch (IllegalArgumentException iae) {
335 iae.printStackTrace(System.err);
336 JOptionPane.showMessageDialog(this, iae.getMessage(),
337 "Illegal Argument Exception", JOptionPane.ERROR_MESSAGE);
338 }
339
340 // enable/disable the other tabs and generate option depending on whether any
341 // data has been loaded.
342 boolean enabled = options.getDataPartitions().size() > 0;
343 for (int i = 1; i < tabbedPane.getTabCount(); i++) {
344 tabbedPane.setEnabledAt(i, enabled);
345 }
346 getExportAction().setEnabled(enabled);
347 }
348
349 /**
350 * get all the options for all panels
351 */
352 private void getAllOptions() {
353 try {
354 dataPanel.getOptions(options);
355 tipDatesPanel.getOptions(options);
356 traitsPanel.getOptions(options);
357 taxonSetPanel.getOptions(options);
358 siteModelsPanel.getOptions(options);
359 clockModelsPanel.getOptions(options);
360 treesPanel.getOptions(options);
361 ancestralStatesPanel.getOptions(options);
362 priorsPanel.getOptions(options);
363 operatorsPanel.getOptions(options);
364 mcmcPanel.getOptions(options);
365 } catch (IllegalArgumentException iae) {
366 iae.printStackTrace(System.err);
367 JOptionPane.showMessageDialog(this, iae.getMessage(),
368 "Illegal Argument Exception", JOptionPane.ERROR_MESSAGE);
369 }
370 }
371
372 public void doSelectAll() {
373 if (currentPanel == dataPanel) {
374 dataPanel.selectAll();
375 }
376 }
377
378 public final void dataSelectionChanged(boolean isSelected) {
379 getDeleteAction().setEnabled(isSelected);
380 }
381
382 public final void modelSelectionChanged(boolean isSelected) {
383 getDeleteAction().setEnabled(isSelected);
384 }
385
386 public void doDelete() {
387 if (tabbedPane.getSelectedComponent() == dataPanel) {
388 dataPanel.removeSelection();
389 // } else if (tabbedPane.getSelectedComponent() == modelsPanel) {
390 // modelsPanel.delete();
391 // } else if (tabbedPane.getSelectedComponent() == treesPanel) {
392 // treesPanel.delete();
393 } else {
394 throw new RuntimeException("Delete should only be accessable from the Data and Models panels");
395 }
396
397 setStatusMessage();
398 }
399
400 public boolean requestClose() {
401 if (isDirty() && options.hasData() && isVisible()) {
402 int option = JOptionPane.showConfirmDialog(this,
403 "You have made changes but have not generated\n" +
404 "a BEAST XML file. Do you wish to generate\n" +
405 "before closing this window?",
406 "Unused changes",
407 JOptionPane.YES_NO_CANCEL_OPTION,
408 JOptionPane.WARNING_MESSAGE);
409
410 if (option == JOptionPane.YES_OPTION) {
411 return doGenerate();
412 } else if (option == JOptionPane.CANCEL_OPTION || option == JOptionPane.DEFAULT_OPTION) {
413 return false;
414 }
415 return true;
416 }
417 return true;
418 }
419
420 public void doApplyTemplate() {
421 FileDialog dialog = new FileDialog(this,
422 "Apply Template",
423 FileDialog.LOAD);
424 dialog.setVisible(true);
425 if (dialog.getFile() != null) {
426 File file = new File(dialog.getDirectory(), dialog.getFile());
427 try {
428 readFromFile(file);
429 } catch (FileNotFoundException fnfe) {
430 JOptionPane.showMessageDialog(this, "Unable to open template file: File not found",
431 "Unable to open file",
432 JOptionPane.ERROR_MESSAGE);
433 } catch (IOException ioe) {
434 JOptionPane.showMessageDialog(this, "Unable to read template file: " + ioe.getMessage(),
435 "Unable to read file",
436 JOptionPane.ERROR_MESSAGE);
437 }
438 }
439 }
440
441 protected boolean readFromFile(File file) throws IOException {
442 FileInputStream fileIn =
443 new FileInputStream(file);
444 try {
445 ObjectInputStream in = new ObjectInputStream(fileIn);
446 options = (BeautiOptions) in.readObject();
447 in.close();
448 } catch (IOException ioe) {
449 JOptionPane.showMessageDialog(this,
450 "Unable to read BEAUti file. BEAUti can only read files\n" +
451 "created by 'Saving' within BEAUti. It cannot read BEAST\n" +
452 "XML files. To read data within BEAST XML files, use\n" +
453 "the 'Import' option.",
454 "Unable to read file",
455 JOptionPane.ERROR_MESSAGE);
456 } catch (ClassNotFoundException cnfe) {
457 JOptionPane.showMessageDialog(this, "Unable to read BEAUti file: " + cnfe.getMessage(),
458 "Unable to read file",
459 JOptionPane.ERROR_MESSAGE);
460 cnfe.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
461 return false;
462 }
463 fileIn.close();
464
465 options.registerComponents(components);
466 generator = new BeastGenerator(options, components);
467
468 return true;
469 }
470
471 public String getDefaultFileName() {
472 return options.fileNameStem + ".beauti";
473 }
474
475 protected boolean writeToFile(File file) throws IOException {
476 OutputStream fileOut = new FileOutputStream(file);
477 ObjectOutputStream out =
478 new ObjectOutputStream(fileOut);
479 out.writeObject(options);
480 out.close();
481 fileOut.close();
482 return true;
483 }
484
485 public final void doImport() {
486 File[] files = selectImportFiles("Import Aligment...", true, new FileNameExtensionFilter[] {
487 new FileNameExtensionFilter( "Microsatellite (tab-delimited *.txt) Files", "txt"),
488 new FileNameExtensionFilter(
489 "NEXUS, BEAST or FASTA Files", "nex", "nexus", "nx", "xml", "beast", "fa", "fasta", "afa")});
490 // new FileNameExtensionFilter( "Microsatellite (tab-delimited *.txt) Files", "txt");
491 if (files != null && files.length != 0) {
492 importFiles(files);
493 tabbedPane.setSelectedComponent(dataPanel);
494 }
495 }
496
497 private void importFiles(File[] files) {
498 for (File file : files) {
499 if (file == null || file.getName().equals("")) {
500 JOptionPane.showMessageDialog(this, "Invalid file name",
501 "Invalid file name", JOptionPane.ERROR_MESSAGE);
502 } else {
503 try {
504 BEAUTiImporter beautiImporter = new BEAUTiImporter(this, options);
505 beautiImporter.importFromFile(file);
506
507 setDirty();
508 // } catch (FileNotFoundException fnfe) {
509 // JOptionPane.showMessageDialog(this, "Unable to open file: File not found",
510 // "Unable to open file", JOptionPane.ERROR_MESSAGE);
511 } catch (IOException ioe) {
512 JOptionPane.showMessageDialog(this, "File I/O Error unable to read file: " + ioe.getMessage(),
513 "Unable to read file", JOptionPane.ERROR_MESSAGE);
514 ioe.printStackTrace();
515 // there may be other files in the list so don't return
516 // return;
517
518 } catch (MissingBlockException ex) {
519 JOptionPane.showMessageDialog(this, "TAXON, DATA or CHARACTERS block is missing in Nexus file: " + ex,
520 "Missing Block in Nexus File",
521 JOptionPane.ERROR_MESSAGE);
522 ex.printStackTrace();
523
524 } catch (ImportException ime) {
525 JOptionPane.showMessageDialog(this, "Error parsing imported file: " + ime,
526 "Error reading file",
527 JOptionPane.ERROR_MESSAGE);
528 ime.printStackTrace();
529 } catch (JDOMException jde) {
530 JOptionPane.showMessageDialog(this, "Error parsing imported file: " + jde,
531 "Error reading file",
532 JOptionPane.ERROR_MESSAGE);
533 jde.printStackTrace();
534 }
535 }
536 }
537
538 if (!options.hasIdenticalTaxa()) {
539 setAllOptions(); // need this to refresh panels otherwise it will throw exception
540 dataPanel.selectAll();
541 dataPanel.unlinkTreeModels();
542 }
543
544 setAllOptions();
545
546 }
547
548 public final boolean doImportTraits() {
549 if (options.taxonList != null) { // validation of check empty taxonList
550 File[] files = selectImportFiles("Import Traits File...", false, new FileNameExtensionFilter[] {
551 new FileNameExtensionFilter("Tab-delimited text files", "txt", "tab", "dat") });
552
553 if (files != null && files.length != 0) {
554 try {
555 BEAUTiImporter beautiImporter = new BEAUTiImporter(this, options);
556 beautiImporter.importTraits(files[0]);
557 } catch (FileNotFoundException fnfe) {
558 JOptionPane.showMessageDialog(this, "Unable to open file: File not found",
559 "Unable to open file",
560 JOptionPane.ERROR_MESSAGE);
561 return false;
562 } catch (IOException ioe) {
563 JOptionPane.showMessageDialog(this, "Unable to read file: " + ioe.getMessage(),
564 "Unable to read file",
565 JOptionPane.ERROR_MESSAGE);
566 return false;
567 } catch (Exception ex) {
568 ex.printStackTrace(System.err);
569 JOptionPane.showMessageDialog(this, "Fatal exception: " + ex,
570 "Error reading file",
571 JOptionPane.ERROR_MESSAGE);
572 ex.printStackTrace();
573 return false;
574 }
575 } else {
576 return false;
577 }
578
579 traitsPanel.fireTraitsChanged();
580 setAllOptions();
581
582 tabbedPane.setSelectedComponent(traitsPanel);
583 return true;
584
585 } else {
586 JOptionPane.showMessageDialog(this, "No taxa loaded yet, please import Alignment file.",
587 "No taxa loaded", JOptionPane.ERROR_MESSAGE);
588 return false;
589 }
590 }
591
592 public boolean validateTraitName(String traitName) {
593 // check that the name is valid
594 if (traitName.trim().length() == 0) {
595 Toolkit.getDefaultToolkit().beep();
596 return false;
597 }
598
599 // disallow a trait called 'date'
600 if (traitName.equalsIgnoreCase("date")) {
601 JOptionPane.showMessageDialog(this,
602 "This trait name has a special meaning. Use the 'Tip Date' panel\n" +
603 " to set dates for taxa.",
604 "Reserved trait name",
605 JOptionPane.WARNING_MESSAGE);
606
607 return false;
608 }
609
610 // check that the trait name doesn't exist
611 if (options.traitExists(traitName)) {
612 int option = JOptionPane.showConfirmDialog(this,
613 "A trait of this name already exists. Do you wish to replace\n" +
614 "it with this new trait? This may result in the loss or change\n" +
615 "in trait values for the taxa.",
616 "Overwrite trait?",
617 JOptionPane.YES_NO_OPTION,
618 JOptionPane.WARNING_MESSAGE);
619
620 if (option == JOptionPane.NO_OPTION) {
621 return false;
622 }
623 }
624
625 return true;
626 }
627
628 public void updateDiscreteTraitAnalysis() {
629 setStatusMessage();
630 }
631
632 public void setupEBSP() {
633 dataPanel.selectAll();
634
635 dataPanel.unlinkAll();
636
637 setAllOptions();
638 }
639
640 public PartitionTreePrior getCurrentPartitionTreePrior() {
641 treesPanel.setOptions(options); // need this to refresh the currentTreeModel
642 return treesPanel.currentTreeModel.getPartitionTreePrior();
643 }
644
645 public void setStatusMessage() {
646 int width = this.getWidth() - 260; // minus generate button size
647 if (width < 100) width = 100; // prevent too narrow
648 String tw = TextUtil.wrapText(options.statusMessage(), statusLabel, width);
649 // System.out.println(this.getWidth() + " " + tw);
650 statusLabel.setText(tw);
651 }
652
653 public final boolean doGenerate() {
654
655 try {
656 generator.checkOptions();
657 } catch (Generator.GeneratorException ge) {
658 ge.printStackTrace(System.err);
659 JOptionPane.showMessageDialog(this, ge.getMessage(), "Invalid BEAUti setting : ",
660 JOptionPane.ERROR_MESSAGE);
661 if (ge.getSwitchToPanel() != null) {
662 switchToPanel(ge.getSwitchToPanel());
663 }
664 return false;
665 }
666
667 DefaultPriorTableDialog defaultPriorDialog = new DefaultPriorTableDialog(this);
668 if (!defaultPriorDialog.showDialog(options)) {
669 return false;
670 }
671
672 File file = selectExportFile("Generate BEAST XML File...", new FileNameExtensionFilter("BEAST XML File", "xml", "beast"));
673
674 if (file != null) {
675 try {
676 getAllOptions();
677 generator.generateXML(file);
678
679 } catch (IOException ioe) {
680 ioe.printStackTrace(System.err);
681 JOptionPane.showMessageDialog(this, "Unable to generate file due to I/O issue: " + ioe.getMessage(),
682 "Unable to generate file", JOptionPane.ERROR_MESSAGE);
683 return false;
684 } catch (Generator.GeneratorException e) {
685 e.printStackTrace(System.err);
686 JOptionPane.showMessageDialog(this, "The BEAST XML is incomplete because :\n" + e.getMessage(),
687 "The BEAST XML is incomplete", JOptionPane.ERROR_MESSAGE);
688 return false;
689 } catch (Exception e) {
690 e.printStackTrace(System.err);
691 JOptionPane.showMessageDialog(this, "Unable to generate file: " + e.getMessage(),
692 "Unable to generate file", JOptionPane.ERROR_MESSAGE);
693 return false;
694 }
695
696 clearDirty();
697 return true;
698 }
699
700 return false;
701 }
702
703 /**
704 * Use the native file dialog on the Mac because the Swing one is bad. On linux, the native
705 * one is bad. No preference on Windows.
706 * @param title
707 * @return
708 */
709 public File[] selectImportFiles(final String title, boolean multipleSelection, FileNameExtensionFilter[] fileNameExtensionFilters) {
710 if (Boolean.parseBoolean(System.getProperty("use.native.choosers", Boolean.toString(OSType.isMac())))) {
711 FileDialog importDialog = fileDialogs.get(title);
712 if (importDialog == null) {
713 importDialog = new FileDialog(this, title, FileDialog.LOAD);
714 fileDialogs.put(title, importDialog);
715 }
716
717 importDialog.setVisible(true);
718 if (importDialog.getFile() != null) {
719 return new File[] { new File(importDialog.getDirectory(), importDialog.getFile()) };
720 }
721 } else {
722 JFileChooser importChooser = fileChoosers.get(title);
723 if (importChooser == null) {
724 importChooser = new JFileChooser(Utils.getCWD());
725
726 importChooser.setMultiSelectionEnabled(multipleSelection);
727 for (FileNameExtensionFilter fileNameExtensionFilter : fileNameExtensionFilters) {
728 importChooser.setFileFilter(fileNameExtensionFilter);
729 }
730 importChooser.setDialogTitle(title);
731
732 fileChoosers.put(title, importChooser);
733 }
734
735 int returnVal = importChooser.showOpenDialog(this);
736 if (returnVal == JFileChooser.APPROVE_OPTION) {
737 if (importChooser.isMultiSelectionEnabled()) {
738 return importChooser.getSelectedFiles();
739 } else {
740 return new File[] { importChooser.getSelectedFile() };
741 }
742 }
743 }
744
745 return null;
746 }
747
748 /**
749 * Use the native file dialog on the Mac because the Swing one is bad. On linux, the native
750 * one is bad. No preference on Windows.
751 * @param title
752 * @return
753 */
754 private File selectExportFile(final String title, FileNameExtensionFilter fileNameExtensionFilter) {
755 if (Boolean.parseBoolean(System.getProperty("use.native.choosers", Boolean.toString(OSType.isMac())))) {
756 FileDialog exportDialog = fileDialogs.get(title);
757 if (exportDialog == null) {
758 exportDialog = new FileDialog(this, title, FileDialog.SAVE);
759 fileDialogs.put(title, exportDialog);
760 }
761
762 exportDialog.setFile(options.fileNameStem + ".xml");
763
764 exportDialog.setVisible(true);
765
766 // Mac dialog box will already have asked about overwriting file...
767 if (exportDialog.getFile() != null) {
768 return new File(exportDialog.getDirectory(), exportDialog.getFile());
769 }
770 } else {
771 JFileChooser exportChooser = fileChoosers.get(title);
772 if (exportChooser == null) {
773 exportChooser = new JFileChooser(Utils.getCWD());
774
775 // make JFileChooser chooser remember previous path
776 exportChooser = new JFileChooser(Utils.getCWD());
777 exportChooser.setFileFilter(fileNameExtensionFilter);
778 exportChooser.setDialogTitle(title);
779
780 fileChoosers.put(title, exportChooser);
781 }
782
783 // offer stem as default
784 exportChooser.setSelectedFile(new File(options.fileNameStem + ".xml"));
785
786 final int returnVal = exportChooser.showSaveDialog(this);
787 if (returnVal == JFileChooser.APPROVE_OPTION) {
788 File file = exportChooser.getSelectedFile();
789
790 int n = JOptionPane.YES_OPTION;
791
792 if (file.exists()) {
793 n = JOptionPane.showConfirmDialog(this, file.getName(),
794 "Overwrite the existing file?", JOptionPane.YES_NO_OPTION);
795 }
796
797 if (n == JOptionPane.YES_OPTION) {
798 return file;
799 }
800 }
801 }
802
803 return null;
804 }
805
806 public void switchToPanel(String panelName) {
807 for (int i = 0; i < tabbedPane.getTabCount(); i++) {
808 if (tabbedPane.getTitleAt(i).equals(panelName)) {
809 tabbedPane.setSelectedIndex(i);
810 break;
811 }
812 }
813 }
814
815 public JComponent getExportableComponent() {
816
817 JComponent exportable = null;
818 Component comp = tabbedPane.getSelectedComponent();
819
820 if (comp instanceof Exportable) {
821 exportable = ((Exportable) comp).getExportableComponent();
822 } else if (comp instanceof JComponent) {
823 exportable = (JComponent) comp;
824 }
825
826 return exportable;
827 }
828
829 public Action getImportAction() {
830 return importAlignmentAction;
831 }
832
833 protected AbstractAction importAlignmentAction = new AbstractAction("Import Data...") {
834 private static final long serialVersionUID = 3217702096314745005L;
835
836 public void actionPerformed(java.awt.event.ActionEvent ae) {
837 doImport();
838 }
839 };
840
841 public Action getImportTraitsAction() {
842 return importTraitsAction;
843 }
844
845 protected AbstractAction importTraitsAction = new AbstractAction("Import Traits...") {
846 private static final long serialVersionUID = 3217702096314745005L;
847
848 public void actionPerformed(java.awt.event.ActionEvent ae) {
849 doImportTraits();
850 }
851 };
852
853 public Action getExportAction() {
854 return generateAction;
855 }
856
857 protected AbstractAction generateAction = new AbstractAction("Generate BEAST File...", gearIcon) {
858 private static final long serialVersionUID = -5329102618630268783L;
859
860 public void actionPerformed(java.awt.event.ActionEvent ae) {
861 doGenerate();
862 }
863 };
864
865 }