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