bugfix
[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.IOException;
34 import java.util.LinkedList;
35 import java.util.List;
36 import java.util.Locale;
37 import java.util.NoSuchElementException;
38 import java.util.Set;
39
40 import javax.swing.Box;
41 import javax.swing.JApplet;
42 import javax.swing.JCheckBoxMenuItem;
43 import javax.swing.JFrame;
44 import javax.swing.JLabel;
45 import javax.swing.JMenu;
46 import javax.swing.JMenuBar;
47 import javax.swing.JMenuItem;
48 import javax.swing.JOptionPane;
49 import javax.swing.JPanel;
50 import javax.swing.JRadioButtonMenuItem;
51 import javax.swing.JTextField;
52 import javax.swing.SwingUtilities;
53
54 import org.forester.archaeopteryx.Options.CLADOGRAM_TYPE;
55 import org.forester.archaeopteryx.Options.NODE_LABEL_DIRECTION;
56 import org.forester.archaeopteryx.Options.PHYLOGENY_GRAPHICS_TYPE;
57 import org.forester.archaeopteryx.tools.InferenceManager;
58 import org.forester.archaeopteryx.tools.ProcessPool;
59 import org.forester.archaeopteryx.tools.ProcessRunning;
60 import org.forester.io.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION;
61 import org.forester.phylogeny.Phylogeny;
62 import org.forester.phylogeny.PhylogenyMethods;
63 import org.forester.phylogeny.PhylogenyMethods.DESCENDANT_SORT_PRIORITY;
64 import org.forester.phylogeny.PhylogenyNode;
65 import org.forester.phylogeny.PhylogenyNode.NH_CONVERSION_SUPPORT_VALUE_STYLE;
66 import org.forester.phylogeny.data.Annotation;
67 import org.forester.phylogeny.data.NodeVisualization.NodeFill;
68 import org.forester.phylogeny.data.NodeVisualization.NodeShape;
69 import org.forester.sdi.GSDI;
70 import org.forester.sdi.GSDIR;
71 import org.forester.sdi.SDIException;
72 import org.forester.util.ForesterConstants;
73 import org.forester.util.ForesterUtil;
74
75 public abstract class MainFrame extends JFrame implements ActionListener {
76
77     static final String         USE_MOUSEWHEEL_SHIFT_TO_ROTATE          = "In this display type, use mousewheel + Shift to rotate [or A and S]";
78     static final String         PHYLOXML_REF_TOOL_TIP                   = Constants.PHYLOXML_REFERENCE;                                                                                                                                       //TODO //FIXME
79     static final String         APTX_REF_TOOL_TIP                       = Constants.APTX_REFERENCE;
80     private static final long   serialVersionUID                        = 3655000897845508358L;
81     final static Font           menu_font                               = new Font( Configuration.getDefaultFontFamilyName(),
82                                                                                     Font.PLAIN,
83                                                                                     10 );
84     static final String         TYPE_MENU_HEADER                        = "Type";
85     static final String         RECTANGULAR_TYPE_CBMI_LABEL             = "Rectangular";
86     static final String         EURO_TYPE_CBMI_LABEL                    = "Euro Type";
87     static final String         CURVED_TYPE_CBMI_LABEL                  = "Curved";
88     static final String         TRIANGULAR_TYPE_CBMI_LABEL              = "Triangular";
89     static final String         CONVEX_TYPE_CBMI_LABEL                  = "Convex";
90     static final String         ROUNDED_TYPE_CBMI_LABEL                 = "Rounded";
91     static final String         UNROOTED_TYPE_CBMI_LABEL                = "Unrooted (alpha)";                                                                                                                                                 //TODO
92     static final String         CIRCULAR_TYPE_CBMI_LABEL                = "Circular (alpha)";                                                                                                                                                 //TODO
93     static final String         OPTIONS_HEADER                          = "Options";
94     static final String         SEARCH_SUBHEADER                        = "Search:";
95     static final String         DISPLAY_SUBHEADER                       = "Display:";
96     static final String         SEARCH_TERMS_ONLY_LABEL                 = "Match Complete Terms Only";
97     static final String         SEARCH_CASE_SENSITIVE_LABEL             = "Case Sensitive";
98     static final String         INVERSE_SEARCH_RESULT_LABEL             = "Negate Result";
99     static final String         DISPLAY_BRANCH_LENGTH_VALUES_LABEL      = "Display Branch Length Values";
100     static final String         DISPLAY_SCALE_LABEL                     = "Display Scale";
101     static final String         NON_LINED_UP_CLADOGRAMS_LABEL           = "Non-Lined Up Cladograms";
102     static final String         UNIFORM_CLADOGRAMS_LABEL                = "Total Node Sum Dependent Cladograms";
103     static final String         LABEL_DIRECTION_LABEL                   = "Radial Labels";
104     static final String         LABEL_DIRECTION_TIP                     = "To use radial node labels in radial and unrooted display types";
105     static final String         SCREEN_ANTIALIAS_LABEL                  = "Antialias";
106     static final String         COLOR_LABELS_LABEL                      = "Colorize Labels Same as Parent Branch";
107     static final String         BG_GRAD_LABEL                           = "Background Color Gradient";
108     static final String         DISPLAY_NODE_BOXES_LABEL_EXT            = "Show External Node Shapes";
109     static final String         DISPLAY_NODE_BOXES_LABEL_INT            = "Show Internal Node Shapes";
110     static final String         SHOW_OVERVIEW_LABEL                     = "Show Overview";
111     static final String         FONT_SIZE_MENU_LABEL                    = "Font Size";
112     static final String         NONUNIFORM_CLADOGRAMS_LABEL             = "External Node Sum Dependent Cladograms";
113     static final String         SHOW_DOMAIN_LABELS_LABEL                = "Show Domain Labels";
114     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";
115     static final String         ABBREV_SN_LABEL                         = "Abbreviate Scientific Taxonomic Names";
116     static final String         TAXONOMY_COLORIZE_NODE_SHAPES_LABEL     = "Colorize Node Shapes According to Taxonomy";
117     static final String         CYCLE_NODE_SHAPE_LABEL                  = "Cycle Node Shapes";
118     static final String         CYCLE_NODE_FILL_LABEL                   = "Cycle Node Fill Type";
119     static final String         CHOOSE_NODE_SIZE_LABEL                  = "Choose Node Shape Size";
120     static final String         SHOW_CONF_STDDEV_LABEL                  = "Show Confidence Standard Deviations";
121     static final String         USE_BRACKETS_FOR_CONF_IN_NH_LABEL       = "Use Brackets for Confidence Values";
122     static final String         USE_INTERNAL_NAMES_FOR_CONF_IN_NH_LABEL = "Use Internal Node Names for Confidence Values";
123     static final String         SHOW_BASIC_TREE_INFORMATION_LABEL       = "Show Basic Tree Information";
124     JMenuBar                    _jmenubar;
125     JMenu                       _file_jmenu;
126     JMenu                       _tools_menu;
127     JMenu                       _view_jmenu;
128     JMenu                       _options_jmenu;
129     JMenu                       _font_size_menu;
130     JMenu                       _help_jmenu;
131     JMenuItem[]                 _load_phylogeny_from_webservice_menu_items;
132     // Analysis menu
133     JMenu                       _analysis_menu;
134     JMenuItem                   _load_species_tree_item;
135     JMenuItem                   _gsdi_item;
136     JMenuItem                   _gsdir_item;
137     JMenuItem                   _lineage_inference;
138     JMenuItem                   _function_analysis;
139     // file menu:
140     JMenuItem                   _open_item;
141     JMenuItem                   _open_url_item;
142     JMenuItem                   _save_item;
143     JMenuItem                   _save_all_item;
144     JMenuItem                   _close_item;
145     JMenuItem                   _exit_item;
146     JMenuItem                   _new_item;
147     // tools menu:
148     JMenuItem                   _midpoint_root_item;
149     JMenuItem                   _taxcolor_item;
150     JMenuItem                   _confcolor_item;
151     JMenuItem                   _color_rank_jmi;
152     JMenuItem                   _collapse_species_specific_subtrees;
153     JMenuItem                   _collapse_below_threshold;                                                                                                                                                                                    //TODO implememt me
154     JMenuItem                   _obtain_detailed_taxonomic_information_jmi;
155     JMenuItem                   _obtain_detailed_taxonomic_information_deleting_jmi;
156     JMenuItem                   _obtain_seq_information_jmi;
157     JMenuItem                   _move_node_names_to_tax_sn_jmi;
158     JMenuItem                   _move_node_names_to_seq_names_jmi;
159     JMenuItem                   _extract_tax_code_from_node_names_jmi;
160     JMenuItem                   _annotate_item;
161     // font size menu:
162     JMenuItem                   _super_tiny_fonts_item;
163     JMenuItem                   _tiny_fonts_item;
164     JMenuItem                   _small_fonts_item;
165     JMenuItem                   _medium_fonts_item;
166     JMenuItem                   _large_fonts_item;
167     // options menu:
168     // _  screen and print
169     JMenuItem                   _choose_font_mi;
170     JMenuItem                   _switch_colors_mi;
171     JCheckBoxMenuItem           _label_direction_cbmi;
172     // _  screen display
173     JCheckBoxMenuItem           _screen_antialias_cbmi;
174     JCheckBoxMenuItem           _background_gradient_cbmi;
175     JRadioButtonMenuItem        _non_lined_up_cladograms_rbmi;
176     JRadioButtonMenuItem        _uniform_cladograms_rbmi;
177     JRadioButtonMenuItem        _ext_node_dependent_cladogram_rbmi;
178     JCheckBoxMenuItem           _show_branch_length_values_cbmi;
179     JCheckBoxMenuItem           _show_scale_cbmi;                                                                                                                                                                                             //TODO fix me
180     JCheckBoxMenuItem           _show_overview_cbmi;
181     JCheckBoxMenuItem           _show_domain_labels;
182     JCheckBoxMenuItem           _abbreviate_scientific_names;
183     JCheckBoxMenuItem           _color_labels_same_as_parent_branch;
184     JMenuItem                   _overview_placment_mi;
185     JMenuItem                   _choose_minimal_confidence_mi;
186     JCheckBoxMenuItem           _show_default_node_shapes_internal_cbmi;
187     JCheckBoxMenuItem           _show_default_node_shapes_external_cbmi;
188     JMenuItem                   _cycle_node_shape_mi;
189     JMenuItem                   _cycle_node_fill_mi;
190     JMenuItem                   _choose_node_size_mi;
191     JCheckBoxMenuItem           _taxonomy_colorize_node_shapes_cbmi;
192     JCheckBoxMenuItem           _show_confidence_stddev_cbmi;
193     // _  print
194     JCheckBoxMenuItem           _graphics_export_visible_only_cbmi;
195     JCheckBoxMenuItem           _antialias_print_cbmi;
196     JCheckBoxMenuItem           _print_black_and_white_cbmi;
197     JCheckBoxMenuItem           _print_using_actual_size_cbmi;
198     JCheckBoxMenuItem           _graphics_export_using_actual_size_cbmi;
199     JMenuItem                   _print_size_mi;
200     JMenuItem                   _choose_pdf_width_mi;
201     // _  parsing
202     JCheckBoxMenuItem           _internal_number_are_confidence_for_nh_parsing_cbmi;
203     JRadioButtonMenuItem        _extract_taxonomy_no_rbmi;
204     JRadioButtonMenuItem        _extract_taxonomy_yes_rbmi;
205     JRadioButtonMenuItem        _extract_taxonomy_pfam_rbmi;
206     JCheckBoxMenuItem           _replace_underscores_cbmi;
207     JCheckBoxMenuItem           _use_brackets_for_conf_in_nh_export_cbmi;
208     JCheckBoxMenuItem           _use_internal_names_for_conf_in_nh_export_cbmi;
209     // _  search
210     JCheckBoxMenuItem           _search_case_senstive_cbmi;
211     JCheckBoxMenuItem           _search_whole_words_only_cbmi;
212     JCheckBoxMenuItem           _inverse_search_result_cbmi;
213     // type menu:
214     JMenu                       _type_menu;
215     JCheckBoxMenuItem           _rectangular_type_cbmi;
216     JCheckBoxMenuItem           _triangular_type_cbmi;
217     JCheckBoxMenuItem           _curved_type_cbmi;
218     JCheckBoxMenuItem           _convex_type_cbmi;
219     JCheckBoxMenuItem           _euro_type_cbmi;
220     JCheckBoxMenuItem           _rounded_type_cbmi;
221     JCheckBoxMenuItem           _unrooted_type_cbmi;
222     JCheckBoxMenuItem           _circular_type_cbmi;
223     // view as text menu:
224     JMenuItem                   _view_as_NH_item;
225     JMenuItem                   _view_as_XML_item;
226     JMenuItem                   _view_as_nexus_item;
227     JMenuItem                   _display_basic_information_item;
228     // help menu:
229     JMenuItem                   _about_item;
230     JMenuItem                   _help_item;
231     JMenuItem                   _website_item;
232     JMenuItem                   _phyloxml_website_item;
233     JMenuItem                   _phyloxml_ref_item;
234     JMenuItem                   _aptx_ref_item;
235     // process menu:
236     JMenu                       _process_menu;
237     // Handy pointers to child components:
238     MainPanel                   _mainpanel;
239     Container                   _contentpane;
240     final LinkedList<TextFrame> _textframes                             = new LinkedList<TextFrame>();                                                                                                                                         ;
241     Configuration               _configuration;
242     JMenuItem                   _remove_branch_color_item;
243     Options                     _options;
244     private Phylogeny           _species_tree;
245     InferenceManager            _inference_manager;
246     final ProcessPool           _process_pool;
247     private String              _previous_node_annotation_ref;
248
249     MainFrame() {
250         _process_pool = ProcessPool.createInstance();
251     }
252
253     /**
254      * Action performed.
255      */
256     @Override
257     public void actionPerformed( final ActionEvent e ) {
258         final Object o = e.getSource();
259         boolean is_applet = false;
260         JApplet applet = null;
261         if ( getCurrentTreePanel() != null ) {
262             is_applet = getCurrentTreePanel().isApplet();
263             if ( is_applet ) {
264                 applet = getCurrentTreePanel().obtainApplet();
265             }
266         }
267         if ( o == _open_url_item ) {
268             readPhylogeniesFromURL();
269         }
270         else if ( o == _exit_item ) {
271             close();
272         }
273         else if ( o == _gsdi_item ) {
274             if ( isSubtreeDisplayed() ) {
275                 return;
276             }
277             executeGSDI();
278         }
279         else if ( o == _gsdir_item ) {
280             if ( isSubtreeDisplayed() ) {
281                 return;
282             }
283             executeGSDIR();
284         }
285         else if ( o == _taxcolor_item ) {
286             taxColor();
287         }
288         else if ( o == _confcolor_item ) {
289             confColor();
290         }
291         else if ( o == _color_rank_jmi ) {
292             colorRank();
293         }
294         else if ( o == _collapse_species_specific_subtrees ) {
295             if ( isSubtreeDisplayed() ) {
296                 return;
297             }
298             if ( getCurrentTreePanel() != null ) {
299                 getCurrentTreePanel().collapseSpeciesSpecificSubtrees();
300             }
301         }
302         else if ( o == _remove_branch_color_item ) {
303             if ( isSubtreeDisplayed() ) {
304                 return;
305             }
306             removeBranchColors();
307         }
308         else if ( o == _midpoint_root_item ) {
309             if ( isSubtreeDisplayed() ) {
310                 return;
311             }
312             midpointRoot();
313         }
314         else if ( o == _annotate_item ) {
315             annotateSequences();
316         }
317         else if ( o == _switch_colors_mi ) {
318             switchColors();
319         }
320         else if ( o == _display_basic_information_item ) {
321             displayBasicInformation();
322         }
323         else if ( o == _view_as_NH_item ) {
324             viewAsNH();
325         }
326         else if ( o == _view_as_XML_item ) {
327             viewAsXML();
328         }
329         else if ( o == _view_as_nexus_item ) {
330             viewAsNexus();
331         }
332         else if ( o == _super_tiny_fonts_item ) {
333             if ( getCurrentTreePanel() != null ) {
334                 getCurrentTreePanel().setSuperTinyFonts();
335                 getCurrentTreePanel().repaint();
336             }
337         }
338         else if ( o == _tiny_fonts_item ) {
339             if ( getCurrentTreePanel() != null ) {
340                 getCurrentTreePanel().setTinyFonts();
341                 getCurrentTreePanel().repaint();
342             }
343         }
344         else if ( o == _small_fonts_item ) {
345             if ( getCurrentTreePanel() != null ) {
346                 getCurrentTreePanel().setSmallFonts();
347                 getCurrentTreePanel().repaint();
348             }
349         }
350         else if ( o == _medium_fonts_item ) {
351             if ( getCurrentTreePanel() != null ) {
352                 getCurrentTreePanel().setMediumFonts();
353                 getCurrentTreePanel().repaint();
354             }
355         }
356         else if ( o == _large_fonts_item ) {
357             if ( getCurrentTreePanel() != null ) {
358                 getCurrentTreePanel().setLargeFonts();
359                 getCurrentTreePanel().repaint();
360             }
361         }
362         else if ( o == _choose_font_mi ) {
363             chooseFont();
364         }
365         else if ( o == _choose_minimal_confidence_mi ) {
366             chooseMinimalConfidence();
367         }
368         else if ( o == _choose_node_size_mi ) {
369             chooseNodeSize( getOptions(), this );
370         }
371         else if ( o == _overview_placment_mi ) {
372             MainFrame.cycleOverview( getOptions(), getCurrentTreePanel() );
373         }
374         else if ( o == _cycle_node_fill_mi ) {
375             MainFrame.cycleNodeFill( getOptions(), getCurrentTreePanel() );
376         }
377         else if ( o == _cycle_node_shape_mi ) {
378             MainFrame.cycleNodeShape( getOptions(), getCurrentTreePanel() );
379         }
380         else if ( o == _screen_antialias_cbmi ) {
381             updateOptions( getOptions() );
382             updateScreenTextAntialias( getMainPanel().getTreePanels() );
383         }
384         else if ( o == _background_gradient_cbmi ) {
385             updateOptions( getOptions() );
386         }
387         else if ( o == _show_domain_labels ) {
388             updateOptions( getOptions() );
389         }
390         else if ( o == _abbreviate_scientific_names ) {
391             updateOptions( getOptions() );
392         }
393         else if ( o == _color_labels_same_as_parent_branch ) {
394             updateOptions( getOptions() );
395         }
396         else if ( o == _show_default_node_shapes_internal_cbmi ) {
397             updateOptions( getOptions() );
398         }
399         else if ( o == _show_default_node_shapes_external_cbmi ) {
400             updateOptions( getOptions() );
401         }
402         else if ( o == _taxonomy_colorize_node_shapes_cbmi ) {
403             updateOptions( getOptions() );
404         }
405         else if ( o == _non_lined_up_cladograms_rbmi ) {
406             updateOptions( getOptions() );
407             showWhole();
408         }
409         else if ( o == _uniform_cladograms_rbmi ) {
410             updateOptions( getOptions() );
411             showWhole();
412         }
413         else if ( o == _ext_node_dependent_cladogram_rbmi ) {
414             updateOptions( getOptions() );
415             showWhole();
416         }
417         else if ( o == _search_case_senstive_cbmi ) {
418             updateOptions( getOptions() );
419             getMainPanel().getControlPanel().search();
420         }
421         else if ( o == _search_whole_words_only_cbmi ) {
422             updateOptions( getOptions() );
423             getMainPanel().getControlPanel().search();
424         }
425         else if ( o == _inverse_search_result_cbmi ) {
426             updateOptions( getOptions() );
427             getMainPanel().getControlPanel().search();
428         }
429         else if ( o == _show_scale_cbmi ) {
430             updateOptions( getOptions() );
431         }
432         else if ( o == _show_branch_length_values_cbmi ) {
433             updateOptions( getOptions() );
434         }
435         else if ( o == _show_confidence_stddev_cbmi ) {
436             updateOptions( getOptions() );
437         }
438         else if ( o == _use_brackets_for_conf_in_nh_export_cbmi ) {
439             if ( _use_brackets_for_conf_in_nh_export_cbmi.isSelected() ) {
440                 _use_internal_names_for_conf_in_nh_export_cbmi.setSelected( false );
441             }
442             updateOptions( getOptions() );
443         }
444         else if ( o == _use_internal_names_for_conf_in_nh_export_cbmi ) {
445             if ( _use_internal_names_for_conf_in_nh_export_cbmi.isSelected() ) {
446                 _use_brackets_for_conf_in_nh_export_cbmi.setSelected( false );
447             }
448             updateOptions( getOptions() );
449         }
450         else if ( o == _label_direction_cbmi ) {
451             updateOptions( getOptions() );
452         }
453         else if ( o == _show_overview_cbmi ) {
454             updateOptions( getOptions() );
455             if ( getCurrentTreePanel() != null ) {
456                 getCurrentTreePanel().updateOvSizes();
457             }
458         }
459         else if ( ( o == _rectangular_type_cbmi ) || ( o == _triangular_type_cbmi ) || ( o == _curved_type_cbmi )
460                 || ( o == _convex_type_cbmi ) || ( o == _euro_type_cbmi ) || ( o == _rounded_type_cbmi )
461                 || ( o == _unrooted_type_cbmi ) || ( o == _circular_type_cbmi ) ) {
462             typeChanged( o );
463         }
464         else if ( o == _about_item ) {
465             about();
466         }
467         else if ( o == _help_item ) {
468             help();
469         }
470         else if ( o == _website_item ) {
471             try {
472                 AptxUtil.openWebsite( Constants.APTX_WEB_SITE, is_applet, applet );
473             }
474             catch ( final IOException e1 ) {
475                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
476             }
477         }
478         else if ( o == _phyloxml_website_item ) {
479             try {
480                 AptxUtil.openWebsite( Constants.PHYLOXML_WEB_SITE, is_applet, applet );
481             }
482             catch ( final IOException e1 ) {
483                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
484             }
485         }
486         else if ( o == _aptx_ref_item ) {
487             try {
488                 AptxUtil.openWebsite( Constants.APTX_REFERENCE_URL, is_applet, applet );
489             }
490             catch ( final IOException e1 ) {
491                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
492             }
493         }
494         else if ( o == _phyloxml_ref_item ) {
495             try {
496                 AptxUtil.openWebsite( Constants.PHYLOXML_REFERENCE_URL, is_applet, applet );
497             }
498             catch ( final IOException e1 ) {
499                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
500             }
501         }
502         else {
503             if ( _load_phylogeny_from_webservice_menu_items != null ) {
504                 for( int i = 0; i < _load_phylogeny_from_webservice_menu_items.length; ++i ) {
505                     if ( o == _load_phylogeny_from_webservice_menu_items[ i ] ) {
506                         readPhylogeniesFromWebservice( i );
507                     }
508                 }
509             }
510         }
511         _contentpane.repaint();
512     }
513
514     public Configuration getConfiguration() {
515         return _configuration;
516     }
517
518     /**
519      * This method returns the current external node data which
520      * has been selected by the user by clicking the "Return ..."
521      * menu item. This method is expected to be called from Javascript or
522      * something like it.
523      * 
524      * @return current external node data as String
525      */
526     public String getCurrentExternalNodesDataBuffer() {
527         return getCurrentTreePanel().getCurrentExternalNodesDataBufferAsString();
528     }
529
530     public int getCurrentExternalNodesDataBufferChangeCounter() {
531         return getCurrentTreePanel().getCurrentExternalNodesDataBufferChangeCounter();
532     }
533
534     public int getCurrentExternalNodesDataBufferLength() {
535         return getCurrentTreePanel().getCurrentExternalNodesDataBufferAsString().length();
536     }
537
538     public InferenceManager getInferenceManager() {
539         return _inference_manager;
540     }
541
542     public MainPanel getMainPanel() {
543         return _mainpanel;
544     }
545
546     public Options getOptions() {
547         return _options;
548     }
549
550     public ProcessPool getProcessPool() {
551         return _process_pool;
552     }
553
554     public void showTextFrame( final String s, final String title ) {
555         checkTextFrames();
556         _textframes.addLast( TextFrame.instantiate( s, title, _textframes ) );
557     }
558
559     public void showWhole() {
560         _mainpanel.getControlPanel().showWhole();
561     }
562
563     public void updateProcessMenu() {
564         // In general Swing is not thread safe.
565         // See "Swing's Threading Policy".
566         SwingUtilities.invokeLater( new Runnable() {
567
568             @Override
569             public void run() {
570                 doUpdateProcessMenu();
571             }
572         } );
573     }
574
575     void activateSaveAllIfNeeded() {
576         if ( ( getMainPanel().getTabbedPane() != null ) && ( getMainPanel().getTabbedPane().getTabCount() > 1 ) ) {
577             _save_all_item.setEnabled( true );
578         }
579         else {
580             _save_all_item.setEnabled( false );
581         }
582     }
583
584     void buildFileMenu() {
585         _file_jmenu = createMenu( "File", getConfiguration() );
586         _file_jmenu.add( _open_url_item = new JMenuItem( "Read tree from URL/webservice..." ) );
587         _file_jmenu.addSeparator();
588         _file_jmenu.add( _exit_item = new JMenuItem( "Exit" ) );
589         customizeJMenuItem( _open_url_item );
590         customizeJMenuItem( _exit_item );
591         _jmenubar.add( _file_jmenu );
592     }
593
594     void buildFontSizeMenu() {
595         _font_size_menu = createMenu( FONT_SIZE_MENU_LABEL, getConfiguration() );
596         _font_size_menu.add( _super_tiny_fonts_item = new JMenuItem( "Super Tiny Fonts" ) );
597         _font_size_menu.add( _tiny_fonts_item = new JMenuItem( "Tiny Fonts" ) );
598         _font_size_menu.add( _small_fonts_item = new JMenuItem( "Small Fonts" ) );
599         _font_size_menu.add( _medium_fonts_item = new JMenuItem( "Medium Fonts" ) );
600         _font_size_menu.add( _large_fonts_item = new JMenuItem( "Large Fonts" ) );
601         customizeJMenuItem( _super_tiny_fonts_item );
602         customizeJMenuItem( _tiny_fonts_item );
603         customizeJMenuItem( _small_fonts_item );
604         customizeJMenuItem( _medium_fonts_item );
605         customizeJMenuItem( _large_fonts_item );
606         _jmenubar.add( _font_size_menu );
607     }
608
609     void buildHelpMenu() {
610         _help_jmenu = createMenu( "Help", getConfiguration() );
611         _help_jmenu.add( _help_item = new JMenuItem( "Help" ) );
612         _help_jmenu.add( _website_item = new JMenuItem( "Archaeopteryx Home" ) );
613         _aptx_ref_item = new JMenuItem( "Archaeopteryx Reference" );
614         _help_jmenu.add( _phyloxml_website_item = new JMenuItem( "phyloXML Home" ) );
615         _help_jmenu.add( _phyloxml_ref_item = new JMenuItem( "phyloXML Reference" ) );
616         _help_jmenu.addSeparator();
617         _help_jmenu.add( _about_item = new JMenuItem( "About" ) );
618         customizeJMenuItem( _help_item );
619         customizeJMenuItem( _website_item );
620         customizeJMenuItem( _phyloxml_website_item );
621         customizeJMenuItem( _aptx_ref_item );
622         customizeJMenuItem( _phyloxml_ref_item );
623         customizeJMenuItem( _about_item );
624         _phyloxml_ref_item.setToolTipText( PHYLOXML_REF_TOOL_TIP );
625         _aptx_ref_item.setToolTipText( APTX_REF_TOOL_TIP );
626         _jmenubar.add( _help_jmenu );
627     }
628
629     void buildTypeMenu() {
630         _type_menu = createMenu( TYPE_MENU_HEADER, getConfiguration() );
631         _type_menu.add( _rectangular_type_cbmi = new JCheckBoxMenuItem( MainFrame.RECTANGULAR_TYPE_CBMI_LABEL ) );
632         _type_menu.add( _euro_type_cbmi = new JCheckBoxMenuItem( MainFrame.EURO_TYPE_CBMI_LABEL ) );
633         _type_menu.add( _rounded_type_cbmi = new JCheckBoxMenuItem( MainFrame.ROUNDED_TYPE_CBMI_LABEL ) );
634         _type_menu.add( _curved_type_cbmi = new JCheckBoxMenuItem( MainFrame.CURVED_TYPE_CBMI_LABEL ) );
635         _type_menu.add( _triangular_type_cbmi = new JCheckBoxMenuItem( MainFrame.TRIANGULAR_TYPE_CBMI_LABEL ) );
636         _type_menu.add( _convex_type_cbmi = new JCheckBoxMenuItem( MainFrame.CONVEX_TYPE_CBMI_LABEL ) );
637         _type_menu.add( _unrooted_type_cbmi = new JCheckBoxMenuItem( MainFrame.UNROOTED_TYPE_CBMI_LABEL ) );
638         _type_menu.add( _circular_type_cbmi = new JCheckBoxMenuItem( MainFrame.CIRCULAR_TYPE_CBMI_LABEL ) );
639         customizeCheckBoxMenuItem( _rectangular_type_cbmi, false );
640         customizeCheckBoxMenuItem( _triangular_type_cbmi, false );
641         customizeCheckBoxMenuItem( _euro_type_cbmi, false );
642         customizeCheckBoxMenuItem( _rounded_type_cbmi, false );
643         customizeCheckBoxMenuItem( _curved_type_cbmi, false );
644         customizeCheckBoxMenuItem( _convex_type_cbmi, false );
645         customizeCheckBoxMenuItem( _unrooted_type_cbmi, false );
646         customizeCheckBoxMenuItem( _circular_type_cbmi, false );
647         _unrooted_type_cbmi.setToolTipText( MainFrame.USE_MOUSEWHEEL_SHIFT_TO_ROTATE );
648         _circular_type_cbmi.setToolTipText( MainFrame.USE_MOUSEWHEEL_SHIFT_TO_ROTATE );
649         initializeTypeMenu( getOptions() );
650         _jmenubar.add( _type_menu );
651     }
652
653     void buildViewMenu() {
654         _view_jmenu = createMenu( "View", getConfiguration() );
655         _view_jmenu.add( _display_basic_information_item = new JMenuItem( SHOW_BASIC_TREE_INFORMATION_LABEL ) );
656         _view_jmenu.addSeparator();
657         _view_jmenu.add( _view_as_XML_item = new JMenuItem( "View as phyloXML" ) );
658         _view_jmenu.add( _view_as_NH_item = new JMenuItem( "View as Newick" ) );
659         _view_jmenu.add( _view_as_nexus_item = new JMenuItem( "View as Nexus" ) );
660         customizeJMenuItem( _display_basic_information_item );
661         customizeJMenuItem( _view_as_NH_item );
662         customizeJMenuItem( _view_as_XML_item );
663         customizeJMenuItem( _view_as_nexus_item );
664         _jmenubar.add( _view_jmenu );
665     }
666
667     void checkTextFrames() {
668         if ( _textframes.size() > 5 ) {
669             try {
670                 if ( _textframes.getFirst() != null ) {
671                     _textframes.getFirst().removeMe();
672                 }
673                 else {
674                     _textframes.removeFirst();
675                 }
676             }
677             catch ( final NoSuchElementException e ) {
678                 // Ignore.
679             }
680         }
681     }
682
683     void close() {
684         removeAllTextFrames();
685         if ( _mainpanel != null ) {
686             _mainpanel.terminate();
687         }
688         if ( _contentpane != null ) {
689             _contentpane.removeAll();
690         }
691         setVisible( false );
692         dispose();
693     }
694
695     void colorRank() {
696         if ( _mainpanel.getCurrentTreePanel() != null ) {
697             final String[] ranks = AptxUtil.getAllPossibleRanks();
698             final String rank = ( String ) JOptionPane
699                     .showInputDialog( this,
700                                       "What rank should the colorization be based on",
701                                       "Rank Selection",
702                                       JOptionPane.QUESTION_MESSAGE,
703                                       null,
704                                       ranks,
705                                       null );
706             if ( !ForesterUtil.isEmpty( rank ) ) {
707                 _mainpanel.getCurrentTreePanel().colorRank( rank );
708             }
709         }
710     }
711
712     void confColor() {
713         if ( _mainpanel.getCurrentTreePanel() != null ) {
714             _mainpanel.getCurrentTreePanel().confColor();
715         }
716     }
717
718     void customizeCheckBoxMenuItem( final JCheckBoxMenuItem item, final boolean is_selected ) {
719         if ( item != null ) {
720             item.setFont( MainFrame.menu_font );
721             if ( !getConfiguration().isUseNativeUI() ) {
722                 item.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
723                 item.setForeground( getConfiguration().getGuiMenuTextColor() );
724             }
725             item.setSelected( is_selected );
726             item.addActionListener( this );
727         }
728     }
729
730     JMenuItem customizeJMenuItem( final JMenuItem jmi ) {
731         if ( jmi != null ) {
732             jmi.setFont( MainFrame.menu_font );
733             if ( !getConfiguration().isUseNativeUI() ) {
734                 jmi.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
735                 jmi.setForeground( getConfiguration().getGuiMenuTextColor() );
736             }
737             jmi.addActionListener( this );
738         }
739         return jmi;
740     }
741
742     void customizeRadioButtonMenuItem( final JRadioButtonMenuItem item, final boolean is_selected ) {
743         if ( item != null ) {
744             item.setFont( MainFrame.menu_font );
745             if ( !getConfiguration().isUseNativeUI() ) {
746                 item.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
747                 item.setForeground( getConfiguration().getGuiMenuTextColor() );
748             }
749             item.setSelected( is_selected );
750             item.addActionListener( this );
751         }
752     }
753
754     void displayBasicInformation() {
755         if ( ( _mainpanel.getCurrentPhylogeny() != null ) && !_mainpanel.getCurrentPhylogeny().isEmpty() ) {
756             String title = "Basic Information";
757             if ( !ForesterUtil.isEmpty( _mainpanel.getCurrentPhylogeny().getName() ) ) {
758                 title = _mainpanel.getCurrentPhylogeny().getName() + " " + title;
759             }
760             showTextFrame( AptxUtil.createBasicInformation( _mainpanel.getCurrentPhylogeny() ), title );
761         }
762     }
763
764     void exceptionOccuredDuringOpenFile( final Exception e ) {
765         try {
766             _mainpanel.getCurrentTreePanel().setArrowCursor();
767         }
768         catch ( final Exception ex ) {
769             // Do nothing.
770         }
771         JOptionPane.showMessageDialog( this,
772                                        ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ),
773                                        "Error during File|Open",
774                                        JOptionPane.ERROR_MESSAGE );
775     }
776
777     void exceptionOccuredDuringSaveAs( final Exception e ) {
778         try {
779             _mainpanel.getCurrentTreePanel().setArrowCursor();
780         }
781         catch ( final Exception ex ) {
782             // Do nothing.
783         }
784         JOptionPane.showMessageDialog( this, "Exception" + e, "Error during File|SaveAs", JOptionPane.ERROR_MESSAGE );
785     }
786
787     void executeGSDI() {
788         if ( !isOKforSDI( false, true ) ) {
789             return;
790         }
791         if ( !_mainpanel.getCurrentPhylogeny().isRooted() ) {
792             JOptionPane.showMessageDialog( this,
793                                            "Gene tree is not rooted.",
794                                            "Cannot execute GSDI",
795                                            JOptionPane.ERROR_MESSAGE );
796             return;
797         }
798         final Phylogeny gene_tree = _mainpanel.getCurrentPhylogeny().copy();
799         gene_tree.setAllNodesToNotCollapse();
800         gene_tree.recalculateNumberOfExternalDescendants( false );
801         GSDI gsdi = null;
802         final Phylogeny species_tree = getSpeciesTree().copy();
803         try {
804             gsdi = new GSDI( gene_tree, species_tree, false, true, true, true );
805         }
806         catch ( final SDIException e ) {
807             JOptionPane.showMessageDialog( this,
808                                            e.getLocalizedMessage(),
809                                            "Error during GSDI",
810                                            JOptionPane.ERROR_MESSAGE );
811             return;
812         }
813         catch ( final Exception e ) {
814             AptxUtil.unexpectedException( e );
815             return;
816         }
817         gene_tree.setRerootable( false );
818         gene_tree.clearHashIdToNodeMap();
819         gene_tree.recalculateNumberOfExternalDescendants( true );
820         _mainpanel.addPhylogenyInNewTab( gene_tree, getConfiguration(), "gene tree", null );
821         getMainPanel().getControlPanel().setShowEvents( true );
822         showWhole();
823         final int selected = _mainpanel.getTabbedPane().getSelectedIndex();
824         _mainpanel.addPhylogenyInNewTab( species_tree, getConfiguration(), "species tree", null );
825         showWhole();
826         _mainpanel.getTabbedPane().setSelectedIndex( selected );
827         showWhole();
828         _mainpanel.getCurrentTreePanel().setEdited( true );
829         final int poly = PhylogenyMethods.countNumberOfPolytomies( species_tree );
830         if ( gsdi.getStrippedExternalGeneTreeNodes().size() > 0 ) {
831             JOptionPane.showMessageDialog( this,
832                                            "Duplications: " + gsdi.getDuplicationsSum() + "\n"
833                                                    + "Potential duplications: "
834                                                    + gsdi.getSpeciationOrDuplicationEventsSum() + "\n"
835                                                    + "Speciations: " + gsdi.getSpeciationsSum() + "\n"
836                                                    + "Stripped gene tree nodes: "
837                                                    + gsdi.getStrippedExternalGeneTreeNodes().size() + "\n"
838                                                    + "Taxonomy linkage based on: " + gsdi.getTaxCompBase() + "\n"
839                                                    + "Number of polytomies in species tree used: " + poly + "\n",
840                                            "GSDI successfully completed",
841                                            JOptionPane.WARNING_MESSAGE );
842         }
843         else {
844             JOptionPane.showMessageDialog( this,
845                                            "Duplications: " + gsdi.getDuplicationsSum() + "\n"
846                                                    + "Potential duplications: "
847                                                    + gsdi.getSpeciationOrDuplicationEventsSum() + "\n"
848                                                    + "Speciations: " + gsdi.getSpeciationsSum() + "\n"
849                                                    + "Stripped gene tree nodes: "
850                                                    + gsdi.getStrippedExternalGeneTreeNodes().size() + "\n"
851                                                    + "Taxonomy linkage based on: " + gsdi.getTaxCompBase() + "\n"
852                                                    + "Number of polytomies in species tree used: " + poly + "\n",
853                                            "GSDI successfully completed",
854                                            JOptionPane.INFORMATION_MESSAGE );
855         }
856     }
857
858     void executeGSDIR() {
859         if ( !isOKforSDI( false, false ) ) {
860             return;
861         }
862         final int p = PhylogenyMethods.countNumberOfPolytomies( _mainpanel.getCurrentPhylogeny() );
863         if ( ( p > 0 )
864                 && !( ( p == 1 ) && ( _mainpanel.getCurrentPhylogeny().getRoot().getNumberOfDescendants() == 3 ) ) ) {
865             JOptionPane.showMessageDialog( this,
866                                            "Gene tree is not completely binary",
867                                            "Cannot execute GSDI",
868                                            JOptionPane.ERROR_MESSAGE );
869             return;
870         }
871         final Phylogeny gene_tree = _mainpanel.getCurrentPhylogeny().copy();
872         gene_tree.setAllNodesToNotCollapse();
873         gene_tree.recalculateNumberOfExternalDescendants( false );
874         GSDIR gsdir = null;
875         final Phylogeny species_tree = getSpeciesTree().copy();
876         try {
877             gsdir = new GSDIR( gene_tree, species_tree, true, true, true );
878         }
879         catch ( final SDIException e ) {
880             JOptionPane.showMessageDialog( this,
881                                            e.getLocalizedMessage(),
882                                            "Error during GSDIR",
883                                            JOptionPane.ERROR_MESSAGE );
884             return;
885         }
886         catch ( final Exception e ) {
887             AptxUtil.unexpectedException( e );
888             return;
889         }
890         final Phylogeny result_gene_tree = gsdir.getMinDuplicationsSumGeneTree();
891         result_gene_tree.setRerootable( false );
892         result_gene_tree.clearHashIdToNodeMap();
893         result_gene_tree.recalculateNumberOfExternalDescendants( true );
894         PhylogenyMethods.orderAppearance( result_gene_tree.getRoot(), true, true, DESCENDANT_SORT_PRIORITY.NODE_NAME );
895         _mainpanel.addPhylogenyInNewTab( result_gene_tree, getConfiguration(), "gene tree", null );
896         getMainPanel().getControlPanel().setShowEvents( true );
897         showWhole();
898         final int selected = _mainpanel.getTabbedPane().getSelectedIndex();
899         _mainpanel.addPhylogenyInNewTab( species_tree, getConfiguration(), "species tree", null );
900         showWhole();
901         _mainpanel.getTabbedPane().setSelectedIndex( selected );
902         showWhole();
903         _mainpanel.getCurrentTreePanel().setEdited( true );
904         final int poly = PhylogenyMethods.countNumberOfPolytomies( species_tree );
905         if ( gsdir.getStrippedExternalGeneTreeNodes().size() > 0 ) {
906             JOptionPane.showMessageDialog( this,
907                                            "Minimal duplications: " + gsdir.getMinDuplicationsSum() + "\n"
908                                                    + "Speciations: " + gsdir.getSpeciationsSum() + "\n"
909                                                    + "Stripped gene tree nodes: "
910                                                    + gsdir.getStrippedExternalGeneTreeNodes().size() + "\n"
911                                                    + "Taxonomy linkage based on: " + gsdir.getTaxCompBase() + "\n"
912                                                    + "Number of polytomies in species tree used: " + poly + "\n",
913                                            "GSDIR successfully completed",
914                                            JOptionPane.WARNING_MESSAGE );
915         }
916         else {
917             JOptionPane.showMessageDialog( this,
918                                            "Minimal duplications: " + gsdir.getMinDuplicationsSum() + "\n"
919                                                    + "Speciations: " + gsdir.getSpeciationsSum() + "\n"
920                                                    + "Stripped gene tree nodes: "
921                                                    + gsdir.getStrippedExternalGeneTreeNodes().size() + "\n"
922                                                    + "Taxonomy linkage based on: " + gsdir.getTaxCompBase() + "\n"
923                                                    + "Number of polytomies in species tree used: " + poly + "\n",
924                                            "GSDIR successfully completed",
925                                            JOptionPane.INFORMATION_MESSAGE );
926         }
927     }
928
929     boolean GAndSDoHaveMoreThanOneSpeciesInComman( final Phylogeny gene_tree ) {
930         if ( ( gene_tree == null ) || gene_tree.isEmpty() ) {
931             JOptionPane.showMessageDialog( this,
932                                            "Gene tree and species tree have no species in common.",
933                                            "Error during SDI",
934                                            JOptionPane.ERROR_MESSAGE );
935             return false;
936         }
937         else if ( gene_tree.getNumberOfExternalNodes() < 2 ) {
938             JOptionPane.showMessageDialog( this,
939                                            "Gene tree and species tree have only one species in common.",
940                                            "Error during SDI",
941                                            JOptionPane.ERROR_MESSAGE );
942             return false;
943         }
944         else {
945             return true;
946         }
947     }
948
949     TreePanel getCurrentTreePanel() {
950         return getMainPanel().getCurrentTreePanel();
951     }
952
953     JMenu getHelpMenu() {
954         return _help_jmenu;
955     }
956
957     JCheckBoxMenuItem getlabelDirectionCbmi() {
958         return _label_direction_cbmi;
959     }
960
961     JMenuBar getMenuBarOfMainFrame() {
962         return _jmenubar;
963     }
964
965     final Phylogeny getSpeciesTree() {
966         return _species_tree;
967     }
968
969     void help() {
970         final StringBuilder sb = new StringBuilder();
971         sb.append( "Display options\n" );
972         sb.append( "-------------------\n" );
973         sb.append( "Use the checkboxes to select types of information to display on the tree.\n\n" );
974         sb.append( "Clickable tree nodes\n" );
975         sb.append( "--------------------\n" );
976         sb.append( "Tree nodes can be clicked, the action is determined by the 'click on node to' menu\n" );
977         sb.append( "or by right clicking:\n" );
978         sb.append( "o  Display Node Data -- display information for a node\n" );
979         sb.append( "o  Collapse/Uncollapse -- collapse and uncollapse subtree from clicked node\n" );
980         sb.append( "o  Root/Reroot -- change tree root to clicked node\n" );
981         sb.append( "o  Sub/Super Tree -- toggle between subtree from clicked node and whole tree\n" );
982         sb.append( "o  Swap Descendants -- switch descendant on either side of clicked node\n" );
983         sb.append( "o  Colorize Subtree -- color a subtree\n" );
984         sb.append( "o  Open Sequence Web -- launch a web browser to display sequence information\n" );
985         sb.append( "o  Open Taxonomy Web -- launch a web browser to display taxonomy information\n" );
986         sb.append( "-  there may be additional choices depending on this particular setup\n\n" );
987         sb.append( "Right clicking on a node always displays the information of a node.\n\n" );
988         sb.append( "Zooming\n" );
989         sb.append( "---------\n" );
990         sb.append( "The mouse wheel and the plus and minus keys control zooming.\n" );
991         sb.append( "Mouse wheel+Ctrl changes the text size.\n" );
992         sb.append( "Mouse wheel+Shift controls zooming in vertical direction only.\n" );
993         sb.append( "Use the buttons on the control panel to zoom the tree in and out, horizontally or vertically.\n" );
994         sb.append( "The entire tree can be fitted into the window by clicking the \"F\" button, or by pressing F, Delete, or Home.\n" );
995         sb.append( "The up, down, left, and right keys can be used to move the visible part (if zoomed in).\n" );
996         sb.append( "Up, down, left, and right+Shift can be used to control zooming horizontally and vertically.\n" );
997         sb.append( "Plus and minus keys+Ctrl change the text size; F+Ctrl, Delete+Ctrl, or Home+Ctrl resets it.\n\n" );
998         sb.append( "Quick tree manipulation:\n" );
999         sb.append( "------------------------\n" );
1000         sb.append( "Order Subtrees -- order the tree by branch length\n" );
1001         sb.append( "Uncollapse All -- uncollapse any and all collapsed branches\n\n" );
1002         sb.append( "Memory problems (Java heap space error)\n" );
1003         sb.append( "---------------------------------------\n" );
1004         sb.append( "Since the Java default memory allocation is quite small, it might by necessary (for trees\n" );
1005         sb.append( "with more than approximately 5000 external nodes) to increase the memory which Java can use, with\n" );
1006         sb.append( "the '-Xmx' Java command line option. For example:\n" );
1007         sb.append( "java -Xmx1024m -cp path\\to\\forester.jar org.forester.archaeopteryx.Archaeopteryx\n\n" );
1008         // + "General remarks\n"
1009         // + "---------------\n"
1010         // +
1011         // "o  The application version permits copying to the clipboard \n"
1012         // +
1013         // "    in the \"View\"|\"View as ...\" frame (either by control-c or button press).\n"
1014         // +
1015         // "o  Changes made to a subtree affect this subtree and its subtrees,\n"
1016         // + "    but not any of its parent tree(s).\n"
1017         // +
1018         // "o  Archaeopteryx tries to detect whether the numerical values in a NH tree\n"
1019         // +
1020         // "    are likely to be bootstrap values instead of branch length values.\n\n"
1021         // +
1022         // " Remarks regarding SDI (Speciation Duplication Inference):\n"
1023         // +
1024         // "o  Each external node of the gene tree (in display) needs to be associated with\n"
1025         // +
1026         // "    a species: either directly through the \"Species\" field, or the species\n"
1027         // +
1028         // "    is part of the sequence name in the form \"XXXX_SPECIES\"\n"
1029         // +
1030         // "    (e.g. \"ACON_DROME\" or \"ACON_DROME/123-4489\" which is also acceptable).\n"
1031         // +
1032         // "o  A species tree for each species of the gene tree needs to be loaded with\n"
1033         // +
1034         // "   \"SDI\"|\"Load species tree\" prior the SDI execution.\n"
1035         // +
1036         // "o  !External nodes of the gene tree associated with species not present in\n"
1037         // +
1038         // "    the species tree are REMOVED prior to SDI execution!\n"
1039         // +
1040         // "o  Both the gene tree and the species tree must be completely binary.\n"
1041         // +
1042         // "o  Duplications and speciations are a function of the position of the root.\n"
1043         // +
1044         // "    Hence, after each manual \"Root/Reroot\"ing some duplications will be\n"
1045         // + "    incorrect and need to be inferred again\n"
1046         // +
1047         // "    with: \"SDI\"|\"SDI (Speciation Duplication Inference)\n\n"
1048         sb.append( "phyloXML\n" );
1049         sb.append( "-------------------\n" );
1050         sb.append( "Reference: " + Constants.PHYLOXML_REFERENCE + "\n" );
1051         sb.append( "Website: " + Constants.PHYLOXML_WEB_SITE + "\n" );
1052         sb.append( "Version: " + ForesterConstants.PHYLO_XML_VERSION + "\n" );
1053         sb.append( "\n" );
1054         sb.append( "For more information: https://sites.google.com/site/cmzmasek/home/software/archaeopteryx\n" );
1055         sb.append( "Email: " + Constants.AUTHOR_EMAIL + "\n\n" );
1056         TextFrame.instantiate( sb.toString(), "Help", _textframes );
1057     }
1058
1059     void initializeTypeMenu( final Options options ) {
1060         setTypeMenuToAllUnselected();
1061         switch ( options.getPhylogenyGraphicsType() ) {
1062             case CONVEX:
1063                 _convex_type_cbmi.setSelected( true );
1064                 break;
1065             case CURVED:
1066                 _curved_type_cbmi.setSelected( true );
1067                 break;
1068             case EURO_STYLE:
1069                 _euro_type_cbmi.setSelected( true );
1070                 break;
1071             case ROUNDED:
1072                 _rounded_type_cbmi.setSelected( true );
1073                 break;
1074             case TRIANGULAR:
1075                 _triangular_type_cbmi.setSelected( true );
1076                 break;
1077             case UNROOTED:
1078                 _unrooted_type_cbmi.setSelected( true );
1079                 break;
1080             case CIRCULAR:
1081                 _circular_type_cbmi.setSelected( true );
1082                 break;
1083             default:
1084                 _rectangular_type_cbmi.setSelected( true );
1085                 break;
1086         }
1087     }
1088
1089     boolean isOKforSDI( final boolean species_tree_has_to_binary, final boolean gene_tree_has_to_binary ) {
1090         if ( ( _mainpanel.getCurrentPhylogeny() == null ) || _mainpanel.getCurrentPhylogeny().isEmpty() ) {
1091             return false;
1092         }
1093         else if ( ( getSpeciesTree() == null ) || getSpeciesTree().isEmpty() ) {
1094             JOptionPane.showMessageDialog( this,
1095                                            "No species tree loaded",
1096                                            "Cannot execute GSDI",
1097                                            JOptionPane.ERROR_MESSAGE );
1098             return false;
1099         }
1100         else if ( species_tree_has_to_binary && !getSpeciesTree().isCompletelyBinary() ) {
1101             JOptionPane.showMessageDialog( this,
1102                                            "Species tree is not completely binary",
1103                                            "Cannot execute GSDI",
1104                                            JOptionPane.ERROR_MESSAGE );
1105             return false;
1106         }
1107         else if ( gene_tree_has_to_binary && !_mainpanel.getCurrentPhylogeny().isCompletelyBinary() ) {
1108             JOptionPane.showMessageDialog( this,
1109                                            "Gene tree is not completely binary",
1110                                            "Cannot execute GSDI",
1111                                            JOptionPane.ERROR_MESSAGE );
1112             return false;
1113         }
1114         else {
1115             return true;
1116         }
1117     }
1118
1119     boolean isSubtreeDisplayed() {
1120         if ( getCurrentTreePanel() != null ) {
1121             if ( getCurrentTreePanel().isCurrentTreeIsSubtree() ) {
1122                 JOptionPane
1123                         .showMessageDialog( this,
1124                                             "This operation can only be performed on a complete tree, not on the currently displayed sub-tree only.",
1125                                             "Operation can not be exectuted on a sub-tree",
1126                                             JOptionPane.WARNING_MESSAGE );
1127                 return true;
1128             }
1129         }
1130         return false;
1131     }
1132
1133     void midpointRoot() {
1134         if ( _mainpanel.getCurrentTreePanel() != null ) {
1135             _mainpanel.getCurrentTreePanel().midpointRoot();
1136         }
1137     }
1138
1139     abstract void readPhylogeniesFromURL();
1140
1141     void readPhylogeniesFromWebservice( final int i ) {
1142         final UrlTreeReader reader = new UrlTreeReader( this, i );
1143         new Thread( reader ).start();
1144     }
1145
1146     void removeAllTextFrames() {
1147         for( final TextFrame tf : _textframes ) {
1148             if ( tf != null ) {
1149                 tf.close();
1150             }
1151         }
1152         _textframes.clear();
1153     }
1154
1155     void setConfiguration( final Configuration configuration ) {
1156         _configuration = configuration;
1157     }
1158
1159     void setInferenceManager( final InferenceManager i ) {
1160         _inference_manager = i;
1161     }
1162
1163     void setOptions( final Options options ) {
1164         _options = options;
1165     }
1166
1167     void setSelectedTypeInTypeMenu( final PHYLOGENY_GRAPHICS_TYPE type ) {
1168         setTypeMenuToAllUnselected();
1169         switch ( type ) {
1170             case CIRCULAR:
1171                 _circular_type_cbmi.setSelected( true );
1172                 break;
1173             case CONVEX:
1174                 _convex_type_cbmi.setSelected( true );
1175                 break;
1176             case CURVED:
1177                 _curved_type_cbmi.setSelected( true );
1178                 break;
1179             case EURO_STYLE:
1180                 _euro_type_cbmi.setSelected( true );
1181                 break;
1182             case ROUNDED:
1183                 _rounded_type_cbmi.setSelected( true );
1184                 break;
1185             case RECTANGULAR:
1186                 _rectangular_type_cbmi.setSelected( true );
1187                 break;
1188             case TRIANGULAR:
1189                 _triangular_type_cbmi.setSelected( true );
1190                 break;
1191             case UNROOTED:
1192                 _unrooted_type_cbmi.setSelected( true );
1193                 break;
1194             default:
1195                 throw new IllegalArgumentException( "unknown type: " + type );
1196         }
1197     }
1198
1199     final void setSpeciesTree( final Phylogeny species_tree ) {
1200         _species_tree = species_tree;
1201     }
1202
1203     void setTypeMenuToAllUnselected() {
1204         _convex_type_cbmi.setSelected( false );
1205         _curved_type_cbmi.setSelected( false );
1206         _euro_type_cbmi.setSelected( false );
1207         _rounded_type_cbmi.setSelected( false );
1208         _triangular_type_cbmi.setSelected( false );
1209         _rectangular_type_cbmi.setSelected( false );
1210         _unrooted_type_cbmi.setSelected( false );
1211         _circular_type_cbmi.setSelected( false );
1212     }
1213
1214     void switchColors() {
1215         final TreeColorSet colorset = _mainpanel.getTreeColorSet();
1216         final ColorSchemeChooser csc = new ColorSchemeChooser( getMainPanel(), colorset );
1217         csc.setVisible( true );
1218     }
1219
1220     void taxColor() {
1221         if ( _mainpanel.getCurrentTreePanel() != null ) {
1222             _mainpanel.getCurrentTreePanel().taxColor();
1223         }
1224     }
1225
1226     void typeChanged( final Object o ) {
1227         updateTypeCheckboxes( getOptions(), o );
1228         updateOptions( getOptions() );
1229         if ( getCurrentTreePanel() != null ) {
1230             final PHYLOGENY_GRAPHICS_TYPE previous_type = getCurrentTreePanel().getPhylogenyGraphicsType();
1231             final PHYLOGENY_GRAPHICS_TYPE new_type = getOptions().getPhylogenyGraphicsType();
1232             if ( ( ( previous_type == PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) && ( new_type != PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) )
1233                     || ( ( previous_type == PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) && ( new_type != PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) )
1234                     || ( ( previous_type != PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) && ( new_type == PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) )
1235                     || ( ( previous_type != PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) && ( new_type == PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) ) ) {
1236                 getCurrentTreePanel().getControlPanel().showWhole();
1237             }
1238             if ( getCurrentTreePanel().isPhyHasBranchLengths() && ( new_type != PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) ) {
1239                 getCurrentTreePanel().getControlPanel().setDrawPhylogramEnabled( true );
1240             }
1241             else {
1242                 getCurrentTreePanel().getControlPanel().setDrawPhylogramEnabled( false );
1243             }
1244             getCurrentTreePanel().setPhylogenyGraphicsType( getOptions().getPhylogenyGraphicsType() );
1245             updateScreenTextAntialias( getMainPanel().getTreePanels() );
1246             if ( getCurrentTreePanel().getControlPanel().getDynamicallyHideData() != null ) {
1247                 if ( new_type == PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) {
1248                     getCurrentTreePanel().getControlPanel().getDynamicallyHideData().setEnabled( false );
1249                 }
1250                 else {
1251                     getCurrentTreePanel().getControlPanel().getDynamicallyHideData().setEnabled( true );
1252                 }
1253             }
1254         }
1255     }
1256
1257     void updateOptions( final Options options ) {
1258         options.setAntialiasScreen( ( _screen_antialias_cbmi != null ) && _screen_antialias_cbmi.isSelected() );
1259         options.setBackgroundColorGradient( ( _background_gradient_cbmi != null )
1260                 && _background_gradient_cbmi.isSelected() );
1261         options.setShowDomainLabels( ( _show_domain_labels != null ) && _show_domain_labels.isSelected() );
1262         options.setAbbreviateScientificTaxonNames( ( _abbreviate_scientific_names != null )
1263                 && _abbreviate_scientific_names.isSelected() );
1264         options.setColorLabelsSameAsParentBranch( ( _color_labels_same_as_parent_branch != null )
1265                 && _color_labels_same_as_parent_branch.isSelected() );
1266         options.setShowDefaultNodeShapesInternal( ( _show_default_node_shapes_internal_cbmi != null )
1267                 && _show_default_node_shapes_internal_cbmi.isSelected() );
1268         options.setShowDefaultNodeShapesExternal( ( _show_default_node_shapes_external_cbmi != null )
1269                 && _show_default_node_shapes_external_cbmi.isSelected() );
1270         options.setTaxonomyColorizeNodeShapes( ( _taxonomy_colorize_node_shapes_cbmi != null )
1271                 && _taxonomy_colorize_node_shapes_cbmi.isSelected() );
1272         if ( ( _non_lined_up_cladograms_rbmi != null ) && ( _non_lined_up_cladograms_rbmi.isSelected() ) ) {
1273             options.setCladogramType( CLADOGRAM_TYPE.NON_LINED_UP );
1274         }
1275         else if ( ( _uniform_cladograms_rbmi != null ) && ( _uniform_cladograms_rbmi.isSelected() ) ) {
1276             options.setCladogramType( CLADOGRAM_TYPE.TOTAL_NODE_SUM_DEP );
1277         }
1278         else if ( ( _ext_node_dependent_cladogram_rbmi != null ) && ( _ext_node_dependent_cladogram_rbmi.isSelected() ) ) {
1279             options.setCladogramType( CLADOGRAM_TYPE.EXT_NODE_SUM_DEP );
1280         }
1281         options.setSearchCaseSensitive( ( _search_case_senstive_cbmi != null )
1282                 && _search_case_senstive_cbmi.isSelected() );
1283         if ( ( _show_scale_cbmi != null ) && _show_scale_cbmi.isEnabled() ) {
1284             options.setShowScale( _show_scale_cbmi.isSelected() );
1285         }
1286         if ( _label_direction_cbmi != null ) {
1287             if ( _label_direction_cbmi.isSelected() ) {
1288                 options.setNodeLabelDirection( NODE_LABEL_DIRECTION.RADIAL );
1289             }
1290             else {
1291                 options.setNodeLabelDirection( NODE_LABEL_DIRECTION.HORIZONTAL );
1292             }
1293         }
1294         options.setShowOverview( ( _show_overview_cbmi != null ) && _show_overview_cbmi.isSelected() );
1295         options.setShowConfidenceStddev( ( _show_confidence_stddev_cbmi != null )
1296                 && _show_confidence_stddev_cbmi.isSelected() );
1297         if ( ( _show_branch_length_values_cbmi != null ) && _show_branch_length_values_cbmi.isEnabled() ) {
1298             options.setShowBranchLengthValues( _show_branch_length_values_cbmi.isSelected() );
1299         }
1300         options.setPrintUsingActualSize( ( _print_using_actual_size_cbmi != null )
1301                 && ( _print_using_actual_size_cbmi.isSelected() ) );
1302         options.setGraphicsExportUsingActualSize( ( _graphics_export_using_actual_size_cbmi != null )
1303                 && ( _graphics_export_using_actual_size_cbmi.isSelected() ) );
1304         options.setAntialiasPrint( ( _antialias_print_cbmi != null ) && _antialias_print_cbmi.isSelected() );
1305         if ( ( _use_brackets_for_conf_in_nh_export_cbmi != null )
1306                 && _use_brackets_for_conf_in_nh_export_cbmi.isSelected() ) {
1307             options.setNhConversionSupportValueStyle( NH_CONVERSION_SUPPORT_VALUE_STYLE.IN_SQUARE_BRACKETS );
1308         }
1309         else if ( ( _use_internal_names_for_conf_in_nh_export_cbmi != null )
1310                 && _use_internal_names_for_conf_in_nh_export_cbmi.isSelected() ) {
1311             options.setNhConversionSupportValueStyle( NH_CONVERSION_SUPPORT_VALUE_STYLE.AS_INTERNAL_NODE_NAMES );
1312         }
1313         else {
1314             options.setNhConversionSupportValueStyle( NH_CONVERSION_SUPPORT_VALUE_STYLE.NONE );
1315         }
1316         options.setPrintBlackAndWhite( ( _print_black_and_white_cbmi != null )
1317                 && _print_black_and_white_cbmi.isSelected() );
1318         options.setInternalNumberAreConfidenceForNhParsing( ( _internal_number_are_confidence_for_nh_parsing_cbmi != null )
1319                 && _internal_number_are_confidence_for_nh_parsing_cbmi.isSelected() );
1320         if ( ( _extract_taxonomy_yes_rbmi != null ) && _extract_taxonomy_yes_rbmi.isSelected() ) {
1321             options.setTaxonomyExtractio( TAXONOMY_EXTRACTION.PFAM_STYLE_RELAXED );
1322         }
1323         else if ( ( _extract_taxonomy_pfam_rbmi != null ) && _extract_taxonomy_pfam_rbmi.isSelected() ) {
1324             options.setTaxonomyExtractio( TAXONOMY_EXTRACTION.PFAM_STYLE_STRICT );
1325         }
1326         else if ( ( _extract_taxonomy_no_rbmi != null ) && _extract_taxonomy_no_rbmi.isSelected() ) {
1327             options.setTaxonomyExtractio( TAXONOMY_EXTRACTION.NO );
1328         }
1329         options.setReplaceUnderscoresInNhParsing( ( _replace_underscores_cbmi != null )
1330                 && _replace_underscores_cbmi.isSelected() );
1331         options.setMatchWholeTermsOnly( ( _search_whole_words_only_cbmi != null )
1332                 && _search_whole_words_only_cbmi.isSelected() );
1333         options.setInverseSearchResult( ( _inverse_search_result_cbmi != null )
1334                 && _inverse_search_result_cbmi.isSelected() );
1335         if ( _graphics_export_visible_only_cbmi != null ) {
1336             options.setGraphicsExportVisibleOnly( _graphics_export_visible_only_cbmi.isSelected() );
1337             if ( _graphics_export_visible_only_cbmi.isSelected() && ( _graphics_export_using_actual_size_cbmi != null ) ) {
1338                 _graphics_export_using_actual_size_cbmi.setSelected( true );
1339                 _graphics_export_using_actual_size_cbmi.setEnabled( false );
1340             }
1341             else {
1342                 _graphics_export_using_actual_size_cbmi.setEnabled( true );
1343             }
1344         }
1345         if ( ( _rectangular_type_cbmi != null ) && _rectangular_type_cbmi.isSelected() ) {
1346             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
1347         }
1348         else if ( ( _triangular_type_cbmi != null ) && _triangular_type_cbmi.isSelected() ) {
1349             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.TRIANGULAR );
1350         }
1351         else if ( ( _curved_type_cbmi != null ) && _curved_type_cbmi.isSelected() ) {
1352             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.CURVED );
1353         }
1354         else if ( ( _convex_type_cbmi != null ) && _convex_type_cbmi.isSelected() ) {
1355             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.CONVEX );
1356         }
1357         else if ( ( _euro_type_cbmi != null ) && _euro_type_cbmi.isSelected() ) {
1358             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.EURO_STYLE );
1359         }
1360         else if ( ( _rounded_type_cbmi != null ) && _rounded_type_cbmi.isSelected() ) {
1361             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.ROUNDED );
1362         }
1363         else if ( ( _unrooted_type_cbmi != null ) && _unrooted_type_cbmi.isSelected() ) {
1364             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.UNROOTED );
1365         }
1366         else if ( ( _circular_type_cbmi != null ) && _circular_type_cbmi.isSelected() ) {
1367             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.CIRCULAR );
1368         }
1369     }
1370
1371     void updateTypeCheckboxes( final Options options, final Object o ) {
1372         setTypeMenuToAllUnselected();
1373         ( ( JCheckBoxMenuItem ) o ).setSelected( true );
1374     }
1375
1376     void viewAsNexus() {
1377         if ( ( _mainpanel.getCurrentPhylogeny() != null ) && !_mainpanel.getCurrentPhylogeny().isEmpty() ) {
1378             String title = "Nexus";
1379             if ( !ForesterUtil.isEmpty( _mainpanel.getCurrentPhylogeny().getName() ) ) {
1380                 title = _mainpanel.getCurrentPhylogeny().getName() + " " + title;
1381             }
1382             showTextFrame( _mainpanel.getCurrentPhylogeny().toNexus( getOptions().getNhConversionSupportValueStyle() ),
1383                            title );
1384         }
1385     }
1386
1387     void viewAsNH() {
1388         if ( ( _mainpanel.getCurrentPhylogeny() != null ) && !_mainpanel.getCurrentPhylogeny().isEmpty() ) {
1389             String title = "New Hampshire";
1390             if ( !ForesterUtil.isEmpty( _mainpanel.getCurrentPhylogeny().getName() ) ) {
1391                 title = _mainpanel.getCurrentPhylogeny().getName() + " " + title;
1392             }
1393             showTextFrame( _mainpanel.getCurrentPhylogeny()
1394                                    .toNewHampshire( false, getOptions().getNhConversionSupportValueStyle() ),
1395                            title );
1396         }
1397     }
1398
1399     void viewAsXML() {
1400         if ( ( _mainpanel.getCurrentPhylogeny() != null ) && !_mainpanel.getCurrentPhylogeny().isEmpty() ) {
1401             String title = "phyloXML";
1402             if ( !ForesterUtil.isEmpty( _mainpanel.getCurrentPhylogeny().getName() ) ) {
1403                 title = _mainpanel.getCurrentPhylogeny().getName() + " " + title;
1404             }
1405             showTextFrame( _mainpanel.getCurrentPhylogeny().toPhyloXML( 0 ), title );
1406         }
1407     }
1408
1409     private void annotateSequences() {
1410         if ( getCurrentTreePanel() != null ) {
1411             final Set<Long> nodes = getCurrentTreePanel().getFoundNodes();
1412             if ( ( nodes == null ) || nodes.isEmpty() ) {
1413                 JOptionPane
1414                         .showMessageDialog( this,
1415                                             "Need to select nodes, either via direct selection or via the \"Search\" function",
1416                                             "No nodes selected for annotation",
1417                                             JOptionPane.ERROR_MESSAGE );
1418                 return;
1419             }
1420             final Phylogeny phy = getMainPanel().getCurrentPhylogeny();
1421             if ( ( phy != null ) && !phy.isEmpty() ) {
1422                 final JTextField ref_field = new JTextField( 10 );
1423                 final JTextField desc_filed = new JTextField( 20 );
1424                 ref_field.setText( ForesterUtil.isEmpty( getPreviousNodeAnnotationReference() ) ? ""
1425                         : getPreviousNodeAnnotationReference() );
1426                 final JPanel my_panel = new JPanel();
1427                 my_panel.add( new JLabel( "Reference " ) );
1428                 my_panel.add( ref_field );
1429                 my_panel.add( Box.createHorizontalStrut( 15 ) );
1430                 my_panel.add( new JLabel( "Description " ) );
1431                 my_panel.add( desc_filed );
1432                 final int result = JOptionPane.showConfirmDialog( null,
1433                                                                   my_panel,
1434                                                                   "Enter the sequence annotation(s) for the "
1435                                                                           + nodes.size() + " selected nodes",
1436                                                                   JOptionPane.OK_CANCEL_OPTION );
1437                 if ( result == JOptionPane.OK_OPTION ) {
1438                     String ref = ref_field.getText();
1439                     String desc = desc_filed.getText();
1440                     if ( !ForesterUtil.isEmpty( ref ) ) {
1441                         ref = ref.trim();
1442                         ref = ref.replaceAll( "\\s+", " " );
1443                         if ( ( ref.indexOf( ':' ) < 1 ) || ( ref.indexOf( ':' ) > ref.length() - 2 )
1444                                 || ( ref.length() < 3 ) ) {
1445                             JOptionPane.showMessageDialog( this,
1446                                                            "Reference needs to be in the form of \"GO:1234567\"",
1447                                                            "Illegal Format for Annotation Reference",
1448                                                            JOptionPane.ERROR_MESSAGE );
1449                             return;
1450                         }
1451                     }
1452                     if ( ref != null ) {
1453                         setPreviousNodeAnnotationReference( ref );
1454                     }
1455                     if ( desc != null ) {
1456                         desc = desc.trim();
1457                         desc = desc.replaceAll( "\\s+", " " );
1458                     }
1459                     if ( !ForesterUtil.isEmpty( ref ) || !ForesterUtil.isEmpty( desc ) ) {
1460                         for( final Long id : nodes ) {
1461                             final PhylogenyNode n = phy.getNode( id );
1462                             ForesterUtil.ensurePresenceOfSequence( n );
1463                             final Annotation ann = ForesterUtil.isEmpty( ref ) ? new Annotation()
1464                                     : new Annotation( ref );
1465                             if ( !ForesterUtil.isEmpty( desc ) ) {
1466                                 ann.setDesc( desc );
1467                             }
1468                             n.getNodeData().getSequence().addAnnotation( ann );
1469                         }
1470                     }
1471                     getMainPanel().getControlPanel().showAnnotations();
1472                 }
1473             }
1474         }
1475     }
1476
1477     private void chooseFont() {
1478         final FontChooser fc = new FontChooser();
1479         fc.setFont( getMainPanel().getTreeFontSet().getLargeFont() );
1480         fc.showDialog( this, "Select the Base Font" );
1481         getMainPanel().getTreeFontSet().setBaseFont( fc.getFont() );
1482     }
1483
1484     private void chooseMinimalConfidence() {
1485         final String s = ( String ) JOptionPane
1486                 .showInputDialog( this,
1487                                   "Please enter the minimum for confidence values to be displayed.\n"
1488                                           + "[current value: " + getOptions().getMinConfidenceValue() + "]\n",
1489                                   "Minimal Confidence Value",
1490                                   JOptionPane.QUESTION_MESSAGE,
1491                                   null,
1492                                   null,
1493                                   getOptions().getMinConfidenceValue() );
1494         if ( !ForesterUtil.isEmpty( s ) ) {
1495             boolean success = true;
1496             double m = 0.0;
1497             final String m_str = s.trim();
1498             if ( !ForesterUtil.isEmpty( m_str ) ) {
1499                 try {
1500                     m = Double.parseDouble( m_str );
1501                 }
1502                 catch ( final Exception ex ) {
1503                     success = false;
1504                 }
1505             }
1506             else {
1507                 success = false;
1508             }
1509             if ( success && ( m >= 0.0 ) ) {
1510                 getOptions().setMinConfidenceValue( m );
1511             }
1512         }
1513     }
1514
1515     private void doUpdateProcessMenu() {
1516         if ( _process_pool.size() > 0 ) {
1517             if ( _process_menu == null ) {
1518                 _process_menu = createMenu( "", getConfiguration() );
1519                 _process_menu.setForeground( Color.RED );
1520             }
1521             _process_menu.removeAll();
1522             final String text = "processes running: " + _process_pool.size();
1523             _process_menu.setText( text );
1524             _jmenubar.add( _process_menu );
1525             for( int i = 0; i < _process_pool.size(); ++i ) {
1526                 final ProcessRunning p = _process_pool.getProcessByIndex( i );
1527                 _process_menu.add( customizeJMenuItem( new JMenuItem( p.getName() + " [" + p.getStart() + "]" ) ) );
1528             }
1529         }
1530         else {
1531             if ( _process_menu != null ) {
1532                 _process_menu.removeAll();
1533                 _jmenubar.remove( _process_menu );
1534             }
1535         }
1536         _jmenubar.validate();
1537         _jmenubar.repaint();
1538         repaint();
1539     }
1540
1541     private String getPreviousNodeAnnotationReference() {
1542         return _previous_node_annotation_ref;
1543     }
1544
1545     private void removeBranchColors() {
1546         if ( getMainPanel().getCurrentPhylogeny() != null ) {
1547             AptxUtil.removeBranchColors( getMainPanel().getCurrentPhylogeny() );
1548         }
1549     }
1550
1551     private void setPreviousNodeAnnotationReference( final String previous_node_annotation_ref ) {
1552         _previous_node_annotation_ref = previous_node_annotation_ref;
1553     }
1554
1555     /**
1556      * Display the about box.
1557      */
1558     static void about() {
1559         final StringBuffer about = new StringBuffer( "Archaeopteryx\nVersion " + Constants.VERSION + "\n" );
1560         about.append( "Copyright (C) 2013 Christian M. Zmasek\n" );
1561         about.append( "All Rights Reserved\n" );
1562         about.append( "License: GNU Lesser General Public License (LGPL)\n" );
1563         about.append( "Last modified: " + Constants.PRG_DATE + "\n" );
1564         about.append( "Based on: " + ForesterUtil.getForesterLibraryInformation() + "\n" );
1565         about.append( "phyloXML version : " + ForesterConstants.PHYLO_XML_VERSION + "\n" );
1566         about.append( "phyloXML location: " + ForesterConstants.PHYLO_XML_LOCATION + "\n" );
1567         if ( !ForesterUtil.isEmpty( ForesterUtil.JAVA_VERSION ) && !ForesterUtil.isEmpty( ForesterUtil.JAVA_VENDOR ) ) {
1568             about.append( "[your Java version: " + ForesterUtil.JAVA_VERSION + " " + ForesterUtil.JAVA_VENDOR + "]\n" );
1569         }
1570         if ( !ForesterUtil.isEmpty( ForesterUtil.OS_NAME ) && !ForesterUtil.isEmpty( ForesterUtil.OS_ARCH )
1571                 && !ForesterUtil.isEmpty( ForesterUtil.OS_VERSION ) ) {
1572             about.append( "[your OS: " + ForesterUtil.OS_NAME + " " + ForesterUtil.OS_ARCH + " "
1573                     + ForesterUtil.OS_VERSION + "]\n" );
1574         }
1575         final Runtime rt = java.lang.Runtime.getRuntime();
1576         final long free_memory = rt.freeMemory() / 1000000;
1577         final long total_memory = rt.totalMemory() / 1000000;
1578         about.append( "[free memory: " + free_memory + "MB, total memory: " + total_memory + "MB]\n" );
1579         about.append( "[locale: " + Locale.getDefault() + "]\n" );
1580         about.append( "References:\n" );
1581         about.append( Constants.PHYLOXML_REFERENCE_SHORT + "\n" );
1582         about.append( "For more information & download:\n" );
1583         about.append( Constants.APTX_WEB_SITE + "\n" );
1584         about.append( "Comments: " + Constants.AUTHOR_EMAIL );
1585         JOptionPane.showMessageDialog( null, about, Constants.PRG_NAME, JOptionPane.PLAIN_MESSAGE );
1586     }
1587
1588     static void chooseNodeSize( final Options options, final Component parent ) {
1589         final String s = ( String ) JOptionPane.showInputDialog( parent,
1590                                                                  "Please enter the default size for node shapes.\n"
1591                                                                          + "[current value: "
1592                                                                          + options.getDefaultNodeShapeSize() + "]\n",
1593                                                                  "Node Shape Size",
1594                                                                  JOptionPane.QUESTION_MESSAGE,
1595                                                                  null,
1596                                                                  null,
1597                                                                  options.getDefaultNodeShapeSize() );
1598         if ( !ForesterUtil.isEmpty( s ) ) {
1599             boolean success = true;
1600             double m = 0.0;
1601             final String m_str = s.trim();
1602             if ( !ForesterUtil.isEmpty( m_str ) ) {
1603                 try {
1604                     m = Double.parseDouble( m_str );
1605                 }
1606                 catch ( final Exception ex ) {
1607                     success = false;
1608                 }
1609             }
1610             else {
1611                 success = false;
1612             }
1613             if ( success && ( m >= 0.0 ) ) {
1614                 final short size = ForesterUtil.roundToShort( m );
1615                 if ( size >= 0.0 ) {
1616                     options.setDefaultNodeShapeSize( size );
1617                 }
1618             }
1619         }
1620     }
1621
1622     static String createCurrentFontDesc( final TreeFontSet tree_font_set ) {
1623         return tree_font_set.getLargeFont().getFamily() + " " + tree_font_set.getLargeFont().getSize();
1624     }
1625
1626     static JMenu createMenu( final String title, final Configuration conf ) {
1627         final JMenu jmenu = new JMenu( title );
1628         if ( !conf.isUseNativeUI() ) {
1629             jmenu.setFont( MainFrame.menu_font );
1630             jmenu.setBackground( conf.getGuiMenuBackgroundColor() );
1631             jmenu.setForeground( conf.getGuiMenuTextColor() );
1632         }
1633         return jmenu;
1634     }
1635
1636     static JMenuItem customizeMenuItemAsLabel( final JMenuItem label, final Configuration configuration ) {
1637         label.setFont( MainFrame.menu_font.deriveFont( Font.BOLD ) );
1638         if ( !configuration.isUseNativeUI() ) {
1639             label.setBackground( configuration.getGuiMenuBackgroundColor() );
1640             label.setForeground( configuration.getGuiMenuTextColor() );
1641             label.setOpaque( true );
1642         }
1643         label.setSelected( false );
1644         label.setEnabled( false );
1645         return label;
1646     }
1647
1648     static void cycleNodeFill( final Options op, final TreePanel tree_panel ) {
1649         switch ( op.getDefaultNodeFill() ) {
1650             case GRADIENT:
1651                 op.setDefaultNodeFill( NodeFill.SOLID );
1652                 break;
1653             case NONE:
1654                 op.setDefaultNodeFill( NodeFill.GRADIENT );
1655                 break;
1656             case SOLID:
1657                 op.setDefaultNodeFill( NodeFill.NONE );
1658                 break;
1659             default:
1660                 throw new RuntimeException( "unknown fill: " + op.getDefaultNodeFill() );
1661         }
1662     }
1663
1664     static void cycleNodeShape( final Options op, final TreePanel tree_panel ) {
1665         switch ( op.getDefaultNodeShape() ) {
1666             case CIRCLE:
1667                 op.setDefaultNodeShape( NodeShape.RECTANGLE );
1668                 break;
1669             case RECTANGLE:
1670                 op.setDefaultNodeShape( NodeShape.CIRCLE );
1671                 break;
1672             default:
1673                 throw new RuntimeException( "unknown shape: " + op.getDefaultNodeShape() );
1674         }
1675     }
1676
1677     static void cycleOverview( final Options op, final TreePanel tree_panel ) {
1678         switch ( op.getOvPlacement() ) {
1679             case LOWER_LEFT:
1680                 op.setOvPlacement( Options.OVERVIEW_PLACEMENT_TYPE.UPPER_LEFT );
1681                 break;
1682             case LOWER_RIGHT:
1683                 op.setOvPlacement( Options.OVERVIEW_PLACEMENT_TYPE.LOWER_LEFT );
1684                 break;
1685             case UPPER_LEFT:
1686                 op.setOvPlacement( Options.OVERVIEW_PLACEMENT_TYPE.UPPER_RIGHT );
1687                 break;
1688             case UPPER_RIGHT:
1689                 op.setOvPlacement( Options.OVERVIEW_PLACEMENT_TYPE.LOWER_RIGHT );
1690                 break;
1691             default:
1692                 throw new RuntimeException( "unknown placement: " + op.getOvPlacement() );
1693         }
1694         if ( tree_panel != null ) {
1695             tree_panel.updateOvSettings();
1696         }
1697     }
1698
1699     static void setCycleNodeFillMenuItem( final JMenuItem mi, final Options options ) {
1700         if ( ( options != null ) && ( options.getDefaultNodeFill() != null ) ) {
1701             mi.setText( "Cycle Node Shape Fill Type... (current: "
1702                     + options.getDefaultNodeFill().toString().toLowerCase() + ")" );
1703         }
1704         else {
1705             mi.setText( "Cycle Node Shape Fill Type..." );
1706         }
1707     }
1708
1709     static void setCycleNodeShapeMenuItem( final JMenuItem mi, final Options options ) {
1710         if ( ( options != null ) && ( options.getDefaultNodeShape() != null ) ) {
1711             mi.setText( "Cycle Node Shape Fill Type... (current: "
1712                     + options.getDefaultNodeShape().toString().toLowerCase() + ")" );
1713         }
1714         else {
1715             mi.setText( "Cycle Node Shape Fill Type..." );
1716         }
1717     }
1718
1719     static void setOvPlacementColorChooseMenuItem( final JMenuItem mi, final Options options ) {
1720         if ( ( options != null ) && ( options.getOvPlacement() != null ) ) {
1721             mi.setText( "Cycle Overview Placement... (current: " + options.getOvPlacement() + ")" );
1722         }
1723         else {
1724             mi.setText( "Cycle Overview Placement..." );
1725         }
1726     }
1727
1728     static void setTextColorChooseMenuItem( final JMenuItem mi, final TreePanel tree_panel ) {
1729         if ( ( tree_panel != null ) && ( tree_panel.getTreeColorSet() != null ) ) {
1730             mi.setText( "Select Colors... (current: " + tree_panel.getTreeColorSet().getCurrentColorSchemeName() + ")" );
1731         }
1732         else {
1733             mi.setText( "Select Colors..." );
1734         }
1735     }
1736
1737     static void setTextForFontChooserMenuItem( final JMenuItem mi, final String font_desc ) {
1738         mi.setText( "Select Font... (current: " + font_desc + ")" );
1739     }
1740
1741     static void setTextMinSupportMenuItem( final JMenuItem mi, final Options options, final TreePanel current_tree_panel ) {
1742         if ( ( current_tree_panel == null ) || ( current_tree_panel.getPhylogeny() == null ) ) {
1743             mi.setEnabled( true );
1744         }
1745         else if ( AptxUtil.isHasAtLeastOneBranchWithSupportValues( current_tree_panel.getPhylogeny() ) ) {
1746             mi.setEnabled( true );
1747         }
1748         else {
1749             mi.setEnabled( false );
1750         }
1751         mi.setText( "Enter Min Confidence Value... (current: " + options.getMinConfidenceValue() + ")" );
1752     }
1753
1754     static void setTextNodeSizeMenuItem( final JMenuItem mi, final Options options ) {
1755         mi.setText( "Enter Default Node Shape Size... (current: " + options.getDefaultNodeShapeSize() + ")" );
1756     }
1757
1758     static void updateOptionsMenuDependingOnPhylogenyType( final MainPanel main_panel,
1759                                                            final JCheckBoxMenuItem scale,
1760                                                            final JCheckBoxMenuItem branch_lengths,
1761                                                            final JRadioButtonMenuItem non_lined_up,
1762                                                            final JRadioButtonMenuItem uniform_clado,
1763                                                            final JRadioButtonMenuItem nonuniform_clado,
1764                                                            final JCheckBoxMenuItem label_direction_cbmi ) {
1765         final TreePanel tree_panel = main_panel.getCurrentTreePanel();
1766         final ControlPanel control = main_panel.getControlPanel();
1767         final Options options = main_panel.getOptions();
1768         scale.setSelected( options.isShowScale() );
1769         branch_lengths.setSelected( options.isShowBranchLengthValues() );
1770         // non_lined_up.setSelected( options.isNonLinedUpCladogram() );
1771         if ( ( tree_panel != null ) && ( !tree_panel.isPhyHasBranchLengths() ) ) {
1772             scale.setSelected( false );
1773             scale.setEnabled( false );
1774             branch_lengths.setSelected( false );
1775             branch_lengths.setEnabled( false );
1776         }
1777         else if ( ( tree_panel != null ) && !control.isDrawPhylogram() ) {
1778             scale.setSelected( false );
1779             scale.setEnabled( false );
1780             branch_lengths.setEnabled( true );
1781         }
1782         else {
1783             scale.setEnabled( true );
1784             branch_lengths.setEnabled( true );
1785         }
1786         if ( ( tree_panel != null )
1787                 && ( ( tree_panel.getPhylogenyGraphicsType() != PHYLOGENY_GRAPHICS_TYPE.ROUNDED )
1788                         && ( tree_panel.getPhylogenyGraphicsType() != PHYLOGENY_GRAPHICS_TYPE.EURO_STYLE ) && ( tree_panel
1789                         .getPhylogenyGraphicsType() != PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR ) ) ) {
1790             branch_lengths.setSelected( false );
1791             branch_lengths.setEnabled( false );
1792         }
1793         if ( tree_panel != null ) {
1794             if ( ( tree_panel.getPhylogenyGraphicsType() == PHYLOGENY_GRAPHICS_TYPE.CIRCULAR )
1795                     || ( tree_panel.getPhylogenyGraphicsType() == PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) ) {
1796                 non_lined_up.setEnabled( false );
1797                 uniform_clado.setEnabled( false );
1798                 nonuniform_clado.setEnabled( false );
1799             }
1800             else {
1801                 non_lined_up.setEnabled( true );
1802                 uniform_clado.setEnabled( true );
1803                 nonuniform_clado.setEnabled( true );
1804             }
1805         }
1806         else {
1807             if ( ( tree_panel != null )
1808                     && ( ( tree_panel.getPhylogenyGraphicsType() != PHYLOGENY_GRAPHICS_TYPE.EURO_STYLE ) && ( tree_panel
1809                             .getPhylogenyGraphicsType() != PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR ) ) ) {
1810                 branch_lengths.setSelected( false );
1811                 branch_lengths.setEnabled( false );
1812             }
1813             if ( ( tree_panel != null )
1814                     && ( ( tree_panel.getPhylogenyGraphicsType() == PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) || ( tree_panel
1815                             .getPhylogenyGraphicsType() == PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) ) ) {
1816                 non_lined_up.setEnabled( false );
1817             }
1818             else {
1819                 // non_lined_up.setSelected( options.isNonLinedUpCladogram() );
1820                 non_lined_up.setEnabled( true );
1821             }
1822         }
1823         label_direction_cbmi.setEnabled( true );
1824         if ( tree_panel != null ) {
1825             if ( ( tree_panel.getPhylogenyGraphicsType() != PHYLOGENY_GRAPHICS_TYPE.UNROOTED )
1826                     && ( tree_panel.getPhylogenyGraphicsType() != PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) ) {
1827                 label_direction_cbmi.setEnabled( false );
1828             }
1829             if ( tree_panel.getPhylogenyGraphicsType() == PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) {
1830                 scale.setSelected( false );
1831                 scale.setEnabled( false );
1832             }
1833         }
1834     }
1835
1836     static void updateScreenTextAntialias( final List<TreePanel> treepanels ) {
1837         for( final TreePanel tree_panel : treepanels ) {
1838             tree_panel.setTextAntialias();
1839         }
1840     }
1841 }