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