in progress
[jalview.git] / forester / java / src / org / forester / archaeopteryx / MainFrame.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2010 Christian M. Zmasek
6 // All rights reserved
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 //
22 // Contact: phylosoft @ gmail . com
23 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
24
25 package org.forester.archaeopteryx;
26
27 import java.awt.Color;
28 import java.awt.Component;
29 import java.awt.Container;
30 import java.awt.Font;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
33 import java.io.File;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.LinkedList;
37 import java.util.List;
38 import java.util.Locale;
39 import java.util.NoSuchElementException;
40
41 import javax.swing.Box;
42 import javax.swing.JApplet;
43 import javax.swing.JCheckBoxMenuItem;
44 import javax.swing.JFileChooser;
45 import javax.swing.JFrame;
46 import javax.swing.JLabel;
47 import javax.swing.JMenu;
48 import javax.swing.JMenuBar;
49 import javax.swing.JMenuItem;
50 import javax.swing.JOptionPane;
51 import javax.swing.JPanel;
52 import javax.swing.JRadioButtonMenuItem;
53 import javax.swing.JTextField;
54 import javax.swing.SwingUtilities;
55 import javax.swing.filechooser.FileFilter;
56
57 import org.forester.archaeopteryx.AptxUtil.GraphicsExportType;
58 import org.forester.archaeopteryx.Options.CLADOGRAM_TYPE;
59 import org.forester.archaeopteryx.Options.NODE_LABEL_DIRECTION;
60 import org.forester.archaeopteryx.Options.PHYLOGENY_GRAPHICS_TYPE;
61 import org.forester.archaeopteryx.tools.AncestralTaxonomyInferrer;
62 import org.forester.archaeopteryx.tools.InferenceManager;
63 import org.forester.archaeopteryx.tools.ProcessPool;
64 import org.forester.archaeopteryx.tools.ProcessRunning;
65 import org.forester.io.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION;
66 import org.forester.io.writers.PhylogenyWriter;
67 import org.forester.phylogeny.Phylogeny;
68 import org.forester.phylogeny.PhylogenyMethods;
69 import org.forester.phylogeny.PhylogenyMethods.DESCENDANT_SORT_PRIORITY;
70 import org.forester.phylogeny.PhylogenyNode;
71 import org.forester.phylogeny.PhylogenyNode.NH_CONVERSION_SUPPORT_VALUE_STYLE;
72 import org.forester.phylogeny.data.Annotation;
73 import org.forester.phylogeny.data.NodeDataField;
74 import org.forester.phylogeny.data.NodeVisualData.NodeFill;
75 import org.forester.phylogeny.data.NodeVisualData.NodeShape;
76 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
77 import org.forester.sdi.GSDI;
78 import org.forester.sdi.GSDIR;
79 import org.forester.sdi.SDIException;
80 import org.forester.util.ForesterConstants;
81 import org.forester.util.ForesterUtil;
82 import org.forester.util.WindowsUtils;
83
84 public abstract class MainFrame extends JFrame implements ActionListener {
85
86     final static NHFilter            nhfilter                                = new NHFilter();
87     final static NHXFilter           nhxfilter                               = new NHXFilter();
88     final static XMLFilter           xmlfilter                               = new XMLFilter();
89     final static TolFilter           tolfilter                               = new TolFilter();
90     final static NexusFilter         nexusfilter                             = new NexusFilter();
91     final static PdfFilter           pdffilter                               = new PdfFilter();
92     final static GraphicsFileFilter  graphicsfilefilter                      = new GraphicsFileFilter();
93     final static MsaFileFilter       msafilter                               = new MsaFileFilter();
94     final static SequencesFileFilter seqsfilter                              = new SequencesFileFilter();
95     final static DefaultFilter       defaultfilter                           = new DefaultFilter();
96     static final String              USE_MOUSEWHEEL_SHIFT_TO_ROTATE          = "In this display type, use mousewheel + Shift to rotate [or A and S]";
97     static final String              PHYLOXML_REF_TOOL_TIP                   = AptxConstants.PHYLOXML_REFERENCE;                                                                                                                                                //TODO //FIXME
98     static final String              APTX_REF_TOOL_TIP                       = AptxConstants.APTX_REFERENCE;
99     private static final long        serialVersionUID                        = 3655000897845508358L;
100     final static Font                menu_font                               = new Font( Configuration.getDefaultFontFamilyName(),
101                                                                                          Font.PLAIN,
102                                                                                          10 );
103     static final String              TYPE_MENU_HEADER                        = "Type";
104     static final String              RECTANGULAR_TYPE_CBMI_LABEL             = "Rectangular";
105     static final String              EURO_TYPE_CBMI_LABEL                    = "Euro Type";
106     static final String              CURVED_TYPE_CBMI_LABEL                  = "Curved";
107     static final String              TRIANGULAR_TYPE_CBMI_LABEL              = "Triangular";
108     static final String              CONVEX_TYPE_CBMI_LABEL                  = "Convex";
109     static final String              ROUNDED_TYPE_CBMI_LABEL                 = "Rounded";
110     static final String              UNROOTED_TYPE_CBMI_LABEL                = "Unrooted (alpha)";                                                                                                                                                          //TODO
111     static final String              CIRCULAR_TYPE_CBMI_LABEL                = "Circular (alpha)";                                                                                                                                                          //TODO
112     static final String              OPTIONS_HEADER                          = "Options";
113     static final String              SEARCH_SUBHEADER                        = "Search:";
114     static final String              DISPLAY_SUBHEADER                       = "Display:";
115     static final String              SEARCH_TERMS_ONLY_LABEL                 = "Match Complete Terms Only";
116     static final String              SEARCH_REGEX_LABEL                      = "Search with Regular Expressions";
117     static final String              SEARCH_CASE_SENSITIVE_LABEL             = "Case Sensitive";
118     static final String              INVERSE_SEARCH_RESULT_LABEL             = "Negate Result";
119     static final String              COLOR_BY_TAXONOMIC_GROUP                = "Colorize by Taxonomic Group";
120     static final String              DISPLAY_SCALE_LABEL                     = "Scale";
121     static final String              NON_LINED_UP_CLADOGRAMS_LABEL           = "Non-Lined Up Cladograms";
122     static final String              UNIFORM_CLADOGRAMS_LABEL                = "Total Node Sum Dependent Cladograms";
123     static final String              LABEL_DIRECTION_LABEL                   = "Radial Labels";
124     static final String              LABEL_DIRECTION_TIP                     = "To use radial node labels in radial and unrooted display types";
125     static final String              SEARCH_WITH_REGEX_TIP                   = "To search using regular expressions (~Java/Perl syntax). For example, use \"^B.+\\d{2,}$\" to search for everything starting with a B and ending with at least two digits.";
126     static final String              SCREEN_ANTIALIAS_LABEL                  = "Antialias";
127     static final String              COLOR_LABELS_LABEL                      = "Colorize Labels Same as Parent Branch";
128     static final String              BG_GRAD_LABEL                           = "Background Color Gradient";
129     static final String              DISPLAY_NODE_BOXES_LABEL_EXT            = "Shapes for External Nodes";
130     static final String              DISPLAY_NODE_BOXES_LABEL_INT            = "Shapes for Internal Nodes";
131     static final String              DISPLAY_NODE_BOXES_LABEL_MARKED         = "Shapes for Nodes with Visual Data";
132     static final String              SHOW_OVERVIEW_LABEL                     = "Overview";
133     static final String              FONT_SIZE_MENU_LABEL                    = "Font Size";
134     static final String              NONUNIFORM_CLADOGRAMS_LABEL             = "External Node Sum Dependent Cladograms";
135     static final String              SHOW_DOMAIN_LABELS_LABEL                = "Domain Labels";
136     static final String              SHOW_ANN_REF_SOURCE_LABEL               = "Seq Annotation Ref Sources";
137     static final String              COLOR_LABELS_TIP                        = "To use parent branch colors for node labels as well, need to turn off taxonomy dependent colorization and turn on branch colorization for this to become apparent";
138     static final String              ABBREV_SN_LABEL                         = "Abbreviate Scientific Taxonomic Names";
139     static final String              TAXONOMY_COLORIZE_NODE_SHAPES_LABEL     = "Colorize Node Shapes According to Taxonomy";
140     static final String              CYCLE_NODE_SHAPE_LABEL                  = "Cycle Node Shapes";
141     static final String              CYCLE_NODE_FILL_LABEL                   = "Cycle Node Fill Type";
142     static final String              CHOOSE_NODE_SIZE_LABEL                  = "Choose Node Shape Size";
143     static final String              SHOW_CONF_STDDEV_LABEL                  = "Confidence Standard Deviations";
144     static final String              USE_BRACKETS_FOR_CONF_IN_NH_LABEL       = "Use Brackets for Confidence Values";
145     static final String              USE_INTERNAL_NAMES_FOR_CONF_IN_NH_LABEL = "Use Internal Node Names for Confidence Values";
146     static final String              SHOW_BASIC_TREE_INFORMATION_LABEL       = "Basic Tree Information";
147     static final String              RIGHT_LINE_UP_DOMAINS                   = "Right-align Domain Architectures";
148     static final String              LINE_UP_RENDERABLE_DATA                 = "Line Up Diagrams (such as Domain Architectures)";
149     static final String              INFER_ANCESTOR_TAXONOMIES               = "Infer Ancestor Taxonomies";
150     static final String              OBTAIN_DETAILED_TAXONOMIC_INFORMATION   = "Obtain Detailed Taxonomic Information";
151     JMenuBar                         _jmenubar;
152     JMenu                            _file_jmenu;
153     JMenu                            _tools_menu;
154     JMenu                            _view_jmenu;
155     JMenu                            _options_jmenu;
156     JMenu                            _font_size_menu;
157     JMenu                            _help_jmenu;
158     JMenuItem[]                      _load_phylogeny_from_webservice_menu_items;
159     // Analysis menu
160     JMenu                            _analysis_menu;
161     JMenuItem                        _load_species_tree_item;
162     JMenuItem                        _gsdi_item;
163     JMenuItem                        _gsdir_item;
164     JMenuItem                        _lineage_inference;
165     // file menu:
166     JMenuItem                        _open_item;
167     JMenuItem                        _open_url_item;
168     JMenuItem                        _save_item;
169     JMenuItem                        _save_all_item;
170     JMenuItem                        _close_item;
171     JMenuItem                        _exit_item;
172     JMenuItem                        _new_item;
173     JMenuItem                        _print_item;
174     JMenuItem                        _write_to_pdf_item;
175     JMenuItem                        _write_to_jpg_item;
176     JMenuItem                        _write_to_gif_item;
177     JMenuItem                        _write_to_tif_item;
178     JMenuItem                        _write_to_png_item;
179     JMenuItem                        _write_to_bmp_item;
180     // tools menu:
181     JMenuItem                        _midpoint_root_item;
182     JMenuItem                        _taxcolor_item;
183     JMenuItem                        _confcolor_item;
184     JMenuItem                        _color_rank_jmi;
185     JMenuItem                        _collapse_species_specific_subtrees;
186     JMenuItem                        _obtain_detailed_taxonomic_information_jmi;
187     JMenuItem                        _obtain_detailed_taxonomic_information_deleting_jmi;
188     JMenuItem                        _obtain_seq_information_jmi;
189     JMenuItem                        _move_node_names_to_tax_sn_jmi;
190     JMenuItem                        _move_node_names_to_seq_names_jmi;
191     JMenuItem                        _extract_tax_code_from_node_names_jmi;
192     JMenuItem                        _annotate_item;
193     JMenuItem                        _remove_branch_color_item;
194     JMenuItem                        _remove_visual_styles_item;
195     JMenuItem                        _delete_selected_nodes_item;
196     JMenuItem                        _delete_not_selected_nodes_item;
197     // font size menu:
198     JMenuItem                        _super_tiny_fonts_item;
199     JMenuItem                        _tiny_fonts_item;
200     JMenuItem                        _small_fonts_item;
201     JMenuItem                        _medium_fonts_item;
202     JMenuItem                        _large_fonts_item;
203     // options menu:
204     // _  screen and print
205     JMenuItem                        _choose_font_mi;
206     JMenuItem                        _switch_colors_mi;
207     JCheckBoxMenuItem                _label_direction_cbmi;
208     // _  screen display
209     JCheckBoxMenuItem                _screen_antialias_cbmi;
210     JCheckBoxMenuItem                _background_gradient_cbmi;
211     JRadioButtonMenuItem             _non_lined_up_cladograms_rbmi;
212     JRadioButtonMenuItem             _uniform_cladograms_rbmi;
213     JRadioButtonMenuItem             _ext_node_dependent_cladogram_rbmi;
214     JCheckBoxMenuItem                _color_by_taxonomic_group_cbmi;
215     JCheckBoxMenuItem                _show_scale_cbmi;                                                                                                                                                                                                      //TODO fix me
216     JCheckBoxMenuItem                _show_overview_cbmi;
217     JCheckBoxMenuItem                _show_domain_labels;
218     JCheckBoxMenuItem                _show_annotation_ref_source;
219     JCheckBoxMenuItem                _abbreviate_scientific_names;
220     JCheckBoxMenuItem                _color_labels_same_as_parent_branch;
221     JMenuItem                        _overview_placment_mi;
222     JMenuItem                        _choose_minimal_confidence_mi;
223     JCheckBoxMenuItem                _show_default_node_shapes_internal_cbmi;
224     JCheckBoxMenuItem                _show_default_node_shapes_external_cbmi;
225     JCheckBoxMenuItem                _show_default_node_shapes_for_marked_cbmi;
226     JMenuItem                        _cycle_node_shape_mi;
227     JMenuItem                        _cycle_node_fill_mi;
228     JMenuItem                        _choose_node_size_mi;
229     JMenuItem                        _cycle_data_return;
230     JCheckBoxMenuItem                _show_confidence_stddev_cbmi;
231     JCheckBoxMenuItem                _right_line_up_domains_cbmi;
232     JCheckBoxMenuItem                _line_up_renderable_data_cbmi;
233     // _  print
234     JCheckBoxMenuItem                _graphics_export_visible_only_cbmi;
235     JCheckBoxMenuItem                _antialias_print_cbmi;
236     JCheckBoxMenuItem                _print_black_and_white_cbmi;
237     //JMenuItem                        _print_size_mi;
238     JMenuItem                        _choose_pdf_width_mi;
239     // _  parsing
240     JCheckBoxMenuItem                _internal_number_are_confidence_for_nh_parsing_cbmi;
241     JRadioButtonMenuItem             _extract_taxonomy_no_rbmi;
242     JRadioButtonMenuItem             _extract_taxonomy_agressive_rbmi;
243     JRadioButtonMenuItem             _extract_taxonomy_pfam_strict_rbmi;
244     JRadioButtonMenuItem             _extract_taxonomy_pfam_relaxed_rbmi;
245     JCheckBoxMenuItem                _replace_underscores_cbmi;
246     JCheckBoxMenuItem                _allow_errors_in_distance_to_parent_cbmi;
247     JCheckBoxMenuItem                _use_brackets_for_conf_in_nh_export_cbmi;
248     JCheckBoxMenuItem                _use_internal_names_for_conf_in_nh_export_cbmi;
249     JCheckBoxMenuItem                _parse_beast_style_extended_nexus_tags_cbmi;
250     // _  search
251     JCheckBoxMenuItem                _search_case_senstive_cbmi;
252     JCheckBoxMenuItem                _search_whole_words_only_cbmi;
253     JCheckBoxMenuItem                _inverse_search_result_cbmi;
254     JCheckBoxMenuItem                _search_with_regex_cbmi;
255     JCheckBoxMenuItem                _color_all_found_nodes_when_coloring_subtree_cbmi;
256     // type menu:
257     JMenu                            _type_menu;
258     JCheckBoxMenuItem                _rectangular_type_cbmi;
259     JCheckBoxMenuItem                _triangular_type_cbmi;
260     JCheckBoxMenuItem                _curved_type_cbmi;
261     JCheckBoxMenuItem                _convex_type_cbmi;
262     JCheckBoxMenuItem                _euro_type_cbmi;
263     JCheckBoxMenuItem                _rounded_type_cbmi;
264     JCheckBoxMenuItem                _unrooted_type_cbmi;
265     JCheckBoxMenuItem                _circular_type_cbmi;
266     // view as text menu:
267     JMenuItem                        _view_as_NH_item;
268     JMenuItem                        _view_as_XML_item;
269     JMenuItem                        _view_as_nexus_item;
270     JMenuItem                        _display_basic_information_item;
271     // help menu:
272     JMenuItem                        _about_item;
273     JMenuItem                        _help_item;
274     JMenuItem                        _website_item;
275     JMenuItem                        _phyloxml_website_item;
276     JMenuItem                        _phyloxml_ref_item;
277     JMenuItem                        _aptx_ref_item;
278     //
279     File                             _current_dir;
280     JFileChooser                     _writetopdf_filechooser;
281     JFileChooser                     _save_filechooser;
282     JFileChooser                     _writetographics_filechooser;
283     // process menu:
284     JMenu                            _process_menu;
285     MainPanel                        _mainpanel;
286     Container                        _contentpane;
287     final LinkedList<TextFrame>      _textframes                             = new LinkedList<TextFrame>();                                                                                                                                                  ;
288     Configuration                    _configuration;
289     Options                          _options;
290     private Phylogeny                _species_tree;
291     InferenceManager                 _inference_manager;
292     final ProcessPool                _process_pool;
293     private String                   _previous_node_annotation_ref;
294
295     MainFrame() {
296         _process_pool = ProcessPool.createInstance();
297         _writetopdf_filechooser = new JFileChooser();
298         _writetopdf_filechooser.setMultiSelectionEnabled( false );
299         _writetopdf_filechooser.addChoosableFileFilter( pdffilter );
300         _writetographics_filechooser = new JFileChooser();
301         _writetographics_filechooser.setMultiSelectionEnabled( false );
302         _writetographics_filechooser.addChoosableFileFilter( graphicsfilefilter );
303         _save_filechooser = new JFileChooser();
304         _save_filechooser.setMultiSelectionEnabled( false );
305         _save_filechooser.setFileFilter( xmlfilter );
306         _save_filechooser.addChoosableFileFilter( nhfilter );
307         _save_filechooser.addChoosableFileFilter( nexusfilter );
308         _save_filechooser.addChoosableFileFilter( _save_filechooser.getAcceptAllFileFilter() );
309         try {
310             final String home_dir = System.getProperty( "user.home" );
311             _save_filechooser.setCurrentDirectory( new File( home_dir ) );
312             _writetopdf_filechooser.setCurrentDirectory( new File( home_dir ) );
313             _writetographics_filechooser.setCurrentDirectory( new File( home_dir ) );
314         }
315         catch ( final Exception e ) {
316             e.printStackTrace();
317             // Do nothing. Not important.
318         }
319     }
320
321     /**
322      * Action performed.
323      */
324     @Override
325     public void actionPerformed( final ActionEvent e ) {
326         final Object o = e.getSource();
327         boolean is_applet = false;
328         JApplet applet = null;
329         if ( getCurrentTreePanel() != null ) {
330             is_applet = getCurrentTreePanel().isApplet();
331             if ( is_applet ) {
332                 applet = getCurrentTreePanel().obtainApplet();
333             }
334         }
335         if ( o == _exit_item ) {
336             close();
337         }
338         else if ( o == _gsdi_item ) {
339             if ( isSubtreeDisplayed() ) {
340                 return;
341             }
342             executeGSDI();
343         }
344         else if ( o == _gsdir_item ) {
345             if ( isSubtreeDisplayed() ) {
346                 return;
347             }
348             executeGSDIR();
349         }
350         else if ( o == _taxcolor_item ) {
351             taxColor();
352         }
353         else if ( o == _confcolor_item ) {
354             confColor();
355         }
356         else if ( o == _color_rank_jmi ) {
357             colorRank();
358         }
359         else if ( o == _collapse_species_specific_subtrees ) {
360             if ( isSubtreeDisplayed() ) {
361                 return;
362             }
363             if ( getCurrentTreePanel() != null ) {
364                 getCurrentTreePanel().collapseSpeciesSpecificSubtrees();
365             }
366         }
367         else if ( o == _remove_branch_color_item ) {
368             if ( isSubtreeDisplayed() ) {
369                 return;
370             }
371             removeBranchColors();
372         }
373         else if ( o == _remove_visual_styles_item ) {
374             if ( isSubtreeDisplayed() ) {
375                 return;
376             }
377             removeVisualStyles();
378         }
379         else if ( o == _midpoint_root_item ) {
380             if ( isSubtreeDisplayed() ) {
381                 return;
382             }
383             midpointRoot();
384         }
385         else if ( o == _delete_selected_nodes_item ) {
386             if ( isSubtreeDisplayed() ) {
387                 return;
388             }
389             deleteSelectedNodes( true );
390         }
391         else if ( o == _delete_not_selected_nodes_item ) {
392             if ( isSubtreeDisplayed() ) {
393                 return;
394             }
395             deleteSelectedNodes( false );
396         }
397         else if ( o == _annotate_item ) {
398             annotateSequences();
399         }
400         else if ( o == _switch_colors_mi ) {
401             switchColors();
402         }
403         else if ( o == _display_basic_information_item ) {
404             if ( getCurrentTreePanel() != null ) {
405                 displayBasicInformation( getCurrentTreePanel().getTreeFile() );
406             }
407         }
408         else if ( o == _view_as_NH_item ) {
409             viewAsNH();
410         }
411         else if ( o == _view_as_XML_item ) {
412             viewAsXML();
413         }
414         else if ( o == _view_as_nexus_item ) {
415             viewAsNexus();
416         }
417         else if ( o == _super_tiny_fonts_item ) {
418             if ( getCurrentTreePanel() != null ) {
419                 getCurrentTreePanel().setSuperTinyFonts();
420                 getCurrentTreePanel().repaint();
421             }
422         }
423         else if ( o == _tiny_fonts_item ) {
424             if ( getCurrentTreePanel() != null ) {
425                 getCurrentTreePanel().setTinyFonts();
426                 getCurrentTreePanel().repaint();
427             }
428         }
429         else if ( o == _small_fonts_item ) {
430             if ( getCurrentTreePanel() != null ) {
431                 getCurrentTreePanel().setSmallFonts();
432                 getCurrentTreePanel().repaint();
433             }
434         }
435         else if ( o == _medium_fonts_item ) {
436             if ( getCurrentTreePanel() != null ) {
437                 getCurrentTreePanel().setMediumFonts();
438                 getCurrentTreePanel().repaint();
439             }
440         }
441         else if ( o == _large_fonts_item ) {
442             if ( getCurrentTreePanel() != null ) {
443                 getCurrentTreePanel().setLargeFonts();
444                 getCurrentTreePanel().repaint();
445             }
446         }
447         else if ( o == _choose_font_mi ) {
448             chooseFont();
449         }
450         else if ( o == _choose_minimal_confidence_mi ) {
451             chooseMinimalConfidence();
452         }
453         else if ( o == _choose_node_size_mi ) {
454             chooseNodeSize( getOptions(), this );
455         }
456         else if ( o == _overview_placment_mi ) {
457             MainFrame.cycleOverview( getOptions(), getCurrentTreePanel() );
458         }
459         else if ( o == _cycle_node_fill_mi ) {
460             MainFrame.cycleNodeFill( getOptions() );
461         }
462         else if ( o == _cycle_node_shape_mi ) {
463             MainFrame.cycleNodeShape( getOptions() );
464         }
465         else if ( o == _cycle_data_return ) {
466             MainFrame.cycleNodeDataReturn( getOptions(), getConfiguration() );
467         }
468         else if ( o == _screen_antialias_cbmi ) {
469             updateOptions( getOptions() );
470             updateScreenTextAntialias( getMainPanel().getTreePanels() );
471         }
472         else if ( o == _background_gradient_cbmi ) {
473             updateOptions( getOptions() );
474         }
475         else if ( o == _show_domain_labels ) {
476             updateOptions( getOptions() );
477         }
478         else if ( o == _show_annotation_ref_source ) {
479             updateOptions( getOptions() );
480         }
481         else if ( o == _abbreviate_scientific_names ) {
482             updateOptions( getOptions() );
483         }
484         else if ( o == _color_labels_same_as_parent_branch ) {
485             updateOptions( getOptions() );
486         }
487         else if ( o == _show_default_node_shapes_internal_cbmi ) {
488             updateOptions( getOptions() );
489         }
490         else if ( o == _show_default_node_shapes_external_cbmi ) {
491             updateOptions( getOptions() );
492         }
493         else if ( o == _show_default_node_shapes_for_marked_cbmi ) {
494             updateOptions( getOptions() );
495         }
496         else if ( o == _non_lined_up_cladograms_rbmi ) {
497             updateOptions( getOptions() );
498             showWhole();
499         }
500         else if ( o == _uniform_cladograms_rbmi ) {
501             updateOptions( getOptions() );
502             showWhole();
503         }
504         else if ( o == _ext_node_dependent_cladogram_rbmi ) {
505             updateOptions( getOptions() );
506             showWhole();
507         }
508         else if ( o == _search_case_senstive_cbmi ) {
509             updateOptions( getOptions() );
510             getMainPanel().getControlPanel().search0();
511             getMainPanel().getControlPanel().search1();
512         }
513         else if ( o == _search_whole_words_only_cbmi ) {
514             if ( ( _search_with_regex_cbmi != null ) && _search_whole_words_only_cbmi.isSelected() ) {
515                 _search_with_regex_cbmi.setSelected( false );
516             }
517             updateOptions( getOptions() );
518             getMainPanel().getControlPanel().search0();
519             getMainPanel().getControlPanel().search1();
520         }
521         else if ( o == _inverse_search_result_cbmi ) {
522             updateOptions( getOptions() );
523             getMainPanel().getControlPanel().search0();
524             getMainPanel().getControlPanel().search1();
525         }
526         else if ( o == _search_with_regex_cbmi ) {
527             if ( ( _search_whole_words_only_cbmi != null ) && _search_with_regex_cbmi.isSelected() ) {
528                 _search_whole_words_only_cbmi.setSelected( false );
529             }
530             if ( ( _search_case_senstive_cbmi != null ) && _search_with_regex_cbmi.isSelected() ) {
531                 _search_case_senstive_cbmi.setSelected( true );
532             }
533             updateOptions( getOptions() );
534             getMainPanel().getControlPanel().search0();
535             getMainPanel().getControlPanel().search1();
536         }
537         else if ( o == _color_all_found_nodes_when_coloring_subtree_cbmi ) {
538             updateOptions( getOptions() );
539         }
540         else if ( o == _parse_beast_style_extended_nexus_tags_cbmi ) {
541             updateOptions( getOptions() );
542         }
543         else if ( o == _show_scale_cbmi ) {
544             updateOptions( getOptions() );
545         }
546         else if ( o == _color_by_taxonomic_group_cbmi ) {
547             updateOptions( getOptions() );
548         }
549         else if ( o == _show_confidence_stddev_cbmi ) {
550             updateOptions( getOptions() );
551         }
552         else if ( o == _use_brackets_for_conf_in_nh_export_cbmi ) {
553             if ( _use_brackets_for_conf_in_nh_export_cbmi.isSelected() ) {
554                 _use_internal_names_for_conf_in_nh_export_cbmi.setSelected( false );
555             }
556             updateOptions( getOptions() );
557         }
558         else if ( o == _use_internal_names_for_conf_in_nh_export_cbmi ) {
559             if ( _use_internal_names_for_conf_in_nh_export_cbmi.isSelected() ) {
560                 _use_brackets_for_conf_in_nh_export_cbmi.setSelected( false );
561             }
562             updateOptions( getOptions() );
563         }
564         else if ( o == _label_direction_cbmi ) {
565             updateOptions( getOptions() );
566         }
567         else if ( o == _show_overview_cbmi ) {
568             updateOptions( getOptions() );
569             if ( getCurrentTreePanel() != null ) {
570                 getCurrentTreePanel().updateOvSizes();
571             }
572         }
573         else if ( o == _line_up_renderable_data_cbmi ) {
574             if ( !_line_up_renderable_data_cbmi.isSelected() ) {
575                 _right_line_up_domains_cbmi.setSelected( false );
576             }
577             updateOptions( getOptions() );
578         }
579         else if ( o == _right_line_up_domains_cbmi ) {
580             if ( _right_line_up_domains_cbmi.isSelected() ) {
581                 _line_up_renderable_data_cbmi.setSelected( true );
582             }
583             updateOptions( getOptions() );
584         }
585         else if ( ( o == _rectangular_type_cbmi ) || ( o == _triangular_type_cbmi ) || ( o == _curved_type_cbmi )
586                 || ( o == _convex_type_cbmi ) || ( o == _euro_type_cbmi ) || ( o == _rounded_type_cbmi )
587                 || ( o == _unrooted_type_cbmi ) || ( o == _circular_type_cbmi ) ) {
588             typeChanged( o );
589         }
590         else if ( o == _about_item ) {
591             about();
592         }
593         else if ( o == _help_item ) {
594             try {
595                 AptxUtil.openWebsite( AptxConstants.APTX_DOC_SITE, is_applet, applet );
596             }
597             catch ( final IOException e1 ) {
598                 ForesterUtil.printErrorMessage( AptxConstants.PRG_NAME, e1.toString() );
599             }
600         }
601         else if ( o == _website_item ) {
602             try {
603                 AptxUtil.openWebsite( AptxConstants.APTX_WEB_SITE, is_applet, applet );
604             }
605             catch ( final IOException e1 ) {
606                 ForesterUtil.printErrorMessage( AptxConstants.PRG_NAME, e1.toString() );
607             }
608         }
609         else if ( o == _phyloxml_website_item ) {
610             try {
611                 AptxUtil.openWebsite( AptxConstants.PHYLOXML_WEB_SITE, is_applet, applet );
612             }
613             catch ( final IOException e1 ) {
614                 ForesterUtil.printErrorMessage( AptxConstants.PRG_NAME, e1.toString() );
615             }
616         }
617         else if ( o == _aptx_ref_item ) {
618             try {
619                 AptxUtil.openWebsite( AptxConstants.APTX_REFERENCE_URL, is_applet, applet );
620             }
621             catch ( final IOException e1 ) {
622                 ForesterUtil.printErrorMessage( AptxConstants.PRG_NAME, e1.toString() );
623             }
624         }
625         else if ( o == _phyloxml_ref_item ) {
626             try {
627                 AptxUtil.openWebsite( AptxConstants.PHYLOXML_REFERENCE_URL, is_applet, applet );
628             }
629             catch ( final IOException e1 ) {
630                 ForesterUtil.printErrorMessage( AptxConstants.PRG_NAME, e1.toString() );
631             }
632         }
633         else if ( o == _write_to_pdf_item ) {
634             final File curr_dir = writeToPdf( _mainpanel.getCurrentPhylogeny(),
635                                               getMainPanel(),
636                                               _writetopdf_filechooser,
637                                               _current_dir,
638                                               getContentPane(),
639                                               this );
640             if ( curr_dir != null ) {
641                 setCurrentDir( curr_dir );
642             }
643         }
644         else if ( o == _save_all_item ) {
645             writeAllToFile();
646         }
647         else if ( o == _write_to_jpg_item ) {
648             final File new_dir = writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(),
649                                                       GraphicsExportType.JPG,
650                                                       _mainpanel,
651                                                       _writetographics_filechooser,
652                                                       this,
653                                                       getContentPane(),
654                                                       _current_dir );
655             if ( new_dir != null ) {
656                 setCurrentDir( new_dir );
657             }
658         }
659         else if ( o == _write_to_gif_item ) {
660             final File new_dir = writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(),
661                                                       GraphicsExportType.GIF,
662                                                       _mainpanel,
663                                                       _writetographics_filechooser,
664                                                       this,
665                                                       getContentPane(),
666                                                       _current_dir );
667             if ( new_dir != null ) {
668                 setCurrentDir( new_dir );
669             }
670         }
671         else if ( o == _write_to_tif_item ) {
672             final File new_dir = writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(),
673                                                       GraphicsExportType.TIFF,
674                                                       _mainpanel,
675                                                       _writetographics_filechooser,
676                                                       this,
677                                                       getContentPane(),
678                                                       _current_dir );
679             if ( new_dir != null ) {
680                 setCurrentDir( new_dir );
681             }
682         }
683         else if ( o == _write_to_bmp_item ) {
684             final File new_dir = writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(),
685                                                       GraphicsExportType.BMP,
686                                                       _mainpanel,
687                                                       _writetographics_filechooser,
688                                                       this,
689                                                       getContentPane(),
690                                                       _current_dir );
691             if ( new_dir != null ) {
692                 setCurrentDir( new_dir );
693             }
694         }
695         else if ( o == _write_to_png_item ) {
696             final File new_dir = writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(),
697                                                       GraphicsExportType.PNG,
698                                                       _mainpanel,
699                                                       _writetographics_filechooser,
700                                                       this,
701                                                       getContentPane(),
702                                                       _current_dir );
703             if ( new_dir != null ) {
704                 setCurrentDir( new_dir );
705             }
706         }
707         else if ( o == _print_item ) {
708             print( getCurrentTreePanel(), getOptions(), this );
709         }
710         else if ( o == _save_item ) {
711             final File new_dir = writeToFile( _mainpanel.getCurrentPhylogeny(),
712                                               getMainPanel(),
713                                               _save_filechooser,
714                                               _current_dir,
715                                               getContentPane(),
716                                               this );
717             if ( new_dir != null ) {
718                 setCurrentDir( new_dir );
719             }
720         }
721         else if ( o == _graphics_export_visible_only_cbmi ) {
722             updateOptions( getOptions() );
723         }
724         else if ( o == _antialias_print_cbmi ) {
725             updateOptions( getOptions() );
726         }
727         else if ( o == _print_black_and_white_cbmi ) {
728             updateOptions( getOptions() );
729         }
730         else if ( o == _choose_pdf_width_mi ) {
731             choosePdfWidth();
732         }
733         else if ( o == _lineage_inference ) {
734             if ( isSubtreeDisplayed() ) {
735                 JOptionPane.showMessageDialog( this,
736                                                "Subtree is shown.",
737                                                "Cannot infer ancestral taxonomies",
738                                                JOptionPane.ERROR_MESSAGE );
739                 return;
740             }
741             executeLineageInference();
742         }
743         else {
744             if ( _load_phylogeny_from_webservice_menu_items != null ) {
745                 for( int i = 0; i < _load_phylogeny_from_webservice_menu_items.length; ++i ) {
746                     if ( o == _load_phylogeny_from_webservice_menu_items[ i ] ) {
747                         readPhylogeniesFromWebservice( i );
748                     }
749                 }
750             }
751         }
752         _contentpane.repaint();
753     }
754
755     public Configuration getConfiguration() {
756         return _configuration;
757     }
758
759     /**
760      * This method returns the current external node data which
761      * has been selected by the user by clicking the "Return ..."
762      * menu item. This method is expected to be called from Javascript or
763      * something like it.
764      *
765      * @return current external node data as String
766      */
767     public String getCurrentExternalNodesDataBuffer() {
768         return getCurrentTreePanel().getCurrentExternalNodesDataBufferAsString();
769     }
770
771     public int getCurrentExternalNodesDataBufferChangeCounter() {
772         return getCurrentTreePanel().getCurrentExternalNodesDataBufferChangeCounter();
773     }
774
775     public int getCurrentExternalNodesDataBufferLength() {
776         return getCurrentTreePanel().getCurrentExternalNodesDataBufferAsString().length();
777     }
778
779     public InferenceManager getInferenceManager() {
780         return _inference_manager;
781     }
782
783     public MainPanel getMainPanel() {
784         return _mainpanel;
785     }
786
787     public Options getOptions() {
788         return _options;
789     }
790
791     public ProcessPool getProcessPool() {
792         return _process_pool;
793     }
794
795     public void showTextFrame( final String s, final String title ) {
796         checkTextFrames();
797         _textframes.addLast( TextFrame.instantiate( s, title, _textframes ) );
798     }
799
800     public void showWhole() {
801         _mainpanel.getControlPanel().showWhole();
802     }
803
804     public void updateProcessMenu() {
805         // In general Swing is not thread safe.
806         // See "Swing's Threading Policy".
807         SwingUtilities.invokeLater( new Runnable() {
808
809             @Override
810             public void run() {
811                 doUpdateProcessMenu();
812             }
813         } );
814     }
815
816     private void annotateSequences() {
817         if ( getCurrentTreePanel() != null ) {
818             List<PhylogenyNode> nodes = null;
819             if ( ( getCurrentTreePanel().getFoundNodes0() != null )
820                     || ( getCurrentTreePanel().getFoundNodes1() != null ) ) {
821                 nodes = getCurrentTreePanel().getFoundNodesAsListOfPhylogenyNodes();
822             }
823             if ( ( nodes == null ) || nodes.isEmpty() ) {
824                 JOptionPane
825                         .showMessageDialog( this,
826                                             "Need to select nodes, either via direct selection or via the \"Search\" function",
827                                             "No nodes selected for annotation",
828                                             JOptionPane.ERROR_MESSAGE );
829                 return;
830             }
831             final Phylogeny phy = getMainPanel().getCurrentPhylogeny();
832             if ( ( phy != null ) && !phy.isEmpty() ) {
833                 final JTextField ref_field = new JTextField( 10 );
834                 final JTextField desc_filed = new JTextField( 20 );
835                 ref_field.setText( ForesterUtil.isEmpty( getPreviousNodeAnnotationReference() ) ? ""
836                         : getPreviousNodeAnnotationReference() );
837                 final JPanel my_panel = new JPanel();
838                 my_panel.add( new JLabel( "Reference " ) );
839                 my_panel.add( ref_field );
840                 my_panel.add( Box.createHorizontalStrut( 15 ) );
841                 my_panel.add( new JLabel( "Description " ) );
842                 my_panel.add( desc_filed );
843                 final int result = JOptionPane.showConfirmDialog( null,
844                                                                   my_panel,
845                                                                   "Enter the sequence annotation(s) for the "
846                                                                           + nodes.size() + " selected nodes",
847                                                                   JOptionPane.OK_CANCEL_OPTION );
848                 if ( result == JOptionPane.OK_OPTION ) {
849                     String ref = ref_field.getText();
850                     String desc = desc_filed.getText();
851                     if ( !ForesterUtil.isEmpty( ref ) ) {
852                         ref = ref.trim();
853                         ref = ref.replaceAll( "\\s+", " " );
854                         if ( ( ref.indexOf( ':' ) < 1 ) || ( ref.indexOf( ':' ) > ( ref.length() - 2 ) )
855                                 || ( ref.length() < 3 ) ) {
856                             JOptionPane.showMessageDialog( this,
857                                                            "Reference needs to be in the form of \"GO:1234567\"",
858                                                            "Illegal Format for Annotation Reference",
859                                                            JOptionPane.ERROR_MESSAGE );
860                             return;
861                         }
862                     }
863                     if ( ref != null ) {
864                         setPreviousNodeAnnotationReference( ref );
865                     }
866                     if ( desc != null ) {
867                         desc = desc.trim();
868                         desc = desc.replaceAll( "\\s+", " " );
869                     }
870                     if ( !ForesterUtil.isEmpty( ref ) || !ForesterUtil.isEmpty( desc ) ) {
871                         for( final PhylogenyNode n : nodes ) {
872                             ForesterUtil.ensurePresenceOfSequence( n );
873                             final Annotation ann = ForesterUtil.isEmpty( ref ) ? new Annotation()
874                                     : new Annotation( ref );
875                             if ( !ForesterUtil.isEmpty( desc ) ) {
876                                 ann.setDesc( desc );
877                             }
878                             n.getNodeData().getSequence().addAnnotation( ann );
879                         }
880                     }
881                     getMainPanel().getControlPanel().showAnnotations();
882                 }
883             }
884         }
885     }
886
887     private void chooseFont() {
888         final FontChooser fc = new FontChooser();
889         fc.setFont( getMainPanel().getTreeFontSet().getLargeFont() );
890         fc.showDialog( this, "Select the Base Font" );
891         getMainPanel().getTreeFontSet().setBaseFont( fc.getFont() );
892         getControlPanel().displayedPhylogenyMightHaveChanged( true );
893         getMainPanel().getCurrentTreePanel().resetPreferredSize();
894         getMainPanel().getCurrentTreePanel().updateOvSizes();
895        
896         repaint();
897     }
898
899     private void chooseMinimalConfidence() {
900         final String s = ( String ) JOptionPane
901                 .showInputDialog( this,
902                                   "Please enter the minimum for confidence values to be displayed.\n"
903                                           + "[current value: " + getOptions().getMinConfidenceValue() + "]\n",
904                                   "Minimal Confidence Value",
905                                   JOptionPane.QUESTION_MESSAGE,
906                                   null,
907                                   null,
908                                   getOptions().getMinConfidenceValue() );
909         if ( !ForesterUtil.isEmpty( s ) ) {
910             boolean success = true;
911             double m = 0.0;
912             final String m_str = s.trim();
913             if ( !ForesterUtil.isEmpty( m_str ) ) {
914                 try {
915                     m = Double.parseDouble( m_str );
916                 }
917                 catch ( final Exception ex ) {
918                     success = false;
919                 }
920             }
921             else {
922                 success = false;
923             }
924             if ( success && ( m >= 0.0 ) ) {
925                 getOptions().setMinConfidenceValue( m );
926             }
927         }
928     }
929
930     private void deleteSelectedNodes( final boolean delete ) {
931         final Phylogeny phy = getMainPanel().getCurrentPhylogeny();
932         if ( ( phy == null ) || ( phy.getNumberOfExternalNodes() < 2 ) ) {
933             return;
934         }
935         final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
936         if ( ( getCurrentTreePanel().getFoundNodes0() != null ) || ( getCurrentTreePanel().getFoundNodes1() != null ) ) {
937             final List<PhylogenyNode> all_selected_nodes = getCurrentTreePanel().getFoundNodesAsListOfPhylogenyNodes();
938             for( final PhylogenyNode n : all_selected_nodes ) {
939                 if ( n.isExternal() ) {
940                     nodes.add( n );
941                 }
942             }
943         }
944         String function = "Retain";
945         if ( delete ) {
946             function = "Delete";
947         }
948         if ( ( nodes == null ) || nodes.isEmpty() ) {
949             JOptionPane
950                     .showMessageDialog( this,
951                                         "Need to select external nodes, either via direct selection or via the \"Search\" function",
952                                         "No external nodes selected to " + function.toLowerCase(),
953                                         JOptionPane.ERROR_MESSAGE );
954             return;
955         }
956         final int todo = nodes.size();
957         final int ext = phy.getNumberOfExternalNodes();
958         int res = todo;
959         if ( delete ) {
960             res = ext - todo;
961         }
962         if ( res < 1 ) {
963             JOptionPane.showMessageDialog( this,
964                                            "Cannot delete all nodes",
965                                            "Attempt to delete all nodes ",
966                                            JOptionPane.ERROR_MESSAGE );
967             return;
968         }
969         final int result = JOptionPane.showConfirmDialog( null, function + " " + todo
970                 + " external node(s), from a total of " + ext + " external nodes," + "\nresulting in tree with " + res
971                 + " nodes?", function + " external nodes", JOptionPane.OK_CANCEL_OPTION );
972         if ( result == JOptionPane.OK_OPTION ) {
973             if ( !delete ) {
974                 final List<PhylogenyNode> to_delete = new ArrayList<PhylogenyNode>();
975                 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
976                     final PhylogenyNode n = it.next();
977                     if ( !nodes.contains( n ) ) {
978                         to_delete.add( n );
979                     }
980                 }
981                 for( final PhylogenyNode n : to_delete ) {
982                     phy.deleteSubtree( n, true );
983                 }
984             }
985             else {
986                 for( final PhylogenyNode n : nodes ) {
987                     phy.deleteSubtree( n, true );
988                 }
989             }
990             resetSearch();
991             getCurrentTreePanel().setNodeInPreorderToNull();
992             phy.externalNodesHaveChanged();
993             phy.clearHashIdToNodeMap();
994             phy.recalculateNumberOfExternalDescendants( true );
995             getCurrentTreePanel().resetNodeIdToDistToLeafMap();
996             getCurrentTreePanel().setEdited( true );
997             repaint();
998         }
999     }
1000
1001     private void doUpdateProcessMenu() {
1002         if ( _process_pool.size() > 0 ) {
1003             if ( _process_menu == null ) {
1004                 _process_menu = createMenu( "", getConfiguration() );
1005                 _process_menu.setForeground( Color.RED );
1006             }
1007             _process_menu.removeAll();
1008             final String text = "processes running: " + _process_pool.size();
1009             _process_menu.setText( text );
1010             _jmenubar.add( _process_menu );
1011             for( int i = 0; i < _process_pool.size(); ++i ) {
1012                 final ProcessRunning p = _process_pool.getProcessByIndex( i );
1013                 _process_menu.add( customizeJMenuItem( new JMenuItem( p.getName() + " [" + p.getStart() + "]" ) ) );
1014             }
1015         }
1016         else {
1017             if ( _process_menu != null ) {
1018                 _process_menu.removeAll();
1019                 _jmenubar.remove( _process_menu );
1020             }
1021         }
1022         _jmenubar.validate();
1023         _jmenubar.repaint();
1024         repaint();
1025     }
1026
1027     private String getPreviousNodeAnnotationReference() {
1028         return _previous_node_annotation_ref;
1029     }
1030
1031     private void removeBranchColors() {
1032         if ( getMainPanel().getCurrentPhylogeny() != null ) {
1033             AptxUtil.removeBranchColors( getMainPanel().getCurrentPhylogeny() );
1034         }
1035     }
1036
1037     private void removeVisualStyles() {
1038         if ( getMainPanel().getCurrentPhylogeny() != null ) {
1039             AptxUtil.removeVisualStyles( getMainPanel().getCurrentPhylogeny() );
1040         }
1041     }
1042
1043     private void setPreviousNodeAnnotationReference( final String previous_node_annotation_ref ) {
1044         _previous_node_annotation_ref = previous_node_annotation_ref;
1045     }
1046
1047     private void writeAllToFile() {
1048         if ( ( getMainPanel().getTabbedPane() == null ) || ( getMainPanel().getTabbedPane().getTabCount() < 1 ) ) {
1049             return;
1050         }
1051         final File my_dir = getCurrentDir();
1052         if ( my_dir != null ) {
1053             _save_filechooser.setCurrentDirectory( my_dir );
1054         }
1055         _save_filechooser.setSelectedFile( new File( "" ) );
1056         final int result = _save_filechooser.showSaveDialog( _contentpane );
1057         final File file = _save_filechooser.getSelectedFile();
1058         setCurrentDir( _save_filechooser.getCurrentDirectory() );
1059         if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
1060             if ( file.exists() ) {
1061                 final int i = JOptionPane.showConfirmDialog( this,
1062                                                              file + " already exists. Overwrite?",
1063                                                              "Warning",
1064                                                              JOptionPane.OK_CANCEL_OPTION,
1065                                                              JOptionPane.WARNING_MESSAGE );
1066                 if ( i != JOptionPane.OK_OPTION ) {
1067                     return;
1068                 }
1069                 else {
1070                     try {
1071                         file.delete();
1072                     }
1073                     catch ( final Exception e ) {
1074                         JOptionPane.showMessageDialog( this,
1075                                                        "Failed to delete: " + file,
1076                                                        "Error",
1077                                                        JOptionPane.WARNING_MESSAGE );
1078                     }
1079                 }
1080             }
1081             final int count = getMainPanel().getTabbedPane().getTabCount();
1082             final List<Phylogeny> trees = new ArrayList<Phylogeny>();
1083             for( int i = 0; i < count; ++i ) {
1084                 final Phylogeny phy = getMainPanel().getPhylogeny( i );
1085                 if ( ForesterUtil.isEmpty( phy.getName() )
1086                         && !ForesterUtil.isEmpty( getMainPanel().getTabbedPane().getTitleAt( i ) ) ) {
1087                     phy.setName( getMainPanel().getTabbedPane().getTitleAt( i ) );
1088                 }
1089                 trees.add( phy );
1090                 getMainPanel().getTreePanels().get( i ).setEdited( false );
1091             }
1092             final PhylogenyWriter writer = new PhylogenyWriter();
1093             try {
1094                 writer.toPhyloXML( file, trees, 0, ForesterUtil.LINE_SEPARATOR );
1095             }
1096             catch ( final IOException e ) {
1097                 JOptionPane.showMessageDialog( this,
1098                                                "Failed to write to: " + file,
1099                                                "Error",
1100                                                JOptionPane.WARNING_MESSAGE );
1101             }
1102         }
1103     }
1104
1105     void activateSaveAllIfNeeded() {
1106         if ( ( getMainPanel().getTabbedPane() != null ) && ( getMainPanel().getTabbedPane().getTabCount() > 1 ) ) {
1107             _save_all_item.setEnabled( true );
1108         }
1109         else {
1110             _save_all_item.setEnabled( false );
1111         }
1112     }
1113
1114     void buildFileMenu() {
1115         _file_jmenu = MainFrame.createMenu( "File", getConfiguration() );
1116         _file_jmenu.add( _save_item = new JMenuItem( "Save Tree As..." ) );
1117         _file_jmenu.addSeparator();
1118         _file_jmenu.add( _write_to_pdf_item = new JMenuItem( "Export to PDF file ..." ) );
1119         if ( AptxUtil.canWriteFormat( "tif" ) || AptxUtil.canWriteFormat( "tiff" ) || AptxUtil.canWriteFormat( "TIF" ) ) {
1120             _file_jmenu.add( _write_to_tif_item = new JMenuItem( "Export to TIFF file..." ) );
1121         }
1122         _file_jmenu.add( _write_to_png_item = new JMenuItem( "Export to PNG file..." ) );
1123         _file_jmenu.add( _write_to_jpg_item = new JMenuItem( "Export to JPG file..." ) );
1124         if ( AptxUtil.canWriteFormat( "gif" ) ) {
1125             _file_jmenu.add( _write_to_gif_item = new JMenuItem( "Export to GIF file..." ) );
1126         }
1127         if ( AptxUtil.canWriteFormat( "bmp" ) ) {
1128             _file_jmenu.add( _write_to_bmp_item = new JMenuItem( "Export to BMP file..." ) );
1129         }
1130         _file_jmenu.addSeparator();
1131         _file_jmenu.add( _print_item = new JMenuItem( "Print..." ) );
1132         _file_jmenu.addSeparator();
1133         _file_jmenu.add( _exit_item = new JMenuItem( "Exit" ) );
1134         customizeJMenuItem( _save_item );
1135         customizeJMenuItem( _write_to_pdf_item );
1136         customizeJMenuItem( _write_to_png_item );
1137         customizeJMenuItem( _write_to_jpg_item );
1138         customizeJMenuItem( _write_to_gif_item );
1139         customizeJMenuItem( _write_to_tif_item );
1140         customizeJMenuItem( _write_to_bmp_item );
1141         customizeJMenuItem( _print_item );
1142         customizeJMenuItem( _exit_item );
1143         _jmenubar.add( _file_jmenu );
1144     }
1145
1146     void buildFontSizeMenu() {
1147         _font_size_menu = createMenu( FONT_SIZE_MENU_LABEL, getConfiguration() );
1148         _font_size_menu.add( _super_tiny_fonts_item = new JMenuItem( "Super Tiny Fonts" ) );
1149         _font_size_menu.add( _tiny_fonts_item = new JMenuItem( "Tiny Fonts" ) );
1150         _font_size_menu.add( _small_fonts_item = new JMenuItem( "Small Fonts" ) );
1151         _font_size_menu.add( _medium_fonts_item = new JMenuItem( "Medium Fonts" ) );
1152         _font_size_menu.add( _large_fonts_item = new JMenuItem( "Large Fonts" ) );
1153         customizeJMenuItem( _super_tiny_fonts_item );
1154         customizeJMenuItem( _tiny_fonts_item );
1155         customizeJMenuItem( _small_fonts_item );
1156         customizeJMenuItem( _medium_fonts_item );
1157         customizeJMenuItem( _large_fonts_item );
1158         _jmenubar.add( _font_size_menu );
1159     }
1160
1161     void buildHelpMenu() {
1162         _help_jmenu = createMenu( "Help", getConfiguration() );
1163         _help_jmenu.add( _help_item = new JMenuItem( "Documentation" ) );
1164         _help_jmenu.addSeparator();
1165         _help_jmenu.add( _website_item = new JMenuItem( "Archaeopteryx Home" ) );
1166         _aptx_ref_item = new JMenuItem( "Archaeopteryx Reference" ); //TODO need to add this...
1167         _help_jmenu.add( _phyloxml_website_item = new JMenuItem( "phyloXML Home" ) );
1168         _help_jmenu.add( _phyloxml_ref_item = new JMenuItem( "phyloXML Reference" ) );
1169         _help_jmenu.addSeparator();
1170         _help_jmenu.add( _about_item = new JMenuItem( "About" ) );
1171         customizeJMenuItem( _help_item );
1172         customizeJMenuItem( _website_item );
1173         customizeJMenuItem( _phyloxml_website_item );
1174         customizeJMenuItem( _aptx_ref_item );
1175         customizeJMenuItem( _phyloxml_ref_item );
1176         customizeJMenuItem( _about_item );
1177         _phyloxml_ref_item.setToolTipText( PHYLOXML_REF_TOOL_TIP );
1178         _aptx_ref_item.setToolTipText( APTX_REF_TOOL_TIP );
1179         _jmenubar.add( _help_jmenu );
1180     }
1181
1182     void buildTypeMenu() {
1183         _type_menu = createMenu( TYPE_MENU_HEADER, getConfiguration() );
1184         _type_menu.add( _rectangular_type_cbmi = new JCheckBoxMenuItem( MainFrame.RECTANGULAR_TYPE_CBMI_LABEL ) );
1185         _type_menu.add( _euro_type_cbmi = new JCheckBoxMenuItem( MainFrame.EURO_TYPE_CBMI_LABEL ) );
1186         _type_menu.add( _rounded_type_cbmi = new JCheckBoxMenuItem( MainFrame.ROUNDED_TYPE_CBMI_LABEL ) );
1187         _type_menu.add( _curved_type_cbmi = new JCheckBoxMenuItem( MainFrame.CURVED_TYPE_CBMI_LABEL ) );
1188         _type_menu.add( _triangular_type_cbmi = new JCheckBoxMenuItem( MainFrame.TRIANGULAR_TYPE_CBMI_LABEL ) );
1189         _type_menu.add( _convex_type_cbmi = new JCheckBoxMenuItem( MainFrame.CONVEX_TYPE_CBMI_LABEL ) );
1190         _type_menu.add( _unrooted_type_cbmi = new JCheckBoxMenuItem( MainFrame.UNROOTED_TYPE_CBMI_LABEL ) );
1191         _type_menu.add( _circular_type_cbmi = new JCheckBoxMenuItem( MainFrame.CIRCULAR_TYPE_CBMI_LABEL ) );
1192         customizeCheckBoxMenuItem( _rectangular_type_cbmi, false );
1193         customizeCheckBoxMenuItem( _triangular_type_cbmi, false );
1194         customizeCheckBoxMenuItem( _euro_type_cbmi, false );
1195         customizeCheckBoxMenuItem( _rounded_type_cbmi, false );
1196         customizeCheckBoxMenuItem( _curved_type_cbmi, false );
1197         customizeCheckBoxMenuItem( _convex_type_cbmi, false );
1198         customizeCheckBoxMenuItem( _unrooted_type_cbmi, false );
1199         customizeCheckBoxMenuItem( _circular_type_cbmi, false );
1200         _unrooted_type_cbmi.setToolTipText( MainFrame.USE_MOUSEWHEEL_SHIFT_TO_ROTATE );
1201         _circular_type_cbmi.setToolTipText( MainFrame.USE_MOUSEWHEEL_SHIFT_TO_ROTATE );
1202         initializeTypeMenu( getOptions() );
1203         _jmenubar.add( _type_menu );
1204     }
1205
1206     void buildViewMenu() {
1207         _view_jmenu = createMenu( "View", getConfiguration() );
1208         _view_jmenu.add( _display_basic_information_item = new JMenuItem( SHOW_BASIC_TREE_INFORMATION_LABEL ) );
1209         _view_jmenu.addSeparator();
1210         _view_jmenu.add( _view_as_XML_item = new JMenuItem( "as phyloXML" ) );
1211         _view_jmenu.add( _view_as_NH_item = new JMenuItem( "as Newick" ) );
1212         _view_jmenu.add( _view_as_nexus_item = new JMenuItem( "as Nexus" ) );
1213         customizeJMenuItem( _display_basic_information_item );
1214         customizeJMenuItem( _view_as_NH_item );
1215         customizeJMenuItem( _view_as_XML_item );
1216         customizeJMenuItem( _view_as_nexus_item );
1217         _jmenubar.add( _view_jmenu );
1218     }
1219
1220     void checkTextFrames() {
1221         if ( _textframes.size() > 5 ) {
1222             try {
1223                 if ( _textframes.getFirst() != null ) {
1224                     _textframes.getFirst().removeMe();
1225                 }
1226                 else {
1227                     _textframes.removeFirst();
1228                 }
1229             }
1230             catch ( final NoSuchElementException e ) {
1231                 // Ignore.
1232             }
1233         }
1234     }
1235
1236     void choosePdfWidth() {
1237         final String s = ( String ) JOptionPane.showInputDialog( this,
1238                                                                  "Please enter the default line width for PDF export.\n"
1239                                                                          + "[current value: "
1240                                                                          + getOptions().getPrintLineWidth() + "]\n",
1241                                                                  "Line Width for PDF Export",
1242                                                                  JOptionPane.QUESTION_MESSAGE,
1243                                                                  null,
1244                                                                  null,
1245                                                                  getOptions().getPrintLineWidth() );
1246         if ( !ForesterUtil.isEmpty( s ) ) {
1247             boolean success = true;
1248             float f = 0.0f;
1249             final String m_str = s.trim();
1250             if ( !ForesterUtil.isEmpty( m_str ) ) {
1251                 try {
1252                     f = Float.parseFloat( m_str );
1253                 }
1254                 catch ( final Exception ex ) {
1255                     success = false;
1256                 }
1257             }
1258             else {
1259                 success = false;
1260             }
1261             if ( success && ( f > 0.0 ) ) {
1262                 getOptions().setPrintLineWidth( f );
1263             }
1264         }
1265     }
1266
1267     void close() {
1268         removeAllTextFrames();
1269         if ( _mainpanel != null ) {
1270             _mainpanel.terminate();
1271         }
1272         if ( _contentpane != null ) {
1273             _contentpane.removeAll();
1274         }
1275         setVisible( false );
1276         dispose();
1277     }
1278
1279     void colorRank() {
1280         if ( _mainpanel.getCurrentTreePanel() != null ) {
1281             final String[] ranks = AptxUtil.getAllPossibleRanks();
1282             final String rank = ( String ) JOptionPane
1283                     .showInputDialog( this,
1284                                       "What rank should the colorization be based on",
1285                                       "Rank Selection",
1286                                       JOptionPane.QUESTION_MESSAGE,
1287                                       null,
1288                                       ranks,
1289                                       null );
1290             if ( !ForesterUtil.isEmpty( rank ) ) {
1291                 _mainpanel.getCurrentTreePanel().colorRank( rank );
1292             }
1293         }
1294     }
1295
1296     void confColor() {
1297         if ( _mainpanel.getCurrentTreePanel() != null ) {
1298             _mainpanel.getCurrentTreePanel().confColor();
1299         }
1300     }
1301
1302     void customizeCheckBoxMenuItem( final JCheckBoxMenuItem item, final boolean is_selected ) {
1303         if ( item != null ) {
1304             item.setFont( MainFrame.menu_font );
1305             if ( !getConfiguration().isUseNativeUI() ) {
1306                 item.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
1307                 item.setForeground( getConfiguration().getGuiMenuTextColor() );
1308             }
1309             item.setSelected( is_selected );
1310             item.addActionListener( this );
1311         }
1312     }
1313
1314     JMenuItem customizeJMenuItem( final JMenuItem jmi ) {
1315         if ( jmi != null ) {
1316             jmi.setFont( MainFrame.menu_font );
1317             if ( !getConfiguration().isUseNativeUI() ) {
1318                 jmi.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
1319                 jmi.setForeground( getConfiguration().getGuiMenuTextColor() );
1320             }
1321             jmi.addActionListener( this );
1322         }
1323         return jmi;
1324     }
1325
1326     void customizeRadioButtonMenuItem( final JRadioButtonMenuItem item, final boolean is_selected ) {
1327         if ( item != null ) {
1328             item.setFont( MainFrame.menu_font );
1329             if ( !getConfiguration().isUseNativeUI() ) {
1330                 item.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
1331                 item.setForeground( getConfiguration().getGuiMenuTextColor() );
1332             }
1333             item.setSelected( is_selected );
1334             item.addActionListener( this );
1335         }
1336     }
1337
1338     void displayBasicInformation( final File treefile ) {
1339         if ( ( _mainpanel.getCurrentPhylogeny() != null ) && !_mainpanel.getCurrentPhylogeny().isEmpty() ) {
1340             String title = "Basic Information";
1341             if ( !ForesterUtil.isEmpty( _mainpanel.getCurrentPhylogeny().getName() ) ) {
1342                 title = title + " for \"" + _mainpanel.getCurrentPhylogeny().getName() + "\"";
1343             }
1344             showTextFrame( AptxUtil.createBasicInformation( _mainpanel.getCurrentPhylogeny(), treefile ), title );
1345         }
1346     }
1347
1348     void exceptionOccuredDuringOpenFile( final Exception e ) {
1349         try {
1350             _mainpanel.getCurrentTreePanel().setArrowCursor();
1351         }
1352         catch ( final Exception ex ) {
1353             // Do nothing.
1354         }
1355         JOptionPane.showMessageDialog( this,
1356                                        ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ),
1357                                        "Error during File|Open",
1358                                        JOptionPane.ERROR_MESSAGE );
1359     }
1360
1361     void executeGSDI() {
1362         if ( !isOKforSDI( false, true ) ) {
1363             return;
1364         }
1365         if ( !_mainpanel.getCurrentPhylogeny().isRooted() ) {
1366             JOptionPane.showMessageDialog( this,
1367                                            "Gene tree is not rooted.",
1368                                            "Cannot execute GSDI",
1369                                            JOptionPane.ERROR_MESSAGE );
1370             return;
1371         }
1372         final Phylogeny gene_tree = _mainpanel.getCurrentPhylogeny().copy();
1373         gene_tree.setAllNodesToNotCollapse();
1374         gene_tree.recalculateNumberOfExternalDescendants( false );
1375         GSDI gsdi = null;
1376         final Phylogeny species_tree = getSpeciesTree().copy();
1377         try {
1378             gsdi = new GSDI( gene_tree, species_tree, false, true, true, true );
1379         }
1380         catch ( final SDIException e ) {
1381             JOptionPane.showMessageDialog( this,
1382                                            e.getLocalizedMessage(),
1383                                            "Error during GSDI",
1384                                            JOptionPane.ERROR_MESSAGE );
1385             return;
1386         }
1387         catch ( final Exception e ) {
1388             AptxUtil.unexpectedException( e );
1389             return;
1390         }
1391         gene_tree.setRerootable( false );
1392         gene_tree.clearHashIdToNodeMap();
1393         gene_tree.recalculateNumberOfExternalDescendants( true );
1394         _mainpanel.addPhylogenyInNewTab( gene_tree, getConfiguration(), "gene tree", null );
1395         getMainPanel().getControlPanel().setShowEvents( true );
1396         showWhole();
1397         final int selected = _mainpanel.getTabbedPane().getSelectedIndex();
1398         _mainpanel.addPhylogenyInNewTab( species_tree, getConfiguration(), "species tree", null );
1399         showWhole();
1400         _mainpanel.getTabbedPane().setSelectedIndex( selected );
1401         showWhole();
1402         _mainpanel.getCurrentTreePanel().setEdited( true );
1403         final int poly = PhylogenyMethods.countNumberOfPolytomies( species_tree );
1404         if ( gsdi.getStrippedExternalGeneTreeNodes().size() > 0 ) {
1405             JOptionPane.showMessageDialog( this,
1406                                            "Duplications: " + gsdi.getDuplicationsSum() + "\n"
1407                                                    + "Potential duplications: "
1408                                                    + gsdi.getSpeciationOrDuplicationEventsSum() + "\n"
1409                                                    + "Speciations: " + gsdi.getSpeciationsSum() + "\n"
1410                                                    + "Stripped gene tree nodes: "
1411                                                    + gsdi.getStrippedExternalGeneTreeNodes().size() + "\n"
1412                                                    + "Taxonomy linkage based on: " + gsdi.getTaxCompBase() + "\n"
1413                                                    + "Number of polytomies in species tree used: " + poly + "\n",
1414                                            "GSDI successfully completed",
1415                                            JOptionPane.WARNING_MESSAGE );
1416         }
1417         else {
1418             JOptionPane.showMessageDialog( this,
1419                                            "Duplications: " + gsdi.getDuplicationsSum() + "\n"
1420                                                    + "Potential duplications: "
1421                                                    + gsdi.getSpeciationOrDuplicationEventsSum() + "\n"
1422                                                    + "Speciations: " + gsdi.getSpeciationsSum() + "\n"
1423                                                    + "Stripped gene tree nodes: "
1424                                                    + gsdi.getStrippedExternalGeneTreeNodes().size() + "\n"
1425                                                    + "Taxonomy linkage based on: " + gsdi.getTaxCompBase() + "\n"
1426                                                    + "Number of polytomies in species tree used: " + poly + "\n",
1427                                            "GSDI successfully completed",
1428                                            JOptionPane.INFORMATION_MESSAGE );
1429         }
1430     }
1431
1432     void executeGSDIR() {
1433         if ( !isOKforSDI( false, false ) ) {
1434             return;
1435         }
1436         final int p = PhylogenyMethods.countNumberOfPolytomies( _mainpanel.getCurrentPhylogeny() );
1437         if ( ( p > 0 )
1438                 && !( ( p == 1 ) && ( _mainpanel.getCurrentPhylogeny().getRoot().getNumberOfDescendants() == 3 ) ) ) {
1439             JOptionPane.showMessageDialog( this,
1440                                            "Gene tree is not completely binary",
1441                                            "Cannot execute GSDI",
1442                                            JOptionPane.ERROR_MESSAGE );
1443             return;
1444         }
1445         final Phylogeny gene_tree = _mainpanel.getCurrentPhylogeny().copy();
1446         gene_tree.setAllNodesToNotCollapse();
1447         gene_tree.recalculateNumberOfExternalDescendants( false );
1448         GSDIR gsdir = null;
1449         final Phylogeny species_tree = getSpeciesTree().copy();
1450         try {
1451             gsdir = new GSDIR( gene_tree, species_tree, true, true, true );
1452         }
1453         catch ( final SDIException e ) {
1454             JOptionPane.showMessageDialog( this,
1455                                            e.getLocalizedMessage(),
1456                                            "Error during GSDIR",
1457                                            JOptionPane.ERROR_MESSAGE );
1458             return;
1459         }
1460         catch ( final Exception e ) {
1461             AptxUtil.unexpectedException( e );
1462             return;
1463         }
1464         final Phylogeny result_gene_tree = gsdir.getMinDuplicationsSumGeneTree();
1465         result_gene_tree.setRerootable( false );
1466         result_gene_tree.clearHashIdToNodeMap();
1467         result_gene_tree.recalculateNumberOfExternalDescendants( true );
1468         PhylogenyMethods.orderAppearance( result_gene_tree.getRoot(), true, true, DESCENDANT_SORT_PRIORITY.NODE_NAME );
1469         _mainpanel.addPhylogenyInNewTab( result_gene_tree, getConfiguration(), "gene tree", null );
1470         getMainPanel().getControlPanel().setShowEvents( true );
1471         showWhole();
1472         final int selected = _mainpanel.getTabbedPane().getSelectedIndex();
1473         _mainpanel.addPhylogenyInNewTab( species_tree, getConfiguration(), "species tree", null );
1474         showWhole();
1475         _mainpanel.getTabbedPane().setSelectedIndex( selected );
1476         showWhole();
1477         _mainpanel.getCurrentTreePanel().setEdited( true );
1478         final int poly = PhylogenyMethods.countNumberOfPolytomies( species_tree );
1479         if ( gsdir.getStrippedExternalGeneTreeNodes().size() > 0 ) {
1480             JOptionPane.showMessageDialog( this,
1481                                            "Minimal duplications: " + gsdir.getMinDuplicationsSum() + "\n"
1482                                                    + "Speciations: " + gsdir.getSpeciationsSum() + "\n"
1483                                                    + "Stripped gene tree nodes: "
1484                                                    + gsdir.getStrippedExternalGeneTreeNodes().size() + "\n"
1485                                                    + "Taxonomy linkage based on: " + gsdir.getTaxCompBase() + "\n"
1486                                                    + "Number of polytomies in species tree used: " + poly + "\n",
1487                                            "GSDIR successfully completed",
1488                                            JOptionPane.WARNING_MESSAGE );
1489         }
1490         else {
1491             JOptionPane.showMessageDialog( this,
1492                                            "Minimal duplications: " + gsdir.getMinDuplicationsSum() + "\n"
1493                                                    + "Speciations: " + gsdir.getSpeciationsSum() + "\n"
1494                                                    + "Stripped gene tree nodes: "
1495                                                    + gsdir.getStrippedExternalGeneTreeNodes().size() + "\n"
1496                                                    + "Taxonomy linkage based on: " + gsdir.getTaxCompBase() + "\n"
1497                                                    + "Number of polytomies in species tree used: " + poly + "\n",
1498                                            "GSDIR successfully completed",
1499                                            JOptionPane.INFORMATION_MESSAGE );
1500         }
1501     }
1502
1503     void executeLineageInference() {
1504         if ( ( _mainpanel.getCurrentPhylogeny() == null ) || ( _mainpanel.getCurrentPhylogeny().isEmpty() ) ) {
1505             return;
1506         }
1507         if ( !_mainpanel.getCurrentPhylogeny().isRooted() ) {
1508             JOptionPane.showMessageDialog( this,
1509                                            "Phylogeny is not rooted.",
1510                                            "Cannot infer ancestral taxonomies",
1511                                            JOptionPane.ERROR_MESSAGE );
1512             return;
1513         }
1514         final AncestralTaxonomyInferrer inferrer = new AncestralTaxonomyInferrer( this,
1515                                                                                   _mainpanel.getCurrentTreePanel(),
1516                                                                                   _mainpanel.getCurrentPhylogeny()
1517                                                                                           .copy() );
1518         new Thread( inferrer ).start();
1519     }
1520
1521     boolean GAndSDoHaveMoreThanOneSpeciesInComman( final Phylogeny gene_tree ) {
1522         if ( ( gene_tree == null ) || gene_tree.isEmpty() ) {
1523             JOptionPane.showMessageDialog( this,
1524                                            "Gene tree and species tree have no species in common.",
1525                                            "Error during SDI",
1526                                            JOptionPane.ERROR_MESSAGE );
1527             return false;
1528         }
1529         else if ( gene_tree.getNumberOfExternalNodes() < 2 ) {
1530             JOptionPane.showMessageDialog( this,
1531                                            "Gene tree and species tree have only one species in common.",
1532                                            "Error during SDI",
1533                                            JOptionPane.ERROR_MESSAGE );
1534             return false;
1535         }
1536         else {
1537             return true;
1538         }
1539     }
1540
1541     ControlPanel getControlPanel() {
1542         return getMainPanel().getControlPanel();
1543     }
1544
1545     File getCurrentDir() {
1546         if ( ( _current_dir == null ) || !_current_dir.canRead() ) {
1547             if ( ForesterUtil.isWindows() ) {
1548                 try {
1549                     _current_dir = new File( WindowsUtils.getCurrentUserDesktopPath() );
1550                 }
1551                 catch ( final Exception e ) {
1552                     _current_dir = null;
1553                 }
1554             }
1555         }
1556         if ( ( _current_dir == null ) || !_current_dir.canRead() ) {
1557             if ( System.getProperty( "user.home" ) != null ) {
1558                 _current_dir = new File( System.getProperty( "user.home" ) );
1559             }
1560             else if ( System.getProperty( "user.dir" ) != null ) {
1561                 _current_dir = new File( System.getProperty( "user.dir" ) );
1562             }
1563         }
1564         return _current_dir;
1565     }
1566
1567     TreePanel getCurrentTreePanel() {
1568         return getMainPanel().getCurrentTreePanel();
1569     }
1570
1571     JMenu getHelpMenu() {
1572         return _help_jmenu;
1573     }
1574
1575     JCheckBoxMenuItem getlabelDirectionCbmi() {
1576         return _label_direction_cbmi;
1577     }
1578
1579     JMenuBar getMenuBarOfMainFrame() {
1580         return _jmenubar;
1581     }
1582
1583     final Phylogeny getSpeciesTree() {
1584         return _species_tree;
1585     }
1586
1587     void initializeTypeMenu( final Options options ) {
1588         setTypeMenuToAllUnselected();
1589         switch ( options.getPhylogenyGraphicsType() ) {
1590             case CONVEX:
1591                 _convex_type_cbmi.setSelected( true );
1592                 break;
1593             case CURVED:
1594                 _curved_type_cbmi.setSelected( true );
1595                 break;
1596             case EURO_STYLE:
1597                 _euro_type_cbmi.setSelected( true );
1598                 break;
1599             case ROUNDED:
1600                 _rounded_type_cbmi.setSelected( true );
1601                 break;
1602             case TRIANGULAR:
1603                 _triangular_type_cbmi.setSelected( true );
1604                 break;
1605             case UNROOTED:
1606                 _unrooted_type_cbmi.setSelected( true );
1607                 break;
1608             case CIRCULAR:
1609                 _circular_type_cbmi.setSelected( true );
1610                 break;
1611             default:
1612                 _rectangular_type_cbmi.setSelected( true );
1613                 break;
1614         }
1615     }
1616
1617     boolean isOKforSDI( final boolean species_tree_has_to_binary, final boolean gene_tree_has_to_binary ) {
1618         if ( ( _mainpanel.getCurrentPhylogeny() == null ) || _mainpanel.getCurrentPhylogeny().isEmpty() ) {
1619             return false;
1620         }
1621         else if ( ( getSpeciesTree() == null ) || getSpeciesTree().isEmpty() ) {
1622             JOptionPane.showMessageDialog( this,
1623                                            "No species tree loaded",
1624                                            "Cannot execute GSDI",
1625                                            JOptionPane.ERROR_MESSAGE );
1626             return false;
1627         }
1628         else if ( species_tree_has_to_binary && !getSpeciesTree().isCompletelyBinary() ) {
1629             JOptionPane.showMessageDialog( this,
1630                                            "Species tree is not completely binary",
1631                                            "Cannot execute GSDI",
1632                                            JOptionPane.ERROR_MESSAGE );
1633             return false;
1634         }
1635         else if ( gene_tree_has_to_binary && !_mainpanel.getCurrentPhylogeny().isCompletelyBinary() ) {
1636             JOptionPane.showMessageDialog( this,
1637                                            "Gene tree is not completely binary",
1638                                            "Cannot execute GSDI",
1639                                            JOptionPane.ERROR_MESSAGE );
1640             return false;
1641         }
1642         else {
1643             return true;
1644         }
1645     }
1646
1647     boolean isSubtreeDisplayed() {
1648         if ( getCurrentTreePanel() != null ) {
1649             if ( getCurrentTreePanel().isCurrentTreeIsSubtree() ) {
1650                 JOptionPane
1651                         .showMessageDialog( this,
1652                                             "This operation can only be performed on a complete tree, not on the currently displayed sub-tree only.",
1653                                             "Operation can not be exectuted on a sub-tree",
1654                                             JOptionPane.WARNING_MESSAGE );
1655                 return true;
1656             }
1657         }
1658         return false;
1659     }
1660
1661     void midpointRoot() {
1662         if ( _mainpanel.getCurrentTreePanel() != null ) {
1663             _mainpanel.getCurrentTreePanel().midpointRoot();
1664         }
1665     }
1666
1667     void readPhylogeniesFromWebservice( final int i ) {
1668         final UrlTreeReader reader = new UrlTreeReader( this, i );
1669         new Thread( reader ).start();
1670     }
1671
1672     void removeAllTextFrames() {
1673         for( final TextFrame tf : _textframes ) {
1674             if ( tf != null ) {
1675                 tf.close();
1676             }
1677         }
1678         _textframes.clear();
1679     }
1680
1681     void resetSearch() {
1682         getMainPanel().getCurrentTreePanel().setFoundNodes0( null );
1683         getMainPanel().getCurrentTreePanel().setFoundNodes1( null );
1684         getMainPanel().getControlPanel().setSearchFoundCountsOnLabel0( 0 );
1685         getMainPanel().getControlPanel().getSearchFoundCountsLabel0().setVisible( false );
1686         getMainPanel().getControlPanel().getSearchTextField0().setText( "" );
1687         getMainPanel().getControlPanel().getSearchResetButton0().setEnabled( false );
1688         getMainPanel().getControlPanel().getSearchResetButton0().setVisible( false );
1689         getMainPanel().getControlPanel().setSearchFoundCountsOnLabel1( 0 );
1690         getMainPanel().getControlPanel().getSearchFoundCountsLabel1().setVisible( false );
1691         getMainPanel().getControlPanel().getSearchTextField1().setText( "" );
1692         getMainPanel().getControlPanel().getSearchResetButton1().setEnabled( false );
1693         getMainPanel().getControlPanel().getSearchResetButton1().setVisible( false );
1694     }
1695
1696     void setConfiguration( final Configuration configuration ) {
1697         _configuration = configuration;
1698     }
1699
1700     void setCurrentDir( final File current_dir ) {
1701         _current_dir = current_dir;
1702     }
1703
1704     void setInferenceManager( final InferenceManager i ) {
1705         _inference_manager = i;
1706     }
1707
1708     void setOptions( final Options options ) {
1709         _options = options;
1710     }
1711
1712     void setSelectedTypeInTypeMenu( final PHYLOGENY_GRAPHICS_TYPE type ) {
1713         setTypeMenuToAllUnselected();
1714         switch ( type ) {
1715             case CIRCULAR:
1716                 _circular_type_cbmi.setSelected( true );
1717                 break;
1718             case CONVEX:
1719                 _convex_type_cbmi.setSelected( true );
1720                 break;
1721             case CURVED:
1722                 _curved_type_cbmi.setSelected( true );
1723                 break;
1724             case EURO_STYLE:
1725                 _euro_type_cbmi.setSelected( true );
1726                 break;
1727             case ROUNDED:
1728                 _rounded_type_cbmi.setSelected( true );
1729                 break;
1730             case RECTANGULAR:
1731                 _rectangular_type_cbmi.setSelected( true );
1732                 break;
1733             case TRIANGULAR:
1734                 _triangular_type_cbmi.setSelected( true );
1735                 break;
1736             case UNROOTED:
1737                 _unrooted_type_cbmi.setSelected( true );
1738                 break;
1739             default:
1740                 throw new IllegalArgumentException( "unknown type: " + type );
1741         }
1742     }
1743
1744     final void setSpeciesTree( final Phylogeny species_tree ) {
1745         _species_tree = species_tree;
1746     }
1747
1748     void setTypeMenuToAllUnselected() {
1749         _convex_type_cbmi.setSelected( false );
1750         _curved_type_cbmi.setSelected( false );
1751         _euro_type_cbmi.setSelected( false );
1752         _rounded_type_cbmi.setSelected( false );
1753         _triangular_type_cbmi.setSelected( false );
1754         _rectangular_type_cbmi.setSelected( false );
1755         _unrooted_type_cbmi.setSelected( false );
1756         _circular_type_cbmi.setSelected( false );
1757     }
1758
1759     void switchColors() {
1760         final TreeColorSet colorset = _mainpanel.getTreeColorSet();
1761         final ColorSchemeChooser csc = new ColorSchemeChooser( getMainPanel(), colorset );
1762         csc.setVisible( true );
1763     }
1764
1765     void taxColor() {
1766         if ( _mainpanel.getCurrentTreePanel() != null ) {
1767             _mainpanel.getCurrentTreePanel().taxColor();
1768         }
1769     }
1770
1771     void typeChanged( final Object o ) {
1772         updateTypeCheckboxes( getOptions(), o );
1773         updateOptions( getOptions() );
1774         if ( getCurrentTreePanel() != null ) {
1775             final PHYLOGENY_GRAPHICS_TYPE previous_type = getCurrentTreePanel().getPhylogenyGraphicsType();
1776             final PHYLOGENY_GRAPHICS_TYPE new_type = getOptions().getPhylogenyGraphicsType();
1777             if ( ( ( previous_type == PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) && ( new_type != PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) )
1778                     || ( ( previous_type == PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) && ( new_type != PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) )
1779                     || ( ( previous_type != PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) && ( new_type == PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) )
1780                     || ( ( previous_type != PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) && ( new_type == PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) ) ) {
1781                 getCurrentTreePanel().getControlPanel().showWhole();
1782             }
1783             if ( getCurrentTreePanel().isPhyHasBranchLengths() && ( new_type != PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) ) {
1784                 getCurrentTreePanel().getControlPanel().setDrawPhylogramEnabled( true );
1785             }
1786             else {
1787                 getCurrentTreePanel().getControlPanel().setDrawPhylogramEnabled( false );
1788             }
1789             getCurrentTreePanel().setPhylogenyGraphicsType( getOptions().getPhylogenyGraphicsType() );
1790             updateScreenTextAntialias( getMainPanel().getTreePanels() );
1791             if ( getCurrentTreePanel().getControlPanel().getDynamicallyHideData() != null ) {
1792                 if ( new_type == PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) {
1793                     getCurrentTreePanel().getControlPanel().getDynamicallyHideData().setEnabled( false );
1794                 }
1795                 else {
1796                     getCurrentTreePanel().getControlPanel().getDynamicallyHideData().setEnabled( true );
1797                 }
1798             }
1799         }
1800     }
1801
1802     void updateOptions( final Options options ) {
1803         options.setAntialiasScreen( ( _screen_antialias_cbmi != null ) && _screen_antialias_cbmi.isSelected() );
1804         options.setBackgroundColorGradient( ( _background_gradient_cbmi != null )
1805                 && _background_gradient_cbmi.isSelected() );
1806         options.setShowDomainLabels( ( _show_domain_labels != null ) && _show_domain_labels.isSelected() );
1807         options.setShowAnnotationRefSource( ( _show_annotation_ref_source != null )
1808                 && _show_annotation_ref_source.isSelected() );
1809         options.setAbbreviateScientificTaxonNames( ( _abbreviate_scientific_names != null )
1810                 && _abbreviate_scientific_names.isSelected() );
1811         options.setColorLabelsSameAsParentBranch( ( _color_labels_same_as_parent_branch != null )
1812                 && _color_labels_same_as_parent_branch.isSelected() );
1813         options.setShowDefaultNodeShapesInternal( ( _show_default_node_shapes_internal_cbmi != null )
1814                 && _show_default_node_shapes_internal_cbmi.isSelected() );
1815         options.setShowDefaultNodeShapesExternal( ( _show_default_node_shapes_external_cbmi != null )
1816                 && _show_default_node_shapes_external_cbmi.isSelected() );
1817         options.setShowDefaultNodeShapesForMarkedNodes( ( _show_default_node_shapes_for_marked_cbmi != null )
1818                 && _show_default_node_shapes_for_marked_cbmi.isSelected() );
1819         if ( ( _non_lined_up_cladograms_rbmi != null ) && ( _non_lined_up_cladograms_rbmi.isSelected() ) ) {
1820             options.setCladogramType( CLADOGRAM_TYPE.NON_LINED_UP );
1821         }
1822         else if ( ( _uniform_cladograms_rbmi != null ) && ( _uniform_cladograms_rbmi.isSelected() ) ) {
1823             options.setCladogramType( CLADOGRAM_TYPE.TOTAL_NODE_SUM_DEP );
1824         }
1825         else if ( ( _ext_node_dependent_cladogram_rbmi != null ) && ( _ext_node_dependent_cladogram_rbmi.isSelected() ) ) {
1826             options.setCladogramType( CLADOGRAM_TYPE.EXT_NODE_SUM_DEP );
1827         }
1828         options.setSearchCaseSensitive( ( _search_case_senstive_cbmi != null )
1829                 && _search_case_senstive_cbmi.isSelected() );
1830         if ( ( _show_scale_cbmi != null ) && _show_scale_cbmi.isEnabled() ) {
1831             options.setShowScale( _show_scale_cbmi.isSelected() );
1832         }
1833         if ( _label_direction_cbmi != null ) {
1834             if ( _label_direction_cbmi.isSelected() ) {
1835                 options.setNodeLabelDirection( NODE_LABEL_DIRECTION.RADIAL );
1836             }
1837             else {
1838                 options.setNodeLabelDirection( NODE_LABEL_DIRECTION.HORIZONTAL );
1839             }
1840         }
1841         options.setShowOverview( ( _show_overview_cbmi != null ) && _show_overview_cbmi.isSelected() );
1842         options.setShowConfidenceStddev( ( _show_confidence_stddev_cbmi != null )
1843                 && _show_confidence_stddev_cbmi.isSelected() );
1844         if ( ( _color_by_taxonomic_group_cbmi != null ) && _color_by_taxonomic_group_cbmi.isEnabled() ) {
1845             options.setColorByTaxonomicGroup( _color_by_taxonomic_group_cbmi.isSelected() );
1846         }
1847         options.setAntialiasPrint( ( _antialias_print_cbmi != null ) && _antialias_print_cbmi.isSelected() );
1848         if ( ( _use_brackets_for_conf_in_nh_export_cbmi != null )
1849                 && _use_brackets_for_conf_in_nh_export_cbmi.isSelected() ) {
1850             options.setNhConversionSupportValueStyle( NH_CONVERSION_SUPPORT_VALUE_STYLE.IN_SQUARE_BRACKETS );
1851         }
1852         else if ( ( _use_internal_names_for_conf_in_nh_export_cbmi != null )
1853                 && _use_internal_names_for_conf_in_nh_export_cbmi.isSelected() ) {
1854             options.setNhConversionSupportValueStyle( NH_CONVERSION_SUPPORT_VALUE_STYLE.AS_INTERNAL_NODE_NAMES );
1855         }
1856         else {
1857             options.setNhConversionSupportValueStyle( NH_CONVERSION_SUPPORT_VALUE_STYLE.NONE );
1858         }
1859         options.setPrintBlackAndWhite( ( _print_black_and_white_cbmi != null )
1860                 && _print_black_and_white_cbmi.isSelected() );
1861         options.setInternalNumberAreConfidenceForNhParsing( ( _internal_number_are_confidence_for_nh_parsing_cbmi != null )
1862                 && _internal_number_are_confidence_for_nh_parsing_cbmi.isSelected() );
1863         if ( ( _extract_taxonomy_pfam_strict_rbmi != null ) && _extract_taxonomy_pfam_strict_rbmi.isSelected() ) {
1864             options.setTaxonomyExtraction( TAXONOMY_EXTRACTION.PFAM_STYLE_STRICT );
1865         }
1866         else if ( ( _extract_taxonomy_pfam_relaxed_rbmi != null ) && _extract_taxonomy_pfam_relaxed_rbmi.isSelected() ) {
1867             options.setTaxonomyExtraction( TAXONOMY_EXTRACTION.PFAM_STYLE_RELAXED );
1868         }
1869         else if ( ( _extract_taxonomy_agressive_rbmi != null ) && _extract_taxonomy_agressive_rbmi.isSelected() ) {
1870             options.setTaxonomyExtraction( TAXONOMY_EXTRACTION.AGGRESSIVE );
1871         }
1872         else if ( ( _extract_taxonomy_no_rbmi != null ) && _extract_taxonomy_no_rbmi.isSelected() ) {
1873             options.setTaxonomyExtraction( TAXONOMY_EXTRACTION.NO );
1874         }
1875         options.setReplaceUnderscoresInNhParsing( ( _replace_underscores_cbmi != null )
1876                 && _replace_underscores_cbmi.isSelected() );
1877         options.setAllowErrorsInDistanceToParent( ( _allow_errors_in_distance_to_parent_cbmi != null )
1878                 && _allow_errors_in_distance_to_parent_cbmi.isSelected() );
1879         options.setMatchWholeTermsOnly( ( _search_whole_words_only_cbmi != null )
1880                 && _search_whole_words_only_cbmi.isSelected() );
1881         options.setSearchWithRegex( ( _search_with_regex_cbmi != null ) && _search_with_regex_cbmi.isSelected() );
1882         options.setInverseSearchResult( ( _inverse_search_result_cbmi != null )
1883                 && _inverse_search_result_cbmi.isSelected() );
1884         if ( _graphics_export_visible_only_cbmi != null ) {
1885             options.setGraphicsExportVisibleOnly( _graphics_export_visible_only_cbmi.isSelected() );    
1886         }
1887         if ( ( _rectangular_type_cbmi != null ) && _rectangular_type_cbmi.isSelected() ) {
1888             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
1889         }
1890         else if ( ( _triangular_type_cbmi != null ) && _triangular_type_cbmi.isSelected() ) {
1891             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.TRIANGULAR );
1892         }
1893         else if ( ( _curved_type_cbmi != null ) && _curved_type_cbmi.isSelected() ) {
1894             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.CURVED );
1895         }
1896         else if ( ( _convex_type_cbmi != null ) && _convex_type_cbmi.isSelected() ) {
1897             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.CONVEX );
1898         }
1899         else if ( ( _euro_type_cbmi != null ) && _euro_type_cbmi.isSelected() ) {
1900             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.EURO_STYLE );
1901         }
1902         else if ( ( _rounded_type_cbmi != null ) && _rounded_type_cbmi.isSelected() ) {
1903             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.ROUNDED );
1904         }
1905         else if ( ( _unrooted_type_cbmi != null ) && _unrooted_type_cbmi.isSelected() ) {
1906             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.UNROOTED );
1907         }
1908         else if ( ( _circular_type_cbmi != null ) && _circular_type_cbmi.isSelected() ) {
1909             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.CIRCULAR );
1910         }
1911         if ( ( _right_line_up_domains_cbmi != null ) && _right_line_up_domains_cbmi.isEnabled() ) {
1912             options.setRightLineUpDomains( _right_line_up_domains_cbmi.isSelected() );
1913         }
1914         if ( ( _line_up_renderable_data_cbmi != null ) && _line_up_renderable_data_cbmi.isEnabled() ) {
1915             options.setLineUpRendarableNodeData( _line_up_renderable_data_cbmi.isSelected() );
1916         }
1917         if ( ( _color_all_found_nodes_when_coloring_subtree_cbmi != null ) && _color_all_found_nodes_when_coloring_subtree_cbmi.isEnabled() ) {
1918             options.setColorAllFoundNodesWhenColoringSubtree( _color_all_found_nodes_when_coloring_subtree_cbmi.isSelected() );
1919         }
1920         if ( ( _parse_beast_style_extended_nexus_tags_cbmi != null ) && _parse_beast_style_extended_nexus_tags_cbmi.isEnabled() ) {
1921             options.setParseBeastStyleExtendedNexusTags(_parse_beast_style_extended_nexus_tags_cbmi.isSelected() );
1922         }
1923     }
1924
1925     void updateTypeCheckboxes( final Options options, final Object o ) {
1926         setTypeMenuToAllUnselected();
1927         ( ( JCheckBoxMenuItem ) o ).setSelected( true );
1928     }
1929
1930     void viewAsNexus() {
1931         if ( ( _mainpanel.getCurrentPhylogeny() != null ) && !_mainpanel.getCurrentPhylogeny().isEmpty() ) {
1932             String title = "Nexus";
1933             if ( !ForesterUtil.isEmpty( _mainpanel.getCurrentPhylogeny().getName() ) ) {
1934                 title = "\"" + getMainPanel().getCurrentPhylogeny().getName() + "\" in " + title;
1935             }
1936             showTextFrame( _mainpanel.getCurrentPhylogeny().toNexus( getOptions().getNhConversionSupportValueStyle() ),
1937                            title );
1938         }
1939     }
1940
1941     void viewAsNH() {
1942         if ( ( _mainpanel.getCurrentPhylogeny() != null ) && !_mainpanel.getCurrentPhylogeny().isEmpty() ) {
1943             String title = "New Hampshire";
1944             if ( !ForesterUtil.isEmpty( _mainpanel.getCurrentPhylogeny().getName() ) ) {
1945                 title = "\"" + getMainPanel().getCurrentPhylogeny().getName() + "\" in " + title;
1946             }
1947             showTextFrame( _mainpanel.getCurrentPhylogeny().toNewHampshire( getOptions()
1948                                    .getNhConversionSupportValueStyle() ),
1949                            title );
1950         }
1951     }
1952
1953     void viewAsXML() {
1954         if ( ( _mainpanel.getCurrentPhylogeny() != null ) && !_mainpanel.getCurrentPhylogeny().isEmpty() ) {
1955             String title = "phyloXML";
1956             if ( !ForesterUtil.isEmpty( _mainpanel.getCurrentPhylogeny().getName() ) ) {
1957                 title = "\"" + getMainPanel().getCurrentPhylogeny().getName() + "\" in " + title;
1958             }
1959             showTextFrame( _mainpanel.getCurrentPhylogeny().toPhyloXML( 0 ), title );
1960         }
1961     }
1962
1963     private static void cycleNodeDataReturn( final Options op, final Configuration conf ) {
1964         switch ( op.getExtDescNodeDataToReturn() ) {
1965             case UNKNOWN:
1966                 op.setExtDescNodeDataToReturn( NodeDataField.DOMAINS_ALL );
1967                 break;
1968             case DOMAINS_ALL:
1969                 op.setExtDescNodeDataToReturn( NodeDataField.DOMAINS_COLLAPSED_PER_PROTEIN );
1970                 break;
1971             case DOMAINS_COLLAPSED_PER_PROTEIN:
1972                 op.setExtDescNodeDataToReturn( NodeDataField.SEQ_ANNOTATIONS );
1973                 break;
1974             case SEQ_ANNOTATIONS:
1975                 op.setExtDescNodeDataToReturn( NodeDataField.GO_TERM_IDS );
1976                 break;
1977             case GO_TERM_IDS:
1978                 op.setExtDescNodeDataToReturn( NodeDataField.SEQUENCE_MOL_SEQ_FASTA );
1979                 break;
1980             case SEQUENCE_MOL_SEQ_FASTA:
1981                 if ( ( conf != null ) && ( conf.getExtDescNodeDataToReturn() != null )
1982                         && ( conf.getExtDescNodeDataToReturn() != NodeDataField.DOMAINS_ALL )
1983                         && ( conf.getExtDescNodeDataToReturn() != NodeDataField.DOMAINS_COLLAPSED_PER_PROTEIN )
1984                         && ( conf.getExtDescNodeDataToReturn() != NodeDataField.SEQ_ANNOTATIONS )
1985                         && ( conf.getExtDescNodeDataToReturn() != NodeDataField.GO_TERM_IDS )
1986                         && ( conf.getExtDescNodeDataToReturn() != NodeDataField.SEQUENCE_MOL_SEQ_FASTA ) ) {
1987                     op.setExtDescNodeDataToReturn( conf.getExtDescNodeDataToReturn() );
1988                 }
1989                 else {
1990                     op.setExtDescNodeDataToReturn( NodeDataField.UNKNOWN );
1991                 }
1992                 break;
1993             default:
1994                 op.setExtDescNodeDataToReturn( NodeDataField.UNKNOWN );
1995         }
1996     }
1997
1998     /**
1999      * Display the about box.
2000      */
2001     static void about() {
2002         final StringBuffer about = new StringBuffer( "Archaeopteryx\nVersion " + AptxConstants.VERSION + "\n" );
2003         about.append( "Copyright (C) 2016 Christian M Zmasek\n" );
2004         about.append( "All Rights Reserved\n" );
2005         about.append( "License: GNU Lesser General Public License (LGPL)\n" );
2006         about.append( "Last modified: " + AptxConstants.PRG_DATE + "\n" );
2007         about.append( "Based on: " + ForesterUtil.getForesterLibraryInformation() + "\n" );
2008         about.append( "phyloXML version : " + ForesterConstants.PHYLO_XML_VERSION + "\n" );
2009         about.append( "phyloXML location: " + ForesterConstants.PHYLO_XML_LOCATION + "\n" );
2010         if ( !ForesterUtil.isEmpty( ForesterUtil.JAVA_VERSION ) && !ForesterUtil.isEmpty( ForesterUtil.JAVA_VENDOR ) ) {
2011             about.append( "[your Java version: " + ForesterUtil.JAVA_VERSION + " " + ForesterUtil.JAVA_VENDOR + "]\n" );
2012         }
2013         if ( !ForesterUtil.isEmpty( ForesterUtil.OS_NAME ) && !ForesterUtil.isEmpty( ForesterUtil.OS_ARCH )
2014                 && !ForesterUtil.isEmpty( ForesterUtil.OS_VERSION ) ) {
2015             about.append( "[your OS: " + ForesterUtil.OS_NAME + " " + ForesterUtil.OS_ARCH + " "
2016                     + ForesterUtil.OS_VERSION + "]\n" );
2017         }
2018         final Runtime rt = java.lang.Runtime.getRuntime();
2019         final long free_memory = rt.freeMemory() / 1000000;
2020         final long total_memory = rt.totalMemory() / 1000000;
2021         about.append( "[free memory: " + free_memory + "MB, total memory: " + total_memory + "MB]\n" );
2022         about.append( "[locale: " + Locale.getDefault() + "]\n" );
2023         about.append( "References:\n" );
2024         about.append( AptxConstants.PHYLOXML_REFERENCE_SHORT + "\n" );
2025         about.append( "For more information & download:\n" );
2026         about.append( AptxConstants.APTX_WEB_SITE + "\n" );
2027         about.append( "Documentation:\n" );
2028         about.append( AptxConstants.APTX_DOC_SITE + "\n" );
2029         about.append( "Comments: " + AptxConstants.AUTHOR_EMAIL );
2030         JOptionPane.showMessageDialog( null, about, AptxConstants.PRG_NAME, JOptionPane.PLAIN_MESSAGE );
2031     }
2032
2033     static void chooseNodeSize( final Options options, final Component parent ) {
2034         final String s = ( String ) JOptionPane.showInputDialog( parent,
2035                                                                  "Please enter the default size for node shapes.\n"
2036                                                                          + "[current value: "
2037                                                                          + options.getDefaultNodeShapeSize() + "]\n",
2038                                                                  "Node Shape Size",
2039                                                                  JOptionPane.QUESTION_MESSAGE,
2040                                                                  null,
2041                                                                  null,
2042                                                                  options.getDefaultNodeShapeSize() );
2043         if ( !ForesterUtil.isEmpty( s ) ) {
2044             boolean success = true;
2045             double m = 0.0;
2046             final String m_str = s.trim();
2047             if ( !ForesterUtil.isEmpty( m_str ) ) {
2048                 try {
2049                     m = Double.parseDouble( m_str );
2050                 }
2051                 catch ( final Exception ex ) {
2052                     success = false;
2053                 }
2054             }
2055             else {
2056                 success = false;
2057             }
2058             if ( success && ( m >= 0.0 ) ) {
2059                 final short size = ForesterUtil.roundToShort( m );
2060                 if ( size >= 0.0 ) {
2061                     options.setDefaultNodeShapeSize( size );
2062                 }
2063             }
2064         }
2065     }
2066
2067     static String createCurrentFontDesc( final TreeFontSet tree_font_set ) {
2068         return tree_font_set.getLargeFont().getFamily() + " " + tree_font_set.getLargeFont().getSize();
2069     }
2070
2071     static JMenu createMenu( final String title, final Configuration conf ) {
2072         final JMenu jmenu = new JMenu( title );
2073         if ( !conf.isUseNativeUI() ) {
2074             jmenu.setFont( MainFrame.menu_font );
2075             jmenu.setBackground( conf.getGuiMenuBackgroundColor() );
2076             jmenu.setForeground( conf.getGuiMenuTextColor() );
2077         }
2078         return jmenu;
2079     }
2080
2081     static JMenuItem customizeMenuItemAsLabel( final JMenuItem label, final Configuration configuration ) {
2082         label.setFont( MainFrame.menu_font.deriveFont( Font.BOLD ) );
2083         if ( !configuration.isUseNativeUI() ) {
2084             label.setBackground( configuration.getGuiMenuBackgroundColor() );
2085             label.setForeground( configuration.getGuiMenuTextColor() );
2086             label.setOpaque( true );
2087         }
2088         label.setSelected( false );
2089         label.setEnabled( false );
2090         return label;
2091     }
2092
2093     static void cycleNodeFill( final Options op ) {
2094         switch ( op.getDefaultNodeFill() ) {
2095             case GRADIENT:
2096                 op.setDefaultNodeFill( NodeFill.SOLID );
2097                 break;
2098             case NONE:
2099                 op.setDefaultNodeFill( NodeFill.GRADIENT );
2100                 break;
2101             case SOLID:
2102                 op.setDefaultNodeFill( NodeFill.NONE );
2103                 break;
2104             default:
2105                 throw new RuntimeException( "unknown fill: " + op.getDefaultNodeFill() );
2106         }
2107     }
2108
2109     static void cycleNodeShape( final Options op ) {
2110         switch ( op.getDefaultNodeShape() ) {
2111             case CIRCLE:
2112                 op.setDefaultNodeShape( NodeShape.RECTANGLE );
2113                 break;
2114             case RECTANGLE:
2115                 op.setDefaultNodeShape( NodeShape.CIRCLE );
2116                 break;
2117             default:
2118                 throw new RuntimeException( "unknown shape: " + op.getDefaultNodeShape() );
2119         }
2120     }
2121
2122     static void cycleOverview( final Options op, final TreePanel tree_panel ) {
2123         switch ( op.getOvPlacement() ) {
2124             case LOWER_LEFT:
2125                 op.setOvPlacement( Options.OVERVIEW_PLACEMENT_TYPE.UPPER_LEFT );
2126                 break;
2127             case LOWER_RIGHT:
2128                 op.setOvPlacement( Options.OVERVIEW_PLACEMENT_TYPE.LOWER_LEFT );
2129                 break;
2130             case UPPER_LEFT:
2131                 op.setOvPlacement( Options.OVERVIEW_PLACEMENT_TYPE.UPPER_RIGHT );
2132                 break;
2133             case UPPER_RIGHT:
2134                 op.setOvPlacement( Options.OVERVIEW_PLACEMENT_TYPE.LOWER_RIGHT );
2135                 break;
2136             default:
2137                 throw new RuntimeException( "unknown placement: " + op.getOvPlacement() );
2138         }
2139         if ( tree_panel != null ) {
2140             tree_panel.updateOvSettings();
2141         }
2142     }
2143
2144     static void exceptionOccuredDuringSaveAs( final Exception e, final TreePanel tp, final Component comp ) {
2145         try {
2146             tp.setArrowCursor();
2147         }
2148         catch ( final Exception ex ) {
2149             // Do nothing.
2150         }
2151         JOptionPane.showMessageDialog( comp, "Exception" + e, "Error during File|SaveAs", JOptionPane.ERROR_MESSAGE );
2152     }
2153
2154     static void print( final TreePanel tp, final Options op, final Component c ) {
2155         if ( ( tp == null ) || ( tp.getPhylogeny() == null ) || tp.getPhylogeny().isEmpty() ) {
2156             return;
2157         }
2158         final String job_name = AptxConstants.PRG_NAME;
2159         boolean error = false;
2160         String printer_name = null;
2161         try {
2162             printer_name = Printer.print( tp, job_name );
2163         }
2164         catch ( final Exception e ) {
2165             error = true;
2166             JOptionPane.showMessageDialog( c, e.getMessage(), "Printing Error", JOptionPane.ERROR_MESSAGE );
2167         }
2168         if ( !error && ( printer_name != null ) ) {
2169             String msg = "Printing data sent to printer";
2170             if ( printer_name.length() > 1 ) {
2171                 msg += " [" + printer_name + "]";
2172             }
2173             JOptionPane.showMessageDialog( c, msg, "Printing...", JOptionPane.INFORMATION_MESSAGE );
2174         }
2175         if ( !op.isPrintUsingActualSize() ) {
2176             tp.getControlPanel().showWhole();
2177         }
2178     }
2179
2180     static void printPhylogenyToPdf( final String file_name,
2181                                      final Options opts,
2182                                      final TreePanel tp,
2183                                      final Component comp ) {
2184        
2185         String pdf_written_to = "";
2186         boolean error = false;
2187         try {
2188             if ( opts.isPrintUsingActualSize() ) {
2189                 pdf_written_to = PdfExporter.writePhylogenyToPdf( file_name, tp, tp.getWidth() , tp.getHeight()  );
2190             }
2191             else {
2192                 // Never false.
2193             }
2194         }
2195         catch ( final IOException e ) {
2196             error = true;
2197             JOptionPane.showMessageDialog( comp, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE );
2198         }
2199         if ( !error ) {
2200             if ( !ForesterUtil.isEmpty( pdf_written_to ) ) {
2201                 JOptionPane.showMessageDialog( comp,
2202                                                "Wrote PDF to: " + pdf_written_to,
2203                                                "Information",
2204                                                JOptionPane.INFORMATION_MESSAGE );
2205             }
2206             else {
2207                 JOptionPane.showMessageDialog( comp,
2208                                                "There was an unknown problem when attempting to write to PDF file: \""
2209                                                        + file_name + "\"",
2210                                                "Error",
2211                                                JOptionPane.ERROR_MESSAGE );
2212             }
2213         }
2214         if ( !opts.isPrintUsingActualSize() ) {
2215             tp.getControlPanel().showWhole();
2216         }
2217     }
2218
2219     static void setCycleDataReturnMenuItem( final JMenuItem mi, final Options options ) {
2220         if ( ( options != null ) && ( options.getExtDescNodeDataToReturn() != null ) ) {
2221             mi.setText( "Cycle Node Return Data... (current: " + options.getExtDescNodeDataToReturn().toString() + ")" );
2222         }
2223         else {
2224             mi.setText( "Cycle Node Return Data..." );
2225         }
2226     }
2227
2228     static void setCycleNodeFillMenuItem( final JMenuItem mi, final Options options ) {
2229         if ( ( options != null ) && ( options.getDefaultNodeFill() != null ) ) {
2230             mi.setText( "Cycle Node Shape Fill Type... (current: "
2231                     + options.getDefaultNodeFill().toString().toLowerCase() + ")" );
2232         }
2233         else {
2234             mi.setText( "Cycle Node Shape Fill Type..." );
2235         }
2236     }
2237
2238     static void setCycleNodeShapeMenuItem( final JMenuItem mi, final Options options ) {
2239         if ( ( options != null ) && ( options.getDefaultNodeShape() != null ) ) {
2240             mi.setText( "Cycle Node Shape Fill Type... (current: "
2241                     + options.getDefaultNodeShape().toString().toLowerCase() + ")" );
2242         }
2243         else {
2244             mi.setText( "Cycle Node Shape Fill Type..." );
2245         }
2246     }
2247
2248     static void setOvPlacementColorChooseMenuItem( final JMenuItem mi, final Options options ) {
2249         if ( ( options != null ) && ( options.getOvPlacement() != null ) ) {
2250             mi.setText( "Cycle Overview Placement... (current: " + options.getOvPlacement() + ")" );
2251         }
2252         else {
2253             mi.setText( "Cycle Overview Placement..." );
2254         }
2255     }
2256
2257     static void setTextColorChooseMenuItem( final JMenuItem mi, final TreePanel tree_panel ) {
2258         if ( ( tree_panel != null ) && ( tree_panel.getTreeColorSet() != null ) ) {
2259             mi.setText( "Select Color Scheme... (current: " + tree_panel.getTreeColorSet().getCurrentColorSchemeName()
2260                     + ")" );
2261         }
2262         else {
2263             mi.setText( "Select Color Scheme..." );
2264         }
2265     }
2266
2267     static void setTextForFontChooserMenuItem( final JMenuItem mi, final String font_desc ) {
2268         mi.setText( "Select Default Font... (current: " + font_desc + ")" );
2269     }
2270
2271     static void setTextForPdfLineWidthChooserMenuItem( final JMenuItem mi, final Options o ) {
2272         mi.setText( "Enter Default Line Width for PDF Export... (current: " + o.getPrintLineWidth() + ")" );
2273     }
2274
2275     static void setTextMinSupportMenuItem( final JMenuItem mi, final Options options, final TreePanel current_tree_panel ) {
2276         if ( ( current_tree_panel == null ) || ( current_tree_panel.getPhylogeny() == null ) ) {
2277             mi.setEnabled( true );
2278         }
2279         else if ( AptxUtil.isHasAtLeastOneBranchWithSupportValues( current_tree_panel.getPhylogeny() ) ) {
2280             mi.setEnabled( true );
2281         }
2282         else {
2283             mi.setEnabled( false );
2284         }
2285         mi.setText( "Enter Min Confidence Value... (current: " + options.getMinConfidenceValue() + ")" );
2286     }
2287
2288     static void setTextNodeSizeMenuItem( final JMenuItem mi, final Options options ) {
2289         mi.setText( "Enter Default Node Shape Size... (current: " + options.getDefaultNodeShapeSize() + ")" );
2290     }
2291
2292     static void updateScreenTextAntialias( final List<TreePanel> treepanels ) {
2293         for( final TreePanel tree_panel : treepanels ) {
2294             tree_panel.setTextAntialias();
2295         }
2296     }
2297
2298     static boolean writeAsNewHampshire( final TreePanel tp, final Options op, boolean exception, final File file ) {
2299         try {
2300             final PhylogenyWriter writer = new PhylogenyWriter();
2301             writer.toNewHampshire( tp.getPhylogeny(), true, op.getNhConversionSupportValueStyle(), file );
2302         }
2303         catch ( final Exception e ) {
2304             exception = true;
2305             exceptionOccuredDuringSaveAs( e, tp, tp );
2306         }
2307         return exception;
2308     }
2309
2310     static boolean writeAsNexus( final TreePanel tp, final Options op, boolean exception, final File file ) {
2311         try {
2312             final PhylogenyWriter writer = new PhylogenyWriter();
2313             writer.toNexus( file, tp.getPhylogeny(), op.getNhConversionSupportValueStyle() );
2314         }
2315         catch ( final Exception e ) {
2316             exception = true;
2317             exceptionOccuredDuringSaveAs( e, tp, tp );
2318         }
2319         return exception;
2320     }
2321
2322     static boolean writeAsPhyloXml( final TreePanel tp, final Options op, boolean exception, final File file ) {
2323         try {
2324             final PhylogenyWriter writer = new PhylogenyWriter();
2325             writer.toPhyloXML( file, tp.getPhylogeny(), 0 );
2326         }
2327         catch ( final Exception e ) {
2328             exception = true;
2329             exceptionOccuredDuringSaveAs( e, tp, tp );
2330         }
2331         return exception;
2332     }
2333
2334     static void writePhylogenyToGraphicsFile( final String file_name,
2335                                               final GraphicsExportType type,
2336                                               final MainPanel mp,
2337                                               final Component comp,
2338                                               final Container contentpane ) {
2339         mp.getCurrentTreePanel().calcParametersForPainting( mp.getCurrentTreePanel().getWidth(),
2340                                                             mp.getCurrentTreePanel().getHeight() );
2341         String file_written_to = "";
2342         boolean error = false;
2343         try {
2344             file_written_to = AptxUtil.writePhylogenyToGraphicsFile( file_name,
2345                                                                      mp.getCurrentTreePanel().getWidth(),
2346                                                                      mp.getCurrentTreePanel().getHeight(),
2347                                                                      mp.getCurrentTreePanel(),
2348                                                                      mp.getControlPanel(),
2349                                                                      type,
2350                                                                      mp.getOptions() );
2351         }
2352         catch ( final IOException e ) {
2353             error = true;
2354             JOptionPane.showMessageDialog( comp, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE );
2355         }
2356         if ( !error ) {
2357             if ( ( file_written_to != null ) && ( file_written_to.length() > 0 ) ) {
2358                 JOptionPane.showMessageDialog( comp,
2359                                                "Wrote image to: " + file_written_to,
2360                                                "Graphics Export",
2361                                                JOptionPane.INFORMATION_MESSAGE );
2362             }
2363             else {
2364                 JOptionPane.showMessageDialog( comp,
2365                                                "There was an unknown problem when attempting to write to an image file: \""
2366                                                        + file_name + "\"",
2367                                                "Error",
2368                                                JOptionPane.ERROR_MESSAGE );
2369             }
2370         }
2371         contentpane.repaint();
2372     }
2373
2374     static File writeToFile( final Phylogeny t,
2375                              final MainPanel mp,
2376                              final JFileChooser save_filechooser,
2377                              final File current_dir,
2378                              final Container contentpane,
2379                              final Component comp ) {
2380         File new_file = null;
2381         if ( t == null ) {
2382             return null;
2383         }
2384         String initial_filename = null;
2385         if ( mp.getCurrentTreePanel().getTreeFile() != null ) {
2386             try {
2387                 initial_filename = mp.getCurrentTreePanel().getTreeFile().getCanonicalPath();
2388             }
2389             catch ( final IOException e ) {
2390                 initial_filename = null;
2391             }
2392         }
2393         if ( !ForesterUtil.isEmpty( initial_filename ) ) {
2394             save_filechooser.setSelectedFile( new File( initial_filename ) );
2395         }
2396         else {
2397             save_filechooser.setSelectedFile( new File( "" ) );
2398         }
2399         final File my_dir = current_dir;
2400         if ( my_dir != null ) {
2401             save_filechooser.setCurrentDirectory( my_dir );
2402         }
2403         final int result = save_filechooser.showSaveDialog( contentpane );
2404         final File file = save_filechooser.getSelectedFile();
2405         new_file = save_filechooser.getCurrentDirectory();
2406         boolean exception = false;
2407         if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
2408             if ( file.exists() ) {
2409                 final int i = JOptionPane.showConfirmDialog( comp,
2410                                                              file + " already exists.\nOverwrite?",
2411                                                              "Overwrite?",
2412                                                              JOptionPane.OK_CANCEL_OPTION,
2413                                                              JOptionPane.QUESTION_MESSAGE );
2414                 if ( i != JOptionPane.OK_OPTION ) {
2415                     return null;
2416                 }
2417                 else {
2418                     final File to = new File( file.getAbsoluteFile().toString() + AptxConstants.BACKUP_FILE_SUFFIX );
2419                     try {
2420                         ForesterUtil.copyFile( file, to );
2421                     }
2422                     catch ( final Exception e ) {
2423                         JOptionPane.showMessageDialog( comp,
2424                                                        "Failed to create backup copy " + to,
2425                                                        "Failed to Create Backup Copy",
2426                                                        JOptionPane.WARNING_MESSAGE );
2427                     }
2428                     try {
2429                         file.delete();
2430                     }
2431                     catch ( final Exception e ) {
2432                         JOptionPane.showMessageDialog( comp,
2433                                                        "Failed to delete: " + file,
2434                                                        "Failed to Delete",
2435                                                        JOptionPane.WARNING_MESSAGE );
2436                     }
2437                 }
2438             }
2439             if ( save_filechooser.getFileFilter() == MainFrame.nhfilter ) {
2440                 exception = writeAsNewHampshire( mp.getCurrentTreePanel(), mp.getOptions(), exception, file );
2441             }
2442             else if ( save_filechooser.getFileFilter() == MainFrame.xmlfilter ) {
2443                 exception = writeAsPhyloXml( mp.getCurrentTreePanel(), mp.getOptions(), exception, file );
2444             }
2445             else if ( save_filechooser.getFileFilter() == MainFrame.nexusfilter ) {
2446                 exception = writeAsNexus( mp.getCurrentTreePanel(), mp.getOptions(), exception, file );
2447             }
2448             // "*.*":
2449             else {
2450                 final String file_name = file.getName().trim().toLowerCase();
2451                 if ( file_name.endsWith( ".nh" ) || file_name.endsWith( ".newick" ) || file_name.endsWith( ".phy" )
2452                         || file_name.endsWith( ".tree" ) ) {
2453                     exception = writeAsNewHampshire( mp.getCurrentTreePanel(), mp.getOptions(), exception, file );
2454                 }
2455                 else if ( file_name.endsWith( ".nex" ) || file_name.endsWith( ".nexus" ) ) {
2456                     exception = writeAsNexus( mp.getCurrentTreePanel(), mp.getOptions(), exception, file );
2457                 }
2458                 // XML is default:
2459                 else {
2460                     exception = writeAsPhyloXml( mp.getCurrentTreePanel(), mp.getOptions(), exception, file );
2461                 }
2462             }
2463             if ( !exception ) {
2464                 mp.setTitleOfSelectedTab( file.getName() );
2465                 mp.getCurrentTreePanel().setTreeFile( file );
2466                 mp.getCurrentTreePanel().setEdited( false );
2467             }
2468         }
2469         return new_file;
2470     }
2471
2472     static File writeToGraphicsFile( final Phylogeny t,
2473                                      final GraphicsExportType type,
2474                                      final MainPanel mp,
2475                                      final JFileChooser writetographics_filechooser,
2476                                      final Component component,
2477                                      final Container contentpane,
2478                                      final File current_dir ) {
2479         File new_dir = null;
2480         if ( ( t == null ) || t.isEmpty() ) {
2481             return null;
2482         }
2483         String initial_filename = "";
2484         if ( mp.getCurrentTreePanel().getTreeFile() != null ) {
2485             initial_filename = mp.getCurrentTreePanel().getTreeFile().toString();
2486         }
2487         if ( initial_filename.indexOf( '.' ) > 0 ) {
2488             initial_filename = initial_filename.substring( 0, initial_filename.lastIndexOf( '.' ) );
2489         }
2490         initial_filename = initial_filename + "." + type;
2491         writetographics_filechooser.setSelectedFile( new File( initial_filename ) );
2492         final File my_dir = current_dir;
2493         if ( my_dir != null ) {
2494             writetographics_filechooser.setCurrentDirectory( my_dir );
2495         }
2496         final int result = writetographics_filechooser.showSaveDialog( contentpane );
2497         File file = writetographics_filechooser.getSelectedFile();
2498         //setCurrentDir( writetographics_filechooser.getCurrentDirectory() );
2499         new_dir = writetographics_filechooser.getCurrentDirectory();
2500         if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
2501             if ( !file.toString().toLowerCase().endsWith( type.toString() ) ) {
2502                 file = new File( file.toString() + "." + type );
2503             }
2504             if ( file.exists() ) {
2505                 final int i = JOptionPane.showConfirmDialog( component,
2506                                                              file + " already exists. Overwrite?",
2507                                                              "Warning",
2508                                                              JOptionPane.OK_CANCEL_OPTION,
2509                                                              JOptionPane.WARNING_MESSAGE );
2510                 if ( i != JOptionPane.OK_OPTION ) {
2511                     return null;
2512                 }
2513                 else {
2514                     try {
2515                         file.delete();
2516                     }
2517                     catch ( final Exception e ) {
2518                         JOptionPane.showMessageDialog( component,
2519                                                        "Failed to delete: " + file,
2520                                                        "Error",
2521                                                        JOptionPane.WARNING_MESSAGE );
2522                     }
2523                 }
2524             }
2525             writePhylogenyToGraphicsFile( file.toString(), type, mp, component, contentpane );
2526         }
2527         return new_dir;
2528     }
2529
2530     static File writeToPdf( final Phylogeny t,
2531                             final MainPanel mp,
2532                             final JFileChooser writetopdf_filechooser,
2533                             final File curr_dir,
2534                             final Container contentpane,
2535                             final Component component ) {
2536         if ( ( t == null ) || t.isEmpty() ) {
2537             return null;
2538         }
2539         String initial_filename = "";
2540         if ( mp.getCurrentTreePanel().getTreeFile() != null ) {
2541             initial_filename = mp.getCurrentTreePanel().getTreeFile().toString();
2542         }
2543         if ( initial_filename.indexOf( '.' ) > 0 ) {
2544             initial_filename = initial_filename.substring( 0, initial_filename.lastIndexOf( '.' ) );
2545         }
2546         initial_filename = initial_filename + ".pdf";
2547         writetopdf_filechooser.setSelectedFile( new File( initial_filename ) );
2548         final File my_dir = curr_dir;
2549         if ( my_dir != null ) {
2550             writetopdf_filechooser.setCurrentDirectory( my_dir );
2551         }
2552         final int result = writetopdf_filechooser.showSaveDialog( contentpane );
2553         File file = writetopdf_filechooser.getSelectedFile();
2554         // setCurrentDir( writetopdf_filechooser.getCurrentDirectory() );
2555         final File new_current_dir = writetopdf_filechooser.getCurrentDirectory();
2556         if ( ( file != null ) && ( result == JFileChooser.APPROVE_OPTION ) ) {
2557             if ( !file.toString().toLowerCase().endsWith( ".pdf" ) ) {
2558                 file = new File( file.toString() + ".pdf" );
2559             }
2560             if ( file.exists() ) {
2561                 final int i = JOptionPane.showConfirmDialog( component,
2562                                                              file + " already exists. Overwrite?",
2563                                                              "WARNING",
2564                                                              JOptionPane.OK_CANCEL_OPTION,
2565                                                              JOptionPane.WARNING_MESSAGE );
2566                 if ( i != JOptionPane.OK_OPTION ) {
2567                     return null;
2568                 }
2569             }
2570             printPhylogenyToPdf( file.toString(), mp.getOptions(), mp.getCurrentTreePanel(), component );
2571         }
2572         return new_current_dir;
2573     }
2574 }
2575
2576 class DefaultFilter extends FileFilter {
2577
2578     @Override
2579     public boolean accept( final File f ) {
2580         final String file_name = f.getName().trim().toLowerCase();
2581         return file_name.endsWith( ".nh" ) || file_name.endsWith( ".newick" ) || file_name.endsWith( ".phy" )
2582                 || file_name.endsWith( ".nwk" ) || file_name.endsWith( ".phb" ) || file_name.endsWith( ".ph" )
2583                 || file_name.endsWith( ".tr" ) || file_name.endsWith( ".dnd" ) || file_name.endsWith( ".tree" )
2584                 || file_name.endsWith( ".nhx" ) || file_name.endsWith( ".xml" ) || file_name.endsWith( ".phyloxml" )
2585                 || file_name.endsWith( "phylo.xml" ) || file_name.endsWith( ".pxml" ) || file_name.endsWith( ".nexus" )
2586                 || file_name.endsWith( ".nx" ) || file_name.endsWith( ".nex" ) || file_name.endsWith( ".tre" )
2587                 || file_name.endsWith( ".zip" ) || file_name.endsWith( ".tol" ) || file_name.endsWith( ".tolxml" )
2588                 || file_name.endsWith( ".con" ) || f.isDirectory();
2589     }
2590
2591     @Override
2592     public String getDescription() {
2593         return "All supported files (*.xml, *.phyloxml, *phylo.xml, *.nhx, *.nh, *.newick, *.nex, *.nexus, *.phy, *.tre, *.tree, *.tol, ...)";
2594     }
2595 }
2596
2597 class GraphicsFileFilter extends FileFilter {
2598
2599     @Override
2600     public boolean accept( final File f ) {
2601         final String file_name = f.getName().trim().toLowerCase();
2602         return file_name.endsWith( ".jpg" ) || file_name.endsWith( ".jpeg" ) || file_name.endsWith( ".png" )
2603                 || file_name.endsWith( ".gif" ) || file_name.endsWith( ".bmp" ) || f.isDirectory();
2604     }
2605
2606     @Override
2607     public String getDescription() {
2608         return "Image files (*.jpg, *.jpeg, *.png, *.gif, *.bmp)";
2609     }
2610 }
2611
2612 class MsaFileFilter extends FileFilter {
2613
2614     @Override
2615     public boolean accept( final File f ) {
2616         final String file_name = f.getName().trim().toLowerCase();
2617         return file_name.endsWith( ".msa" ) || file_name.endsWith( ".aln" ) || file_name.endsWith( ".fasta" )
2618                 || file_name.endsWith( ".fas" ) || file_name.endsWith( ".fa" ) || f.isDirectory();
2619     }
2620
2621     @Override
2622     public String getDescription() {
2623         return "Multiple sequence alignment files (*.msa, *.aln, *.fasta, *.fa, *.fas)";
2624     }
2625 }
2626
2627 class NexusFilter extends FileFilter {
2628
2629     @Override
2630     public boolean accept( final File f ) {
2631         final String file_name = f.getName().trim().toLowerCase();
2632         return file_name.endsWith( ".nex" ) || file_name.endsWith( ".nexus" ) || file_name.endsWith( ".nx" )
2633                 || file_name.endsWith( ".tre" ) || f.isDirectory();
2634     }
2635
2636     @Override
2637     public String getDescription() {
2638         return "Nexus files (*.nex, *.nexus, *.nx, *.tre)";
2639     }
2640 } // NexusFilter
2641
2642 class NHFilter extends FileFilter {
2643
2644     @Override
2645     public boolean accept( final File f ) {
2646         final String file_name = f.getName().trim().toLowerCase();
2647         return file_name.endsWith( ".nh" ) || file_name.endsWith( ".newick" ) || file_name.endsWith( ".phy" )
2648                 || file_name.endsWith( ".tr" ) || file_name.endsWith( ".tree" ) || file_name.endsWith( ".dnd" )
2649                 || file_name.endsWith( ".ph" ) || file_name.endsWith( ".phb" ) || file_name.endsWith( ".nwk" )
2650                 || f.isDirectory();
2651     }
2652
2653     @Override
2654     public String getDescription() {
2655         return "New Hampshire - Newick files (*.nh, *.newick, *.phy, *.tree, *.dnd, *.tr, *.ph, *.phb, *.nwk)";
2656     }
2657 } // NHFilter
2658
2659 class NHXFilter extends FileFilter {
2660
2661     @Override
2662     public boolean accept( final File f ) {
2663         final String file_name = f.getName().trim().toLowerCase();
2664         return file_name.endsWith( ".nhx" ) || f.isDirectory();
2665     }
2666
2667     @Override
2668     public String getDescription() {
2669         return "NHX files (*.nhx) [deprecated]";
2670     }
2671 }
2672
2673 class PdfFilter extends FileFilter {
2674
2675     @Override
2676     public boolean accept( final File f ) {
2677         return f.getName().trim().toLowerCase().endsWith( ".pdf" ) || f.isDirectory();
2678     }
2679
2680     @Override
2681     public String getDescription() {
2682         return "PDF files (*.pdf)";
2683     }
2684 } // PdfFilter
2685
2686 class SequencesFileFilter extends FileFilter {
2687
2688     @Override
2689     public boolean accept( final File f ) {
2690         final String file_name = f.getName().trim().toLowerCase();
2691         return file_name.endsWith( ".fasta" ) || file_name.endsWith( ".fa" ) || file_name.endsWith( ".fas" )
2692                 || file_name.endsWith( ".seqs" ) || f.isDirectory();
2693     }
2694
2695     @Override
2696     public String getDescription() {
2697         return "Sequences files (*.fasta, *.fa, *.fas, *.seqs )";
2698     }
2699 }
2700
2701 class TolFilter extends FileFilter {
2702
2703     @Override
2704     public boolean accept( final File f ) {
2705         final String file_name = f.getName().trim().toLowerCase();
2706         return ( file_name.endsWith( ".tol" ) || file_name.endsWith( ".tolxml" ) || file_name.endsWith( ".zip" ) || f
2707                 .isDirectory() ) && ( !file_name.endsWith( ".xml.zip" ) );
2708     }
2709
2710     @Override
2711     public String getDescription() {
2712         return "Tree of Life files (*.tol, *.tolxml)";
2713     }
2714 } // TolFilter
2715
2716 class XMLFilter extends FileFilter {
2717
2718     @Override
2719     public boolean accept( final File f ) {
2720         final String file_name = f.getName().trim().toLowerCase();
2721         return file_name.endsWith( ".xml" ) || file_name.endsWith( ".phyloxml" ) || file_name.endsWith( "phylo.xml" )
2722                 || file_name.endsWith( ".pxml" ) || file_name.endsWith( ".zip" ) || f.isDirectory();
2723     }
2724
2725     @Override
2726     public String getDescription() {
2727         return "phyloXML files (*.xml, *.phyloxml, *phylo.xml, *.pxml, *.zip)";
2728     }
2729 } // XMLFilter