f37e8a72f9dc2d2f4019b13a14811f815ba30028
[jalview.git] / forester / java / src / org / forester / archaeopteryx / MainFrameApplication.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // Copyright (C) 2003-2007 Ethalinda K.S. Cannon
8 // All rights reserved
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library 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 GNU
18 // 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 this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/forester
26
27 package org.forester.archaeopteryx;
28
29 import java.awt.BorderLayout;
30 import java.awt.Font;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ComponentAdapter;
33 import java.awt.event.ComponentEvent;
34 import java.awt.event.WindowAdapter;
35 import java.awt.event.WindowEvent;
36 import java.io.File;
37 import java.io.FileInputStream;
38 import java.io.IOException;
39 import java.net.MalformedURLException;
40 import java.net.URL;
41 import java.util.ArrayList;
42 import java.util.HashSet;
43 import java.util.List;
44 import java.util.Set;
45
46 import javax.swing.ButtonGroup;
47 import javax.swing.JCheckBoxMenuItem;
48 import javax.swing.JFileChooser;
49 import javax.swing.JMenu;
50 import javax.swing.JMenuBar;
51 import javax.swing.JMenuItem;
52 import javax.swing.JOptionPane;
53 import javax.swing.JRadioButtonMenuItem;
54 import javax.swing.UIManager;
55 import javax.swing.UnsupportedLookAndFeelException;
56 import javax.swing.WindowConstants;
57 import javax.swing.event.ChangeEvent;
58 import javax.swing.event.ChangeListener;
59 import javax.swing.filechooser.FileFilter;
60
61 import org.forester.archaeopteryx.Options.CLADOGRAM_TYPE;
62 import org.forester.archaeopteryx.Options.NODE_LABEL_DIRECTION;
63 import org.forester.archaeopteryx.Options.PHYLOGENY_GRAPHICS_TYPE;
64 import org.forester.archaeopteryx.Util.GraphicsExportType;
65 import org.forester.archaeopteryx.tools.AncestralTaxonomyInferrer;
66 import org.forester.archaeopteryx.tools.GoAnnotation;
67 import org.forester.archaeopteryx.tools.PhyloInferenceDialog;
68 import org.forester.archaeopteryx.tools.PhylogeneticInferenceOptions;
69 import org.forester.archaeopteryx.tools.PhylogeneticInferrer;
70 import org.forester.archaeopteryx.tools.SequenceDataRetriver;
71 import org.forester.archaeopteryx.tools.TaxonomyDataObtainer;
72 import org.forester.archaeopteryx.webservices.PhylogeniesWebserviceClient;
73 import org.forester.archaeopteryx.webservices.WebservicesManager;
74 import org.forester.io.parsers.FastaParser;
75 import org.forester.io.parsers.GeneralMsaParser;
76 import org.forester.io.parsers.PhylogenyParser;
77 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
78 import org.forester.io.parsers.nhx.NHXParser;
79 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
80 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
81 import org.forester.io.parsers.tol.TolParser;
82 import org.forester.io.writers.PhylogenyWriter;
83 import org.forester.io.writers.SequenceWriter;
84 import org.forester.msa.Msa;
85 import org.forester.msa.MsaFormatException;
86 import org.forester.phylogeny.Phylogeny;
87 import org.forester.phylogeny.PhylogenyMethods;
88 import org.forester.phylogeny.PhylogenyNode;
89 import org.forester.phylogeny.data.Confidence;
90 import org.forester.phylogeny.data.Taxonomy;
91 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
92 import org.forester.phylogeny.factories.PhylogenyFactory;
93 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
94 import org.forester.sdi.GSDI;
95 import org.forester.sdi.SDI;
96 import org.forester.sdi.SDIR;
97 import org.forester.sdi.SDIse;
98 import org.forester.sequence.Sequence;
99 import org.forester.util.BasicDescriptiveStatistics;
100 import org.forester.util.BasicTable;
101 import org.forester.util.BasicTableParser;
102 import org.forester.util.DescriptiveStatistics;
103 import org.forester.util.ForesterUtil;
104 import org.forester.util.ForesterUtil.PhylogenyNodeField;
105 import org.forester.util.ForesterUtil.TAXONOMY_EXTRACTION;
106 import org.forester.util.WindowsUtils;
107
108 class DefaultFilter extends FileFilter {
109
110     @Override
111     public boolean accept( final File f ) {
112         final String file_name = f.getName().trim().toLowerCase();
113         return file_name.endsWith( ".nh" ) || file_name.endsWith( ".newick" ) || file_name.endsWith( ".phy" )
114                 || file_name.endsWith( ".nwk" ) || file_name.endsWith( ".phb" ) || file_name.endsWith( ".ph" )
115                 || file_name.endsWith( ".tr" ) || file_name.endsWith( ".dnd" ) || file_name.endsWith( ".tree" )
116                 || file_name.endsWith( ".nhx" ) || file_name.endsWith( ".xml" ) || file_name.endsWith( ".phyloxml" )
117                 || file_name.endsWith( "phylo.xml" ) || file_name.endsWith( ".pxml" ) || file_name.endsWith( ".nexus" )
118                 || file_name.endsWith( ".nx" ) || file_name.endsWith( ".nex" ) || file_name.endsWith( ".tre" )
119                 || file_name.endsWith( ".zip" ) || file_name.endsWith( ".tol" ) || file_name.endsWith( ".tolxml" )
120                 || file_name.endsWith( ".con" ) || f.isDirectory();
121     }
122
123     @Override
124     public String getDescription() {
125         return "All supported files (*.xml, *.phyloxml, *phylo.xml, *.nhx, *.nh, *.newick, *.nex, *.nexus, *.phy, *.tre, *.tree, *.tol, ...)";
126     }
127 }
128
129 class GraphicsFileFilter extends FileFilter {
130
131     @Override
132     public boolean accept( final File f ) {
133         final String file_name = f.getName().trim().toLowerCase();
134         return file_name.endsWith( ".jpg" ) || file_name.endsWith( ".jpeg" ) || file_name.endsWith( ".png" )
135                 || file_name.endsWith( ".gif" ) || file_name.endsWith( ".bmp" ) || f.isDirectory();
136     }
137
138     @Override
139     public String getDescription() {
140         return "Image files (*.jpg, *.jpeg, *.png, *.gif, *.bmp)";
141     }
142 }
143
144 class MsaFileFilter extends FileFilter {
145
146     @Override
147     public boolean accept( final File f ) {
148         final String file_name = f.getName().trim().toLowerCase();
149         return file_name.endsWith( ".msa" ) || file_name.endsWith( ".aln" ) || file_name.endsWith( ".fasta" )
150                 || file_name.endsWith( ".fas" ) || file_name.endsWith( ".fa" ) || f.isDirectory();
151     }
152
153     @Override
154     public String getDescription() {
155         return "Multiple sequence alignment files (*.msa, *.aln, *.fasta, *.fa, *.fas)";
156     }
157 }
158
159 class SequencesFileFilter extends FileFilter {
160
161     @Override
162     public boolean accept( final File f ) {
163         final String file_name = f.getName().trim().toLowerCase();
164         return file_name.endsWith( ".fasta" ) || file_name.endsWith( ".fa" ) || file_name.endsWith( ".fas" )
165                 || file_name.endsWith( ".seqs" ) || f.isDirectory();
166     }
167
168     @Override
169     public String getDescription() {
170         return "Sequences files (*.fasta, *.fa, *.fas, *.seqs )";
171     }
172 }
173
174 public final class MainFrameApplication extends MainFrame {
175
176     private final static int                 FRAME_X_SIZE                    = 800;
177     private final static int                 FRAME_Y_SIZE                    = 800;
178     // Filters for the file-open dialog (classes defined in this file)
179     private final static NHFilter            nhfilter                        = new NHFilter();
180     private final static NHXFilter           nhxfilter                       = new NHXFilter();
181     private final static XMLFilter           xmlfilter                       = new XMLFilter();
182     private final static TolFilter           tolfilter                       = new TolFilter();
183     private final static NexusFilter         nexusfilter                     = new NexusFilter();
184     private final static PdfFilter           pdffilter                       = new PdfFilter();
185     private final static GraphicsFileFilter  graphicsfilefilter              = new GraphicsFileFilter();
186     private final static MsaFileFilter       msafilter                       = new MsaFileFilter();
187     private final static SequencesFileFilter seqsfilter                      = new SequencesFileFilter();
188     private final static DefaultFilter       defaultfilter                   = new DefaultFilter();
189     private static final long                serialVersionUID                = -799735726778865234L;
190     private final JFileChooser               _values_filechooser;
191     private final JFileChooser               _open_filechooser;
192     private final JFileChooser               _msa_filechooser;
193     private final JFileChooser               _seqs_filechooser;
194     private final JFileChooser               _open_filechooser_for_species_tree;
195     private final JFileChooser               _save_filechooser;
196     private final JFileChooser               _writetopdf_filechooser;
197     private final JFileChooser               _writetographics_filechooser;
198     // Analysis menu
199     private JMenu                            _analysis_menu;
200     private JMenuItem                        _load_species_tree_item;
201     private JMenuItem                        _sdi_item;
202     private JMenuItem                        _gsdi_item;
203     private JMenuItem                        _root_min_dups_item;
204     private JMenuItem                        _root_min_cost_l_item;
205     private JMenuItem                        _lineage_inference;
206     private JMenuItem                        _function_analysis;
207     // Application-only print menu items
208     private JMenuItem                        _print_item;
209     private JMenuItem                        _write_to_pdf_item;
210     private JMenuItem                        _write_to_jpg_item;
211     private JMenuItem                        _write_to_gif_item;
212     private JMenuItem                        _write_to_tif_item;
213     private JMenuItem                        _write_to_png_item;
214     private JMenuItem                        _write_to_bmp_item;
215     private Phylogeny                        _species_tree;
216     private File                             _current_dir;
217     private ButtonGroup                      _radio_group_1;
218     // Others:
219     double                                   _min_not_collapse               = Constants.MIN_NOT_COLLAPSE_DEFAULT;
220     // Phylogeny Inference menu
221     private JMenu                            _inference_menu;
222     private JMenuItem                        _inference_from_msa_item;
223     private JMenuItem                        _inference_from_seqs_item;
224     // Phylogeny Inference
225     private PhylogeneticInferenceOptions     _phylogenetic_inference_options = null;
226     private Msa                              _msa                            = null;
227     private File                             _msa_file                       = null;
228     private List<Sequence>                   _seqs                           = null;
229     private File                             _seqs_file                      = null;
230     // expression values menu:
231     JMenuItem                                _read_values_jmi;
232
233     private MainFrameApplication( final Phylogeny[] phys, final Configuration config, final String title ) {
234         _configuration = config;
235         if ( _configuration == null ) {
236             throw new IllegalArgumentException( "configuration is null" );
237         }
238         try {
239             if ( _configuration.isUseNativeUI() ) {
240                 UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
241             }
242             else {
243                 UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
244             }
245             //UIManager.setLookAndFeel( "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" );
246         }
247         catch ( final UnsupportedLookAndFeelException e ) {
248             Util.dieWithSystemError( "UnsupportedLookAndFeelException: " + e.toString() );
249         }
250         catch ( final ClassNotFoundException e ) {
251             Util.dieWithSystemError( "ClassNotFoundException: " + e.toString() );
252         }
253         catch ( final InstantiationException e ) {
254             Util.dieWithSystemError( "InstantiationException: " + e.toString() );
255         }
256         catch ( final IllegalAccessException e ) {
257             Util.dieWithSystemError( "IllegalAccessException: " + e.toString() );
258         }
259         catch ( final Exception e ) {
260             Util.dieWithSystemError( e.toString() );
261         }
262         // hide until everything is ready
263         setVisible( false );
264         setOptions( Options.createInstance( _configuration ) );
265         setPhylogeneticInferenceOptions( PhylogeneticInferenceOptions.createInstance( _configuration ) );
266         _textframe = null;
267         _species_tree = null;
268         // set title
269         setTitle( Constants.PRG_NAME + " " + Constants.VERSION + " (" + Constants.PRG_DATE + ")" );
270         _mainpanel = new MainPanel( _configuration, this );
271         // The file dialogs
272         _open_filechooser = new JFileChooser();
273         _open_filechooser.setCurrentDirectory( new File( "." ) );
274         _open_filechooser.setMultiSelectionEnabled( false );
275         _open_filechooser.addChoosableFileFilter( MainFrameApplication.xmlfilter );
276         _open_filechooser.addChoosableFileFilter( MainFrameApplication.nhxfilter );
277         _open_filechooser.addChoosableFileFilter( MainFrameApplication.nhfilter );
278         _open_filechooser.addChoosableFileFilter( MainFrameApplication.nexusfilter );
279         _open_filechooser.addChoosableFileFilter( MainFrameApplication.tolfilter );
280         _open_filechooser.addChoosableFileFilter( _open_filechooser.getAcceptAllFileFilter() );
281         _open_filechooser.setFileFilter( MainFrameApplication.defaultfilter );
282         _open_filechooser_for_species_tree = new JFileChooser();
283         _open_filechooser_for_species_tree.setCurrentDirectory( new File( "." ) );
284         _open_filechooser_for_species_tree.setMultiSelectionEnabled( false );
285         _open_filechooser_for_species_tree.addChoosableFileFilter( MainFrameApplication.xmlfilter );
286         _open_filechooser_for_species_tree.addChoosableFileFilter( MainFrameApplication.tolfilter );
287         _open_filechooser_for_species_tree.setFileFilter( MainFrameApplication.xmlfilter );
288         _save_filechooser = new JFileChooser();
289         _save_filechooser.setCurrentDirectory( new File( "." ) );
290         _save_filechooser.setMultiSelectionEnabled( false );
291         _save_filechooser.setFileFilter( MainFrameApplication.xmlfilter );
292         _save_filechooser.addChoosableFileFilter( MainFrameApplication.nhxfilter );
293         _save_filechooser.addChoosableFileFilter( MainFrameApplication.nhfilter );
294         _save_filechooser.addChoosableFileFilter( MainFrameApplication.nexusfilter );
295         _save_filechooser.addChoosableFileFilter( _save_filechooser.getAcceptAllFileFilter() );
296         _writetopdf_filechooser = new JFileChooser();
297         _writetopdf_filechooser.addChoosableFileFilter( MainFrameApplication.pdffilter );
298         _writetographics_filechooser = new JFileChooser();
299         _writetographics_filechooser.addChoosableFileFilter( MainFrameApplication.graphicsfilefilter );
300         // Msa:
301         _msa_filechooser = new JFileChooser();
302         _msa_filechooser.setName( "Read Multiple Sequence Alignment File" );
303         _msa_filechooser.setCurrentDirectory( new File( "." ) );
304         _msa_filechooser.setMultiSelectionEnabled( false );
305         _msa_filechooser.addChoosableFileFilter( _msa_filechooser.getAcceptAllFileFilter() );
306         _msa_filechooser.addChoosableFileFilter( MainFrameApplication.msafilter );
307         // Seqs:
308         _seqs_filechooser = new JFileChooser();
309         _seqs_filechooser.setName( "Read Sequences File" );
310         _seqs_filechooser.setCurrentDirectory( new File( "." ) );
311         _seqs_filechooser.setMultiSelectionEnabled( false );
312         _seqs_filechooser.addChoosableFileFilter( _seqs_filechooser.getAcceptAllFileFilter() );
313         _seqs_filechooser.addChoosableFileFilter( MainFrameApplication.seqsfilter );
314         // Expression
315         _values_filechooser = new JFileChooser();
316         _values_filechooser.setCurrentDirectory( new File( "." ) );
317         _values_filechooser.setMultiSelectionEnabled( false );
318         // build the menu bar
319         _jmenubar = new JMenuBar();
320         if ( !_configuration.isUseNativeUI() ) {
321             _jmenubar.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
322         }
323         buildFileMenu();
324         if ( Constants.__ALLOW_PHYLOGENETIC_INFERENCE ) {
325             buildPhylogeneticInferenceMenu();
326         }
327         buildAnalysisMenu();
328         buildToolsMenu();
329         buildViewMenu();
330         buildFontSizeMenu();
331         buildOptionsMenu();
332         buildTypeMenu();
333         buildHelpMenu();
334         setJMenuBar( _jmenubar );
335         _jmenubar.add( _help_jmenu );
336         _contentpane = getContentPane();
337         _contentpane.setLayout( new BorderLayout() );
338         _contentpane.add( _mainpanel, BorderLayout.CENTER );
339         // App is this big
340         setSize( MainFrameApplication.FRAME_X_SIZE, MainFrameApplication.FRAME_Y_SIZE );
341         //        addWindowFocusListener( new WindowAdapter() {
342         //
343         //            @Override
344         //            public void windowGainedFocus( WindowEvent e ) {
345         //                requestFocusInWindow();
346         //            }
347         //        } );
348         // The window listener
349         setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE );
350         addWindowListener( new WindowAdapter() {
351
352             @Override
353             public void windowClosing( final WindowEvent e ) {
354                 if ( isUnsavedDataPresent() ) {
355                     final int r = JOptionPane.showConfirmDialog( null,
356                                                                  "Exit despite potentially unsaved changes?",
357                                                                  "Exit?",
358                                                                  JOptionPane.YES_NO_OPTION );
359                     if ( r != JOptionPane.YES_OPTION ) {
360                         return;
361                     }
362                 }
363                 else {
364                     final int r = JOptionPane.showConfirmDialog( null,
365                                                                  "Exit Archaeopteryx?",
366                                                                  "Exit?",
367                                                                  JOptionPane.YES_NO_OPTION );
368                     if ( r != JOptionPane.YES_OPTION ) {
369                         return;
370                     }
371                 }
372                 exit();
373             }
374         } );
375         // The component listener
376         addComponentListener( new ComponentAdapter() {
377
378             @Override
379             public void componentResized( final ComponentEvent e ) {
380                 if ( _mainpanel.getCurrentTreePanel() != null ) {
381                     _mainpanel.getCurrentTreePanel().setParametersForPainting( _mainpanel.getCurrentTreePanel()
382                                                                                        .getWidth(),
383                                                                                _mainpanel.getCurrentTreePanel()
384                                                                                        .getHeight(),
385                                                                                false );
386                 }
387             }
388         } );
389         requestFocusInWindow();
390         // addKeyListener( this );
391         setVisible( true );
392         if ( ( phys != null ) && ( phys.length > 0 ) ) {
393             Util.addPhylogeniesToTabs( phys, title, null, _configuration, _mainpanel );
394             validate();
395             getMainPanel().getControlPanel().showWholeAll();
396             getMainPanel().getControlPanel().showWhole();
397         }
398         activateSaveAllIfNeeded();
399         // ...and its children
400         _contentpane.repaint();
401         System.gc();
402     }
403
404     private MainFrameApplication( final Phylogeny[] phys, final String config_file, final String title ) {
405         // Reads the config file (false, false => not url, not applet):
406         this( phys, new Configuration( config_file, false, false ), title );
407     }
408
409     @Override
410     public void actionPerformed( final ActionEvent e ) {
411         try {
412             super.actionPerformed( e );
413             final Object o = e.getSource();
414             // Handle app-specific actions here:
415             if ( o == _open_item ) {
416                 readPhylogeniesFromFile();
417             }
418             else if ( o == _save_item ) {
419                 writeToFile( _mainpanel.getCurrentPhylogeny() );
420                 // If subtree currently displayed, save it, instead of complete
421                 // tree.
422             }
423             else if ( o == _new_item ) {
424                 newTree();
425             }
426             else if ( o == _save_all_item ) {
427                 writeAllToFile();
428             }
429             else if ( o == _close_item ) {
430                 closeCurrentPane();
431             }
432             else if ( o == _write_to_pdf_item ) {
433                 writeToPdf( _mainpanel.getCurrentPhylogeny() );
434             }
435             else if ( o == _write_to_jpg_item ) {
436                 writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(), GraphicsExportType.JPG );
437             }
438             else if ( o == _write_to_png_item ) {
439                 writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(), GraphicsExportType.PNG );
440             }
441             else if ( o == _write_to_gif_item ) {
442                 writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(), GraphicsExportType.GIF );
443             }
444             else if ( o == _write_to_tif_item ) {
445                 writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(), GraphicsExportType.TIFF );
446             }
447             else if ( o == _write_to_bmp_item ) {
448                 writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(), GraphicsExportType.BMP );
449             }
450             else if ( o == _print_item ) {
451                 print();
452             }
453             else if ( o == _load_species_tree_item ) {
454                 readSpeciesTreeFromFile();
455             }
456             else if ( o == _sdi_item ) {
457                 if ( isSubtreeDisplayed() ) {
458                     return;
459                 }
460                 executeSDI();
461             }
462             else if ( o == _lineage_inference ) {
463                 if ( isSubtreeDisplayed() ) {
464                     JOptionPane.showMessageDialog( this,
465                                                    "Subtree is shown.",
466                                                    "Cannot infer ancestral taxonomies",
467                                                    JOptionPane.ERROR_MESSAGE );
468                     return;
469                 }
470                 executeLineageInference();
471             }
472             else if ( o == _function_analysis ) {
473                 executeFunctionAnalysis();
474             }
475             else if ( o == _obtain_detailed_taxonomic_information_jmi ) {
476                 if ( isSubtreeDisplayed() ) {
477                     return;
478                 }
479                 obtainDetailedTaxonomicInformation();
480             }
481             else if ( o == _obtain_uniprot_seq_information_jmi ) {
482                 obtainUniProtSequenceInformation();
483             }
484             else if ( o == _read_values_jmi ) {
485                 if ( isSubtreeDisplayed() ) {
486                     return;
487                 }
488                 addExpressionValuesFromFile();
489             }
490             else if ( o == _move_node_names_to_tax_sn_jmi ) {
491                 moveNodeNamesToTaxSn();
492             }
493             else if ( o == _move_node_names_to_seq_names_jmi ) {
494                 moveNodeNamesToSeqNames();
495             }
496             else if ( o == _extract_tax_code_from_node_names_jmi ) {
497                 extractTaxCodeFromNodeNames();
498             }
499             else if ( o == _gsdi_item ) {
500                 if ( isSubtreeDisplayed() ) {
501                     return;
502                 }
503                 executeGSDI();
504             }
505             else if ( o == _root_min_dups_item ) {
506                 if ( isSubtreeDisplayed() ) {
507                     return;
508                 }
509                 executeSDIR( false );
510             }
511             else if ( o == _root_min_cost_l_item ) {
512                 if ( isSubtreeDisplayed() ) {
513                     return;
514                 }
515                 executeSDIR( true );
516             }
517             else if ( o == _graphics_export_visible_only_cbmi ) {
518                 updateOptions( getOptions() );
519             }
520             else if ( o == _antialias_print_cbmi ) {
521                 updateOptions( getOptions() );
522             }
523             else if ( o == _print_black_and_white_cbmi ) {
524                 updateOptions( getOptions() );
525             }
526             else if ( o == _print_using_actual_size_cbmi ) {
527                 updateOptions( getOptions() );
528             }
529             else if ( o == _graphics_export_using_actual_size_cbmi ) {
530                 updateOptions( getOptions() );
531             }
532             else if ( o == _print_size_mi ) {
533                 choosePrintSize();
534             }
535             else if ( o == _choose_pdf_width_mi ) {
536                 choosePdfWidth();
537             }
538             else if ( o == _internal_number_are_confidence_for_nh_parsing_cbmi ) {
539                 updateOptions( getOptions() );
540             }
541             else if ( o == _replace_underscores_cbmi ) {
542                 if ( ( _extract_pfam_style_tax_codes_cbmi != null ) && _replace_underscores_cbmi.isSelected() ) {
543                     _extract_pfam_style_tax_codes_cbmi.setSelected( false );
544                 }
545                 updateOptions( getOptions() );
546             }
547             else if ( o == _collapse_below_threshold ) {
548                 if ( isSubtreeDisplayed() ) {
549                     return;
550                 }
551                 collapseBelowThreshold();
552             }
553             else if ( o == _extract_pfam_style_tax_codes_cbmi ) {
554                 if ( ( _replace_underscores_cbmi != null ) && _extract_pfam_style_tax_codes_cbmi.isSelected() ) {
555                     _replace_underscores_cbmi.setSelected( false );
556                 }
557                 updateOptions( getOptions() );
558             }
559             else if ( o == _inference_from_msa_item ) {
560                 executePhyleneticInference( false );
561             }
562             else if ( o == _inference_from_seqs_item ) {
563                 executePhyleneticInference( true );
564             }
565             _contentpane.repaint();
566         }
567         catch ( final Exception ex ) {
568             Util.unexpectedException( ex );
569         }
570         catch ( final Error err ) {
571             Util.unexpectedError( err );
572         }
573     }
574
575     void buildAnalysisMenu() {
576         _analysis_menu = MainFrame.createMenu( "Analysis", getConfiguration() );
577         _analysis_menu.add( _sdi_item = new JMenuItem( "SDI (Speciation Duplication Inference)" ) );
578         if ( !Constants.__RELEASE && !Constants.__SNAPSHOT_RELEASE ) {
579             _analysis_menu.add( _gsdi_item = new JMenuItem( "GSDI (Generalized Speciation Duplication Inference)" ) );
580         }
581         _analysis_menu.addSeparator();
582         _analysis_menu.add( _root_min_dups_item = new JMenuItem( "Root by Minimizing Duplications | Height (SDI)" ) );
583         _analysis_menu.add( _root_min_cost_l_item = new JMenuItem( "Root by Minimizing Cost L | Height (SDI)" ) );
584         _analysis_menu.addSeparator();
585         _analysis_menu.add( _load_species_tree_item = new JMenuItem( "Load Species Tree..." ) );
586         customizeJMenuItem( _sdi_item );
587         customizeJMenuItem( _gsdi_item );
588         customizeJMenuItem( _root_min_dups_item );
589         customizeJMenuItem( _root_min_cost_l_item );
590         customizeJMenuItem( _load_species_tree_item );
591         _analysis_menu.addSeparator();
592         _analysis_menu.add( _lineage_inference = new JMenuItem( "Infer Ancestor Taxonomies" ) );
593         customizeJMenuItem( _lineage_inference );
594         _lineage_inference.setToolTipText( "Inference of ancestor taxonomies/lineages" );
595         _jmenubar.add( _analysis_menu );
596     }
597
598     void buildPhylogeneticInferenceMenu() {
599         _inference_menu = MainFrame.createMenu( "Inference", getConfiguration() );
600         _inference_menu
601                 .add( _inference_from_msa_item = new JMenuItem( "From Multiple Sequence Alignment...(EXPERIMENTAL - DO NOT USE!!) " ) );
602         customizeJMenuItem( _inference_from_msa_item );
603         _inference_from_msa_item.setToolTipText( "Basic phylogenetic inference from MSA" );
604         _inference_menu
605                 .add( _inference_from_seqs_item = new JMenuItem( "From Unaligned Sequences...(EXPERIMENTAL - DO NOT USE!!) " ) );
606         customizeJMenuItem( _inference_from_seqs_item );
607         _inference_from_seqs_item.setToolTipText( "Basic phylogenetic inference including multiple sequence alignment" );
608         _jmenubar.add( _inference_menu );
609     }
610
611     @Override
612     void buildFileMenu() {
613         _file_jmenu = MainFrame.createMenu( "File", getConfiguration() );
614         _file_jmenu.add( _open_item = new JMenuItem( "Read Tree from File..." ) );
615         _file_jmenu.addSeparator();
616         _file_jmenu.add( _open_url_item = new JMenuItem( "Read Tree from URL/Webservice..." ) );
617         _file_jmenu.addSeparator();
618         final WebservicesManager webservices_manager = WebservicesManager.getInstance();
619         _load_phylogeny_from_webservice_menu_items = new JMenuItem[ webservices_manager
620                 .getAvailablePhylogeniesWebserviceClients().size() ];
621         for( int i = 0; i < webservices_manager.getAvailablePhylogeniesWebserviceClients().size(); ++i ) {
622             final PhylogeniesWebserviceClient client = webservices_manager.getAvailablePhylogeniesWebserviceClient( i );
623             _load_phylogeny_from_webservice_menu_items[ i ] = new JMenuItem( client.getMenuName() );
624             _file_jmenu.add( _load_phylogeny_from_webservice_menu_items[ i ] );
625         }
626         if ( getConfiguration().isEditable() ) {
627             _file_jmenu.addSeparator();
628             _file_jmenu.add( _new_item = new JMenuItem( "New" ) );
629             _new_item.setToolTipText( "to create a new tree with one node, as source for manual tree construction" );
630         }
631         _file_jmenu.addSeparator();
632         _file_jmenu.add( _save_item = new JMenuItem( "Save Tree As..." ) );
633         _file_jmenu.add( _save_all_item = new JMenuItem( "Save All Trees As..." ) );
634         _save_all_item.setToolTipText( "Write all phylogenies to one file." );
635         _save_all_item.setEnabled( false );
636         _file_jmenu.addSeparator();
637         _file_jmenu.add( _write_to_pdf_item = new JMenuItem( "Export to PDF file ..." ) );
638         if ( Util.canWriteFormat( "tif" ) || Util.canWriteFormat( "tiff" ) || Util.canWriteFormat( "TIF" ) ) {
639             _file_jmenu.add( _write_to_tif_item = new JMenuItem( "Export to TIFF file..." ) );
640         }
641         _file_jmenu.add( _write_to_png_item = new JMenuItem( "Export to PNG file..." ) );
642         _file_jmenu.add( _write_to_jpg_item = new JMenuItem( "Export to JPG file..." ) );
643         if ( Util.canWriteFormat( "gif" ) ) {
644             _file_jmenu.add( _write_to_gif_item = new JMenuItem( "Export to GIF file..." ) );
645         }
646         if ( Util.canWriteFormat( "bmp" ) ) {
647             _file_jmenu.add( _write_to_bmp_item = new JMenuItem( "Export to BMP file..." ) );
648         }
649         _file_jmenu.addSeparator();
650         _file_jmenu.add( _print_item = new JMenuItem( "Print..." ) );
651         _file_jmenu.addSeparator();
652         _file_jmenu.add( _close_item = new JMenuItem( "Close Tab" ) );
653         _close_item.setToolTipText( "To close the current pane." );
654         _close_item.setEnabled( true );
655         _file_jmenu.addSeparator();
656         _file_jmenu.add( _exit_item = new JMenuItem( "Exit" ) );
657         // For print in color option item
658         customizeJMenuItem( _open_item );
659         _open_item
660                 .setFont( new Font( _open_item.getFont().getFontName(), Font.BOLD, _open_item.getFont().getSize() + 4 ) );
661         customizeJMenuItem( _open_url_item );
662         for( int i = 0; i < webservices_manager.getAvailablePhylogeniesWebserviceClients().size(); ++i ) {
663             customizeJMenuItem( _load_phylogeny_from_webservice_menu_items[ i ] );
664         }
665         customizeJMenuItem( _save_item );
666         if ( getConfiguration().isEditable() ) {
667             customizeJMenuItem( _new_item );
668         }
669         customizeJMenuItem( _close_item );
670         customizeJMenuItem( _save_all_item );
671         customizeJMenuItem( _write_to_pdf_item );
672         customizeJMenuItem( _write_to_png_item );
673         customizeJMenuItem( _write_to_jpg_item );
674         customizeJMenuItem( _write_to_gif_item );
675         customizeJMenuItem( _write_to_tif_item );
676         customizeJMenuItem( _write_to_bmp_item );
677         customizeJMenuItem( _print_item );
678         customizeJMenuItem( _exit_item );
679         _jmenubar.add( _file_jmenu );
680     }
681
682     void buildOptionsMenu() {
683         _options_jmenu = MainFrame.createMenu( OPTIONS_HEADER, getConfiguration() );
684         _options_jmenu.addChangeListener( new ChangeListener() {
685
686             @Override
687             public void stateChanged( final ChangeEvent e ) {
688                 MainFrame.setOvPlacementColorChooseMenuItem( _overview_placment_mi, getCurrentTreePanel() );
689                 MainFrame.setTextColorChooseMenuItem( _switch_colors_mi, getCurrentTreePanel() );
690                 MainFrame
691                         .setTextMinSupportMenuItem( _choose_minimal_confidence_mi, getOptions(), getCurrentTreePanel() );
692                 MainFrame.setTextForFontChooserMenuItem( _choose_font_mi, MainFrame
693                         .createCurrentFontDesc( getMainPanel().getTreeFontSet() ) );
694                 setTextForGraphicsSizeChooserMenuItem( _print_size_mi, getOptions() );
695                 setTextForPdfLineWidthChooserMenuItem( _choose_pdf_width_mi, getOptions() );
696                 MainFrame.updateOptionsMenuDependingOnPhylogenyType( getMainPanel(),
697                                                                      _show_scale_cbmi,
698                                                                      _show_branch_length_values_cbmi,
699                                                                      _non_lined_up_cladograms_rbmi,
700                                                                      _uniform_cladograms_rbmi,
701                                                                      _ext_node_dependent_cladogram_rbmi,
702                                                                      _label_direction_cbmi );
703             }
704         } );
705         _options_jmenu.add( customizeMenuItemAsLabel( new JMenuItem( DISPLAY_SUBHEADER ), getConfiguration() ) );
706         _options_jmenu
707                 .add( _ext_node_dependent_cladogram_rbmi = new JRadioButtonMenuItem( MainFrame.NONUNIFORM_CLADOGRAMS_LABEL ) );
708         _options_jmenu.add( _uniform_cladograms_rbmi = new JRadioButtonMenuItem( MainFrame.UNIFORM_CLADOGRAMS_LABEL ) );
709         _options_jmenu.add( _non_lined_up_cladograms_rbmi = new JRadioButtonMenuItem( NON_LINED_UP_CLADOGRAMS_LABEL ) );
710         _radio_group_1 = new ButtonGroup();
711         _radio_group_1.add( _ext_node_dependent_cladogram_rbmi );
712         _radio_group_1.add( _uniform_cladograms_rbmi );
713         _radio_group_1.add( _non_lined_up_cladograms_rbmi );
714         //
715         _options_jmenu.add( _show_default_node_shapes_cbmi = new JCheckBoxMenuItem( DISPLAY_NODE_BOXES_LABEL ) );
716         _options_jmenu
717                 .add( _taxonomy_colorize_node_shapes_cbmi = new JCheckBoxMenuItem( MainFrame.TAXONOMY_COLORIZE_NODE_SHAPES_LABEL ) );
718         _options_jmenu.add( _cycle_node_shape_mi = new JMenuItem( MainFrame.CYCLE_NODE_SHAPE_LABEL ) );
719         _options_jmenu.add( _cycle_node_fill_mi = new JMenuItem( MainFrame.CYCLE_NODE_FILL_LABEL ) );
720         _options_jmenu.add( _choose_node_size_mi = new JMenuItem( MainFrame.CHOOSE_NODE_SIZE_LABEL ) );
721         //
722         _options_jmenu.add( _show_scale_cbmi = new JCheckBoxMenuItem( DISPLAY_SCALE_LABEL ) );
723         _options_jmenu
724                 .add( _show_branch_length_values_cbmi = new JCheckBoxMenuItem( DISPLAY_BRANCH_LENGTH_VALUES_LABEL ) );
725         _options_jmenu.add( _show_overview_cbmi = new JCheckBoxMenuItem( SHOW_OVERVIEW_LABEL ) );
726         _options_jmenu.add( _label_direction_cbmi = new JCheckBoxMenuItem( LABEL_DIRECTION_LABEL ) );
727         _label_direction_cbmi.setToolTipText( LABEL_DIRECTION_TIP );
728         _options_jmenu.add( _color_labels_same_as_parent_branch = new JCheckBoxMenuItem( COLOR_LABELS_LABEL ) );
729         _color_labels_same_as_parent_branch.setToolTipText( MainFrame.COLOR_LABELS_TIP );
730         _options_jmenu.add( _abbreviate_scientific_names = new JCheckBoxMenuItem( ABBREV_SN_LABEL ) );
731         _options_jmenu.add( _screen_antialias_cbmi = new JCheckBoxMenuItem( SCREEN_ANTIALIAS_LABEL ) );
732         _options_jmenu.add( _background_gradient_cbmi = new JCheckBoxMenuItem( BG_GRAD_LABEL ) );
733         if ( getConfiguration().doDisplayOption( Configuration.show_domain_architectures ) ) {
734             _options_jmenu.add( _show_domain_labels = new JCheckBoxMenuItem( SHOW_DOMAIN_LABELS_LABEL ) );
735         }
736         _options_jmenu.add( _choose_minimal_confidence_mi = new JMenuItem( "" ) );
737         _options_jmenu.add( _overview_placment_mi = new JMenuItem( "" ) );
738         _options_jmenu.add( _switch_colors_mi = new JMenuItem( "" ) );
739         _options_jmenu.add( _choose_font_mi = new JMenuItem( "" ) );
740         _options_jmenu.addSeparator();
741         _options_jmenu.add( customizeMenuItemAsLabel( new JMenuItem( SEARCH_SUBHEADER ), getConfiguration() ) );
742         _options_jmenu.add( _search_case_senstive_cbmi = new JCheckBoxMenuItem( SEARCH_CASE_SENSITIVE_LABEL ) );
743         _options_jmenu.add( _search_whole_words_only_cbmi = new JCheckBoxMenuItem( SEARCH_TERMS_ONLY_LABEL ) );
744         _options_jmenu.add( _inverse_search_result_cbmi = new JCheckBoxMenuItem( INVERSE_SEARCH_RESULT_LABEL ) );
745         _options_jmenu.addSeparator();
746         _options_jmenu.add( customizeMenuItemAsLabel( new JMenuItem( "Graphics Export & Printing:" ),
747                                                       getConfiguration() ) );
748         _options_jmenu.add( _antialias_print_cbmi = new JCheckBoxMenuItem( "Antialias" ) );
749         _options_jmenu.add( _print_black_and_white_cbmi = new JCheckBoxMenuItem( "Export in Black and White" ) );
750         _options_jmenu
751                 .add( _print_using_actual_size_cbmi = new JCheckBoxMenuItem( "Use Current Image Size for PDF export and Printing" ) );
752         _options_jmenu
753                 .add( _graphics_export_using_actual_size_cbmi = new JCheckBoxMenuItem( "Use Current Image Size for PNG, JPG, and GIF export" ) );
754         _options_jmenu
755                 .add( _graphics_export_visible_only_cbmi = new JCheckBoxMenuItem( "Limit to Visible ('Screenshot') for PNG, JPG, and GIF export" ) );
756         _options_jmenu.add( _print_size_mi = new JMenuItem( "" ) );
757         _options_jmenu.add( _choose_pdf_width_mi = new JMenuItem( "" ) );
758         _options_jmenu.addSeparator();
759         _options_jmenu
760                 .add( customizeMenuItemAsLabel( new JMenuItem( "Newick/NHX/Nexus Parsing:" ), getConfiguration() ) );
761         _options_jmenu
762                 .add( _internal_number_are_confidence_for_nh_parsing_cbmi = new JCheckBoxMenuItem( "Internal Numbers Are Confidence Values" ) );
763         _options_jmenu.add( _replace_underscores_cbmi = new JCheckBoxMenuItem( "Replace Underscores with Spaces" ) );
764         _options_jmenu
765                 .add( _extract_pfam_style_tax_codes_cbmi = new JCheckBoxMenuItem( "Extract Taxonomy Codes from Pfam-style Labels" ) );
766         customizeJMenuItem( _choose_font_mi );
767         customizeJMenuItem( _choose_minimal_confidence_mi );
768         customizeJMenuItem( _switch_colors_mi );
769         customizeJMenuItem( _print_size_mi );
770         customizeJMenuItem( _choose_pdf_width_mi );
771         customizeJMenuItem( _overview_placment_mi );
772         //TODO FIXME ~~
773         customizeCheckBoxMenuItem( _show_default_node_shapes_cbmi, getOptions().isShowDefaultNodeShapes() );
774         customizeCheckBoxMenuItem( _taxonomy_colorize_node_shapes_cbmi, getOptions().isTaxonomyColorizeNodeShapes() );
775         customizeJMenuItem( _cycle_node_shape_mi );
776         customizeJMenuItem( _cycle_node_fill_mi );
777         customizeJMenuItem( _choose_node_size_mi );
778         //TODO FIXME ~~
779         customizeCheckBoxMenuItem( _color_labels_same_as_parent_branch, getOptions().isColorLabelsSameAsParentBranch() );
780         customizeCheckBoxMenuItem( _screen_antialias_cbmi, getOptions().isAntialiasScreen() );
781         customizeCheckBoxMenuItem( _background_gradient_cbmi, getOptions().isBackgroundColorGradient() );
782         customizeCheckBoxMenuItem( _show_domain_labels, getOptions().isShowDomainLabels() );
783         customizeCheckBoxMenuItem( _abbreviate_scientific_names, getOptions().isAbbreviateScientificTaxonNames() );
784         customizeCheckBoxMenuItem( _search_case_senstive_cbmi, getOptions().isSearchCaseSensitive() );
785         customizeCheckBoxMenuItem( _show_scale_cbmi, getOptions().isShowScale() );
786         customizeRadioButtonMenuItem( _non_lined_up_cladograms_rbmi,
787                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.NON_LINED_UP );
788         customizeRadioButtonMenuItem( _uniform_cladograms_rbmi,
789                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.TOTAL_NODE_SUM_DEP );
790         customizeRadioButtonMenuItem( _ext_node_dependent_cladogram_rbmi,
791                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.EXT_NODE_SUM_DEP );
792         customizeCheckBoxMenuItem( _show_branch_length_values_cbmi, getOptions().isShowBranchLengthValues() );
793         customizeCheckBoxMenuItem( _show_overview_cbmi, getOptions().isShowOverview() );
794         customizeCheckBoxMenuItem( _label_direction_cbmi,
795                                    getOptions().getNodeLabelDirection() == NODE_LABEL_DIRECTION.RADIAL );
796         customizeCheckBoxMenuItem( _antialias_print_cbmi, getOptions().isAntialiasPrint() );
797         customizeCheckBoxMenuItem( _print_black_and_white_cbmi, getOptions().isPrintBlackAndWhite() );
798         customizeCheckBoxMenuItem( _internal_number_are_confidence_for_nh_parsing_cbmi, getOptions()
799                 .isInternalNumberAreConfidenceForNhParsing() );
800         customizeCheckBoxMenuItem( _extract_pfam_style_tax_codes_cbmi, getOptions()
801                 .isExtractPfamTaxonomyCodesInNhParsing() );
802         customizeCheckBoxMenuItem( _replace_underscores_cbmi, getOptions().isReplaceUnderscoresInNhParsing() );
803         customizeCheckBoxMenuItem( _search_whole_words_only_cbmi, getOptions().isMatchWholeTermsOnly() );
804         customizeCheckBoxMenuItem( _inverse_search_result_cbmi, getOptions().isInverseSearchResult() );
805         customizeCheckBoxMenuItem( _graphics_export_visible_only_cbmi, getOptions().isGraphicsExportVisibleOnly() );
806         customizeCheckBoxMenuItem( _print_using_actual_size_cbmi, getOptions().isPrintUsingActualSize() );
807         customizeCheckBoxMenuItem( _graphics_export_using_actual_size_cbmi, getOptions()
808                 .isGraphicsExportUsingActualSize() );
809         _jmenubar.add( _options_jmenu );
810     }
811
812     void buildToolsMenu() {
813         _tools_menu = createMenu( "Tools", getConfiguration() );
814         _tools_menu.add( _confcolor_item = new JMenuItem( "Colorize Branches Depending on Confidence" ) );
815         customizeJMenuItem( _confcolor_item );
816         _tools_menu.add( _taxcolor_item = new JMenuItem( "Taxonomy Colorize Branches" ) );
817         customizeJMenuItem( _taxcolor_item );
818         _tools_menu.add( _remove_branch_color_item = new JMenuItem( "Delete Branch Colors" ) );
819         _remove_branch_color_item.setToolTipText( "To delete branch color values from the current phylogeny" );
820         customizeJMenuItem( _remove_branch_color_item );
821         _tools_menu.addSeparator();
822         _tools_menu.add( _midpoint_root_item = new JMenuItem( "Midpoint-Root" ) );
823         customizeJMenuItem( _midpoint_root_item );
824         _tools_menu.addSeparator();
825         _tools_menu.add( _collapse_species_specific_subtrees = new JMenuItem( "Collapse Species-Specific Subtrees" ) );
826         customizeJMenuItem( _collapse_species_specific_subtrees );
827         _tools_menu
828                 .add( _collapse_below_threshold = new JMenuItem( "Collapse Branches with Confidence Below Threshold" ) );
829         customizeJMenuItem( _collapse_below_threshold );
830         _collapse_below_threshold
831                 .setToolTipText( "To permanently collapse branches without at least one support value above a given threshold" );
832         _tools_menu.addSeparator();
833         _tools_menu
834                 .add( _move_node_names_to_tax_sn_jmi = new JMenuItem( "Transfer Node Names to Taxonomic Scientific Names" ) );
835         customizeJMenuItem( _move_node_names_to_tax_sn_jmi );
836         _move_node_names_to_tax_sn_jmi.setToolTipText( "To interpret node names as taxonomic scientific names" );
837         _tools_menu.add( _move_node_names_to_seq_names_jmi = new JMenuItem( "Transfer Node Names to Sequence Names" ) );
838         customizeJMenuItem( _move_node_names_to_seq_names_jmi );
839         _move_node_names_to_seq_names_jmi.setToolTipText( "To interpret node names as sequence (protein, gene) names" );
840         _tools_menu
841                 .add( _extract_tax_code_from_node_names_jmi = new JMenuItem( "Extract Taxonomic Codes from Node Names" ) );
842         customizeJMenuItem( _extract_tax_code_from_node_names_jmi );
843         _extract_tax_code_from_node_names_jmi
844                 .setToolTipText( "To extract taxonomic codes (mnemonics) from nodes names in the form of 'xyz_ECOLI'" );
845         _tools_menu.addSeparator();
846         _tools_menu
847                 .add( _infer_common_sn_names_item = new JMenuItem( "Infer Common Parts of Internal Scientific Names" ) );
848         customizeJMenuItem( _infer_common_sn_names_item );
849         _tools_menu.addSeparator();
850         _tools_menu
851                 .add( _obtain_detailed_taxonomic_information_jmi = new JMenuItem( "Obtain Detailed Taxonomic Information" ) );
852         customizeJMenuItem( _obtain_detailed_taxonomic_information_jmi );
853         _obtain_detailed_taxonomic_information_jmi
854                 .setToolTipText( "To add additional taxonomic information (from UniProt Taxonomy)" );
855         _tools_menu
856                 .add( _obtain_uniprot_seq_information_jmi = new JMenuItem( "Obtain Sequence Information (from UniProt)" ) );
857         customizeJMenuItem( _obtain_uniprot_seq_information_jmi );
858         _obtain_uniprot_seq_information_jmi.setToolTipText( "To add additional sequence information (from UniProt)" );
859         _tools_menu.addSeparator();
860         if ( !Constants.__RELEASE ) {
861             _tools_menu.add( _function_analysis = new JMenuItem( "Add UniProtKB Annotations" ) );
862             customizeJMenuItem( _function_analysis );
863             _function_analysis
864                     .setToolTipText( "To add UniProtKB annotations for sequences with appropriate identifiers" );
865             _tools_menu.addSeparator();
866         }
867         _tools_menu.add( _read_values_jmi = new JMenuItem( "Read Vector/Expression Values" ) );
868         customizeJMenuItem( _read_values_jmi );
869         _read_values_jmi.setToolTipText( "To add vector (e.g. gene expression) values (beta)" );
870         _jmenubar.add( _tools_menu );
871     }
872
873     private void choosePdfWidth() {
874         final String s = ( String ) JOptionPane.showInputDialog( this,
875                                                                  "Please enter the default line width for PDF export.\n"
876                                                                          + "[current value: "
877                                                                          + getOptions().getPrintLineWidth() + "]\n",
878                                                                  "Line Width for PDF Export",
879                                                                  JOptionPane.QUESTION_MESSAGE,
880                                                                  null,
881                                                                  null,
882                                                                  getOptions().getPrintLineWidth() );
883         if ( !ForesterUtil.isEmpty( s ) ) {
884             boolean success = true;
885             float f = 0.0f;
886             final String m_str = s.trim();
887             if ( !ForesterUtil.isEmpty( m_str ) ) {
888                 try {
889                     f = Float.parseFloat( m_str );
890                 }
891                 catch ( final Exception ex ) {
892                     success = false;
893                 }
894             }
895             else {
896                 success = false;
897             }
898             if ( success && ( f > 0.0 ) ) {
899                 getOptions().setPrintLineWidth( f );
900             }
901         }
902     }
903
904     private void choosePrintSize() {
905         final String s = ( String ) JOptionPane.showInputDialog( this,
906                                                                  "Please enter values for width and height,\nseparated by a comma.\n"
907                                                                          + "[current values: "
908                                                                          + getOptions().getPrintSizeX() + ", "
909                                                                          + getOptions().getPrintSizeY() + "]\n"
910                                                                          + "[A4: " + Constants.A4_SIZE_X + ", "
911                                                                          + Constants.A4_SIZE_Y + "]\n" + "[US Letter: "
912                                                                          + Constants.US_LETTER_SIZE_X + ", "
913                                                                          + Constants.US_LETTER_SIZE_Y + "]",
914                                                                  "Default Size for Graphics Export",
915                                                                  JOptionPane.QUESTION_MESSAGE,
916                                                                  null,
917                                                                  null,
918                                                                  getOptions().getPrintSizeX() + ", "
919                                                                          + getOptions().getPrintSizeY() );
920         if ( !ForesterUtil.isEmpty( s ) && ( s.indexOf( ',' ) > 0 ) ) {
921             boolean success = true;
922             int x = 0;
923             int y = 0;
924             final String[] str_ary = s.split( "," );
925             if ( str_ary.length == 2 ) {
926                 final String x_str = str_ary[ 0 ].trim();
927                 final String y_str = str_ary[ 1 ].trim();
928                 if ( !ForesterUtil.isEmpty( x_str ) && !ForesterUtil.isEmpty( y_str ) ) {
929                     try {
930                         x = Integer.parseInt( x_str );
931                         y = Integer.parseInt( y_str );
932                     }
933                     catch ( final Exception ex ) {
934                         success = false;
935                     }
936                 }
937                 else {
938                     success = false;
939                 }
940             }
941             else {
942                 success = false;
943             }
944             if ( success && ( x > 1 ) && ( y > 1 ) ) {
945                 getOptions().setPrintSizeX( x );
946                 getOptions().setPrintSizeY( y );
947             }
948         }
949     }
950
951     @Override
952     void close() {
953         if ( isUnsavedDataPresent() ) {
954             final int r = JOptionPane.showConfirmDialog( this,
955                                                          "Exit despite potentially unsaved changes?",
956                                                          "Exit?",
957                                                          JOptionPane.YES_NO_OPTION );
958             if ( r != JOptionPane.YES_OPTION ) {
959                 return;
960             }
961         }
962         exit();
963     }
964
965     private void closeCurrentPane() {
966         if ( getMainPanel().getCurrentTreePanel() != null ) {
967             if ( getMainPanel().getCurrentTreePanel().isEdited() ) {
968                 final int r = JOptionPane.showConfirmDialog( this,
969                                                              "Close tab despite potentially unsaved changes?",
970                                                              "Close Tab?",
971                                                              JOptionPane.YES_NO_OPTION );
972                 if ( r != JOptionPane.YES_OPTION ) {
973                     return;
974                 }
975             }
976             getMainPanel().closeCurrentPane();
977             activateSaveAllIfNeeded();
978         }
979     }
980
981     private void collapse( final Phylogeny phy, final double m ) {
982         final PhylogenyNodeIterator it = phy.iteratorPostorder();
983         final List<PhylogenyNode> to_be_removed = new ArrayList<PhylogenyNode>();
984         double min_support = Double.MAX_VALUE;
985         boolean conf_present = false;
986         while ( it.hasNext() ) {
987             final PhylogenyNode n = it.next();
988             if ( !n.isExternal() && !n.isRoot() ) {
989                 final List<Confidence> c = n.getBranchData().getConfidences();
990                 if ( ( c != null ) && ( c.size() > 0 ) ) {
991                     conf_present = true;
992                     double max = 0;
993                     for( final Confidence confidence : c ) {
994                         if ( confidence.getValue() > max ) {
995                             max = confidence.getValue();
996                         }
997                     }
998                     if ( max < getMinNotCollapseConfidenceValue() ) {
999                         to_be_removed.add( n );
1000                     }
1001                     if ( max < min_support ) {
1002                         min_support = max;
1003                     }
1004                 }
1005             }
1006         }
1007         if ( conf_present ) {
1008             for( final PhylogenyNode node : to_be_removed ) {
1009                 PhylogenyMethods.removeNode( node, phy );
1010             }
1011             if ( to_be_removed.size() > 0 ) {
1012                 phy.externalNodesHaveChanged();
1013                 phy.hashIDs();
1014                 phy.recalculateNumberOfExternalDescendants( true );
1015                 getCurrentTreePanel().resetNodeIdToDistToLeafMap();
1016                 getCurrentTreePanel().setEdited( true );
1017                 getCurrentTreePanel().repaint();
1018             }
1019             if ( to_be_removed.size() > 0 ) {
1020                 JOptionPane.showMessageDialog( this, "Collapsed " + to_be_removed.size()
1021                         + " branches with\nconfidence values below " + getMinNotCollapseConfidenceValue(), "Collapsed "
1022                         + to_be_removed.size() + " branches", JOptionPane.INFORMATION_MESSAGE );
1023             }
1024             else {
1025                 JOptionPane.showMessageDialog( this, "No branch collapsed,\nminimum confidence value per branch is "
1026                         + min_support, "No branch collapsed", JOptionPane.INFORMATION_MESSAGE );
1027             }
1028         }
1029         else {
1030             JOptionPane.showMessageDialog( this,
1031                                            "No branch collapsed because no confidence values present",
1032                                            "No confidence values present",
1033                                            JOptionPane.INFORMATION_MESSAGE );
1034         }
1035     }
1036
1037     private void collapseBelowThreshold() {
1038         if ( getCurrentTreePanel() != null ) {
1039             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1040             if ( ( phy != null ) && !phy.isEmpty() ) {
1041                 final String s = ( String ) JOptionPane.showInputDialog( this,
1042                                                                          "Please enter the minimum confidence value\n",
1043                                                                          "Minimal Confidence Value",
1044                                                                          JOptionPane.QUESTION_MESSAGE,
1045                                                                          null,
1046                                                                          null,
1047                                                                          getMinNotCollapseConfidenceValue() );
1048                 if ( !ForesterUtil.isEmpty( s ) ) {
1049                     boolean success = true;
1050                     double m = 0.0;
1051                     final String m_str = s.trim();
1052                     if ( !ForesterUtil.isEmpty( m_str ) ) {
1053                         try {
1054                             m = Double.parseDouble( m_str );
1055                         }
1056                         catch ( final Exception ex ) {
1057                             success = false;
1058                         }
1059                     }
1060                     else {
1061                         success = false;
1062                     }
1063                     if ( success && ( m >= 0.0 ) ) {
1064                         setMinNotCollapseConfidenceValue( m );
1065                         collapse( phy, m );
1066                     }
1067                 }
1068             }
1069         }
1070     }
1071
1072     private PhyloXmlParser createPhyloXmlParser() {
1073         PhyloXmlParser xml_parser = null;
1074         if ( getConfiguration().isValidatePhyloXmlAgainstSchema() ) {
1075             try {
1076                 xml_parser = PhyloXmlParser.createPhyloXmlParserXsdValidating();
1077             }
1078             catch ( final Exception e ) {
1079                 JOptionPane.showMessageDialog( this,
1080                                                e.getLocalizedMessage(),
1081                                                "failed to create validating XML parser",
1082                                                JOptionPane.WARNING_MESSAGE );
1083             }
1084         }
1085         if ( xml_parser == null ) {
1086             xml_parser = new PhyloXmlParser();
1087         }
1088         return xml_parser;
1089     }
1090
1091     void executeGSDI() {
1092         if ( !isOKforSDI( false, true ) ) {
1093             return;
1094         }
1095         if ( !_mainpanel.getCurrentPhylogeny().isRooted() ) {
1096             JOptionPane.showMessageDialog( this,
1097                                            "Gene tree is not rooted.",
1098                                            "Cannot execute GSDI",
1099                                            JOptionPane.ERROR_MESSAGE );
1100             return;
1101         }
1102         final Phylogeny gene_tree = _mainpanel.getCurrentPhylogeny().copy();
1103         gene_tree.setAllNodesToNotCollapse();
1104         gene_tree.recalculateNumberOfExternalDescendants( false );
1105         GSDI gsdi = null;
1106         int duplications = -1;
1107         try {
1108             gsdi = new GSDI( gene_tree, _species_tree.copy(), true );
1109             duplications = gsdi.getDuplicationsSum();
1110         }
1111         catch ( final Exception e ) {
1112             JOptionPane.showMessageDialog( this, e.toString(), "Error during GSDI", JOptionPane.ERROR_MESSAGE );
1113         }
1114         gene_tree.setRerootable( false );
1115         _mainpanel.getCurrentTreePanel().setTree( gene_tree );
1116         getControlPanel().setShowEvents( true );
1117         showWhole();
1118         _mainpanel.getCurrentTreePanel().setEdited( true );
1119         JOptionPane.showMessageDialog( this,
1120                                        "Number of duplications: " + duplications,
1121                                        "GSDI successfully completed",
1122                                        JOptionPane.INFORMATION_MESSAGE );
1123     }
1124
1125     void executeFunctionAnalysis() {
1126         if ( ( _mainpanel.getCurrentPhylogeny() == null ) || ( _mainpanel.getCurrentPhylogeny().isEmpty() ) ) {
1127             return;
1128         }
1129         final GoAnnotation a = new GoAnnotation( this,
1130                                                  _mainpanel.getCurrentTreePanel(),
1131                                                  _mainpanel.getCurrentPhylogeny() );
1132         new Thread( a ).start();
1133     }
1134
1135     void executeLineageInference() {
1136         if ( ( _mainpanel.getCurrentPhylogeny() == null ) || ( _mainpanel.getCurrentPhylogeny().isEmpty() ) ) {
1137             return;
1138         }
1139         if ( !_mainpanel.getCurrentPhylogeny().isRooted() ) {
1140             JOptionPane.showMessageDialog( this,
1141                                            "Phylogeny is not rooted.",
1142                                            "Cannot infer ancestral taxonomies",
1143                                            JOptionPane.ERROR_MESSAGE );
1144             return;
1145         }
1146         final Phylogeny phy = _mainpanel.getCurrentPhylogeny().copy();
1147         final AncestralTaxonomyInferrer inferrer = new AncestralTaxonomyInferrer( this,
1148                                                                                   _mainpanel.getCurrentTreePanel(),
1149                                                                                   phy );
1150         new Thread( inferrer ).start();
1151     }
1152
1153     private void executePhyleneticInference( final boolean from_unaligned_seqs ) {
1154         final PhyloInferenceDialog dialog = new PhyloInferenceDialog( this,
1155                                                                       getPhylogeneticInferenceOptions(),
1156                                                                       from_unaligned_seqs );
1157         dialog.activate();
1158         if ( dialog.getValue() == JOptionPane.OK_OPTION ) {
1159             if ( !from_unaligned_seqs ) {
1160                 if ( getMsa() != null ) {
1161                     final PhylogeneticInferrer inferrer = new PhylogeneticInferrer( getMsa(),
1162                                                                                     getPhylogeneticInferenceOptions()
1163                                                                                             .copy(), this );
1164                     new Thread( inferrer ).start();
1165                 }
1166                 else {
1167                     JOptionPane.showMessageDialog( this,
1168                                                    "No multiple sequence alignment selected",
1169                                                    "Phylogenetic Inference Not Launched",
1170                                                    JOptionPane.WARNING_MESSAGE );
1171                 }
1172             }
1173             else {
1174                 if ( getSeqs() != null ) {
1175                     final PhylogeneticInferrer inferrer = new PhylogeneticInferrer( getSeqs(),
1176                                                                                     getPhylogeneticInferenceOptions()
1177                                                                                             .copy(), this );
1178                     new Thread( inferrer ).start();
1179                 }
1180                 else {
1181                     JOptionPane.showMessageDialog( this,
1182                                                    "No input sequences selected",
1183                                                    "Phylogenetic Inference Not Launched",
1184                                                    JOptionPane.WARNING_MESSAGE );
1185                 }
1186             }
1187         }
1188     }
1189
1190     void executeSDI() {
1191         if ( !isOKforSDI( true, true ) ) {
1192             return;
1193         }
1194         if ( !_mainpanel.getCurrentPhylogeny().isRooted() ) {
1195             JOptionPane.showMessageDialog( this,
1196                                            "Gene tree is not rooted",
1197                                            "Cannot execute SDI",
1198                                            JOptionPane.ERROR_MESSAGE );
1199             return;
1200         }
1201         final Phylogeny gene_tree = _mainpanel.getCurrentPhylogeny().copy();
1202         gene_tree.setAllNodesToNotCollapse();
1203         gene_tree.recalculateNumberOfExternalDescendants( false );
1204         SDI sdi = null;
1205         int duplications = -1;
1206         try {
1207             sdi = new SDIse( gene_tree, _species_tree.copy() );
1208             duplications = sdi.getDuplicationsSum();
1209         }
1210         catch ( final Exception e ) {
1211             JOptionPane.showMessageDialog( this, e.toString(), "Error during SDI", JOptionPane.ERROR_MESSAGE );
1212         }
1213         gene_tree.setRerootable( false );
1214         _mainpanel.getCurrentTreePanel().setTree( gene_tree );
1215         getControlPanel().setShowEvents( true );
1216         showWhole();
1217         _mainpanel.getCurrentTreePanel().setEdited( true );
1218         JOptionPane.showMessageDialog( this,
1219                                        "Number of duplications: " + duplications,
1220                                        "SDI successfully completed",
1221                                        JOptionPane.INFORMATION_MESSAGE );
1222     }
1223
1224     void executeSDIR( final boolean minimize_cost ) {
1225         if ( !isOKforSDI( true, true ) ) {
1226             return;
1227         }
1228         Phylogeny gene_tree = _mainpanel.getCurrentPhylogeny().copy();
1229         final SDIR sdiunrooted = new SDIR();
1230         gene_tree.setAllNodesToNotCollapse();
1231         gene_tree.recalculateNumberOfExternalDescendants( false );
1232         try {
1233             gene_tree = sdiunrooted.infer( gene_tree, _species_tree, minimize_cost, // minimize cost
1234                                            !minimize_cost, // minimize sum of dups
1235                                            true, // minimize height
1236                                            true, // return tree(s)
1237                                            1 )[ 0 ]; // # of trees to return
1238         }
1239         catch ( final Exception e ) {
1240             JOptionPane.showMessageDialog( this, e.toString(), "Error during SDIR", JOptionPane.ERROR_MESSAGE );
1241             return;
1242         }
1243         final int duplications = sdiunrooted.getMinimalDuplications();
1244         gene_tree.setRerootable( false );
1245         _mainpanel.getCurrentTreePanel().setTree( gene_tree );
1246         getControlPanel().setShowEvents( true );
1247         showWhole();
1248         _mainpanel.getCurrentTreePanel().setEdited( true );
1249         JOptionPane.showMessageDialog( this,
1250                                        "Number of duplications: " + duplications,
1251                                        "SDIR successfully completed",
1252                                        JOptionPane.INFORMATION_MESSAGE );
1253     }
1254
1255     void exit() {
1256         removeTextFrame();
1257         _mainpanel.terminate();
1258         _contentpane.removeAll();
1259         setVisible( false );
1260         dispose();
1261         System.exit( 0 );
1262     }
1263
1264     private void extractTaxCodeFromNodeNames() {
1265         if ( getCurrentTreePanel() != null ) {
1266             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1267             if ( ( phy != null ) && !phy.isEmpty() ) {
1268                 final PhylogenyNodeIterator it = phy.iteratorPostorder();
1269                 while ( it.hasNext() ) {
1270                     final PhylogenyNode n = it.next();
1271                     final String name = n.getName().trim();
1272                     if ( !ForesterUtil.isEmpty( name ) ) {
1273                         final String code = ForesterUtil.extractTaxonomyCodeFromNodeName( name,
1274                                                                                           false,
1275                                                                                           TAXONOMY_EXTRACTION.YES );
1276                         if ( !ForesterUtil.isEmpty( code ) ) {
1277                             PhylogenyMethods.setTaxonomyCode( n, code );
1278                         }
1279                     }
1280                 }
1281             }
1282         }
1283     }
1284
1285     private ControlPanel getControlPanel() {
1286         return getMainPanel().getControlPanel();
1287     }
1288
1289     private File getCurrentDir() {
1290         if ( ( _current_dir == null ) || !_current_dir.canRead() ) {
1291             if ( ForesterUtil.OS_NAME.toLowerCase().indexOf( "win" ) > -1 ) {
1292                 try {
1293                     _current_dir = new File( WindowsUtils.getCurrentUserDesktopPath() );
1294                 }
1295                 catch ( final Exception e ) {
1296                     _current_dir = null;
1297                 }
1298             }
1299         }
1300         if ( ( _current_dir == null ) || !_current_dir.canRead() ) {
1301             if ( System.getProperty( "user.home" ) != null ) {
1302                 _current_dir = new File( System.getProperty( "user.home" ) );
1303             }
1304             else if ( System.getProperty( "user.dir" ) != null ) {
1305                 _current_dir = new File( System.getProperty( "user.dir" ) );
1306             }
1307         }
1308         return _current_dir;
1309     }
1310
1311     @Override
1312     public MainPanel getMainPanel() {
1313         return _mainpanel;
1314     }
1315
1316     private double getMinNotCollapseConfidenceValue() {
1317         return _min_not_collapse;
1318     }
1319
1320     boolean isOKforSDI( final boolean species_tree_has_to_binary, final boolean gene_tree_has_to_binary ) {
1321         if ( ( _mainpanel.getCurrentPhylogeny() == null ) || _mainpanel.getCurrentPhylogeny().isEmpty() ) {
1322             return false;
1323         }
1324         else if ( ( _species_tree == null ) || _species_tree.isEmpty() ) {
1325             JOptionPane.showMessageDialog( this,
1326                                            "No species tree loaded",
1327                                            "Cannot execute SDI",
1328                                            JOptionPane.ERROR_MESSAGE );
1329             return false;
1330         }
1331         else if ( species_tree_has_to_binary && !_species_tree.isCompletelyBinary() ) {
1332             JOptionPane.showMessageDialog( this,
1333                                            "Species tree is not completely binary",
1334                                            "Cannot execute SDI",
1335                                            JOptionPane.ERROR_MESSAGE );
1336             return false;
1337         }
1338         else if ( gene_tree_has_to_binary && !_mainpanel.getCurrentPhylogeny().isCompletelyBinary() ) {
1339             JOptionPane.showMessageDialog( this,
1340                                            "Gene tree is not completely binary",
1341                                            "Cannot execute SDI",
1342                                            JOptionPane.ERROR_MESSAGE );
1343             return false;
1344         }
1345         else {
1346             return true;
1347         }
1348     }
1349
1350     private boolean isUnsavedDataPresent() {
1351         final List<TreePanel> tps = getMainPanel().getTreePanels();
1352         for( final TreePanel tp : tps ) {
1353             if ( tp.isEdited() ) {
1354                 return true;
1355             }
1356         }
1357         return false;
1358     }
1359
1360     private void moveNodeNamesToSeqNames() {
1361         if ( getCurrentTreePanel() != null ) {
1362             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1363             if ( ( phy != null ) && !phy.isEmpty() ) {
1364                 ForesterUtil.transferNodeNameToField( phy, PhylogenyNodeField.SEQUENCE_NAME );
1365             }
1366         }
1367     }
1368
1369     private void moveNodeNamesToTaxSn() {
1370         if ( getCurrentTreePanel() != null ) {
1371             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1372             if ( ( phy != null ) && !phy.isEmpty() ) {
1373                 ForesterUtil.transferNodeNameToField( phy, PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME );
1374             }
1375         }
1376     }
1377
1378     private void newTree() {
1379         final Phylogeny[] phys = new Phylogeny[ 1 ];
1380         final Phylogeny phy = new Phylogeny();
1381         final PhylogenyNode node = new PhylogenyNode();
1382         phy.setRoot( node );
1383         phy.setRooted( true );
1384         phys[ 0 ] = phy;
1385         Util.addPhylogeniesToTabs( phys, "", "", getConfiguration(), getMainPanel() );
1386         _mainpanel.getControlPanel().showWhole();
1387         _mainpanel.getCurrentTreePanel().setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
1388         _mainpanel.getOptions().setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
1389         if ( getMainPanel().getMainFrame() == null ) {
1390             // Must be "E" applet version.
1391             ( ( ArchaeopteryxE ) ( ( MainPanelApplets ) getMainPanel() ).getApplet() )
1392                     .setSelectedTypeInTypeMenu( PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
1393         }
1394         else {
1395             getMainPanel().getMainFrame().setSelectedTypeInTypeMenu( PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
1396         }
1397         activateSaveAllIfNeeded();
1398         System.gc();
1399     }
1400
1401     private void obtainDetailedTaxonomicInformation() {
1402         if ( getCurrentTreePanel() != null ) {
1403             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1404             if ( ( phy != null ) && !phy.isEmpty() ) {
1405                 final TaxonomyDataObtainer t = new TaxonomyDataObtainer( this,
1406                                                                          _mainpanel.getCurrentTreePanel(),
1407                                                                          phy.copy() );
1408                 new Thread( t ).start();
1409             }
1410         }
1411     }
1412
1413     private void obtainUniProtSequenceInformation() {
1414         if ( getCurrentTreePanel() != null ) {
1415             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1416             if ( ( phy != null ) && !phy.isEmpty() ) {
1417                 final SequenceDataRetriver u = new SequenceDataRetriver( this,
1418                                                                          _mainpanel.getCurrentTreePanel(),
1419                                                                          phy.copy() );
1420                 new Thread( u ).start();
1421             }
1422         }
1423     }
1424
1425     private void print() {
1426         if ( ( getCurrentTreePanel() == null ) || ( getCurrentTreePanel().getPhylogeny() == null )
1427                 || getCurrentTreePanel().getPhylogeny().isEmpty() ) {
1428             return;
1429         }
1430         if ( !getOptions().isPrintUsingActualSize() ) {
1431             getCurrentTreePanel().setParametersForPainting( getOptions().getPrintSizeX() - 80,
1432                                                             getOptions().getPrintSizeY() - 140,
1433                                                             true );
1434             getCurrentTreePanel().resetPreferredSize();
1435             getCurrentTreePanel().repaint();
1436         }
1437         final String job_name = Constants.PRG_NAME;
1438         boolean error = false;
1439         String printer_name = null;
1440         try {
1441             printer_name = Printer.print( getCurrentTreePanel(), job_name );
1442         }
1443         catch ( final Exception e ) {
1444             error = true;
1445             JOptionPane.showMessageDialog( this, e.getMessage(), "Printing Error", JOptionPane.ERROR_MESSAGE );
1446         }
1447         if ( !error && ( printer_name != null ) ) {
1448             String msg = "Printing data sent to printer";
1449             if ( printer_name.length() > 1 ) {
1450                 msg += " [" + printer_name + "]";
1451             }
1452             JOptionPane.showMessageDialog( this, msg, "Printing...", JOptionPane.INFORMATION_MESSAGE );
1453         }
1454         if ( !getOptions().isPrintUsingActualSize() ) {
1455             getControlPanel().showWhole();
1456         }
1457     }
1458
1459     private void printPhylogenyToPdf( final String file_name ) {
1460         if ( !getOptions().isPrintUsingActualSize() ) {
1461             getCurrentTreePanel().setParametersForPainting( getOptions().getPrintSizeX(),
1462                                                             getOptions().getPrintSizeY(),
1463                                                             true );
1464             getCurrentTreePanel().resetPreferredSize();
1465             getCurrentTreePanel().repaint();
1466         }
1467         String pdf_written_to = "";
1468         boolean error = false;
1469         try {
1470             if ( getOptions().isPrintUsingActualSize() ) {
1471                 pdf_written_to = PdfExporter.writePhylogenyToPdf( file_name,
1472                                                                   getCurrentTreePanel(),
1473                                                                   getCurrentTreePanel().getWidth(),
1474                                                                   getCurrentTreePanel().getHeight() );
1475             }
1476             else {
1477                 pdf_written_to = PdfExporter.writePhylogenyToPdf( file_name, getCurrentTreePanel(), getOptions()
1478                         .getPrintSizeX(), getOptions().getPrintSizeY() );
1479             }
1480         }
1481         catch ( final IOException e ) {
1482             error = true;
1483             JOptionPane.showMessageDialog( this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE );
1484         }
1485         if ( !error ) {
1486             if ( !ForesterUtil.isEmpty( pdf_written_to ) ) {
1487                 JOptionPane.showMessageDialog( this,
1488                                                "Wrote PDF to: " + pdf_written_to,
1489                                                "Information",
1490                                                JOptionPane.INFORMATION_MESSAGE );
1491             }
1492             else {
1493                 JOptionPane.showMessageDialog( this,
1494                                                "There was an unknown problem when attempting to write to PDF file: \""
1495                                                        + file_name + "\"",
1496                                                "Error",
1497                                                JOptionPane.ERROR_MESSAGE );
1498             }
1499         }
1500         if ( !getOptions().isPrintUsingActualSize() ) {
1501             getControlPanel().showWhole();
1502         }
1503     }
1504
1505     private void addExpressionValuesFromFile() {
1506         if ( ( getCurrentTreePanel() == null ) || ( getCurrentTreePanel().getPhylogeny() == null ) ) {
1507             JOptionPane.showMessageDialog( this,
1508                                            "Need to load evolutionary tree first",
1509                                            "Can Not Read Expression Values",
1510                                            JOptionPane.WARNING_MESSAGE );
1511             return;
1512         }
1513         final File my_dir = getCurrentDir();
1514         if ( my_dir != null ) {
1515             _values_filechooser.setCurrentDirectory( my_dir );
1516         }
1517         final int result = _values_filechooser.showOpenDialog( _contentpane );
1518         final File file = _values_filechooser.getSelectedFile();
1519         if ( ( file != null ) && ( file.length() > 0 ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
1520             BasicTable<String> t = null;
1521             try {
1522                 t = BasicTableParser.parse( file, "\t" );
1523                 if ( t.getNumberOfColumns() < 2 ) {
1524                     t = BasicTableParser.parse( file, "," );
1525                 }
1526                 if ( t.getNumberOfColumns() < 2 ) {
1527                     t = BasicTableParser.parse( file, " " );
1528                 }
1529             }
1530             catch ( final IOException e ) {
1531                 JOptionPane.showMessageDialog( this,
1532                                                e.getMessage(),
1533                                                "Could Not Read Expression Value Table",
1534                                                JOptionPane.ERROR_MESSAGE );
1535                 return;
1536             }
1537             if ( t.getNumberOfColumns() < 2 ) {
1538                 JOptionPane.showMessageDialog( this,
1539                                                "Table contains " + t.getNumberOfColumns() + " column(s)",
1540                                                "Problem with Expression Value Table",
1541                                                JOptionPane.ERROR_MESSAGE );
1542                 return;
1543             }
1544             if ( t.getNumberOfRows() < 1 ) {
1545                 JOptionPane.showMessageDialog( this,
1546                                                "Table contains zero rows",
1547                                                "Problem with Expression Value Table",
1548                                                JOptionPane.ERROR_MESSAGE );
1549                 return;
1550             }
1551             final Phylogeny phy = getCurrentTreePanel().getPhylogeny();
1552             if ( t.getNumberOfRows() != phy.getNumberOfExternalNodes() ) {
1553                 JOptionPane.showMessageDialog( this,
1554                                                "Table contains " + t.getNumberOfRows() + " rows, but tree contains "
1555                                                        + phy.getNumberOfExternalNodes() + " external nodes",
1556                                                "Warning",
1557                                                JOptionPane.WARNING_MESSAGE );
1558             }
1559             final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
1560             int not_found = 0;
1561             for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
1562                 final PhylogenyNode node = iter.next();
1563                 final String node_name = node.getName();
1564                 if ( !ForesterUtil.isEmpty( node_name ) ) {
1565                     int row = -1;
1566                     try {
1567                         row = t.findRow( node_name );
1568                     }
1569                     catch ( final IllegalArgumentException e ) {
1570                         JOptionPane
1571                                 .showMessageDialog( this,
1572                                                     e.getMessage(),
1573                                                     "Error Mapping Node Identifiers to Expression Value Identifiers",
1574                                                     JOptionPane.ERROR_MESSAGE );
1575                         return;
1576                     }
1577                     if ( row < 0 ) {
1578                         if ( node.isExternal() ) {
1579                             not_found++;
1580                         }
1581                         continue;
1582                     }
1583                     final List<Double> l = new ArrayList<Double>();
1584                     for( int col = 1; col < t.getNumberOfColumns(); ++col ) {
1585                         double d = -100;
1586                         try {
1587                             d = Double.parseDouble( t.getValueAsString( col, row ) );
1588                         }
1589                         catch ( final NumberFormatException e ) {
1590                             JOptionPane.showMessageDialog( this,
1591                                                            "Could not parse \"" + t.getValueAsString( col, row )
1592                                                                    + "\" into a decimal value",
1593                                                            "Issue with Expression Value Table",
1594                                                            JOptionPane.ERROR_MESSAGE );
1595                             return;
1596                         }
1597                         stats.addValue( d );
1598                         l.add( d );
1599                     }
1600                     if ( !l.isEmpty() ) {
1601                         if ( node.getNodeData().getProperties() != null ) {
1602                             node.getNodeData().getProperties()
1603                                     .removePropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF );
1604                         }
1605                         node.getNodeData().setVector( l );
1606                     }
1607                 }
1608             }
1609             if ( not_found > 0 ) {
1610                 JOptionPane.showMessageDialog( this, "Could not fine expression values for " + not_found
1611                         + " external node(s)", "Warning", JOptionPane.WARNING_MESSAGE );
1612             }
1613             getCurrentTreePanel().setStatisticsForExpressionValues( stats );
1614         }
1615     }
1616
1617     private void readPhylogeniesFromFile() {
1618         boolean exception = false;
1619         Phylogeny[] phys = null;
1620         // Set an initial directory if none set yet
1621         final File my_dir = getCurrentDir();
1622         _open_filechooser.setMultiSelectionEnabled( true );
1623         // Open file-open dialog and set current directory
1624         if ( my_dir != null ) {
1625             _open_filechooser.setCurrentDirectory( my_dir );
1626         }
1627         final int result = _open_filechooser.showOpenDialog( _contentpane );
1628         // All done: get the file
1629         final File[] files = _open_filechooser.getSelectedFiles();
1630         setCurrentDir( _open_filechooser.getCurrentDirectory() );
1631         boolean nhx_or_nexus = false;
1632         if ( ( files != null ) && ( files.length > 0 ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
1633             for( final File file : files ) {
1634                 if ( ( file != null ) && !file.isDirectory() ) {
1635                     if ( _mainpanel.getCurrentTreePanel() != null ) {
1636                         _mainpanel.getCurrentTreePanel().setWaitCursor();
1637                     }
1638                     else {
1639                         _mainpanel.setWaitCursor();
1640                     }
1641                     if ( ( _open_filechooser.getFileFilter() == MainFrameApplication.nhfilter )
1642                             || ( _open_filechooser.getFileFilter() == MainFrameApplication.nhxfilter ) ) {
1643                         try {
1644                             final NHXParser nhx = new NHXParser();
1645                             setSpecialOptionsForNhxParser( nhx );
1646                             phys = ForesterUtil.readPhylogenies( nhx, file );
1647                             nhx_or_nexus = true;
1648                         }
1649                         catch ( final Exception e ) {
1650                             exception = true;
1651                             exceptionOccuredDuringOpenFile( e );
1652                         }
1653                     }
1654                     else if ( _open_filechooser.getFileFilter() == MainFrameApplication.xmlfilter ) {
1655                         warnIfNotPhyloXmlValidation( getConfiguration() );
1656                         try {
1657                             final PhyloXmlParser xml_parser = createPhyloXmlParser();
1658                             phys = ForesterUtil.readPhylogenies( xml_parser, file );
1659                         }
1660                         catch ( final Exception e ) {
1661                             exception = true;
1662                             exceptionOccuredDuringOpenFile( e );
1663                         }
1664                     }
1665                     else if ( _open_filechooser.getFileFilter() == MainFrameApplication.tolfilter ) {
1666                         try {
1667                             phys = ForesterUtil.readPhylogenies( new TolParser(), file );
1668                         }
1669                         catch ( final Exception e ) {
1670                             exception = true;
1671                             exceptionOccuredDuringOpenFile( e );
1672                         }
1673                     }
1674                     else if ( _open_filechooser.getFileFilter() == MainFrameApplication.nexusfilter ) {
1675                         try {
1676                             final NexusPhylogeniesParser nex = new NexusPhylogeniesParser();
1677                             setSpecialOptionsForNexParser( nex );
1678                             phys = ForesterUtil.readPhylogenies( nex, file );
1679                             nhx_or_nexus = true;
1680                         }
1681                         catch ( final Exception e ) {
1682                             exception = true;
1683                             exceptionOccuredDuringOpenFile( e );
1684                         }
1685                     }
1686                     // "*.*":
1687                     else {
1688                         try {
1689                             final PhylogenyParser parser = ForesterUtil
1690                                     .createParserDependingOnFileType( file, getConfiguration()
1691                                             .isValidatePhyloXmlAgainstSchema() );
1692                             if ( parser instanceof NexusPhylogeniesParser ) {
1693                                 final NexusPhylogeniesParser nex = ( NexusPhylogeniesParser ) parser;
1694                                 setSpecialOptionsForNexParser( nex );
1695                                 nhx_or_nexus = true;
1696                             }
1697                             else if ( parser instanceof NHXParser ) {
1698                                 final NHXParser nhx = ( NHXParser ) parser;
1699                                 setSpecialOptionsForNhxParser( nhx );
1700                                 nhx_or_nexus = true;
1701                             }
1702                             else if ( parser instanceof PhyloXmlParser ) {
1703                                 warnIfNotPhyloXmlValidation( getConfiguration() );
1704                             }
1705                             phys = ForesterUtil.readPhylogenies( parser, file );
1706                         }
1707                         catch ( final Exception e ) {
1708                             exception = true;
1709                             exceptionOccuredDuringOpenFile( e );
1710                         }
1711                     }
1712                     if ( _mainpanel.getCurrentTreePanel() != null ) {
1713                         _mainpanel.getCurrentTreePanel().setArrowCursor();
1714                     }
1715                     else {
1716                         _mainpanel.setArrowCursor();
1717                     }
1718                     if ( !exception && ( phys != null ) && ( phys.length > 0 ) ) {
1719                         boolean one_desc = false;
1720                         if ( nhx_or_nexus ) {
1721                             for( final Phylogeny phy : phys ) {
1722                                 if ( getOptions().isInternalNumberAreConfidenceForNhParsing() ) {
1723                                     ForesterUtil.transferInternalNodeNamesToConfidence( phy );
1724                                 }
1725                                 if ( PhylogenyMethods.getMinimumDescendentsPerInternalNodes( phy ) == 1 ) {
1726                                     one_desc = true;
1727                                     break;
1728                                 }
1729                             }
1730                         }
1731                         Util.addPhylogeniesToTabs( phys,
1732                                                    file.getName(),
1733                                                    file.getAbsolutePath(),
1734                                                    getConfiguration(),
1735                                                    getMainPanel() );
1736                         _mainpanel.getControlPanel().showWhole();
1737                         if ( nhx_or_nexus && one_desc ) {
1738                             JOptionPane
1739                                     .showMessageDialog( this,
1740                                                         "One or more trees contain (a) node(s) with one descendant, "
1741                                                                 + ForesterUtil.LINE_SEPARATOR
1742                                                                 + "possibly indicating illegal parentheses within node names.",
1743                                                         "Warning: Possible Error in New Hampshire Formatted Data",
1744                                                         JOptionPane.WARNING_MESSAGE );
1745                         }
1746                     }
1747                 }
1748             }
1749         }
1750         activateSaveAllIfNeeded();
1751         System.gc();
1752     }
1753
1754     public void readSeqsFromFile() {
1755         // Set an initial directory if none set yet
1756         final File my_dir = getCurrentDir();
1757         _seqs_filechooser.setMultiSelectionEnabled( false );
1758         // Open file-open dialog and set current directory
1759         if ( my_dir != null ) {
1760             _seqs_filechooser.setCurrentDirectory( my_dir );
1761         }
1762         final int result = _seqs_filechooser.showOpenDialog( _contentpane );
1763         // All done: get the seqs
1764         final File file = _seqs_filechooser.getSelectedFile();
1765         setCurrentDir( _seqs_filechooser.getCurrentDirectory() );
1766         if ( ( file != null ) && !file.isDirectory() && ( result == JFileChooser.APPROVE_OPTION ) ) {
1767             setSeqsFile( null );
1768             setSeqs( null );
1769             List<Sequence> seqs = null;
1770             try {
1771                 if ( FastaParser.isLikelyFasta( new FileInputStream( file ) ) ) {
1772                     seqs = FastaParser.parse( new FileInputStream( file ) );
1773                     for( final Sequence seq : seqs ) {
1774                         System.out.println( SequenceWriter.toFasta( seq, 60 ) );
1775                     }
1776                 }
1777                 else {
1778                     //TODO error
1779                 }
1780             }
1781             catch ( final MsaFormatException e ) {
1782                 try {
1783                     _mainpanel.getCurrentTreePanel().setArrowCursor();
1784                 }
1785                 catch ( final Exception ex ) {
1786                     // Do nothing.
1787                 }
1788                 JOptionPane.showMessageDialog( this,
1789                                                e.getLocalizedMessage(),
1790                                                "Multiple sequence file format error",
1791                                                JOptionPane.ERROR_MESSAGE );
1792                 return;
1793             }
1794             catch ( final IOException e ) {
1795                 try {
1796                     _mainpanel.getCurrentTreePanel().setArrowCursor();
1797                 }
1798                 catch ( final Exception ex ) {
1799                     // Do nothing.
1800                 }
1801                 JOptionPane.showMessageDialog( this,
1802                                                e.getLocalizedMessage(),
1803                                                "Failed to read multiple sequence file",
1804                                                JOptionPane.ERROR_MESSAGE );
1805                 return;
1806             }
1807             catch ( final IllegalArgumentException e ) {
1808                 try {
1809                     _mainpanel.getCurrentTreePanel().setArrowCursor();
1810                 }
1811                 catch ( final Exception ex ) {
1812                     // Do nothing.
1813                 }
1814                 JOptionPane.showMessageDialog( this,
1815                                                e.getLocalizedMessage(),
1816                                                "Unexpected error during reading of multiple sequence file",
1817                                                JOptionPane.ERROR_MESSAGE );
1818                 return;
1819             }
1820             catch ( final Exception e ) {
1821                 try {
1822                     _mainpanel.getCurrentTreePanel().setArrowCursor();
1823                 }
1824                 catch ( final Exception ex ) {
1825                     // Do nothing.
1826                 }
1827                 e.printStackTrace();
1828                 JOptionPane.showMessageDialog( this,
1829                                                e.getLocalizedMessage(),
1830                                                "Unexpected error during reading of multiple sequence file",
1831                                                JOptionPane.ERROR_MESSAGE );
1832                 return;
1833             }
1834             if ( ( seqs == null ) || ( seqs.size() < 1 ) ) {
1835                 JOptionPane.showMessageDialog( this,
1836                                                "Multiple sequence file is empty",
1837                                                "Illegal multiple sequence file",
1838                                                JOptionPane.ERROR_MESSAGE );
1839                 return;
1840             }
1841             if ( seqs.size() < 4 ) {
1842                 JOptionPane.showMessageDialog( this,
1843                                                "Multiple sequence file needs to contain at least 3 sequences",
1844                                                "Illegal multiple sequence file",
1845                                                JOptionPane.ERROR_MESSAGE );
1846                 return;
1847             }
1848             //  if ( msa.getLength() < 2 ) {
1849             //       JOptionPane.showMessageDialog( this,
1850             //                                      "Multiple sequence alignment needs to contain at least 2 residues",
1851             //                                      "Illegal multiple sequence file",
1852             //                                      JOptionPane.ERROR_MESSAGE );
1853             //       return;
1854             //   }
1855             System.gc();
1856             setSeqsFile( _seqs_filechooser.getSelectedFile() );
1857             setSeqs( seqs );
1858         }
1859     }
1860
1861     public void readMsaFromFile() {
1862         // Set an initial directory if none set yet
1863         final File my_dir = getCurrentDir();
1864         _msa_filechooser.setMultiSelectionEnabled( false );
1865         // Open file-open dialog and set current directory
1866         if ( my_dir != null ) {
1867             _msa_filechooser.setCurrentDirectory( my_dir );
1868         }
1869         final int result = _msa_filechooser.showOpenDialog( _contentpane );
1870         // All done: get the msa
1871         final File file = _msa_filechooser.getSelectedFile();
1872         setCurrentDir( _msa_filechooser.getCurrentDirectory() );
1873         if ( ( file != null ) && !file.isDirectory() && ( result == JFileChooser.APPROVE_OPTION ) ) {
1874             setMsaFile( null );
1875             setMsa( null );
1876             Msa msa = null;
1877             try {
1878                 if ( FastaParser.isLikelyFasta( new FileInputStream( file ) ) ) {
1879                     msa = FastaParser.parseMsa( new FileInputStream( file ) );
1880                     System.out.println( msa.toString() );
1881                 }
1882                 else {
1883                     msa = GeneralMsaParser.parse( new FileInputStream( file ) );
1884                 }
1885             }
1886             catch ( final MsaFormatException e ) {
1887                 try {
1888                     _mainpanel.getCurrentTreePanel().setArrowCursor();
1889                 }
1890                 catch ( final Exception ex ) {
1891                     // Do nothing.
1892                 }
1893                 JOptionPane.showMessageDialog( this,
1894                                                e.getLocalizedMessage(),
1895                                                "Multiple sequence alignment format error",
1896                                                JOptionPane.ERROR_MESSAGE );
1897                 return;
1898             }
1899             catch ( final IOException e ) {
1900                 try {
1901                     _mainpanel.getCurrentTreePanel().setArrowCursor();
1902                 }
1903                 catch ( final Exception ex ) {
1904                     // Do nothing.
1905                 }
1906                 JOptionPane.showMessageDialog( this,
1907                                                e.getLocalizedMessage(),
1908                                                "Failed to read multiple sequence alignment",
1909                                                JOptionPane.ERROR_MESSAGE );
1910                 return;
1911             }
1912             catch ( final IllegalArgumentException e ) {
1913                 try {
1914                     _mainpanel.getCurrentTreePanel().setArrowCursor();
1915                 }
1916                 catch ( final Exception ex ) {
1917                     // Do nothing.
1918                 }
1919                 JOptionPane.showMessageDialog( this,
1920                                                e.getLocalizedMessage(),
1921                                                "Unexpected error during reading of multiple sequence alignment",
1922                                                JOptionPane.ERROR_MESSAGE );
1923                 return;
1924             }
1925             catch ( final Exception e ) {
1926                 try {
1927                     _mainpanel.getCurrentTreePanel().setArrowCursor();
1928                 }
1929                 catch ( final Exception ex ) {
1930                     // Do nothing.
1931                 }
1932                 e.printStackTrace();
1933                 JOptionPane.showMessageDialog( this,
1934                                                e.getLocalizedMessage(),
1935                                                "Unexpected error during reading of multiple sequence alignment",
1936                                                JOptionPane.ERROR_MESSAGE );
1937                 return;
1938             }
1939             if ( ( msa == null ) || ( msa.getNumberOfSequences() < 1 ) ) {
1940                 JOptionPane.showMessageDialog( this,
1941                                                "Multiple sequence alignment is empty",
1942                                                "Illegal Multiple Sequence Alignment",
1943                                                JOptionPane.ERROR_MESSAGE );
1944                 return;
1945             }
1946             if ( msa.getNumberOfSequences() < 4 ) {
1947                 JOptionPane.showMessageDialog( this,
1948                                                "Multiple sequence alignment needs to contain at least 3 sequences",
1949                                                "Illegal multiple sequence alignment",
1950                                                JOptionPane.ERROR_MESSAGE );
1951                 return;
1952             }
1953             if ( msa.getLength() < 2 ) {
1954                 JOptionPane.showMessageDialog( this,
1955                                                "Multiple sequence alignment needs to contain at least 2 residues",
1956                                                "Illegal multiple sequence alignment",
1957                                                JOptionPane.ERROR_MESSAGE );
1958                 return;
1959             }
1960             System.gc();
1961             setMsaFile( _msa_filechooser.getSelectedFile() );
1962             setMsa( msa );
1963         }
1964     }
1965
1966     @Override
1967     void readPhylogeniesFromURL() {
1968         URL url = null;
1969         Phylogeny[] phys = null;
1970         final String message = "Please enter a complete URL, for example \"http://www.phyloxml.org/examples/apaf.xml\"";
1971         final String url_string = JOptionPane.showInputDialog( this,
1972                                                                message,
1973                                                                "Use URL/webservice to obtain a phylogeny",
1974                                                                JOptionPane.QUESTION_MESSAGE );
1975         boolean nhx_or_nexus = false;
1976         if ( ( url_string != null ) && ( url_string.length() > 0 ) ) {
1977             try {
1978                 url = new URL( url_string );
1979                 PhylogenyParser parser = null;
1980                 if ( url.getHost().toLowerCase().indexOf( "tolweb" ) >= 0 ) {
1981                     parser = new TolParser();
1982                 }
1983                 else {
1984                     parser = ForesterUtil.createParserDependingOnUrlContents( url, getConfiguration()
1985                             .isValidatePhyloXmlAgainstSchema() );
1986                 }
1987                 if ( parser instanceof NexusPhylogeniesParser ) {
1988                     nhx_or_nexus = true;
1989                 }
1990                 else if ( parser instanceof NHXParser ) {
1991                     nhx_or_nexus = true;
1992                 }
1993                 if ( _mainpanel.getCurrentTreePanel() != null ) {
1994                     _mainpanel.getCurrentTreePanel().setWaitCursor();
1995                 }
1996                 else {
1997                     _mainpanel.setWaitCursor();
1998                 }
1999                 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
2000                 phys = factory.create( url.openStream(), parser );
2001             }
2002             catch ( final MalformedURLException e ) {
2003                 JOptionPane.showMessageDialog( this,
2004                                                "Malformed URL: " + url + "\n" + e.getLocalizedMessage(),
2005                                                "Malformed URL",
2006                                                JOptionPane.ERROR_MESSAGE );
2007             }
2008             catch ( final IOException e ) {
2009                 JOptionPane.showMessageDialog( this,
2010                                                "Could not read from " + url + "\n"
2011                                                        + ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ),
2012                                                "Failed to read URL",
2013                                                JOptionPane.ERROR_MESSAGE );
2014             }
2015             catch ( final Exception e ) {
2016                 JOptionPane.showMessageDialog( this,
2017                                                ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ),
2018                                                "Unexpected Exception",
2019                                                JOptionPane.ERROR_MESSAGE );
2020             }
2021             finally {
2022                 if ( _mainpanel.getCurrentTreePanel() != null ) {
2023                     _mainpanel.getCurrentTreePanel().setArrowCursor();
2024                 }
2025                 else {
2026                     _mainpanel.setArrowCursor();
2027                 }
2028             }
2029             if ( ( phys != null ) && ( phys.length > 0 ) ) {
2030                 if ( nhx_or_nexus && getOptions().isInternalNumberAreConfidenceForNhParsing() ) {
2031                     for( final Phylogeny phy : phys ) {
2032                         ForesterUtil.transferInternalNodeNamesToConfidence( phy );
2033                     }
2034                 }
2035                 Util.addPhylogeniesToTabs( phys,
2036                                            new File( url.getFile() ).getName(),
2037                                            new File( url.getFile() ).toString(),
2038                                            getConfiguration(),
2039                                            getMainPanel() );
2040                 _mainpanel.getControlPanel().showWhole();
2041             }
2042         }
2043         activateSaveAllIfNeeded();
2044         System.gc();
2045     }
2046
2047     private void readSpeciesTreeFromFile() {
2048         Phylogeny t = null;
2049         boolean exception = false;
2050         final File my_dir = getCurrentDir();
2051         _open_filechooser_for_species_tree.setSelectedFile( new File( "" ) );
2052         if ( my_dir != null ) {
2053             _open_filechooser_for_species_tree.setCurrentDirectory( my_dir );
2054         }
2055         final int result = _open_filechooser_for_species_tree.showOpenDialog( _contentpane );
2056         final File file = _open_filechooser_for_species_tree.getSelectedFile();
2057         if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
2058             if ( _open_filechooser_for_species_tree.getFileFilter() == MainFrameApplication.xmlfilter ) {
2059                 try {
2060                     final Phylogeny[] trees = ForesterUtil.readPhylogenies( new PhyloXmlParser(), file );
2061                     t = trees[ 0 ];
2062                 }
2063                 catch ( final Exception e ) {
2064                     exception = true;
2065                     exceptionOccuredDuringOpenFile( e );
2066                 }
2067             }
2068             else if ( _open_filechooser_for_species_tree.getFileFilter() == MainFrameApplication.tolfilter ) {
2069                 try {
2070                     final Phylogeny[] trees = ForesterUtil.readPhylogenies( new TolParser(), file );
2071                     t = trees[ 0 ];
2072                 }
2073                 catch ( final Exception e ) {
2074                     exception = true;
2075                     exceptionOccuredDuringOpenFile( e );
2076                 }
2077             }
2078             // "*.*":
2079             else {
2080                 try {
2081                     final Phylogeny[] trees = ForesterUtil.readPhylogenies( new PhyloXmlParser(), file );
2082                     t = trees[ 0 ];
2083                 }
2084                 catch ( final Exception e ) {
2085                     exception = true;
2086                     exceptionOccuredDuringOpenFile( e );
2087                 }
2088             }
2089             if ( !exception && ( t != null ) && !t.isRooted() ) {
2090                 exception = true;
2091                 t = null;
2092                 JOptionPane.showMessageDialog( this,
2093                                                "Species tree is not rooted",
2094                                                "Species tree not loaded",
2095                                                JOptionPane.ERROR_MESSAGE );
2096             }
2097             if ( !exception && ( t != null ) ) {
2098                 final Set<Taxonomy> tax_set = new HashSet<Taxonomy>();
2099                 for( final PhylogenyNodeIterator it = t.iteratorExternalForward(); it.hasNext(); ) {
2100                     final PhylogenyNode node = it.next();
2101                     if ( !node.getNodeData().isHasTaxonomy() ) {
2102                         exception = true;
2103                         t = null;
2104                         JOptionPane
2105                                 .showMessageDialog( this,
2106                                                     "Species tree contains external node(s) without taxonomy information",
2107                                                     "Species tree not loaded",
2108                                                     JOptionPane.ERROR_MESSAGE );
2109                         break;
2110                     }
2111                     else {
2112                         if ( tax_set.contains( node.getNodeData().getTaxonomy() ) ) {
2113                             exception = true;
2114                             t = null;
2115                             JOptionPane.showMessageDialog( this,
2116                                                            "Taxonomy ["
2117                                                                    + node.getNodeData().getTaxonomy().asSimpleText()
2118                                                                    + "] is not unique in species tree",
2119                                                            "Species tree not loaded",
2120                                                            JOptionPane.ERROR_MESSAGE );
2121                             break;
2122                         }
2123                         else {
2124                             tax_set.add( node.getNodeData().getTaxonomy() );
2125                         }
2126                     }
2127                 }
2128             }
2129             if ( !exception && ( t != null ) ) {
2130                 _species_tree = t;
2131                 JOptionPane.showMessageDialog( this,
2132                                                "Species tree successfully loaded",
2133                                                "Species tree loaded",
2134                                                JOptionPane.INFORMATION_MESSAGE );
2135             }
2136             _contentpane.repaint();
2137             System.gc();
2138         }
2139     }
2140
2141     private void setCurrentDir( final File current_dir ) {
2142         _current_dir = current_dir;
2143     }
2144
2145     private void setMinNotCollapseConfidenceValue( final double min_not_collapse ) {
2146         _min_not_collapse = min_not_collapse;
2147     }
2148
2149     private void setSpecialOptionsForNexParser( final NexusPhylogeniesParser nex ) {
2150         nex.setReplaceUnderscores( getOptions().isReplaceUnderscoresInNhParsing() );
2151     }
2152
2153     private void setSpecialOptionsForNhxParser( final NHXParser nhx ) {
2154         nhx.setReplaceUnderscores( getOptions().isReplaceUnderscoresInNhParsing() );
2155         ForesterUtil.TAXONOMY_EXTRACTION te = ForesterUtil.TAXONOMY_EXTRACTION.NO;
2156         if ( getOptions().isExtractPfamTaxonomyCodesInNhParsing() ) {
2157             te = ForesterUtil.TAXONOMY_EXTRACTION.YES;
2158         }
2159         nhx.setTaxonomyExtraction( te );
2160     }
2161
2162     private void writeAllToFile() {
2163         if ( ( getMainPanel().getTabbedPane() == null ) || ( getMainPanel().getTabbedPane().getTabCount() < 1 ) ) {
2164             return;
2165         }
2166         final File my_dir = getCurrentDir();
2167         if ( my_dir != null ) {
2168             _save_filechooser.setCurrentDirectory( my_dir );
2169         }
2170         _save_filechooser.setSelectedFile( new File( "" ) );
2171         final int result = _save_filechooser.showSaveDialog( _contentpane );
2172         final File file = _save_filechooser.getSelectedFile();
2173         setCurrentDir( _save_filechooser.getCurrentDirectory() );
2174         if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
2175             if ( file.exists() ) {
2176                 final int i = JOptionPane.showConfirmDialog( this,
2177                                                              file + " already exists. Overwrite?",
2178                                                              "Warning",
2179                                                              JOptionPane.OK_CANCEL_OPTION,
2180                                                              JOptionPane.WARNING_MESSAGE );
2181                 if ( i != JOptionPane.OK_OPTION ) {
2182                     return;
2183                 }
2184                 else {
2185                     try {
2186                         file.delete();
2187                     }
2188                     catch ( final Exception e ) {
2189                         JOptionPane.showMessageDialog( this,
2190                                                        "Failed to delete: " + file,
2191                                                        "Error",
2192                                                        JOptionPane.WARNING_MESSAGE );
2193                     }
2194                 }
2195             }
2196             final int count = getMainPanel().getTabbedPane().getTabCount();
2197             final List<Phylogeny> trees = new ArrayList<Phylogeny>();
2198             for( int i = 0; i < count; ++i ) {
2199                 trees.add( getMainPanel().getPhylogeny( i ) );
2200                 getMainPanel().getTreePanels().get( i ).setEdited( false );
2201             }
2202             final PhylogenyWriter writer = new PhylogenyWriter();
2203             try {
2204                 writer.toPhyloXML( file, trees, 0, ForesterUtil.LINE_SEPARATOR );
2205             }
2206             catch ( final IOException e ) {
2207                 JOptionPane.showMessageDialog( this,
2208                                                "Failed to write to: " + file,
2209                                                "Error",
2210                                                JOptionPane.WARNING_MESSAGE );
2211             }
2212         }
2213     }
2214
2215     private boolean writeAsNewHampshire( final Phylogeny t, boolean exception, final File file ) {
2216         try {
2217             final PhylogenyWriter writer = new PhylogenyWriter();
2218             writer.toNewHampshire( t, false, true, file );
2219         }
2220         catch ( final Exception e ) {
2221             exception = true;
2222             exceptionOccuredDuringSaveAs( e );
2223         }
2224         return exception;
2225     }
2226
2227     private boolean writeAsNexus( final Phylogeny t, boolean exception, final File file ) {
2228         try {
2229             final PhylogenyWriter writer = new PhylogenyWriter();
2230             writer.toNexus( file, t );
2231         }
2232         catch ( final Exception e ) {
2233             exception = true;
2234             exceptionOccuredDuringSaveAs( e );
2235         }
2236         return exception;
2237     }
2238
2239     private boolean writeAsNHX( final Phylogeny t, boolean exception, final File file ) {
2240         try {
2241             final PhylogenyWriter writer = new PhylogenyWriter();
2242             writer.toNewHampshireX( t, file );
2243         }
2244         catch ( final Exception e ) {
2245             exception = true;
2246             exceptionOccuredDuringSaveAs( e );
2247         }
2248         return exception;
2249     }
2250
2251     private boolean writeAsPhyloXml( final Phylogeny t, boolean exception, final File file ) {
2252         try {
2253             final PhylogenyWriter writer = new PhylogenyWriter();
2254             writer.toPhyloXML( file, t, 0 );
2255         }
2256         catch ( final Exception e ) {
2257             exception = true;
2258             exceptionOccuredDuringSaveAs( e );
2259         }
2260         return exception;
2261     }
2262
2263     private void writePhylogenyToGraphicsFile( final String file_name, final GraphicsExportType type ) {
2264         _mainpanel.getCurrentTreePanel().setParametersForPainting( _mainpanel.getCurrentTreePanel().getWidth(),
2265                                                                    _mainpanel.getCurrentTreePanel().getHeight(),
2266                                                                    true );
2267         String file_written_to = "";
2268         boolean error = false;
2269         try {
2270             file_written_to = Util.writePhylogenyToGraphicsFile( file_name,
2271                                                                  _mainpanel.getCurrentTreePanel().getWidth(),
2272                                                                  _mainpanel.getCurrentTreePanel().getHeight(),
2273                                                                  _mainpanel.getCurrentTreePanel(),
2274                                                                  _mainpanel.getControlPanel(),
2275                                                                  type,
2276                                                                  getOptions() );
2277         }
2278         catch ( final IOException e ) {
2279             error = true;
2280             JOptionPane.showMessageDialog( this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE );
2281         }
2282         if ( !error ) {
2283             if ( ( file_written_to != null ) && ( file_written_to.length() > 0 ) ) {
2284                 JOptionPane.showMessageDialog( this,
2285                                                "Wrote image to: " + file_written_to,
2286                                                "Graphics Export",
2287                                                JOptionPane.INFORMATION_MESSAGE );
2288             }
2289             else {
2290                 JOptionPane.showMessageDialog( this,
2291                                                "There was an unknown problem when attempting to write to an image file: \""
2292                                                        + file_name + "\"",
2293                                                "Error",
2294                                                JOptionPane.ERROR_MESSAGE );
2295             }
2296         }
2297         _contentpane.repaint();
2298     }
2299
2300     private void writeToFile( final Phylogeny t ) {
2301         if ( t == null ) {
2302             return;
2303         }
2304         String initial_filename = null;
2305         if ( getMainPanel().getCurrentTreePanel().getTreeFile() != null ) {
2306             try {
2307                 initial_filename = getMainPanel().getCurrentTreePanel().getTreeFile().getCanonicalPath();
2308             }
2309             catch ( final IOException e ) {
2310                 initial_filename = null;
2311             }
2312         }
2313         if ( !ForesterUtil.isEmpty( initial_filename ) ) {
2314             _save_filechooser.setSelectedFile( new File( initial_filename ) );
2315         }
2316         else {
2317             _save_filechooser.setSelectedFile( new File( "" ) );
2318         }
2319         final File my_dir = getCurrentDir();
2320         if ( my_dir != null ) {
2321             _save_filechooser.setCurrentDirectory( my_dir );
2322         }
2323         final int result = _save_filechooser.showSaveDialog( _contentpane );
2324         final File file = _save_filechooser.getSelectedFile();
2325         setCurrentDir( _save_filechooser.getCurrentDirectory() );
2326         boolean exception = false;
2327         if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
2328             if ( file.exists() ) {
2329                 final int i = JOptionPane.showConfirmDialog( this,
2330                                                              file + " already exists.\nOverwrite?",
2331                                                              "Overwrite?",
2332                                                              JOptionPane.OK_CANCEL_OPTION,
2333                                                              JOptionPane.QUESTION_MESSAGE );
2334                 if ( i != JOptionPane.OK_OPTION ) {
2335                     return;
2336                 }
2337                 else {
2338                     final File to = new File( file.getAbsoluteFile().toString() + Constants.BACKUP_FILE_SUFFIX );
2339                     try {
2340                         ForesterUtil.copyFile( file, to );
2341                     }
2342                     catch ( final Exception e ) {
2343                         JOptionPane.showMessageDialog( this,
2344                                                        "Failed to create backup copy " + to,
2345                                                        "Failed to Create Backup Copy",
2346                                                        JOptionPane.WARNING_MESSAGE );
2347                     }
2348                     try {
2349                         file.delete();
2350                     }
2351                     catch ( final Exception e ) {
2352                         JOptionPane.showMessageDialog( this,
2353                                                        "Failed to delete: " + file,
2354                                                        "Failed to Delete",
2355                                                        JOptionPane.WARNING_MESSAGE );
2356                     }
2357                 }
2358             }
2359             if ( _save_filechooser.getFileFilter() == MainFrameApplication.nhfilter ) {
2360                 exception = writeAsNewHampshire( t, exception, file );
2361             }
2362             else if ( _save_filechooser.getFileFilter() == MainFrameApplication.nhxfilter ) {
2363                 exception = writeAsNHX( t, exception, file );
2364             }
2365             else if ( _save_filechooser.getFileFilter() == MainFrameApplication.xmlfilter ) {
2366                 exception = writeAsPhyloXml( t, exception, file );
2367             }
2368             else if ( _save_filechooser.getFileFilter() == MainFrameApplication.nexusfilter ) {
2369                 exception = writeAsNexus( t, exception, file );
2370             }
2371             // "*.*":
2372             else {
2373                 final String file_name = file.getName().trim().toLowerCase();
2374                 if ( file_name.endsWith( ".nh" ) || file_name.endsWith( ".newick" ) || file_name.endsWith( ".phy" )
2375                         || file_name.endsWith( ".tree" ) ) {
2376                     exception = writeAsNewHampshire( t, exception, file );
2377                 }
2378                 else if ( file_name.endsWith( ".nhx" ) ) {
2379                     exception = writeAsNHX( t, exception, file );
2380                 }
2381                 else if ( file_name.endsWith( ".nex" ) || file_name.endsWith( ".nexus" ) ) {
2382                     exception = writeAsNexus( t, exception, file );
2383                 }
2384                 // XML is default:
2385                 else {
2386                     exception = writeAsPhyloXml( t, exception, file );
2387                 }
2388             }
2389             if ( !exception ) {
2390                 getMainPanel().setTitleOfSelectedTab( file.getName() );
2391                 getMainPanel().getCurrentTreePanel().setTreeFile( file );
2392                 getMainPanel().getCurrentTreePanel().setEdited( false );
2393             }
2394         }
2395     }
2396
2397     private void writeToGraphicsFile( final Phylogeny t, final GraphicsExportType type ) {
2398         if ( ( t == null ) || t.isEmpty() ) {
2399             return;
2400         }
2401         String initial_filename = "";
2402         if ( getMainPanel().getCurrentTreePanel().getTreeFile() != null ) {
2403             initial_filename = getMainPanel().getCurrentTreePanel().getTreeFile().toString();
2404         }
2405         if ( initial_filename.indexOf( '.' ) > 0 ) {
2406             initial_filename = initial_filename.substring( 0, initial_filename.lastIndexOf( '.' ) );
2407         }
2408         initial_filename = initial_filename + "." + type;
2409         _writetographics_filechooser.setSelectedFile( new File( initial_filename ) );
2410         final File my_dir = getCurrentDir();
2411         if ( my_dir != null ) {
2412             _writetographics_filechooser.setCurrentDirectory( my_dir );
2413         }
2414         final int result = _writetographics_filechooser.showSaveDialog( _contentpane );
2415         File file = _writetographics_filechooser.getSelectedFile();
2416         setCurrentDir( _writetographics_filechooser.getCurrentDirectory() );
2417         if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
2418             if ( !file.toString().toLowerCase().endsWith( type.toString() ) ) {
2419                 file = new File( file.toString() + "." + type );
2420             }
2421             if ( file.exists() ) {
2422                 final int i = JOptionPane.showConfirmDialog( this,
2423                                                              file + " already exists. Overwrite?",
2424                                                              "Warning",
2425                                                              JOptionPane.OK_CANCEL_OPTION,
2426                                                              JOptionPane.WARNING_MESSAGE );
2427                 if ( i != JOptionPane.OK_OPTION ) {
2428                     return;
2429                 }
2430                 else {
2431                     try {
2432                         file.delete();
2433                     }
2434                     catch ( final Exception e ) {
2435                         JOptionPane.showMessageDialog( this,
2436                                                        "Failed to delete: " + file,
2437                                                        "Error",
2438                                                        JOptionPane.WARNING_MESSAGE );
2439                     }
2440                 }
2441             }
2442             writePhylogenyToGraphicsFile( file.toString(), type );
2443         }
2444     }
2445
2446     private void writeToPdf( final Phylogeny t ) {
2447         if ( ( t == null ) || t.isEmpty() ) {
2448             return;
2449         }
2450         String initial_filename = "";
2451         if ( getMainPanel().getCurrentTreePanel().getTreeFile() != null ) {
2452             initial_filename = getMainPanel().getCurrentTreePanel().getTreeFile().toString();
2453         }
2454         if ( initial_filename.indexOf( '.' ) > 0 ) {
2455             initial_filename = initial_filename.substring( 0, initial_filename.lastIndexOf( '.' ) );
2456         }
2457         initial_filename = initial_filename + ".pdf";
2458         _writetopdf_filechooser.setSelectedFile( new File( initial_filename ) );
2459         final File my_dir = getCurrentDir();
2460         if ( my_dir != null ) {
2461             _writetopdf_filechooser.setCurrentDirectory( my_dir );
2462         }
2463         final int result = _writetopdf_filechooser.showSaveDialog( _contentpane );
2464         File file = _writetopdf_filechooser.getSelectedFile();
2465         setCurrentDir( _writetopdf_filechooser.getCurrentDirectory() );
2466         if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
2467             if ( !file.toString().toLowerCase().endsWith( ".pdf" ) ) {
2468                 file = new File( file.toString() + ".pdf" );
2469             }
2470             if ( file.exists() ) {
2471                 final int i = JOptionPane.showConfirmDialog( this,
2472                                                              file + " already exists. Overwrite?",
2473                                                              "WARNING",
2474                                                              JOptionPane.OK_CANCEL_OPTION,
2475                                                              JOptionPane.WARNING_MESSAGE );
2476                 if ( i != JOptionPane.OK_OPTION ) {
2477                     return;
2478                 }
2479             }
2480             printPhylogenyToPdf( file.toString() );
2481         }
2482     }
2483
2484     static MainFrame createInstance( final Phylogeny[] phys, final Configuration config, final String title ) {
2485         return new MainFrameApplication( phys, config, title );
2486     }
2487
2488     static MainFrame createInstance( final Phylogeny[] phys, final String config_file_name, final String title ) {
2489         return new MainFrameApplication( phys, config_file_name, title );
2490     }
2491
2492     static void setTextForGraphicsSizeChooserMenuItem( final JMenuItem mi, final Options o ) {
2493         mi.setText( "Enter Default Size for Graphics Export... (current: " + o.getPrintSizeX() + ", "
2494                 + o.getPrintSizeY() + ")" );
2495     }
2496
2497     static void setTextForPdfLineWidthChooserMenuItem( final JMenuItem mi, final Options o ) {
2498         mi.setText( "Enter Default Line Width for PDF Export... (current: " + o.getPrintLineWidth() + ")" );
2499     }
2500
2501     static void warnIfNotPhyloXmlValidation( final Configuration c ) {
2502         if ( !c.isValidatePhyloXmlAgainstSchema() ) {
2503             JOptionPane
2504                     .showMessageDialog( null,
2505                                         ForesterUtil
2506                                                 .wordWrap( "phyloXML XSD-based validation is turned off [enable with line 'validate_against_phyloxml_xsd_schem: true' in configuration file]",
2507                                                            80 ),
2508                                         "Warning",
2509                                         JOptionPane.WARNING_MESSAGE );
2510         }
2511     }
2512
2513     private void setPhylogeneticInferenceOptions( final PhylogeneticInferenceOptions phylogenetic_inference_options ) {
2514         _phylogenetic_inference_options = phylogenetic_inference_options;
2515     }
2516
2517     private PhylogeneticInferenceOptions getPhylogeneticInferenceOptions() {
2518         if ( _phylogenetic_inference_options == null ) {
2519             _phylogenetic_inference_options = new PhylogeneticInferenceOptions();
2520         }
2521         return _phylogenetic_inference_options;
2522     }
2523
2524     public Msa getMsa() {
2525         return _msa;
2526     }
2527
2528     void setMsa( final Msa msa ) {
2529         _msa = msa;
2530     }
2531
2532     void setMsaFile( final File msa_file ) {
2533         _msa_file = msa_file;
2534     }
2535
2536     public File getMsaFile() {
2537         return _msa_file;
2538     }
2539
2540     public List<Sequence> getSeqs() {
2541         return _seqs;
2542     }
2543
2544     void setSeqs( final List<Sequence> seqs ) {
2545         _seqs = seqs;
2546     }
2547
2548     void setSeqsFile( final File seqs_file ) {
2549         _seqs_file = seqs_file;
2550     }
2551
2552     public File getSeqsFile() {
2553         return _seqs_file;
2554     }
2555 } // MainFrameApplication.
2556
2557 class NexusFilter extends FileFilter {
2558
2559     @Override
2560     public boolean accept( final File f ) {
2561         final String file_name = f.getName().trim().toLowerCase();
2562         return file_name.endsWith( ".nex" ) || file_name.endsWith( ".nexus" ) || file_name.endsWith( ".nx" )
2563                 || file_name.endsWith( ".tre" ) || f.isDirectory();
2564     }
2565
2566     @Override
2567     public String getDescription() {
2568         return "Nexus files (*.nex, *.nexus, *.nx, *.tre)";
2569     }
2570 } // NexusFilter
2571
2572 class NHFilter extends FileFilter {
2573
2574     @Override
2575     public boolean accept( final File f ) {
2576         final String file_name = f.getName().trim().toLowerCase();
2577         return file_name.endsWith( ".nh" ) || file_name.endsWith( ".newick" ) || file_name.endsWith( ".phy" )
2578                 || file_name.endsWith( ".tr" ) || file_name.endsWith( ".tree" ) || file_name.endsWith( ".dnd" )
2579                 || file_name.endsWith( ".ph" ) || file_name.endsWith( ".phb" ) || file_name.endsWith( ".nwk" )
2580                 || f.isDirectory();
2581     }
2582
2583     @Override
2584     public String getDescription() {
2585         return "New Hampshire - Newick files (*.nh, *.newick, *.phy, *.tree, *.dnd, *.tr, *.ph, *.phb, *.nwk)";
2586     }
2587 } // NHFilter
2588
2589 class NHXFilter extends FileFilter {
2590
2591     @Override
2592     public boolean accept( final File f ) {
2593         final String file_name = f.getName().trim().toLowerCase();
2594         return file_name.endsWith( ".nhx" ) || f.isDirectory();
2595     }
2596
2597     @Override
2598     public String getDescription() {
2599         return "NHX files (*.nhx)";
2600     }
2601 }
2602
2603 class PdfFilter extends FileFilter {
2604
2605     @Override
2606     public boolean accept( final File f ) {
2607         return f.getName().trim().toLowerCase().endsWith( ".pdf" ) || f.isDirectory();
2608     }
2609
2610     @Override
2611     public String getDescription() {
2612         return "PDF files (*.pdf)";
2613     }
2614 } // PdfFilter
2615
2616 class TolFilter extends FileFilter {
2617
2618     @Override
2619     public boolean accept( final File f ) {
2620         final String file_name = f.getName().trim().toLowerCase();
2621         return ( file_name.endsWith( ".tol" ) || file_name.endsWith( ".tolxml" ) || file_name.endsWith( ".zip" ) || f
2622                 .isDirectory() ) && ( !file_name.endsWith( ".xml.zip" ) );
2623     }
2624
2625     @Override
2626     public String getDescription() {
2627         return "Tree of Life files (*.tol, *.tolxml)";
2628     }
2629 } // TolFilter
2630
2631 class XMLFilter extends FileFilter {
2632
2633     @Override
2634     public boolean accept( final File f ) {
2635         final String file_name = f.getName().trim().toLowerCase();
2636         return file_name.endsWith( ".xml" ) || file_name.endsWith( ".phyloxml" ) || file_name.endsWith( "phylo.xml" )
2637                 || file_name.endsWith( ".pxml" ) || file_name.endsWith( ".zip" ) || f.isDirectory();
2638     }
2639
2640     @Override
2641     public String getDescription() {
2642         return "phyloXML files (*.xml, *.phyloxml, *phylo.xml, *.pxml, *.zip)";
2643     }
2644 } // XMLFilter