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