Thank you, Oracle!
[jalview.git] / forester / java / src / org / forester / archaeopteryx / ArchaeopteryxE.java
1
2 package org.forester.archaeopteryx;
3
4 import java.awt.BorderLayout;
5 import java.awt.Container;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.awt.event.ComponentAdapter;
9 import java.awt.event.ComponentEvent;
10 import java.io.ByteArrayOutputStream;
11 import java.io.File;
12 import java.io.IOException;
13 import java.net.URL;
14 import java.util.LinkedList;
15 import java.util.List;
16 import java.util.NoSuchElementException;
17
18 import javax.swing.ButtonGroup;
19 import javax.swing.JApplet;
20 import javax.swing.JCheckBoxMenuItem;
21 import javax.swing.JFileChooser;
22 import javax.swing.JMenu;
23 import javax.swing.JMenuBar;
24 import javax.swing.JMenuItem;
25 import javax.swing.JOptionPane;
26 import javax.swing.JRadioButtonMenuItem;
27 import javax.swing.UIManager;
28 import javax.swing.UnsupportedLookAndFeelException;
29 import javax.swing.event.ChangeEvent;
30 import javax.swing.event.ChangeListener;
31
32 import org.apache.commons.codec.binary.Base64;
33 import org.forester.archaeopteryx.AptxUtil.GraphicsExportType;
34 import org.forester.archaeopteryx.Options.CLADOGRAM_TYPE;
35 import org.forester.archaeopteryx.Options.NODE_LABEL_DIRECTION;
36 import org.forester.archaeopteryx.Options.PHYLOGENY_GRAPHICS_TYPE;
37 import org.forester.io.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION;
38 import org.forester.phylogeny.Phylogeny;
39 import org.forester.phylogeny.PhylogenyMethods;
40 import org.forester.phylogeny.PhylogenyMethods.DESCENDANT_SORT_PRIORITY;
41 import org.forester.phylogeny.data.SequenceRelation;
42 import org.forester.sdi.GSDI;
43 import org.forester.sdi.GSDIR;
44 import org.forester.sdi.SDIException;
45 import org.forester.util.ForesterConstants;
46 import org.forester.util.ForesterUtil;
47 import org.forester.util.WindowsUtils;
48
49 // Use like this:
50 // <applet archive="forester.jar"
51 // code="org.forester.archaeopteryx.ArchaeopteryxE.class"
52 // codebase="http://www.myserver.org/path/to/forester"
53 // width="600"
54 // height="500"
55 // alt="ArchaeopteryxE is not working on your system (requires at least Sun Java 1.5)!">
56 // <param name="url_of_tree_to_load"
57 // value="http://www.myserver.org/examples/data/apaf.xml">
58 // <param name="config_file"
59 // value="http://www.myserver.org/examples/config/config_file.txt">
60 // </applet>
61 public class ArchaeopteryxE extends JApplet implements ActionListener {
62
63     private final static String         NAME             = "ArchaeopteryxE";
64     private static final long           serialVersionUID = -1220055577935759443L;
65     private Configuration               _configuration;
66     private MainPanel                   _mainpanel;
67     private JMenuBar                    _jmenubar;
68     private JMenu                       _options_jmenu;
69     private JMenu                       _font_size_menu;
70     private JMenuItem                   _super_tiny_fonts_mi;
71     private JMenuItem                   _tiny_fonts_mi;
72     private JMenuItem                   _small_fonts_mi;
73     private JMenuItem                   _medium_fonts_mi;
74     private JMenuItem                   _large_fonts_mi;
75     private JMenu                       _tools_menu;
76     private JMenuItem                   _taxcolor_item;
77     private JMenuItem                   _confcolor_item;
78     private JMenuItem                   _midpoint_root_item;
79     private JMenu                       _view_jmenu;
80     private JMenuItem                   _view_as_XML_item;
81     private JMenuItem                   _view_as_NH_item;
82     private JMenuItem                   _view_as_nexus_item;
83     private JMenuItem                   _display_basic_information_item;
84     private JMenu                       _type_menu;
85     private JCheckBoxMenuItem           _rectangular_type_cbmi;
86     private JCheckBoxMenuItem           _triangular_type_cbmi;
87     private JCheckBoxMenuItem           _curved_type_cbmi;
88     private JCheckBoxMenuItem           _convex_type_cbmi;
89     private JCheckBoxMenuItem           _euro_type_cbmi;
90     private JCheckBoxMenuItem           _rounded_type_cbmi;
91     private JCheckBoxMenuItem           _unrooted_type_cbmi;
92     private JCheckBoxMenuItem           _circular_type_cbmi;
93     private JMenuItem                   _help_item;
94     private JMenuItem                   _about_item;
95     private JMenu                       _help_jmenu;
96     private JMenuItem                   _website_item;
97     private JMenuItem                   _phyloxml_website_item;
98     private JMenuItem                   _phyloxml_ref_item;
99     private JMenuItem                   _aptx_ref_item;
100     private JMenuItem                   _remove_branch_color_item;
101     private JMenuItem                   _remove_visual_styles_item;
102     private JCheckBoxMenuItem           _show_domain_labels;
103     private JCheckBoxMenuItem           _show_annotation_ref_source;
104     private JCheckBoxMenuItem           _color_labels_same_as_parent_branch;
105     private JCheckBoxMenuItem           _abbreviate_scientific_names;
106     private JCheckBoxMenuItem           _screen_antialias_cbmi;
107     private JCheckBoxMenuItem           _background_gradient_cbmi;
108     private JCheckBoxMenuItem           _color_by_taxonomic_group_cbmi;
109     private JRadioButtonMenuItem        _non_lined_up_cladograms_rbmi;
110     private JRadioButtonMenuItem        _uniform_cladograms_rbmi;
111     private JRadioButtonMenuItem        _ext_node_dependent_cladogram_rbmi;
112     private Options                     _options;
113     private JMenuItem                   _choose_font_mi;
114     private JMenuItem                   _switch_colors_mi;
115     JCheckBoxMenuItem                   _label_direction_cbmi;
116     private JCheckBoxMenuItem           _show_scale_cbmi;
117     private JCheckBoxMenuItem           _search_case_senstive_cbmi;
118     private JCheckBoxMenuItem           _search_whole_words_only_cbmi;
119     private JCheckBoxMenuItem           _inverse_search_result_cbmi;
120     private JCheckBoxMenuItem           _search_with_regex_cbmi;
121     private JCheckBoxMenuItem           _show_overview_cbmi;
122     private JMenuItem                   _choose_minimal_confidence_mi;
123     private JMenuItem                   _collapse_species_specific_subtrees;
124     private JMenuItem                   _overview_placment_mi;
125     private ButtonGroup                 _radio_group_1;
126     private JCheckBoxMenuItem           _show_default_node_shapes_internal_cbmi;
127     private JCheckBoxMenuItem           _show_default_node_shapes_external_cbmi;
128     private JCheckBoxMenuItem           _show_default_node_shapes_for_marked_cbmi;
129     private JMenuItem                   _cycle_node_shape_mi;
130     private JMenuItem                   _cycle_node_fill_mi;
131     private JMenuItem                   _choose_node_size_mi;
132     private JCheckBoxMenuItem           _show_confidence_stddev_cbmi;
133     private final LinkedList<TextFrame> _textframes      = new LinkedList<TextFrame>();
134     private JMenu                       _analysis_menu;
135     private JMenuItem                   _gsdi_item;
136     private JMenuItem                   _gsdir_item;
137     private Phylogeny                   _species_tree;
138     private JCheckBoxMenuItem           _right_line_up_domains_cbmi;
139     private JCheckBoxMenuItem           _line_up_renderable_data_cbmi;
140     // file menu:
141     private JMenuItem                   _save_item;
142     private JMenuItem                   _print_item;
143     private JMenuItem                   _write_to_pdf_item;
144     private JMenuItem                   _write_to_jpg_item;
145     private JMenuItem                   _write_to_gif_item;
146     private JMenuItem                   _write_to_tif_item;
147     private JMenuItem                   _write_to_png_item;
148     private JMenuItem                   _write_to_bmp_item;
149     private JMenu                       _file_jmenu;
150     private JFileChooser                _writetopdf_filechooser;
151     private File                        _current_dir;
152     private JFileChooser                _save_filechooser;
153     private JFileChooser                _writetographics_filechooser;
154     private JCheckBoxMenuItem           _graphics_export_visible_only_cbmi;
155     private JCheckBoxMenuItem           _antialias_print_cbmi;
156     private JCheckBoxMenuItem           _print_black_and_white_cbmi;
157     private JCheckBoxMenuItem           _print_using_actual_size_cbmi;
158     private JCheckBoxMenuItem           _graphics_export_using_actual_size_cbmi;
159     private JMenuItem                   _print_size_mi;
160     private JMenuItem                   _choose_pdf_width_mi;
161
162     @Override
163     public void actionPerformed( final ActionEvent e ) {
164         final Object o = e.getSource();
165         if ( o == _midpoint_root_item ) {
166             getMainPanel().getCurrentTreePanel().midpointRoot();
167         }
168         else if ( o == _gsdi_item ) {
169             if ( isSubtreeDisplayed() ) {
170                 return;
171             }
172             executeGSDI();
173         }
174         else if ( o == _gsdir_item ) {
175             if ( isSubtreeDisplayed() ) {
176                 return;
177             }
178             executeGSDIR();
179         }
180         else if ( o == _taxcolor_item ) {
181             getMainPanel().getCurrentTreePanel().taxColor();
182         }
183         else if ( o == _confcolor_item ) {
184             getMainPanel().getCurrentTreePanel().confColor();
185         }
186         else if ( o == _collapse_species_specific_subtrees ) {
187             if ( getCurrentTreePanel() != null ) {
188                 getCurrentTreePanel().collapseSpeciesSpecificSubtrees();
189             }
190         }
191         else if ( o == _remove_branch_color_item ) {
192             removeBranchColors();
193         }
194         else if ( o == _remove_visual_styles_item ) {
195             removeVisualStyles();
196         }
197         else if ( o == _switch_colors_mi ) {
198             switchColors();
199         }
200         else if ( o == _display_basic_information_item ) {
201             displayBasicInformation();
202         }
203         else if ( o == _view_as_NH_item ) {
204             viewAsNH();
205         }
206         else if ( o == _view_as_XML_item ) {
207             viewAsXML();
208         }
209         else if ( o == _view_as_nexus_item ) {
210             viewAsNexus();
211         }
212         else if ( o == _super_tiny_fonts_mi ) {
213             if ( getCurrentTreePanel() != null ) {
214                 getCurrentTreePanel().setSuperTinyFonts();
215                 getCurrentTreePanel().repaint();
216             }
217         }
218         else if ( o == _tiny_fonts_mi ) {
219             if ( getCurrentTreePanel() != null ) {
220                 getCurrentTreePanel().setTinyFonts();
221                 getCurrentTreePanel().repaint();
222             }
223         }
224         else if ( o == _small_fonts_mi ) {
225             if ( getCurrentTreePanel() != null ) {
226                 getCurrentTreePanel().setSmallFonts();
227                 getCurrentTreePanel().repaint();
228             }
229         }
230         else if ( o == _medium_fonts_mi ) {
231             if ( getCurrentTreePanel() != null ) {
232                 getCurrentTreePanel().setMediumFonts();
233                 getCurrentTreePanel().repaint();
234             }
235         }
236         else if ( o == _large_fonts_mi ) {
237             if ( getCurrentTreePanel() != null ) {
238                 getCurrentTreePanel().setLargeFonts();
239                 getCurrentTreePanel().repaint();
240             }
241         }
242         else if ( o == _choose_font_mi ) {
243             chooseFont();
244         }
245         else if ( o == _choose_minimal_confidence_mi ) {
246             chooseMinimalConfidence();
247         }
248         else if ( o == _choose_node_size_mi ) {
249             MainFrame.chooseNodeSize( getOptions(), this );
250         }
251         else if ( o == _overview_placment_mi ) {
252             MainFrame.cycleOverview( getOptions(), getCurrentTreePanel() );
253         }
254         else if ( o == _cycle_node_fill_mi ) {
255             MainFrame.cycleNodeFill( getOptions() );
256         }
257         else if ( o == _cycle_node_shape_mi ) {
258             MainFrame.cycleNodeShape( getOptions() );
259         }
260         else if ( o == _non_lined_up_cladograms_rbmi ) {
261             updateOptions( getOptions() );
262             _mainpanel.getControlPanel().showWhole();
263         }
264         else if ( o == _uniform_cladograms_rbmi ) {
265             updateOptions( getOptions() );
266             _mainpanel.getControlPanel().showWhole();
267         }
268         else if ( o == _ext_node_dependent_cladogram_rbmi ) {
269             updateOptions( getOptions() );
270             _mainpanel.getControlPanel().showWhole();
271         }
272         else if ( o == _search_case_senstive_cbmi ) {
273             updateOptions( getOptions() );
274             getMainPanel().getControlPanel().search0();
275             getMainPanel().getControlPanel().search1();
276         }
277         else if ( o == _search_whole_words_only_cbmi ) {
278             if ( ( _search_with_regex_cbmi != null ) && _search_whole_words_only_cbmi.isSelected() ) {
279                 _search_with_regex_cbmi.setSelected( false );
280             }
281             updateOptions( getOptions() );
282             getMainPanel().getControlPanel().search0();
283             getMainPanel().getControlPanel().search1();
284         }
285         else if ( o == _inverse_search_result_cbmi ) {
286             updateOptions( getOptions() );
287             getMainPanel().getControlPanel().search0();
288             getMainPanel().getControlPanel().search1();
289         }
290         else if ( o == _search_with_regex_cbmi ) {
291             if ( ( _search_whole_words_only_cbmi != null ) && _search_with_regex_cbmi.isSelected() ) {
292                 _search_whole_words_only_cbmi.setSelected( false );
293             }
294             if ( ( _search_case_senstive_cbmi != null ) && _search_with_regex_cbmi.isSelected() ) {
295                 _search_case_senstive_cbmi.setSelected( true );
296             }
297             updateOptions( getOptions() );
298             getMainPanel().getControlPanel().search0();
299             getMainPanel().getControlPanel().search1();
300         }
301         else if ( o == _show_scale_cbmi ) {
302             updateOptions( getOptions() );
303         }
304         else if ( o == _show_confidence_stddev_cbmi ) {
305             updateOptions( getOptions() );
306         }
307         else if ( o == _label_direction_cbmi ) {
308             updateOptions( getOptions() );
309         }
310         else if ( o == _abbreviate_scientific_names ) {
311             updateOptions( getOptions() );
312         }
313         else if ( o == _show_overview_cbmi ) {
314             updateOptions( getOptions() );
315             if ( getCurrentTreePanel() != null ) {
316                 getCurrentTreePanel().updateOvSizes();
317             }
318         }
319         else if ( ( o == _rectangular_type_cbmi ) || ( o == _triangular_type_cbmi ) || ( o == _curved_type_cbmi )
320                 || ( o == _convex_type_cbmi ) || ( o == _rounded_type_cbmi ) || ( o == _euro_type_cbmi )
321                 || ( o == _unrooted_type_cbmi ) || ( o == _circular_type_cbmi ) ) {
322             typeChanged( o );
323         }
324         else if ( o == _screen_antialias_cbmi ) {
325             updateOptions( getOptions() );
326             setupScreenTextAntialias( getMainPanel().getTreePanels(), isScreenAntialias() );
327         }
328         else if ( o == _background_gradient_cbmi ) {
329             updateOptions( getOptions() );
330         }
331         else if ( o == _show_domain_labels ) {
332             updateOptions( getOptions() );
333         }
334         else if ( o == _color_labels_same_as_parent_branch ) {
335             updateOptions( getOptions() );
336         }
337         else if ( o == _show_default_node_shapes_internal_cbmi ) {
338             updateOptions( getOptions() );
339         }
340         else if ( o == _show_default_node_shapes_external_cbmi ) {
341             updateOptions( getOptions() );
342         }
343         else if ( o == _about_item ) {
344             MainFrame.about();
345         }
346         else if ( o == _help_item ) {
347             try {
348                 AptxUtil.openWebsite( Constants.APTX_DOC_SITE, true, this );
349             }
350             catch ( final IOException e1 ) {
351                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
352             }
353         }
354         else if ( o == _website_item ) {
355             try {
356                 AptxUtil.openWebsite( Constants.APTX_WEB_SITE, true, this );
357             }
358             catch ( final IOException e1 ) {
359                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
360             }
361         }
362         else if ( o == _phyloxml_website_item ) {
363             try {
364                 AptxUtil.openWebsite( Constants.PHYLOXML_WEB_SITE, true, this );
365             }
366             catch ( final IOException e1 ) {
367                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
368             }
369         }
370         else if ( o == _aptx_ref_item ) {
371             try {
372                 AptxUtil.openWebsite( Constants.APTX_REFERENCE_URL, true, this );
373             }
374             catch ( final IOException e1 ) {
375                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
376             }
377         }
378         else if ( o == _phyloxml_ref_item ) {
379             try {
380                 AptxUtil.openWebsite( Constants.PHYLOXML_REFERENCE_URL, true, this );
381             }
382             catch ( final IOException e1 ) {
383                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
384             }
385         }
386         else if ( o == _color_by_taxonomic_group_cbmi ) {
387             updateOptions( getOptions() );
388         }
389         else if ( o == _line_up_renderable_data_cbmi ) {
390             if ( !_line_up_renderable_data_cbmi.isSelected() ) {
391                 _right_line_up_domains_cbmi.setSelected( false );
392             }
393             updateOptions( getOptions() );
394         }
395         else if ( o == _right_line_up_domains_cbmi ) {
396             if ( _right_line_up_domains_cbmi.isSelected() ) {
397                 _line_up_renderable_data_cbmi.setSelected( true );
398             }
399             updateOptions( getOptions() );
400         }
401         else if ( o == _write_to_pdf_item ) {
402             final File curr_dir = MainFrame.writeToPdf( _mainpanel.getCurrentPhylogeny(),
403                                                         getMainPanel(),
404                                                         _writetopdf_filechooser,
405                                                         _current_dir,
406                                                         getContentPane(),
407                                                         this );
408             if ( curr_dir != null ) {
409                 setCurrentDir( curr_dir );
410             }
411         }
412         else if ( o == _write_to_jpg_item ) {
413             final File curr_dir = MainFrame.writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(),
414                                                                  GraphicsExportType.JPG,
415                                                                  _mainpanel,
416                                                                  _writetographics_filechooser,
417                                                                  this,
418                                                                  getContentPane(),
419                                                                  _current_dir );
420             if ( curr_dir != null ) {
421                 setCurrentDir( curr_dir );
422             }
423         }
424         else if ( o == _write_to_gif_item ) {
425             final File curr_dir = MainFrame.writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(),
426                                                                  GraphicsExportType.GIF,
427                                                                  _mainpanel,
428                                                                  _writetographics_filechooser,
429                                                                  this,
430                                                                  getContentPane(),
431                                                                  _current_dir );
432             if ( curr_dir != null ) {
433                 setCurrentDir( curr_dir );
434             }
435         }
436         else if ( o == _write_to_tif_item ) {
437             final File curr_dir = MainFrame.writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(),
438                                                                  GraphicsExportType.TIFF,
439                                                                  _mainpanel,
440                                                                  _writetographics_filechooser,
441                                                                  this,
442                                                                  getContentPane(),
443                                                                  _current_dir );
444             if ( curr_dir != null ) {
445                 setCurrentDir( curr_dir );
446             }
447         }
448         else if ( o == _write_to_bmp_item ) {
449             final File curr_dir = MainFrame.writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(),
450                                                                  GraphicsExportType.BMP,
451                                                                  _mainpanel,
452                                                                  _writetographics_filechooser,
453                                                                  this,
454                                                                  getContentPane(),
455                                                                  _current_dir );
456             if ( curr_dir != null ) {
457                 setCurrentDir( curr_dir );
458             }
459         }
460         else if ( o == _write_to_png_item ) {
461             final File curr_dir = MainFrame.writeToGraphicsFile( _mainpanel.getCurrentPhylogeny(),
462                                                                  GraphicsExportType.PNG,
463                                                                  _mainpanel,
464                                                                  _writetographics_filechooser,
465                                                                  this,
466                                                                  getContentPane(),
467                                                                  _current_dir );
468             if ( curr_dir != null ) {
469                 setCurrentDir( curr_dir );
470             }
471         }
472         else if ( o == _print_item ) {
473             MainFrame.print( getCurrentTreePanel(), getOptions(), this );
474         }
475         else if ( o == _save_item ) {
476             final File new_dir = MainFrame.writeToFile( _mainpanel.getCurrentPhylogeny(),
477                                                         getMainPanel(),
478                                                         _save_filechooser,
479                                                         _current_dir,
480                                                         getContentPane(),
481                                                         this );
482             if ( new_dir != null ) {
483                 setCurrentDir( new_dir );
484             }
485         }
486         else if ( o == _graphics_export_visible_only_cbmi ) {
487             updateOptions( getOptions() );
488         }
489         else if ( o == _antialias_print_cbmi ) {
490             updateOptions( getOptions() );
491         }
492         else if ( o == _print_black_and_white_cbmi ) {
493             updateOptions( getOptions() );
494         }
495         else if ( o == _print_using_actual_size_cbmi ) {
496             updateOptions( getOptions() );
497         }
498         else if ( o == _graphics_export_using_actual_size_cbmi ) {
499             updateOptions( getOptions() );
500         }
501         else if ( o == _print_size_mi ) {
502             choosePrintSize();
503         }
504         else if ( o == _choose_pdf_width_mi ) {
505             choosePdfWidth();
506         }
507         repaint();
508     }
509
510     @Override
511     public void destroy() {
512         AptxUtil.printAppletMessage( NAME, "going to be destroyed " );
513         removeAllTextFrames();
514         if ( getMainPanel() != null ) {
515             getMainPanel().terminate();
516         }
517     }
518
519     /**
520      * This method returns the current external node data which
521      * has been selected by the user by clicking the "Return ..."
522      * menu item. This method is expected to be called from Javascript or
523      * something like it.
524      *
525      * @return current external node data as String
526      */
527     public String getCurrentExternalNodesDataBuffer() {
528         return getCurrentTreePanel().getCurrentExternalNodesDataBufferAsString();
529     }
530
531     public int getCurrentExternalNodesDataBufferChangeCounter() {
532         return getCurrentTreePanel().getCurrentExternalNodesDataBufferChangeCounter();
533     }
534
535     public int getCurrentExternalNodesDataBufferLength() {
536         return getCurrentTreePanel().getCurrentExternalNodesDataBufferAsString().length();
537     }
538
539     /**
540      * This method returns the current phylogeny as a string in the chosen format
541      *
542      * @param format must be NH, NHX, NEXUS or PHYLOXML
543      * @return the phylogeny string
544      * @author Herve Menager
545      */
546     public String getCurrentPhylogeny( final String format ) {
547         removeAllTextFrames();
548         if ( ( getMainPanel().getCurrentPhylogeny() == null ) || getMainPanel().getCurrentPhylogeny().isEmpty()
549                 || ( getMainPanel().getCurrentPhylogeny().getNumberOfExternalNodes() > 10000 ) ) {
550             return new String();
551         }
552         switch ( ForesterConstants.PhylogeneticTreeFormats.valueOf( format ) ) {
553             case NH:
554                 return getMainPanel().getCurrentPhylogeny().toNewHampshire();
555             case NHX:
556                 return getMainPanel().getCurrentPhylogeny().toNewHampshireX();
557             case NEXUS:
558                 return getMainPanel().getCurrentPhylogeny().toNexus();
559             case PHYLOXML:
560                 return getMainPanel().getCurrentPhylogeny().toPhyloXML( -1 );
561             default:
562                 break;
563         }
564         return new String();
565     }
566
567     /**
568      * This method returns a view of the current phylogeny in a chosen
569      * graphics format, base64-encoded in a string so that in can be used
570      * from javascript.
571      *
572      * @param format must be GraphicsExportType (gif, jpg, pdf, png, tif, bmp)
573      * @return the phylogeny string
574      * @author Herve Menager
575      */
576     public String getCurrentPhylogenyGraphicsAsBase64EncodedString( final String format ) {
577         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
578         try {
579             AptxUtil.writePhylogenyToGraphicsByteArrayOutputStream( baos,
580                                                                     _mainpanel.getWidth(),
581                                                                     _mainpanel.getHeight(),
582                                                                     getCurrentTreePanel(),
583                                                                     getCurrentTreePanel().getControlPanel(),
584                                                                     GraphicsExportType.valueOf( format ),
585                                                                     getOptions() );
586         }
587         catch ( final IOException ioe ) {
588             ForesterUtil.printErrorMessage( NAME, ioe.toString() );
589             ioe.printStackTrace();
590             JOptionPane.showMessageDialog( this,
591                                            NAME + ": Failed to generate graphics: " + "\nException: " + ioe,
592                                            "Failed to generate graphics",
593                                            JOptionPane.ERROR_MESSAGE );
594             return null;
595         }
596         final byte[] bytes = baos.toByteArray();
597         final String dataImg = Base64.encodeBase64String( bytes );
598         return dataImg;
599     }
600
601     public Options getOptions() {
602         return _options;
603     }
604
605     @Override
606     public void init() {
607         _writetopdf_filechooser = new JFileChooser();
608         _writetopdf_filechooser.setMultiSelectionEnabled( false );
609         _writetopdf_filechooser.addChoosableFileFilter( MainFrame.pdffilter );
610         _writetographics_filechooser = new JFileChooser();
611         _writetographics_filechooser.setMultiSelectionEnabled( false );
612         _writetographics_filechooser.addChoosableFileFilter( MainFrame.graphicsfilefilter );
613         _save_filechooser = new JFileChooser();
614         _save_filechooser.setMultiSelectionEnabled( false );
615         _save_filechooser.setFileFilter( MainFrame.xmlfilter );
616         _save_filechooser.addChoosableFileFilter( MainFrame.nhfilter );
617         _save_filechooser.addChoosableFileFilter( MainFrame.nexusfilter );
618         _save_filechooser.addChoosableFileFilter( _save_filechooser.getAcceptAllFileFilter() );
619         try {
620             final String home_dir = System.getProperty( "user.home" );
621             _save_filechooser.setCurrentDirectory( new File( home_dir ) );
622             _writetopdf_filechooser.setCurrentDirectory( new File( home_dir ) );
623             _writetographics_filechooser.setCurrentDirectory( new File( home_dir ) );
624         }
625         catch ( final Exception e ) {
626             e.printStackTrace();
627             // Do nothing. Not important.
628         }
629         final String config_filename = getParameter( Constants.APPLET_PARAM_NAME_FOR_CONFIG_FILE_URL );
630         AptxUtil.printAppletMessage( NAME, "URL for configuration file is: " + config_filename );
631         final Configuration configuration = new Configuration( config_filename, true, true, true );
632         setConfiguration( configuration );
633         setOptions( Options.createInstance( configuration ) );
634         setupUI();
635         final String tree_url_str = getParameter( Constants.APPLET_PARAM_NAME_FOR_URL_OF_TREE_TO_LOAD );
636         if ( ForesterUtil.isEmpty( tree_url_str ) ) {
637             ForesterUtil.printErrorMessage( NAME, "could not get tree URL from "
638                     + Constants.APPLET_PARAM_NAME_FOR_URL_OF_TREE_TO_LOAD );
639             JOptionPane.showMessageDialog( this, NAME + ": could not get tree URL from "
640                     + Constants.APPLET_PARAM_NAME_FOR_URL_OF_TREE_TO_LOAD, "Failed get URL", JOptionPane.ERROR_MESSAGE );
641             return;
642         }
643         AptxUtil.printAppletMessage( NAME, "URL for phylogenies is " + tree_url_str );
644         // Get URL to tree file
645         URL phys_url = null;
646         try {
647             phys_url = new URL( tree_url_str );
648         }
649         catch ( final Exception e ) {
650             ForesterUtil.printErrorMessage( NAME, "error: " + e );
651             e.printStackTrace();
652             JOptionPane.showMessageDialog( this, NAME + ": Could not create URL from: \"" + tree_url_str
653                                            + "\"\nException: " + e, "Failed to create URL", JOptionPane.ERROR_MESSAGE );
654         }
655         if ( phys_url == null ) {
656             ForesterUtil.printErrorMessage( NAME, "failed to get tree URL from "
657                     + Constants.APPLET_PARAM_NAME_FOR_URL_OF_TREE_TO_LOAD );
658             JOptionPane.showMessageDialog( this,
659                                            NAME + ": Could not create URL from: \"" + tree_url_str,
660                                            "Failed to create URL",
661                                            JOptionPane.ERROR_MESSAGE );
662             return;
663         }
664         // Load the tree from URL
665         Phylogeny[] phys = null;
666         try {
667             phys = AptxUtil.readPhylogeniesFromUrl( phys_url,
668                                                     getConfiguration().isValidatePhyloXmlAgainstSchema(),
669                                                     getConfiguration().isReplaceUnderscoresInNhParsing(),
670                                                     getConfiguration().isInternalNumberAreConfidenceForNhParsing(),
671                                                     getConfiguration().getTaxonomyExtraction(),
672                                                     getConfiguration().isMidpointReroot() );
673         }
674         catch ( final Exception e ) {
675             ForesterUtil.printErrorMessage( NAME, e.toString() );
676             e.printStackTrace();
677             JOptionPane.showMessageDialog( this,
678                                            NAME + ": Failed to read phylogenies: " + "\nException: " + e,
679                                            "Failed to read phylogenies",
680                                            JOptionPane.ERROR_MESSAGE );
681         }
682         if ( phys == null ) {
683             ForesterUtil.printErrorMessage( NAME, "phylogenies from [" + phys_url + "] are null" );
684             JOptionPane.showMessageDialog( this,
685                                            NAME + ": phylogenies from [" + phys_url + "] are null",
686                                            "Failed to read phylogenies",
687                                            JOptionPane.ERROR_MESSAGE );
688             return;
689         }
690         else if ( phys.length < 1 ) {
691             ForesterUtil.printErrorMessage( NAME, "phylogenies from [" + phys_url + "] are empty" );
692             JOptionPane.showMessageDialog( this,
693                                            NAME + ": phylogenies from [" + phys_url + "] are empty",
694                                            "Failed to read phylogenies",
695                                            JOptionPane.ERROR_MESSAGE );
696             return;
697         }
698         else {
699             AptxUtil.printAppletMessage( NAME, "loaded " + phys.length + " phylogenies from: " + phys_url );
700         }
701         //
702         final String species_tree_url_str = getParameter( Constants.APPLET_PARAM_NAME_FOR_URL_OF_SPECIES_TREE_TO_LOAD );
703         if ( !ForesterUtil.isEmpty( species_tree_url_str ) ) {
704             AptxUtil.printAppletMessage( NAME, "URL of species tree to load: \"" + species_tree_url_str + "\"" );
705             Phylogeny[] species_trees = null;
706             try {
707                 final URL species_tree_url = new URL( species_tree_url_str );
708                 species_trees = AptxUtil.readPhylogeniesFromUrl( species_tree_url,
709                                                                  configuration.isValidatePhyloXmlAgainstSchema(),
710                                                                  configuration.isReplaceUnderscoresInNhParsing(),
711                                                                  false,
712                                                                  TAXONOMY_EXTRACTION.NO,
713                                                                  false );
714             }
715             catch ( final IOException e ) {
716                 ForesterUtil.printErrorMessage( NAME, "could not read species tree from  [" + species_tree_url_str
717                                                 + "]" );
718                 JOptionPane.showMessageDialog( this, NAME + ": could not read species tree from  ["
719                         + species_tree_url_str + "]", "Failed to read species tree", JOptionPane.ERROR_MESSAGE );
720             }
721             if ( ( species_trees != null ) && ( species_trees.length > 0 ) ) {
722                 AptxUtil.printAppletMessage( NAME, "successfully read species tree" );
723                 if ( species_trees[ 0 ].isEmpty() ) {
724                     ForesterUtil.printErrorMessage( NAME, "species tree is empty" );
725                 }
726                 else if ( !species_trees[ 0 ].isRooted() ) {
727                     ForesterUtil.printErrorMessage( NAME, "species tree is not rooted" );
728                 }
729                 else {
730                     setSpeciesTree( species_trees[ 0 ] );
731                     AptxUtil.printAppletMessage( NAME, "species tree OK" );
732                 }
733             }
734         }
735         //
736         try {
737             setVisible( false );
738             setMainPanel( new MainPanelApplets( getConfiguration(), this ) );
739             _jmenubar = new JMenuBar();
740             if ( !getConfiguration().isHideControlPanelAndMenubar() ) {
741                 buildFileMenu();
742                 if ( !getConfiguration().isUseNativeUI() ) {
743                     _jmenubar.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
744                 }
745                 if ( getSpeciesTree() != null ) {
746                     buildAnalysisMenu();
747                 }
748                 buildToolsMenu();
749                 buildViewMenu();
750                 buildFontSizeMenu();
751                 buildOptionsMenu();
752                 buildTypeMenu();
753                 buildHelpMenu();
754                 setJMenuBar( _jmenubar );
755             }
756             final Container contentpane = getContentPane();
757             contentpane.setLayout( new BorderLayout() );
758             contentpane.add( getMainPanel(), BorderLayout.CENTER );
759             addComponentListener( new ComponentAdapter() {
760
761                 @Override
762                 public void componentResized( final ComponentEvent e ) {
763                     if ( getMainPanel().getCurrentTreePanel() != null ) {
764                         getMainPanel().getCurrentTreePanel().calcParametersForPainting( getMainPanel()
765                                                                                         .getCurrentTreePanel()
766                                                                                         .getWidth(),
767                                                                                         getMainPanel()
768                                                                                         .getCurrentTreePanel()
769                                                                                         .getHeight() );
770                     }
771                 }
772             } );
773             if ( getConfiguration().isUseTabbedDisplay() ) {
774                 try {
775                     AptxUtil.printAppletMessage( NAME, "using tabbed display" );
776                     AptxUtil.addPhylogeniesToTabs( phys,
777                                                    new File( phys_url.getFile() ).getName(),
778                                                    phys_url.toString(),
779                                                    getConfiguration(),
780                                                    getMainPanel() );
781                 }
782                 catch ( final Exception e ) {
783                     ForesterUtil.printErrorMessage( NAME, e.toString() );
784                     e.printStackTrace();
785                 }
786             }
787             else {
788                 AptxUtil.printAppletMessage( NAME, "not using tabbed display" );
789                 if ( getSpeciesTree() != null ) {
790                     AptxUtil.printAppletMessage( NAME,
791                             "Warning: gsdi (gene duplication inference) only available tabbed display" );
792                 }
793                 AptxUtil.addPhylogenyToPanel( phys, getConfiguration(), getMainPanel() );
794             }
795             validate();
796             setName( NAME );
797             getMainPanel().getControlPanel().showWholeAll();
798             getMainPanel().getControlPanel().showWhole();
799             /* GUILHEM_BEG */
800             getCurrentTreePanel().getControlPanel().getSequenceRelationTypeBox().removeAllItems();
801             for( final SequenceRelation.SEQUENCE_RELATION_TYPE type : getMainPanel().getCurrentPhylogeny()
802                     .getRelevantSequenceRelationTypes() ) {
803                 getCurrentTreePanel().getControlPanel().getSequenceRelationTypeBox().addItem( type );
804             }
805             final String default_relation = getParameter( Constants.APPLET_PARAM_NAME_FOR_DEFAULT_SEQUENCE_RELATION_TYPE );
806             if ( default_relation != null ) {
807                 getCurrentTreePanel().getControlPanel().getSequenceRelationTypeBox().setSelectedItem( default_relation );
808             }
809             final String default_sequence = getParameter( Constants.APPLET_PARAM_NAME_FOR_DEFAULT_QUERY_SEQUENCE );
810             if ( default_sequence != null ) {
811                 getCurrentTreePanel().getControlPanel().getSequenceRelationBox().setSelectedItem( default_sequence );
812             }
813             /* GUILHEM_END */
814             System.gc();
815             AptxUtil.printAppletMessage( NAME, "successfully initialized" );
816             setVisible( true );
817         }
818         catch ( final Exception e ) {
819             ForesterUtil.printErrorMessage( NAME, e.toString() );
820             e.printStackTrace();
821         }
822     }
823
824     public void showTextFrame( final String s, final String title ) {
825         checkTextFrames();
826         _textframes.addLast( TextFrame.instantiate( s, title, _textframes ) );
827     }
828
829     @Override
830     public void start() {
831         if ( getMainPanel() != null ) {
832             getMainPanel().validate();
833         }
834         requestFocus();
835         requestFocusInWindow();
836         requestFocus();
837         AptxUtil.printAppletMessage( NAME, "started" );
838     }
839
840     private void chooseFont() {
841         final FontChooser fc = new FontChooser();
842         fc.setFont( getMainPanel().getTreeFontSet().getLargeFont() );
843         fc.showDialog( this, "Select the Base Font" );
844         getMainPanel().getTreeFontSet().setBaseFont( fc.getFont() );
845     }
846
847     private void chooseMinimalConfidence() {
848         final String s = ( String ) JOptionPane
849                 .showInputDialog( this,
850                                   "Please the minimum for confidence values to be displayed.\n" + "[current value: "
851                                           + getOptions().getMinConfidenceValue() + "]\n",
852                                           "Minimal Confidence Value",
853                                           JOptionPane.QUESTION_MESSAGE,
854                                           null,
855                                           null,
856                                           getOptions().getMinConfidenceValue() );
857         if ( !ForesterUtil.isEmpty( s ) ) {
858             boolean success = true;
859             double m = 0.0;
860             final String m_str = s.trim();
861             if ( !ForesterUtil.isEmpty( m_str ) ) {
862                 try {
863                     m = Double.parseDouble( m_str );
864                 }
865                 catch ( final Exception ex ) {
866                     success = false;
867                 }
868             }
869             else {
870                 success = false;
871             }
872             if ( success && ( m >= 0.0 ) ) {
873                 getOptions().setMinConfidenceValue( m );
874             }
875         }
876     }
877
878     private void choosePdfWidth() {
879         final String s = ( String ) JOptionPane.showInputDialog( this,
880                                                                  "Please enter the default line width for PDF export.\n"
881                                                                          + "[current value: "
882                                                                          + getOptions().getPrintLineWidth() + "]\n",
883                                                                  "Line Width for PDF Export",
884                                                                  JOptionPane.QUESTION_MESSAGE,
885                                                                  null,
886                                                                  null,
887                                                                  getOptions().getPrintLineWidth() );
888         if ( !ForesterUtil.isEmpty( s ) ) {
889             boolean success = true;
890             float f = 0.0f;
891             final String m_str = s.trim();
892             if ( !ForesterUtil.isEmpty( m_str ) ) {
893                 try {
894                     f = Float.parseFloat( m_str );
895                 }
896                 catch ( final Exception ex ) {
897                     success = false;
898                 }
899             }
900             else {
901                 success = false;
902             }
903             if ( success && ( f > 0.0 ) ) {
904                 getOptions().setPrintLineWidth( f );
905             }
906         }
907     }
908
909     private void choosePrintSize() {
910         final String s = ( String ) JOptionPane.showInputDialog( this,
911                                                                  "Please enter values for width and height,\nseparated by a comma.\n"
912                                                                          + "[current values: "
913                                                                          + getOptions().getPrintSizeX() + ", "
914                                                                          + getOptions().getPrintSizeY() + "]\n"
915                                                                          + "[A4: " + Constants.A4_SIZE_X + ", "
916                                                                          + Constants.A4_SIZE_Y + "]\n" + "[US Letter: "
917                                                                          + Constants.US_LETTER_SIZE_X + ", "
918                                                                          + Constants.US_LETTER_SIZE_Y + "]",
919                                                                  "Default Size for Graphics Export",
920                                                                  JOptionPane.QUESTION_MESSAGE,
921                                                                  null,
922                                                                  null,
923                                                                  getOptions().getPrintSizeX() + ", "
924                                                                          + getOptions().getPrintSizeY() );
925         if ( !ForesterUtil.isEmpty( s ) && ( s.indexOf( ',' ) > 0 ) ) {
926             boolean success = true;
927             int x = 0;
928             int y = 0;
929             final String[] str_ary = s.split( "," );
930             if ( str_ary.length == 2 ) {
931                 final String x_str = str_ary[ 0 ].trim();
932                 final String y_str = str_ary[ 1 ].trim();
933                 if ( !ForesterUtil.isEmpty( x_str ) && !ForesterUtil.isEmpty( y_str ) ) {
934                     try {
935                         x = Integer.parseInt( x_str );
936                         y = Integer.parseInt( y_str );
937                     }
938                     catch ( final Exception ex ) {
939                         success = false;
940                     }
941                 }
942                 else {
943                     success = false;
944                 }
945             }
946             else {
947                 success = false;
948             }
949             if ( success && ( x > 1 ) && ( y > 1 ) ) {
950                 getOptions().setPrintSizeX( x );
951                 getOptions().setPrintSizeY( y );
952             }
953         }
954     }
955
956     private void customizeRadioButtonMenuItem( final JRadioButtonMenuItem item, final boolean is_selected ) {
957         if ( item != null ) {
958             item.setFont( MainFrame.menu_font );
959             if ( !getConfiguration().isUseNativeUI() ) {
960                 item.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
961                 item.setForeground( getConfiguration().getGuiMenuTextColor() );
962             }
963             item.setSelected( is_selected );
964             item.addActionListener( this );
965         }
966     }
967
968     private Phylogeny getSpeciesTree() {
969         return _species_tree;
970     }
971
972     private boolean isScreenAntialias() {
973         return true;
974     }
975
976     private void removeBranchColors() {
977         if ( getMainPanel().getCurrentPhylogeny() != null ) {
978             AptxUtil.removeBranchColors( getMainPanel().getCurrentPhylogeny() );
979         }
980     }
981
982     private void removeVisualStyles() {
983         if ( getMainPanel().getCurrentPhylogeny() != null ) {
984             AptxUtil.removeVisualStyles( getMainPanel().getCurrentPhylogeny() );
985         }
986     }
987
988     private void setMainPanel( final MainPanelApplets main_panel ) {
989         _mainpanel = main_panel;
990     }
991
992     private void setSpeciesTree( final Phylogeny species_tree ) {
993         _species_tree = species_tree;
994     }
995
996     private void setupUI() {
997         try {
998             if ( getConfiguration().isUseNativeUI() ) {
999                 UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
1000             }
1001             else {
1002                 UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
1003             }
1004         }
1005         catch ( final UnsupportedLookAndFeelException e ) {
1006             AptxUtil.dieWithSystemError( "UnsupportedLookAndFeelException: " + e.toString() );
1007         }
1008         catch ( final ClassNotFoundException e ) {
1009             AptxUtil.dieWithSystemError( "ClassNotFoundException: " + e.toString() );
1010         }
1011         catch ( final InstantiationException e ) {
1012             AptxUtil.dieWithSystemError( "InstantiationException: " + e.toString() );
1013         }
1014         catch ( final IllegalAccessException e ) {
1015             AptxUtil.dieWithSystemError( "IllegalAccessException: " + e.toString() );
1016         }
1017         catch ( final Exception e ) {
1018             AptxUtil.dieWithSystemError( e.toString() );
1019         }
1020     }
1021
1022     void buildAnalysisMenu() {
1023         _analysis_menu = MainFrame.createMenu( "Analysis", getConfiguration() );
1024         _analysis_menu.add( _gsdi_item = new JMenuItem( "GSDI (Generalized Speciation Duplication Inference)" ) );
1025         _analysis_menu.add( _gsdir_item = new JMenuItem( "GSDIR (GSDI with re-rooting)" ) );
1026         customizeJMenuItem( _gsdi_item );
1027         customizeJMenuItem( _gsdir_item );
1028         //  _analysis_menu.addSeparator();
1029         //  _analysis_menu.add( _lineage_inference = new JMenuItem( INFER_ANCESTOR_TAXONOMIES ) );
1030         //  customizeJMenuItem( _lineage_inference );
1031         //  _lineage_inference.setToolTipText( "Inference of ancestor taxonomies/lineages" );
1032         _jmenubar.add( _analysis_menu );
1033     }
1034
1035     void buildFileMenu() {
1036         _file_jmenu = MainFrame.createMenu( "File", getConfiguration() );
1037         _file_jmenu.add( _save_item = new JMenuItem( "Save Tree As..." ) );
1038         _file_jmenu.addSeparator();
1039         _file_jmenu.add( _write_to_pdf_item = new JMenuItem( "Export to PDF file ..." ) );
1040         if ( AptxUtil.canWriteFormat( "tif" ) || AptxUtil.canWriteFormat( "tiff" ) || AptxUtil.canWriteFormat( "TIF" ) ) {
1041             _file_jmenu.add( _write_to_tif_item = new JMenuItem( "Export to TIFF file..." ) );
1042         }
1043         _file_jmenu.add( _write_to_png_item = new JMenuItem( "Export to PNG file..." ) );
1044         _file_jmenu.add( _write_to_jpg_item = new JMenuItem( "Export to JPG file..." ) );
1045         if ( AptxUtil.canWriteFormat( "gif" ) ) {
1046             _file_jmenu.add( _write_to_gif_item = new JMenuItem( "Export to GIF file..." ) );
1047         }
1048         if ( AptxUtil.canWriteFormat( "bmp" ) ) {
1049             _file_jmenu.add( _write_to_bmp_item = new JMenuItem( "Export to BMP file..." ) );
1050         }
1051         _file_jmenu.addSeparator();
1052         _file_jmenu.add( _print_item = new JMenuItem( "Print..." ) );
1053         customizeJMenuItem( _save_item );
1054         customizeJMenuItem( _write_to_pdf_item );
1055         customizeJMenuItem( _write_to_png_item );
1056         customizeJMenuItem( _write_to_jpg_item );
1057         customizeJMenuItem( _write_to_gif_item );
1058         customizeJMenuItem( _write_to_tif_item );
1059         customizeJMenuItem( _write_to_bmp_item );
1060         customizeJMenuItem( _print_item );
1061         _jmenubar.add( _file_jmenu );
1062     }
1063
1064     void buildFontSizeMenu() {
1065         _font_size_menu = MainFrame.createMenu( MainFrame.FONT_SIZE_MENU_LABEL, getConfiguration() );
1066         _font_size_menu.add( _super_tiny_fonts_mi = new JMenuItem( "Super tiny fonts" ) );
1067         _font_size_menu.add( _tiny_fonts_mi = new JMenuItem( "Tiny fonts" ) );
1068         _font_size_menu.add( _small_fonts_mi = new JMenuItem( "Small fonts" ) );
1069         _font_size_menu.add( _medium_fonts_mi = new JMenuItem( "Medium fonts" ) );
1070         _font_size_menu.add( _large_fonts_mi = new JMenuItem( "Large fonts" ) );
1071         customizeJMenuItem( _super_tiny_fonts_mi );
1072         customizeJMenuItem( _tiny_fonts_mi );
1073         customizeJMenuItem( _small_fonts_mi );
1074         customizeJMenuItem( _medium_fonts_mi );
1075         customizeJMenuItem( _large_fonts_mi );
1076         _jmenubar.add( _font_size_menu );
1077     }
1078
1079     void buildHelpMenu() {
1080         _help_jmenu = MainFrame.createMenu( "Help", getConfiguration() );
1081         _help_jmenu.add( _help_item = new JMenuItem( "Documentation" ) );
1082         _help_jmenu.addSeparator();
1083         _help_jmenu.add( _website_item = new JMenuItem( "Archaeopteryx Home" ) );
1084         _aptx_ref_item = new JMenuItem( "Archaeopteryx Reference" );
1085         _help_jmenu.add( _phyloxml_website_item = new JMenuItem( "phyloXML Home" ) );
1086         _help_jmenu.add( _phyloxml_ref_item = new JMenuItem( "phyloXML Reference" ) );
1087         _help_jmenu.addSeparator();
1088         _help_jmenu.add( _about_item = new JMenuItem( "About" ) );
1089         customizeJMenuItem( _help_item );
1090         customizeJMenuItem( _website_item );
1091         customizeJMenuItem( _phyloxml_website_item );
1092         customizeJMenuItem( _aptx_ref_item );
1093         customizeJMenuItem( _phyloxml_ref_item );
1094         customizeJMenuItem( _about_item );
1095         _phyloxml_ref_item.setToolTipText( MainFrame.PHYLOXML_REF_TOOL_TIP );
1096         _aptx_ref_item.setToolTipText( MainFrame.APTX_REF_TOOL_TIP );
1097         _jmenubar.add( _help_jmenu );
1098     }
1099
1100     void buildOptionsMenu() {
1101         _options_jmenu = MainFrame.createMenu( MainFrame.OPTIONS_HEADER, getConfiguration() );
1102         _options_jmenu.addChangeListener( new ChangeListener() {
1103
1104             @Override
1105             public void stateChanged( final ChangeEvent e ) {
1106                 MainFrame.setOvPlacementColorChooseMenuItem( _overview_placment_mi, getOptions() );
1107                 MainFrame.setTextColorChooseMenuItem( _switch_colors_mi, getCurrentTreePanel() );
1108                 MainFrame
1109                 .setTextMinSupportMenuItem( _choose_minimal_confidence_mi, getOptions(), getCurrentTreePanel() );
1110                 MainFrame.setTextForFontChooserMenuItem( _choose_font_mi, MainFrame
1111                                                          .createCurrentFontDesc( getMainPanel().getTreeFontSet() ) );
1112                 setTextForGraphicsSizeChooserMenuItem( _print_size_mi, getOptions() );
1113                 setTextForPdfLineWidthChooserMenuItem( _choose_pdf_width_mi, getOptions() );
1114                 MainFrame.setCycleNodeFillMenuItem( _cycle_node_fill_mi, getOptions() );
1115                 MainFrame.setCycleNodeShapeMenuItem( _cycle_node_shape_mi, getOptions() );
1116                 MainFrame.setTextNodeSizeMenuItem( _choose_node_size_mi, getOptions() );
1117                 try {
1118                     getMainPanel().getControlPanel().setVisibilityOfDomainStrucureCB();
1119                     getMainPanel().getControlPanel().setVisibilityOfX();
1120                 }
1121                 catch ( final Exception ignore ) {
1122                     // do nothing, not important.
1123                 }
1124             }
1125         } );
1126         _options_jmenu.add( MainFrame.customizeMenuItemAsLabel( new JMenuItem( MainFrame.DISPLAY_SUBHEADER ),
1127                                                                 getConfiguration() ) );
1128         _options_jmenu
1129         .add( _ext_node_dependent_cladogram_rbmi = new JRadioButtonMenuItem( MainFrame.NONUNIFORM_CLADOGRAMS_LABEL ) );
1130         _options_jmenu.add( _uniform_cladograms_rbmi = new JRadioButtonMenuItem( MainFrame.UNIFORM_CLADOGRAMS_LABEL ) );
1131         _options_jmenu
1132         .add( _non_lined_up_cladograms_rbmi = new JRadioButtonMenuItem( MainFrame.NON_LINED_UP_CLADOGRAMS_LABEL ) );
1133         _radio_group_1 = new ButtonGroup();
1134         _radio_group_1.add( _ext_node_dependent_cladogram_rbmi );
1135         _radio_group_1.add( _uniform_cladograms_rbmi );
1136         _radio_group_1.add( _non_lined_up_cladograms_rbmi );
1137         _options_jmenu.add( _show_overview_cbmi = new JCheckBoxMenuItem( MainFrame.SHOW_OVERVIEW_LABEL ) );
1138         _options_jmenu.add( _show_scale_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_SCALE_LABEL ) );
1139         _options_jmenu
1140         .add( _show_default_node_shapes_internal_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_NODE_BOXES_LABEL_INT ) );
1141         _options_jmenu
1142         .add( _show_default_node_shapes_external_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_NODE_BOXES_LABEL_EXT ) );
1143         _options_jmenu
1144         .add( _show_default_node_shapes_for_marked_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_NODE_BOXES_LABEL_MARKED ) );
1145         _options_jmenu.add( _line_up_renderable_data_cbmi = new JCheckBoxMenuItem( MainFrame.LINE_UP_RENDERABLE_DATA ) );
1146         if ( getConfiguration().doDisplayOption( Configuration.show_domain_architectures ) ) {
1147             _options_jmenu.add( _right_line_up_domains_cbmi = new JCheckBoxMenuItem( MainFrame.RIGHT_LINE_UP_DOMAINS ) );
1148             _options_jmenu.add( _show_domain_labels = new JCheckBoxMenuItem( MainFrame.SHOW_DOMAIN_LABELS_LABEL ) );
1149         }
1150         _options_jmenu.add( _show_annotation_ref_source = new JCheckBoxMenuItem( MainFrame.SHOW_ANN_REF_SOURCE_LABEL ) );
1151         _options_jmenu.add( _show_confidence_stddev_cbmi = new JCheckBoxMenuItem( MainFrame.SHOW_CONF_STDDEV_LABEL ) );
1152         _options_jmenu
1153         .add( _color_by_taxonomic_group_cbmi = new JCheckBoxMenuItem( MainFrame.COLOR_BY_TAXONOMIC_GROUP ) );
1154         _options_jmenu
1155         .add( _color_labels_same_as_parent_branch = new JCheckBoxMenuItem( MainFrame.COLOR_LABELS_LABEL ) );
1156         _color_labels_same_as_parent_branch.setToolTipText( MainFrame.COLOR_LABELS_TIP );
1157         _options_jmenu.add( _abbreviate_scientific_names = new JCheckBoxMenuItem( MainFrame.ABBREV_SN_LABEL ) );
1158         _options_jmenu.add( _label_direction_cbmi = new JCheckBoxMenuItem( MainFrame.LABEL_DIRECTION_LABEL ) );
1159         _label_direction_cbmi.setToolTipText( MainFrame.LABEL_DIRECTION_TIP );
1160         _options_jmenu.add( _screen_antialias_cbmi = new JCheckBoxMenuItem( MainFrame.SCREEN_ANTIALIAS_LABEL ) );
1161         _options_jmenu.add( _background_gradient_cbmi = new JCheckBoxMenuItem( MainFrame.BG_GRAD_LABEL ) );
1162         _options_jmenu.add( _cycle_node_shape_mi = new JMenuItem( MainFrame.CYCLE_NODE_SHAPE_LABEL ) );
1163         _options_jmenu.add( _cycle_node_fill_mi = new JMenuItem( MainFrame.CYCLE_NODE_FILL_LABEL ) );
1164         _options_jmenu.add( _choose_node_size_mi = new JMenuItem( MainFrame.CHOOSE_NODE_SIZE_LABEL ) );
1165         _options_jmenu.add( _choose_minimal_confidence_mi = new JMenuItem( "" ) );
1166         _options_jmenu.add( _overview_placment_mi = new JMenuItem( "" ) );
1167         _options_jmenu.add( _switch_colors_mi = new JMenuItem( "" ) );
1168         _options_jmenu.add( _choose_font_mi = new JMenuItem( "" ) );
1169         _options_jmenu.addSeparator();
1170         _options_jmenu.add( MainFrame.customizeMenuItemAsLabel( new JMenuItem( MainFrame.SEARCH_SUBHEADER ),
1171                                                                 getConfiguration() ) );
1172         _options_jmenu
1173         .add( _search_case_senstive_cbmi = new JCheckBoxMenuItem( MainFrame.SEARCH_CASE_SENSITIVE_LABEL ) );
1174         _options_jmenu.add( _search_whole_words_only_cbmi = new JCheckBoxMenuItem( MainFrame.SEARCH_TERMS_ONLY_LABEL ) );
1175         _options_jmenu.add( _search_with_regex_cbmi = new JCheckBoxMenuItem( MainFrame.SEARCH_REGEX_LABEL ) );
1176         _search_with_regex_cbmi.setToolTipText( MainFrame.SEARCH_WITH_REGEX_TIP );
1177         _options_jmenu
1178         .add( _inverse_search_result_cbmi = new JCheckBoxMenuItem( MainFrame.INVERSE_SEARCH_RESULT_LABEL ) );
1179         //
1180         _options_jmenu.addSeparator();
1181         _options_jmenu.add( MainFrame.customizeMenuItemAsLabel( new JMenuItem( "Graphics Export & Printing:" ),
1182                                                                 getConfiguration() ) );
1183         _options_jmenu.add( _antialias_print_cbmi = new JCheckBoxMenuItem( "Antialias" ) );
1184         _options_jmenu.add( _print_black_and_white_cbmi = new JCheckBoxMenuItem( "Export in Black and White" ) );
1185         _options_jmenu
1186         .add( _print_using_actual_size_cbmi = new JCheckBoxMenuItem( "Use Current Image Size for PDF export and Printing" ) );
1187         _options_jmenu
1188         .add( _graphics_export_using_actual_size_cbmi = new JCheckBoxMenuItem( "Use Current Image Size for PNG, JPG, and GIF export" ) );
1189         _options_jmenu
1190         .add( _graphics_export_visible_only_cbmi = new JCheckBoxMenuItem( "Limit to Visible ('Screenshot') for PNG, JPG, and GIF export" ) );
1191         _options_jmenu.add( _print_size_mi = new JMenuItem( "" ) );
1192         _options_jmenu.add( _choose_pdf_width_mi = new JMenuItem( "" ) );
1193         //
1194         customizeCheckBoxMenuItem( _antialias_print_cbmi, getOptions().isAntialiasPrint() );
1195         customizeCheckBoxMenuItem( _print_black_and_white_cbmi, getOptions().isPrintBlackAndWhite() );
1196         customizeCheckBoxMenuItem( _graphics_export_visible_only_cbmi, getOptions().isGraphicsExportVisibleOnly() );
1197         customizeCheckBoxMenuItem( _print_using_actual_size_cbmi, getOptions().isPrintUsingActualSize() );
1198         customizeCheckBoxMenuItem( _graphics_export_using_actual_size_cbmi, getOptions()
1199                                    .isGraphicsExportUsingActualSize() );
1200         customizeJMenuItem( _print_size_mi );
1201         customizeJMenuItem( _choose_pdf_width_mi );
1202         //
1203         customizeJMenuItem( _choose_font_mi );
1204         customizeJMenuItem( _choose_minimal_confidence_mi );
1205         customizeJMenuItem( _switch_colors_mi );
1206         customizeJMenuItem( _overview_placment_mi );
1207         customizeCheckBoxMenuItem( _color_by_taxonomic_group_cbmi, getOptions().isColorByTaxonomicGroup() );
1208         customizeCheckBoxMenuItem( _label_direction_cbmi,
1209                                    getOptions().getNodeLabelDirection() == NODE_LABEL_DIRECTION.RADIAL );
1210         customizeCheckBoxMenuItem( _screen_antialias_cbmi, getOptions().isAntialiasScreen() );
1211         customizeCheckBoxMenuItem( _background_gradient_cbmi, getOptions().isBackgroundColorGradient() );
1212         customizeCheckBoxMenuItem( _show_domain_labels, getOptions().isShowDomainLabels() );
1213         customizeCheckBoxMenuItem( _show_annotation_ref_source, getOptions().isShowAnnotationRefSource() );
1214         customizeCheckBoxMenuItem( _abbreviate_scientific_names, getOptions().isAbbreviateScientificTaxonNames() );
1215         customizeCheckBoxMenuItem( _show_default_node_shapes_external_cbmi, getOptions()
1216                                    .isShowDefaultNodeShapesExternal() );
1217         customizeCheckBoxMenuItem( _show_default_node_shapes_internal_cbmi, getOptions()
1218                                    .isShowDefaultNodeShapesInternal() );
1219         customizeCheckBoxMenuItem( _show_default_node_shapes_for_marked_cbmi, getOptions()
1220                                    .isShowDefaultNodeShapesForMarkedNodes() );
1221         customizeJMenuItem( _cycle_node_shape_mi );
1222         customizeJMenuItem( _cycle_node_fill_mi );
1223         customizeJMenuItem( _choose_node_size_mi );
1224         customizeCheckBoxMenuItem( _color_labels_same_as_parent_branch, getOptions().isColorLabelsSameAsParentBranch() );
1225         customizeCheckBoxMenuItem( _search_case_senstive_cbmi, getOptions().isSearchCaseSensitive() );
1226         customizeCheckBoxMenuItem( _show_scale_cbmi, getOptions().isShowScale() );
1227         customizeRadioButtonMenuItem( _non_lined_up_cladograms_rbmi,
1228                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.NON_LINED_UP );
1229         customizeRadioButtonMenuItem( _uniform_cladograms_rbmi,
1230                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.TOTAL_NODE_SUM_DEP );
1231         customizeRadioButtonMenuItem( _ext_node_dependent_cladogram_rbmi,
1232                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.EXT_NODE_SUM_DEP );
1233         customizeCheckBoxMenuItem( _show_overview_cbmi, getOptions().isShowOverview() );
1234         customizeCheckBoxMenuItem( _search_with_regex_cbmi, getOptions().isSearchWithRegex() );
1235         customizeCheckBoxMenuItem( _search_whole_words_only_cbmi, getOptions().isMatchWholeTermsOnly() );
1236         customizeCheckBoxMenuItem( _inverse_search_result_cbmi, getOptions().isInverseSearchResult() );
1237         customizeCheckBoxMenuItem( _show_confidence_stddev_cbmi, getOptions().isShowConfidenceStddev() );
1238         customizeCheckBoxMenuItem( _line_up_renderable_data_cbmi, getOptions().isLineUpRendarableNodeData() );
1239         customizeCheckBoxMenuItem( _right_line_up_domains_cbmi, getOptions().isRightLineUpDomains() );
1240         _jmenubar.add( _options_jmenu );
1241     }
1242
1243     void buildToolsMenu() {
1244         _tools_menu = MainFrame.createMenu( "Tools", getConfiguration() );
1245         _tools_menu.add( _confcolor_item = new JMenuItem( "Colorize Branches Depending on Confidence" ) );
1246         customizeJMenuItem( _confcolor_item );
1247         _tools_menu.add( _taxcolor_item = new JMenuItem( "Taxonomy Colorize Branches" ) );
1248         customizeJMenuItem( _taxcolor_item );
1249         _tools_menu.addSeparator();
1250         _tools_menu.add( _remove_visual_styles_item = new JMenuItem( "Delete All Visual Styles From Nodes" ) );
1251         _remove_visual_styles_item
1252         .setToolTipText( "To remove all node visual styles (fonts, colors) from the current phylogeny." );
1253         customizeJMenuItem( _remove_visual_styles_item );
1254         _tools_menu.add( _remove_branch_color_item = new JMenuItem( "Delete All Colors From Branches" ) );
1255         _remove_branch_color_item.setToolTipText( "To remove all branch color values from the current phylogeny." );
1256         customizeJMenuItem( _remove_branch_color_item );
1257         _tools_menu.addSeparator();
1258         _tools_menu.add( _midpoint_root_item = new JMenuItem( "Midpoint-Root" ) );
1259         customizeJMenuItem( _midpoint_root_item );
1260         _tools_menu.addSeparator();
1261         _tools_menu.add( _collapse_species_specific_subtrees = new JMenuItem( "Collapse Species-Specific Subtrees" ) );
1262         customizeJMenuItem( _collapse_species_specific_subtrees );
1263         _jmenubar.add( _tools_menu );
1264     }
1265
1266     void buildTypeMenu() {
1267         _type_menu = MainFrame.createMenu( MainFrame.TYPE_MENU_HEADER, getConfiguration() );
1268         _type_menu.add( _rectangular_type_cbmi = new JCheckBoxMenuItem( MainFrame.RECTANGULAR_TYPE_CBMI_LABEL ) );
1269         _type_menu.add( _euro_type_cbmi = new JCheckBoxMenuItem( MainFrame.EURO_TYPE_CBMI_LABEL ) );
1270         _type_menu.add( _rounded_type_cbmi = new JCheckBoxMenuItem( MainFrame.ROUNDED_TYPE_CBMI_LABEL ) );
1271         _type_menu.add( _curved_type_cbmi = new JCheckBoxMenuItem( MainFrame.CURVED_TYPE_CBMI_LABEL ) );
1272         _type_menu.add( _triangular_type_cbmi = new JCheckBoxMenuItem( MainFrame.TRIANGULAR_TYPE_CBMI_LABEL ) );
1273         _type_menu.add( _convex_type_cbmi = new JCheckBoxMenuItem( MainFrame.CONVEX_TYPE_CBMI_LABEL ) );
1274         _type_menu.add( _unrooted_type_cbmi = new JCheckBoxMenuItem( MainFrame.UNROOTED_TYPE_CBMI_LABEL ) );
1275         _type_menu.add( _circular_type_cbmi = new JCheckBoxMenuItem( MainFrame.CIRCULAR_TYPE_CBMI_LABEL ) );
1276         customizeCheckBoxMenuItem( _rectangular_type_cbmi, false );
1277         customizeCheckBoxMenuItem( _triangular_type_cbmi, false );
1278         customizeCheckBoxMenuItem( _euro_type_cbmi, false );
1279         customizeCheckBoxMenuItem( _rounded_type_cbmi, false );
1280         customizeCheckBoxMenuItem( _curved_type_cbmi, false );
1281         customizeCheckBoxMenuItem( _convex_type_cbmi, false );
1282         customizeCheckBoxMenuItem( _unrooted_type_cbmi, false );
1283         customizeCheckBoxMenuItem( _circular_type_cbmi, false );
1284         _unrooted_type_cbmi.setToolTipText( MainFrame.USE_MOUSEWHEEL_SHIFT_TO_ROTATE );
1285         _circular_type_cbmi.setToolTipText( MainFrame.USE_MOUSEWHEEL_SHIFT_TO_ROTATE );
1286         initializeTypeMenu( getOptions() );
1287         _jmenubar.add( _type_menu );
1288     }
1289
1290     void buildViewMenu() {
1291         _view_jmenu = MainFrame.createMenu( "View", getConfiguration() );
1292         _view_jmenu
1293         .add( _display_basic_information_item = new JMenuItem( MainFrame.SHOW_BASIC_TREE_INFORMATION_LABEL ) );
1294         _view_jmenu.addSeparator();
1295         _view_jmenu.add( _view_as_XML_item = new JMenuItem( "as phyloXML" ) );
1296         _view_jmenu.add( _view_as_NH_item = new JMenuItem( "as Newick" ) );
1297         _view_jmenu.add( _view_as_nexus_item = new JMenuItem( "as Nexus" ) );
1298         customizeJMenuItem( _display_basic_information_item );
1299         customizeJMenuItem( _view_as_NH_item );
1300         customizeJMenuItem( _view_as_XML_item );
1301         customizeJMenuItem( _view_as_nexus_item );
1302         _jmenubar.add( _view_jmenu );
1303     }
1304
1305     void checkTextFrames() {
1306         if ( _textframes.size() > 5 ) {
1307             try {
1308                 if ( _textframes.getFirst() != null ) {
1309                     _textframes.getFirst().removeMe();
1310                 }
1311                 else {
1312                     _textframes.removeFirst();
1313                 }
1314             }
1315             catch ( final NoSuchElementException e ) {
1316                 // Ignore.
1317             }
1318         }
1319     }
1320
1321     void clearCurrentExternalNodesDataBuffer() {
1322         getCurrentTreePanel().clearCurrentExternalNodesDataBuffer();
1323     }
1324
1325     void customizeCheckBoxMenuItem( final JCheckBoxMenuItem item, final boolean is_selected ) {
1326         if ( item != null ) {
1327             item.setFont( MainFrame.menu_font );
1328             if ( !getConfiguration().isUseNativeUI() ) {
1329                 item.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
1330                 item.setForeground( getConfiguration().getGuiMenuTextColor() );
1331             }
1332             item.setSelected( is_selected );
1333             item.addActionListener( this );
1334         }
1335     }
1336
1337     void customizeJMenuItem( final JMenuItem jmi ) {
1338         if ( jmi != null ) {
1339             jmi.setFont( MainFrame.menu_font );
1340             if ( !getConfiguration().isUseNativeUI() ) {
1341                 jmi.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
1342                 jmi.setForeground( getConfiguration().getGuiMenuTextColor() );
1343             }
1344             jmi.addActionListener( this );
1345         }
1346     }
1347
1348     void displayBasicInformation() {
1349         if ( ( getMainPanel() != null ) && ( getMainPanel().getCurrentPhylogeny() != null )
1350                 && !getMainPanel().getCurrentPhylogeny().isEmpty() ) {
1351             String title = "Basic Information";
1352             if ( !ForesterUtil.isEmpty( getMainPanel().getCurrentPhylogeny().getName() ) ) {
1353                 title = title + " for \"" + _mainpanel.getCurrentPhylogeny().getName() + "\"";
1354             }
1355             showTextFrame( AptxUtil.createBasicInformation( getMainPanel().getCurrentPhylogeny(), null ), title );
1356         }
1357     }
1358
1359     void executeGSDI() {
1360         if ( !isOKforSDI( false, true ) ) {
1361             return;
1362         }
1363         if ( !_mainpanel.getCurrentPhylogeny().isRooted() ) {
1364             JOptionPane.showMessageDialog( this,
1365                                            "Gene tree is not rooted.",
1366                                            "Cannot execute GSDI",
1367                                            JOptionPane.ERROR_MESSAGE );
1368             return;
1369         }
1370         final Phylogeny gene_tree = _mainpanel.getCurrentPhylogeny().copy();
1371         gene_tree.setAllNodesToNotCollapse();
1372         gene_tree.recalculateNumberOfExternalDescendants( false );
1373         GSDI gsdi = null;
1374         final Phylogeny species_tree = _species_tree.copy();
1375         try {
1376             gsdi = new GSDI( gene_tree, species_tree, false, true, true, true );
1377         }
1378         catch ( final SDIException e ) {
1379             JOptionPane.showMessageDialog( this,
1380                                            e.getLocalizedMessage(),
1381                                            "Error during GSDI",
1382                                            JOptionPane.ERROR_MESSAGE );
1383             return;
1384         }
1385         catch ( final Exception e ) {
1386             AptxUtil.unexpectedException( e );
1387             return;
1388         }
1389         gene_tree.setRerootable( false );
1390         gene_tree.clearHashIdToNodeMap();
1391         gene_tree.recalculateNumberOfExternalDescendants( true );
1392         _mainpanel.addPhylogenyInNewTab( gene_tree, getConfiguration(), "gene tree", null );
1393         getMainPanel().getControlPanel().setShowEvents( true );
1394         showWhole();
1395         final int selected = _mainpanel.getTabbedPane().getSelectedIndex();
1396         _mainpanel.addPhylogenyInNewTab( species_tree, getConfiguration(), "species tree", null );
1397         showWhole();
1398         _mainpanel.getTabbedPane().setSelectedIndex( selected );
1399         showWhole();
1400         _mainpanel.getCurrentTreePanel().setEdited( true );
1401         final int poly = PhylogenyMethods.countNumberOfPolytomies( species_tree );
1402         if ( gsdi.getStrippedExternalGeneTreeNodes().size() > 0 ) {
1403             JOptionPane.showMessageDialog( this,
1404                                            "Duplications: " + gsdi.getDuplicationsSum() + "\n"
1405                                                    + "Potential duplications: "
1406                                                    + gsdi.getSpeciationOrDuplicationEventsSum() + "\n"
1407                                                    + "Speciations: " + gsdi.getSpeciationsSum() + "\n"
1408                                                    + "Stripped gene tree nodes: "
1409                                                    + gsdi.getStrippedExternalGeneTreeNodes().size() + "\n"
1410                                                    + "Taxonomy linkage based on: " + gsdi.getTaxCompBase() + "\n"
1411                                                    + "Number of polytomies in species tree used: " + poly + "\n",
1412                                                    "GSDI successfully completed",
1413                                                    JOptionPane.WARNING_MESSAGE );
1414         }
1415         else {
1416             JOptionPane.showMessageDialog( this,
1417                                            "Duplications: " + gsdi.getDuplicationsSum() + "\n"
1418                                                    + "Potential duplications: "
1419                                                    + gsdi.getSpeciationOrDuplicationEventsSum() + "\n"
1420                                                    + "Speciations: " + gsdi.getSpeciationsSum() + "\n"
1421                                                    + "Stripped gene tree nodes: "
1422                                                    + gsdi.getStrippedExternalGeneTreeNodes().size() + "\n"
1423                                                    + "Taxonomy linkage based on: " + gsdi.getTaxCompBase() + "\n"
1424                                                    + "Number of polytomies in species tree used: " + poly + "\n",
1425                                                    "GSDI successfully completed",
1426                                                    JOptionPane.INFORMATION_MESSAGE );
1427         }
1428     }
1429
1430     void executeGSDIR() {
1431         if ( !isOKforSDI( false, false ) ) {
1432             return;
1433         }
1434         final int p = PhylogenyMethods.countNumberOfPolytomies( _mainpanel.getCurrentPhylogeny() );
1435         if ( ( p > 0 )
1436                 && !( ( p == 1 ) && ( _mainpanel.getCurrentPhylogeny().getRoot().getNumberOfDescendants() == 3 ) ) ) {
1437             JOptionPane.showMessageDialog( this,
1438                                            "Gene tree is not completely binary",
1439                                            "Cannot execute GSDI",
1440                                            JOptionPane.ERROR_MESSAGE );
1441             return;
1442         }
1443         final Phylogeny gene_tree = _mainpanel.getCurrentPhylogeny().copy();
1444         gene_tree.setAllNodesToNotCollapse();
1445         gene_tree.recalculateNumberOfExternalDescendants( false );
1446         GSDIR gsdir = null;
1447         final Phylogeny species_tree = _species_tree.copy();
1448         try {
1449             gsdir = new GSDIR( gene_tree, species_tree, true, true, true );
1450         }
1451         catch ( final SDIException e ) {
1452             JOptionPane.showMessageDialog( this,
1453                                            e.getLocalizedMessage(),
1454                                            "Error during GSDIR",
1455                                            JOptionPane.ERROR_MESSAGE );
1456             return;
1457         }
1458         catch ( final Exception e ) {
1459             AptxUtil.unexpectedException( e );
1460             return;
1461         }
1462         final Phylogeny result_gene_tree = gsdir.getMinDuplicationsSumGeneTree();
1463         result_gene_tree.setRerootable( false );
1464         result_gene_tree.clearHashIdToNodeMap();
1465         result_gene_tree.recalculateNumberOfExternalDescendants( true );
1466         PhylogenyMethods.orderAppearance( result_gene_tree.getRoot(), true, true, DESCENDANT_SORT_PRIORITY.NODE_NAME );
1467         _mainpanel.addPhylogenyInNewTab( result_gene_tree, getConfiguration(), "gene tree", null );
1468         getMainPanel().getControlPanel().setShowEvents( true );
1469         showWhole();
1470         final int selected = _mainpanel.getTabbedPane().getSelectedIndex();
1471         _mainpanel.addPhylogenyInNewTab( species_tree, getConfiguration(), "species tree", null );
1472         showWhole();
1473         _mainpanel.getTabbedPane().setSelectedIndex( selected );
1474         showWhole();
1475         _mainpanel.getCurrentTreePanel().setEdited( true );
1476         final int poly = PhylogenyMethods.countNumberOfPolytomies( species_tree );
1477         if ( gsdir.getStrippedExternalGeneTreeNodes().size() > 0 ) {
1478             JOptionPane.showMessageDialog( this,
1479                                            "Minimal duplications: " + gsdir.getMinDuplicationsSum() + "\n"
1480                                                    + "Speciations: " + gsdir.getSpeciationsSum() + "\n"
1481                                                    + "Stripped gene tree nodes: "
1482                                                    + gsdir.getStrippedExternalGeneTreeNodes().size() + "\n"
1483                                                    + "Taxonomy linkage based on: " + gsdir.getTaxCompBase() + "\n"
1484                                                    + "Number of polytomies in species tree used: " + poly + "\n",
1485                                                    "GSDIR successfully completed",
1486                                                    JOptionPane.WARNING_MESSAGE );
1487         }
1488         else {
1489             JOptionPane.showMessageDialog( this,
1490                                            "Minimal duplications: " + gsdir.getMinDuplicationsSum() + "\n"
1491                                                    + "Speciations: " + gsdir.getSpeciationsSum() + "\n"
1492                                                    + "Stripped gene tree nodes: "
1493                                                    + gsdir.getStrippedExternalGeneTreeNodes().size() + "\n"
1494                                                    + "Taxonomy linkage based on: " + gsdir.getTaxCompBase() + "\n"
1495                                                    + "Number of polytomies in species tree used: " + poly + "\n",
1496                                                    "GSDIR successfully completed",
1497                                                    JOptionPane.INFORMATION_MESSAGE );
1498         }
1499     }
1500
1501     Configuration getConfiguration() {
1502         return _configuration;
1503     }
1504
1505     File getCurrentDir() {
1506         if ( ( _current_dir == null ) || !_current_dir.canRead() ) {
1507             if ( ForesterUtil.isWindows() ) {
1508                 try {
1509                     _current_dir = new File( WindowsUtils.getCurrentUserDesktopPath() );
1510                 }
1511                 catch ( final Exception e ) {
1512                     _current_dir = null;
1513                 }
1514             }
1515         }
1516         if ( ( _current_dir == null ) || !_current_dir.canRead() ) {
1517             if ( System.getProperty( "user.home" ) != null ) {
1518                 _current_dir = new File( System.getProperty( "user.home" ) );
1519             }
1520             else if ( System.getProperty( "user.dir" ) != null ) {
1521                 _current_dir = new File( System.getProperty( "user.dir" ) );
1522             }
1523         }
1524         return _current_dir;
1525     }
1526
1527     TreePanel getCurrentTreePanel() {
1528         return getMainPanel().getCurrentTreePanel();
1529     }
1530
1531     JCheckBoxMenuItem getlabelDirectionCbmi() {
1532         return _label_direction_cbmi;
1533     }
1534
1535     MainPanel getMainPanel() {
1536         return _mainpanel;
1537     }
1538
1539     Options getOtions() {
1540         return _options;
1541     }
1542
1543     void initializeTypeMenu( final Options options ) {
1544         setTypeMenuToAllUnselected();
1545         try {
1546             switch ( options.getPhylogenyGraphicsType() ) {
1547                 case CONVEX:
1548                     _convex_type_cbmi.setSelected( true );
1549                     break;
1550                 case CURVED:
1551                     _curved_type_cbmi.setSelected( true );
1552                     break;
1553                 case EURO_STYLE:
1554                     _euro_type_cbmi.setSelected( true );
1555                     break;
1556                 case ROUNDED:
1557                     _rounded_type_cbmi.setSelected( true );
1558                     break;
1559                 case TRIANGULAR:
1560                     _triangular_type_cbmi.setSelected( true );
1561                     break;
1562                 case UNROOTED:
1563                     _unrooted_type_cbmi.setSelected( true );
1564                     break;
1565                 case CIRCULAR:
1566                     _circular_type_cbmi.setSelected( true );
1567                     break;
1568                 default:
1569                     _rectangular_type_cbmi.setSelected( true );
1570                     break;
1571             }
1572         }
1573         catch ( final NullPointerException np ) {
1574             // In all likelihood, this is caused by menu-less display.
1575         }
1576     }
1577
1578     boolean isOKforSDI( final boolean species_tree_has_to_binary, final boolean gene_tree_has_to_binary ) {
1579         if ( ( _mainpanel.getCurrentPhylogeny() == null ) || _mainpanel.getCurrentPhylogeny().isEmpty() ) {
1580             return false;
1581         }
1582         else if ( ( _species_tree == null ) || _species_tree.isEmpty() ) {
1583             JOptionPane.showMessageDialog( this,
1584                                            "No species tree loaded",
1585                                            "Cannot execute GSDI",
1586                                            JOptionPane.ERROR_MESSAGE );
1587             return false;
1588         }
1589         else if ( species_tree_has_to_binary && !_species_tree.isCompletelyBinary() ) {
1590             JOptionPane.showMessageDialog( this,
1591                                            "Species tree is not completely binary",
1592                                            "Cannot execute GSDI",
1593                                            JOptionPane.ERROR_MESSAGE );
1594             return false;
1595         }
1596         else if ( gene_tree_has_to_binary && !_mainpanel.getCurrentPhylogeny().isCompletelyBinary() ) {
1597             JOptionPane.showMessageDialog( this,
1598                                            "Gene tree is not completely binary",
1599                                            "Cannot execute GSDI",
1600                                            JOptionPane.ERROR_MESSAGE );
1601             return false;
1602         }
1603         else {
1604             return true;
1605         }
1606     }
1607
1608     boolean isSubtreeDisplayed() {
1609         if ( getCurrentTreePanel() != null ) {
1610             if ( getCurrentTreePanel().isCurrentTreeIsSubtree() ) {
1611                 JOptionPane
1612                 .showMessageDialog( this,
1613                                     "This operation can only be performed on a complete tree, not on the currently displayed sub-tree only.",
1614                                     "Operation can not be exectuted on a sub-tree",
1615                                     JOptionPane.WARNING_MESSAGE );
1616                 return true;
1617             }
1618         }
1619         return false;
1620     }
1621
1622     void removeAllTextFrames() {
1623         for( final TextFrame tf : _textframes ) {
1624             if ( tf != null ) {
1625                 tf.close();
1626             }
1627         }
1628         _textframes.clear();
1629     }
1630
1631     void setConfiguration( final Configuration configuration ) {
1632         _configuration = configuration;
1633     }
1634
1635     void setCurrentDir( final File current_dir ) {
1636         _current_dir = current_dir;
1637     }
1638
1639     void setOptions( final Options options ) {
1640         _options = options;
1641     }
1642
1643     void setSelectedTypeInTypeMenu( final PHYLOGENY_GRAPHICS_TYPE type ) {
1644         setTypeMenuToAllUnselected();
1645         try {
1646             switch ( type ) {
1647                 case CIRCULAR:
1648                     _circular_type_cbmi.setSelected( true );
1649                     break;
1650                 case CONVEX:
1651                     _convex_type_cbmi.setSelected( true );
1652                     break;
1653                 case CURVED:
1654                     _curved_type_cbmi.setSelected( true );
1655                     break;
1656                 case EURO_STYLE:
1657                     _euro_type_cbmi.setSelected( true );
1658                     break;
1659                 case ROUNDED:
1660                     _rounded_type_cbmi.setSelected( true );
1661                     break;
1662                 case RECTANGULAR:
1663                     _rectangular_type_cbmi.setSelected( true );
1664                     break;
1665                 case TRIANGULAR:
1666                     _triangular_type_cbmi.setSelected( true );
1667                     break;
1668                 case UNROOTED:
1669                     _unrooted_type_cbmi.setSelected( true );
1670                     break;
1671                 default:
1672                     throw new IllegalArgumentException( "unknown type: " + type );
1673             }
1674         }
1675         catch ( final NullPointerException np ) {
1676             // In all likelihood, this is caused by menu-less display.
1677         }
1678     }
1679
1680     void setTypeMenuToAllUnselected() {
1681         if ( _convex_type_cbmi != null ) {
1682             _convex_type_cbmi.setSelected( false );
1683         }
1684         if ( _curved_type_cbmi != null ) {
1685             _curved_type_cbmi.setSelected( false );
1686         }
1687         if ( _euro_type_cbmi != null ) {
1688             _euro_type_cbmi.setSelected( false );
1689         }
1690         if ( _rounded_type_cbmi != null ) {
1691             _rounded_type_cbmi.setSelected( false );
1692         }
1693         if ( _triangular_type_cbmi != null ) {
1694             _triangular_type_cbmi.setSelected( false );
1695         }
1696         if ( _rectangular_type_cbmi != null ) {
1697             _rectangular_type_cbmi.setSelected( false );
1698         }
1699         if ( _unrooted_type_cbmi != null ) {
1700             _unrooted_type_cbmi.setSelected( false );
1701         }
1702         if ( _circular_type_cbmi != null ) {
1703             _circular_type_cbmi.setSelected( false );
1704         }
1705     }
1706
1707     void showWhole() {
1708         _mainpanel.getControlPanel().showWhole();
1709     }
1710
1711     void switchColors() {
1712         final TreeColorSet colorset = getMainPanel().getCurrentTreePanel().getTreeColorSet();
1713         final ColorSchemeChooser csc = new ColorSchemeChooser( getMainPanel(), colorset );
1714         csc.setVisible( true );
1715         getMainPanel().setTreeColorSet( colorset );
1716     }
1717
1718     void typeChanged( final Object o ) {
1719         updateTypeCheckboxes( getOptions(), o );
1720         updateOptions( getOptions() );
1721         if ( getCurrentTreePanel() != null ) {
1722             final PHYLOGENY_GRAPHICS_TYPE previous_type = getCurrentTreePanel().getPhylogenyGraphicsType();
1723             final PHYLOGENY_GRAPHICS_TYPE new_type = getOptions().getPhylogenyGraphicsType();
1724             if ( ( ( previous_type == PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) && ( new_type != PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) )
1725                     || ( ( previous_type == PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) && ( new_type != PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) )
1726                     || ( ( previous_type != PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) && ( new_type == PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) )
1727                     || ( ( previous_type != PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) && ( new_type == PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) ) ) {
1728                 getCurrentTreePanel().getControlPanel().showWhole();
1729             }
1730             if ( getCurrentTreePanel().isPhyHasBranchLengths() && ( new_type != PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) ) {
1731                 getCurrentTreePanel().getControlPanel().setDrawPhylogramEnabled( true );
1732             }
1733             else {
1734                 getCurrentTreePanel().getControlPanel().setDrawPhylogramEnabled( false );
1735             }
1736             getCurrentTreePanel().setPhylogenyGraphicsType( getOptions().getPhylogenyGraphicsType() );
1737             MainFrame.updateScreenTextAntialias( getMainPanel().getTreePanels() );
1738         }
1739     }
1740
1741     void updateOptions( final Options options ) {
1742         options.setAntialiasScreen( ( _screen_antialias_cbmi != null ) && _screen_antialias_cbmi.isSelected() );
1743         options.setBackgroundColorGradient( ( _background_gradient_cbmi != null )
1744                                             && _background_gradient_cbmi.isSelected() );
1745         options.setShowDomainLabels( ( _show_domain_labels != null ) && _show_domain_labels.isSelected() );
1746         options.setShowAnnotationRefSource( ( _show_annotation_ref_source != null )
1747                                             && _show_annotation_ref_source.isSelected() );
1748         options.setAbbreviateScientificTaxonNames( ( _abbreviate_scientific_names != null )
1749                                                    && _abbreviate_scientific_names.isSelected() );
1750         options.setColorLabelsSameAsParentBranch( ( _color_labels_same_as_parent_branch != null )
1751                                                   && _color_labels_same_as_parent_branch.isSelected() );
1752         options.setShowDefaultNodeShapesInternal( ( _show_default_node_shapes_internal_cbmi != null )
1753                                                   && _show_default_node_shapes_internal_cbmi.isSelected() );
1754         options.setShowDefaultNodeShapesExternal( ( _show_default_node_shapes_external_cbmi != null )
1755                                                   && _show_default_node_shapes_external_cbmi.isSelected() );
1756         options.setShowDefaultNodeShapesForMarkedNodes( ( _show_default_node_shapes_for_marked_cbmi != null )
1757                                                         && _show_default_node_shapes_for_marked_cbmi.isSelected() );
1758         if ( ( _non_lined_up_cladograms_rbmi != null ) && ( _non_lined_up_cladograms_rbmi.isSelected() ) ) {
1759             options.setCladogramType( CLADOGRAM_TYPE.NON_LINED_UP );
1760         }
1761         else if ( ( _uniform_cladograms_rbmi != null ) && ( _uniform_cladograms_rbmi.isSelected() ) ) {
1762             options.setCladogramType( CLADOGRAM_TYPE.TOTAL_NODE_SUM_DEP );
1763         }
1764         else if ( ( _ext_node_dependent_cladogram_rbmi != null ) && ( _ext_node_dependent_cladogram_rbmi.isSelected() ) ) {
1765             options.setCladogramType( CLADOGRAM_TYPE.EXT_NODE_SUM_DEP );
1766         }
1767         options.setSearchCaseSensitive( ( _search_case_senstive_cbmi != null )
1768                                         && _search_case_senstive_cbmi.isSelected() );
1769         if ( ( _show_scale_cbmi != null ) && _show_scale_cbmi.isEnabled() ) {
1770             options.setShowScale( _show_scale_cbmi.isSelected() );
1771         }
1772         if ( _label_direction_cbmi != null ) {
1773             if ( _label_direction_cbmi.isSelected() ) {
1774                 options.setNodeLabelDirection( NODE_LABEL_DIRECTION.RADIAL );
1775             }
1776             else {
1777                 options.setNodeLabelDirection( NODE_LABEL_DIRECTION.HORIZONTAL );
1778             }
1779         }
1780         options.setShowOverview( ( _show_overview_cbmi != null ) && _show_overview_cbmi.isSelected() );
1781         options.setShowConfidenceStddev( ( _show_confidence_stddev_cbmi != null )
1782                                          && _show_confidence_stddev_cbmi.isSelected() );
1783         options.setMatchWholeTermsOnly( ( _search_whole_words_only_cbmi != null )
1784                                         && _search_whole_words_only_cbmi.isSelected() );
1785         options.setSearchWithRegex( ( _search_with_regex_cbmi != null ) && _search_with_regex_cbmi.isSelected() );
1786         options.setInverseSearchResult( ( _inverse_search_result_cbmi != null )
1787                                         && _inverse_search_result_cbmi.isSelected() );
1788         options.setPrintUsingActualSize( ( _print_using_actual_size_cbmi != null )
1789                 && ( _print_using_actual_size_cbmi.isSelected() ) );
1790         options.setGraphicsExportUsingActualSize( ( _graphics_export_using_actual_size_cbmi != null )
1791                 && ( _graphics_export_using_actual_size_cbmi.isSelected() ) );
1792         options.setAntialiasPrint( ( _antialias_print_cbmi != null ) && _antialias_print_cbmi.isSelected() );
1793         options.setPrintBlackAndWhite( ( _print_black_and_white_cbmi != null )
1794                 && _print_black_and_white_cbmi.isSelected() );
1795         if ( ( _rectangular_type_cbmi != null ) && _rectangular_type_cbmi.isSelected() ) {
1796             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
1797         }
1798         else if ( ( _triangular_type_cbmi != null ) && _triangular_type_cbmi.isSelected() ) {
1799             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.TRIANGULAR );
1800         }
1801         else if ( ( _curved_type_cbmi != null ) && _curved_type_cbmi.isSelected() ) {
1802             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.CURVED );
1803         }
1804         else if ( ( _convex_type_cbmi != null ) && _convex_type_cbmi.isSelected() ) {
1805             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.CONVEX );
1806         }
1807         else if ( ( _euro_type_cbmi != null ) && _euro_type_cbmi.isSelected() ) {
1808             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.EURO_STYLE );
1809         }
1810         else if ( ( _rounded_type_cbmi != null ) && _rounded_type_cbmi.isSelected() ) {
1811             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.ROUNDED );
1812         }
1813         else if ( ( _unrooted_type_cbmi != null ) && _unrooted_type_cbmi.isSelected() ) {
1814             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.UNROOTED );
1815         }
1816         else if ( ( _circular_type_cbmi != null ) && _circular_type_cbmi.isSelected() ) {
1817             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.CIRCULAR );
1818         }
1819         if ( ( _color_by_taxonomic_group_cbmi != null ) && _color_by_taxonomic_group_cbmi.isEnabled() ) {
1820             options.setColorByTaxonomicGroup( _color_by_taxonomic_group_cbmi.isSelected() );
1821         }
1822         if ( ( _right_line_up_domains_cbmi != null ) && _right_line_up_domains_cbmi.isEnabled() ) {
1823             options.setRightLineUpDomains( _right_line_up_domains_cbmi.isSelected() );
1824         }
1825         if ( ( _line_up_renderable_data_cbmi != null ) && _line_up_renderable_data_cbmi.isEnabled() ) {
1826             options.setLineUpRendarableNodeData( _line_up_renderable_data_cbmi.isSelected() );
1827         }
1828         if ( _graphics_export_visible_only_cbmi != null ) {
1829             options.setGraphicsExportVisibleOnly( _graphics_export_visible_only_cbmi.isSelected() );
1830             if ( _graphics_export_visible_only_cbmi.isSelected() && ( _graphics_export_using_actual_size_cbmi != null ) ) {
1831                 _graphics_export_using_actual_size_cbmi.setSelected( true );
1832                 _graphics_export_using_actual_size_cbmi.setEnabled( false );
1833             }
1834             else {
1835                 _graphics_export_using_actual_size_cbmi.setEnabled( true );
1836             }
1837         }
1838     }
1839
1840     void updateTypeCheckboxes( final Options options, final Object o ) {
1841         setTypeMenuToAllUnselected();
1842         ( ( JCheckBoxMenuItem ) o ).setSelected( true );
1843     }
1844
1845     void viewAsNexus() {
1846         if ( ( getMainPanel().getCurrentPhylogeny() != null ) && !getMainPanel().getCurrentPhylogeny().isEmpty() ) {
1847             String title = "Nexus";
1848             if ( !ForesterUtil.isEmpty( getMainPanel().getCurrentPhylogeny().getName() ) ) {
1849                 title = "\"" + getMainPanel().getCurrentPhylogeny().getName() + "\" in " + title;
1850             }
1851             showTextFrame( getMainPanel().getCurrentPhylogeny().toNexus( getOptions()
1852                                                                          .getNhConversionSupportValueStyle() ),
1853                                                                          title );
1854         }
1855     }
1856
1857     void viewAsNH() {
1858         if ( ( getMainPanel().getCurrentPhylogeny() != null ) && !getMainPanel().getCurrentPhylogeny().isEmpty() ) {
1859             String title = "New Hampshire";
1860             if ( !ForesterUtil.isEmpty( getMainPanel().getCurrentPhylogeny().getName() ) ) {
1861                 title = "\"" + getMainPanel().getCurrentPhylogeny().getName() + "\" in " + title;
1862             }
1863             showTextFrame( getMainPanel().getCurrentPhylogeny().toNewHampshire( getOptions()
1864                                                                                 .getNhConversionSupportValueStyle() ),
1865                                                                                 title );
1866         }
1867     }
1868
1869     void viewAsXML() {
1870         if ( ( getMainPanel().getCurrentPhylogeny() != null ) && !getMainPanel().getCurrentPhylogeny().isEmpty() ) {
1871             String title = "phyloXML";
1872             if ( !ForesterUtil.isEmpty( getMainPanel().getCurrentPhylogeny().getName() ) ) {
1873                 title = "\"" + getMainPanel().getCurrentPhylogeny().getName() + "\" in " + title;
1874             }
1875             showTextFrame( getMainPanel().getCurrentPhylogeny().toPhyloXML( 0 ), title );
1876         }
1877     }
1878
1879     static void setTextForGraphicsSizeChooserMenuItem( final JMenuItem mi, final Options o ) {
1880         mi.setText( "Enter Default Size for Graphics Export... (current: " + o.getPrintSizeX() + ", "
1881                 + o.getPrintSizeY() + ")" );
1882     }
1883
1884     static void setTextForPdfLineWidthChooserMenuItem( final JMenuItem mi, final Options o ) {
1885         mi.setText( "Enter Default Line Width for PDF Export... (current: " + o.getPrintLineWidth() + ")" );
1886     }
1887
1888     static void setupScreenTextAntialias( final List<TreePanel> treepanels, final boolean antialias ) {
1889         for( final TreePanel tree_panel : treepanels ) {
1890             tree_panel.setTextAntialias();
1891         }
1892     }
1893 }