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