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