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