work on taxonomy extraction for applets for aLeaves MAFFT suite
[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.Map;
17 import java.util.NoSuchElementException;
18
19 import javax.swing.ButtonGroup;
20 import javax.swing.JApplet;
21 import javax.swing.JCheckBoxMenuItem;
22 import javax.swing.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.phylogeny.Phylogeny;
38 import org.forester.phylogeny.data.SequenceRelation;
39 import org.forester.util.ForesterConstants;
40 import org.forester.util.ForesterUtil;
41
42 // Use like this:
43 // <applet archive="forester.jar"
44 // code="org.forester.archaeopteryx.ArchaeopteryxE.class"
45 // codebase="http://www.myserver.org/path/to/forester"
46 // width="600"
47 // height="500"
48 // alt="ArchaeopteryxE is not working on your system (requires at least Sun Java 1.5)!">
49 // <param name="url_of_tree_to_load"
50 // value="http://www.myserver.org/examples/data/apaf.xml">
51 // <param name="config_file"
52 // value="http://www.myserver.org/examples/config/config_file.txt">
53 // </applet>
54 public class ArchaeopteryxE extends JApplet implements ActionListener {
55
56     private final static String         NAME                  = "ArchaeopteryxE";
57     private static final long           serialVersionUID      = -1220055577935759443L;
58     private Configuration               _configuration;
59     private MainPanelApplets            _main_panel;
60     private JMenuBar                    _jmenubar;
61     private JMenu                       _options_jmenu;
62     private JMenu                       _font_size_menu;
63     private JMenuItem                   _super_tiny_fonts_mi;
64     private JMenuItem                   _tiny_fonts_mi;
65     private JMenuItem                   _small_fonts_mi;
66     private JMenuItem                   _medium_fonts_mi;
67     private JMenuItem                   _large_fonts_mi;
68     private JMenu                       _tools_menu;
69     private JMenuItem                   _taxcolor_item;
70     private JMenuItem                   _confcolor_item;
71     private JMenuItem                   _midpoint_root_item;
72     private JMenu                       _view_jmenu;
73     private JMenuItem                   _view_as_XML_item;
74     private JMenuItem                   _view_as_NH_item;
75     private JMenuItem                   _view_as_NHX_item;
76     private JMenuItem                   _view_as_nexus_item;
77     private JMenuItem                   _display_basic_information_item;
78     private JMenu                       _type_menu;
79     private JCheckBoxMenuItem           _rectangular_type_cbmi;
80     private JCheckBoxMenuItem           _triangular_type_cbmi;
81     private JCheckBoxMenuItem           _curved_type_cbmi;
82     private JCheckBoxMenuItem           _convex_type_cbmi;
83     private JCheckBoxMenuItem           _euro_type_cbmi;
84     private JCheckBoxMenuItem           _rounded_type_cbmi;
85     private JCheckBoxMenuItem           _unrooted_type_cbmi;
86     private JCheckBoxMenuItem           _circular_type_cbmi;
87     private JMenuItem                   _help_item;
88     private JMenuItem                   _about_item;
89     private JMenu                       _help_jmenu;
90     private JMenuItem                   _website_item;
91     private JMenuItem                   _phyloxml_website_item;
92     private JMenuItem                   _phyloxml_ref_item;
93     private JMenuItem                   _aptx_ref_item;
94     private JMenuItem                   _remove_branch_color_item;
95     private JMenuItem                   _infer_common_sn_names_item;
96     private JCheckBoxMenuItem           _show_domain_labels;
97     private JCheckBoxMenuItem           _color_labels_same_as_parent_branch;
98     private JCheckBoxMenuItem           _abbreviate_scientific_names;
99     private JCheckBoxMenuItem           _screen_antialias_cbmi;
100     private JCheckBoxMenuItem           _background_gradient_cbmi;
101     private JRadioButtonMenuItem        _non_lined_up_cladograms_rbmi;
102     private JRadioButtonMenuItem        _uniform_cladograms_rbmi;
103     private JRadioButtonMenuItem        _ext_node_dependent_cladogram_rbmi;
104     private Options                     _options;
105     private JMenuItem                   _choose_font_mi;
106     private JMenuItem                   _switch_colors_mi;
107     JCheckBoxMenuItem                   _label_direction_cbmi;
108     private JCheckBoxMenuItem           _show_scale_cbmi;
109     private JCheckBoxMenuItem           _search_case_senstive_cbmi;
110     private JCheckBoxMenuItem           _search_whole_words_only_cbmi;
111     private JCheckBoxMenuItem           _inverse_search_result_cbmi;
112     private JCheckBoxMenuItem           _show_overview_cbmi;
113     private JMenuItem                   _choose_minimal_confidence_mi;
114     private JCheckBoxMenuItem           _show_branch_length_values_cbmi;
115     private JMenuItem                   _collapse_species_specific_subtrees;
116     private JMenuItem                   _overview_placment_mi;
117     private ButtonGroup                 _radio_group_1;
118     private JCheckBoxMenuItem           _show_default_node_shapes_cbmi;
119     private JMenuItem                   _cycle_node_shape_mi;
120     private JMenuItem                   _cycle_node_fill_mi;
121     private JMenuItem                   _choose_node_size_mi;
122     private JCheckBoxMenuItem           _taxonomy_colorize_node_shapes_cbmi;
123     private JCheckBoxMenuItem           _show_confidence_stddev_cbmi;
124     private final LinkedList<TextFrame> _textframes           = new LinkedList<TextFrame>();
125     private String                      _ext_node_data_buffer = "";
126
127     @Override
128     public void actionPerformed( final ActionEvent e ) {
129         final Object o = e.getSource();
130         if ( o == _midpoint_root_item ) {
131             getMainPanel().getCurrentTreePanel().midpointRoot();
132         }
133         else if ( o == _taxcolor_item ) {
134             getMainPanel().getCurrentTreePanel().taxColor();
135         }
136         else if ( o == _confcolor_item ) {
137             getMainPanel().getCurrentTreePanel().confColor();
138         }
139         else if ( o == _collapse_species_specific_subtrees ) {
140             if ( getCurrentTreePanel() != null ) {
141                 getCurrentTreePanel().collapseSpeciesSpecificSubtrees();
142             }
143         }
144         else if ( o == _remove_branch_color_item ) {
145             removeBranchColors();
146         }
147         else if ( o == _switch_colors_mi ) {
148             switchColors();
149         }
150         else if ( o == _display_basic_information_item ) {
151             displayBasicInformation();
152         }
153         else if ( o == _view_as_NH_item ) {
154             viewAsNH();
155         }
156         else if ( o == _view_as_NHX_item ) {
157             viewAsNHX();
158         }
159         else if ( o == _view_as_XML_item ) {
160             viewAsXML();
161         }
162         else if ( o == _view_as_nexus_item ) {
163             viewAsNexus();
164         }
165         else if ( o == _super_tiny_fonts_mi ) {
166             if ( getCurrentTreePanel() != null ) {
167                 getCurrentTreePanel().setSuperTinyFonts();
168                 getCurrentTreePanel().repaint();
169             }
170         }
171         else if ( o == _tiny_fonts_mi ) {
172             if ( getCurrentTreePanel() != null ) {
173                 getCurrentTreePanel().setTinyFonts();
174                 getCurrentTreePanel().repaint();
175             }
176         }
177         else if ( o == _small_fonts_mi ) {
178             if ( getCurrentTreePanel() != null ) {
179                 getCurrentTreePanel().setSmallFonts();
180                 getCurrentTreePanel().repaint();
181             }
182         }
183         else if ( o == _medium_fonts_mi ) {
184             if ( getCurrentTreePanel() != null ) {
185                 getCurrentTreePanel().setMediumFonts();
186                 getCurrentTreePanel().repaint();
187             }
188         }
189         else if ( o == _large_fonts_mi ) {
190             if ( getCurrentTreePanel() != null ) {
191                 getCurrentTreePanel().setLargeFonts();
192                 getCurrentTreePanel().repaint();
193             }
194         }
195         else if ( o == _choose_font_mi ) {
196             chooseFont();
197         }
198         else if ( o == _choose_minimal_confidence_mi ) {
199             chooseMinimalConfidence();
200         }
201         else if ( o == _choose_node_size_mi ) {
202             MainFrame.chooseNodeSize( getOptions(), this );
203         }
204         else if ( o == _overview_placment_mi ) {
205             MainFrame.cycleOverview( getOptions(), getCurrentTreePanel() );
206         }
207         else if ( o == _cycle_node_fill_mi ) {
208             MainFrame.cycleNodeFill( getOptions(), getCurrentTreePanel() );
209         }
210         else if ( o == _cycle_node_shape_mi ) {
211             MainFrame.cycleNodeShape( getOptions(), getCurrentTreePanel() );
212         }
213         else if ( o == _non_lined_up_cladograms_rbmi ) {
214             updateOptions( getOptions() );
215             _main_panel.getControlPanel().showWhole();
216         }
217         else if ( o == _uniform_cladograms_rbmi ) {
218             updateOptions( getOptions() );
219             _main_panel.getControlPanel().showWhole();
220         }
221         else if ( o == _ext_node_dependent_cladogram_rbmi ) {
222             updateOptions( getOptions() );
223             _main_panel.getControlPanel().showWhole();
224         }
225         else if ( o == _search_case_senstive_cbmi ) {
226             updateOptions( getOptions() );
227             getMainPanel().getControlPanel().search();
228         }
229         else if ( o == _search_whole_words_only_cbmi ) {
230             updateOptions( getOptions() );
231             getMainPanel().getControlPanel().search();
232         }
233         else if ( o == _inverse_search_result_cbmi ) {
234             updateOptions( getOptions() );
235             getMainPanel().getControlPanel().search();
236         }
237         else if ( o == _show_scale_cbmi ) {
238             updateOptions( getOptions() );
239         }
240         else if ( o == _show_branch_length_values_cbmi ) {
241             updateOptions( getOptions() );
242         }
243         else if ( o == _show_confidence_stddev_cbmi ) {
244             updateOptions( getOptions() );
245         }
246         else if ( o == _label_direction_cbmi ) {
247             updateOptions( getOptions() );
248         }
249         else if ( o == _show_overview_cbmi ) {
250             updateOptions( getOptions() );
251             if ( getCurrentTreePanel() != null ) {
252                 getCurrentTreePanel().updateOvSizes();
253             }
254         }
255         else if ( ( o == _rectangular_type_cbmi ) || ( o == _triangular_type_cbmi ) || ( o == _curved_type_cbmi )
256                 || ( o == _convex_type_cbmi ) || ( o == _rounded_type_cbmi ) || ( o == _euro_type_cbmi )
257                 || ( o == _unrooted_type_cbmi ) || ( o == _circular_type_cbmi ) ) {
258             typeChanged( o );
259         }
260         else if ( o == _screen_antialias_cbmi ) {
261             updateOptions( getOptions() );
262             setupScreenTextAntialias( getMainPanel().getTreePanels(), isScreenAntialias() );
263         }
264         else if ( o == _background_gradient_cbmi ) {
265             updateOptions( getOptions() );
266         }
267         else if ( o == _show_domain_labels ) {
268             updateOptions( getOptions() );
269         }
270         else if ( o == _color_labels_same_as_parent_branch ) {
271             updateOptions( getOptions() );
272         }
273         else if ( o == _show_default_node_shapes_cbmi ) {
274             updateOptions( getOptions() );
275         }
276         else if ( o == _taxonomy_colorize_node_shapes_cbmi ) {
277             updateOptions( getOptions() );
278         }
279         else if ( o == _about_item ) {
280             MainFrame.about();
281         }
282         else if ( o == _help_item ) {
283             help( getConfiguration().getWebLinks() );
284         }
285         else if ( o == _website_item ) {
286             try {
287                 AptxUtil.openWebsite( Constants.APTX_WEB_SITE, true, this );
288             }
289             catch ( final IOException e1 ) {
290                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
291             }
292         }
293         else if ( o == _phyloxml_website_item ) {
294             try {
295                 AptxUtil.openWebsite( Constants.PHYLOXML_WEB_SITE, true, this );
296             }
297             catch ( final IOException e1 ) {
298                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
299             }
300         }
301         else if ( o == _aptx_ref_item ) {
302             try {
303                 AptxUtil.openWebsite( Constants.APTX_REFERENCE_URL, true, this );
304             }
305             catch ( final IOException e1 ) {
306                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
307             }
308         }
309         else if ( o == _phyloxml_ref_item ) {
310             try {
311                 AptxUtil.openWebsite( Constants.PHYLOXML_REFERENCE_URL, true, this );
312             }
313             catch ( final IOException e1 ) {
314                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
315             }
316         }
317         repaint();
318     }
319
320     @Override
321     public void destroy() {
322         AptxUtil.printAppletMessage( NAME, "going to be destroyed " );
323         removeAllTextFrames();
324         if ( getMainPanel() != null ) {
325             getMainPanel().terminate();
326         }
327     }
328
329     /**
330      * This method returns the current external node data which
331      * has been selected by the user by clicking the "Return ..."
332      * menu item. This method is expected to be called from Javascript or
333      * something like it.
334      * 
335      * @return current external node data as String
336      */
337     public String getCurrentExternalNodesDataBuffer() {
338         return _ext_node_data_buffer;
339     }
340
341     /**
342      * This method returns the current phylogeny as a string in the chosen format
343      * 
344      * @param format must be NH, NHX, NEXUS or PHYLOXML
345      * @return the phylogeny string
346      * @author Herve Menager
347      */
348     public String getCurrentPhylogeny( final String format ) {
349         removeAllTextFrames();
350         if ( ( getMainPanel().getCurrentPhylogeny() == null ) || getMainPanel().getCurrentPhylogeny().isEmpty()
351                 || ( getMainPanel().getCurrentPhylogeny().getNumberOfExternalNodes() > 10000 ) ) {
352             return new String();
353         }
354         switch ( ForesterConstants.PhylogeneticTreeFormats.valueOf( format ) ) {
355             case NH:
356                 return getMainPanel().getCurrentPhylogeny().toNewHampshire();
357             case NHX:
358                 return getMainPanel().getCurrentPhylogeny().toNewHampshireX();
359             case NEXUS:
360                 return getMainPanel().getCurrentPhylogeny().toNexus();
361             case PHYLOXML:
362                 return getMainPanel().getCurrentPhylogeny().toPhyloXML( -1 );
363             default:
364                 break;
365         }
366         return new String();
367     }
368
369     /**
370      * This method returns a view of the current phylogeny in a chosen 
371      * graphics format, base64-encoded in a string so that in can be used
372      * from javascript.
373      * 
374      * @param format must be GraphicsExportType (gif, jpg, pdf, png, tif, bmp)
375      * @return the phylogeny string
376      * @author Herve Menager
377      */
378     public String getCurrentPhylogenyGraphicsAsBase64EncodedString( final String format ) {
379         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
380         try {
381             AptxUtil.writePhylogenyToGraphicsByteArrayOutputStream( baos,
382                                                                     _main_panel.getWidth(),
383                                                                     _main_panel.getHeight(),
384                                                                     getCurrentTreePanel(),
385                                                                     getCurrentTreePanel().getControlPanel(),
386                                                                     GraphicsExportType.valueOf( format ),
387                                                                     getOptions() );
388         }
389         catch ( final IOException ioe ) {
390             ForesterUtil.printErrorMessage( NAME, ioe.toString() );
391             ioe.printStackTrace();
392             JOptionPane.showMessageDialog( this,
393                                            NAME + ": Failed to generate graphics: " + "\nException: " + ioe,
394                                            "Failed to generate graphics",
395                                            JOptionPane.ERROR_MESSAGE );
396             return null;
397         }
398         final byte[] bytes = baos.toByteArray();
399         final String dataImg = Base64.encodeBase64String( bytes );
400         return dataImg;
401     }
402
403     public Options getOptions() {
404         return _options;
405     }
406
407     @Override
408     public void init() {
409         final String config_filename = getParameter( Constants.APPLET_PARAM_NAME_FOR_CONFIG_FILE_URL );
410         AptxUtil.printAppletMessage( NAME, "URL for configuration file is: " + config_filename );
411         final Configuration configuration = new Configuration( config_filename, true, true, true );
412         setConfiguration( configuration );
413         setOptions( Options.createInstance( configuration ) );
414         setupUI();
415         URL phys_url = null;
416         Phylogeny[] phys = null;
417         final String phys_url_string = getParameter( Constants.APPLET_PARAM_NAME_FOR_URL_OF_TREE_TO_LOAD );
418         AptxUtil.printAppletMessage( NAME, "URL for phylogenies is " + phys_url_string );
419         // Get URL to tree file
420         if ( phys_url_string != null ) {
421             try {
422                 phys_url = new URL( phys_url_string );
423             }
424             catch ( final Exception e ) {
425                 ForesterUtil.printErrorMessage( NAME, "error: " + e );
426                 e.printStackTrace();
427                 JOptionPane.showMessageDialog( this, NAME + ": Could not create URL from: \"" + phys_url_string
428                         + "\"\nException: " + e, "Failed to create URL", JOptionPane.ERROR_MESSAGE );
429             }
430         }
431         // Load the tree from URL
432         if ( phys_url != null ) {
433             try {
434                 phys = AptxUtil.readPhylogeniesFromUrl( phys_url,
435                                                         getConfiguration().isValidatePhyloXmlAgainstSchema(),
436                                                         getConfiguration().isReplaceUnderscoresInNhParsing(),
437                                                         getConfiguration().isInternalNumberAreConfidenceForNhParsing(),
438                                                         getConfiguration().getTaxonomyExtraction() );
439             }
440             catch ( final Exception e ) {
441                 ForesterUtil.printErrorMessage( NAME, e.toString() );
442                 e.printStackTrace();
443                 JOptionPane.showMessageDialog( this,
444                                                NAME + ": Failed to read phylogenies: " + "\nException: " + e,
445                                                "Failed to read phylogenies",
446                                                JOptionPane.ERROR_MESSAGE );
447             }
448         }
449         if ( ( phys == null ) || ( phys.length < 1 ) ) {
450             ForesterUtil.printErrorMessage( NAME, "phylogenies from [" + phys_url + "] are null or empty" );
451             JOptionPane.showMessageDialog( this,
452                                            NAME + ": phylogenies from [" + phys_url + "] are null or empty",
453                                            "Failed to read phylogenies",
454                                            JOptionPane.ERROR_MESSAGE );
455             return;
456         }
457         else {
458             AptxUtil.printAppletMessage( NAME, "loaded " + phys.length + " phylogenies from: " + phys_url );
459         }
460         setVisible( false );
461         setMainPanel( new MainPanelApplets( getConfiguration(), this ) );
462         _jmenubar = new JMenuBar();
463         if ( !getConfiguration().isHideControlPanelAndMenubar() ) {
464             if ( !getConfiguration().isUseNativeUI() ) {
465                 _jmenubar.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
466             }
467             buildToolsMenu();
468             buildViewMenu();
469             buildFontSizeMenu();
470             buildOptionsMenu();
471             buildTypeMenu();
472             buildHelpMenu();
473             setJMenuBar( _jmenubar );
474         }
475         final Container contentpane = getContentPane();
476         contentpane.setLayout( new BorderLayout() );
477         contentpane.add( getMainPanel(), BorderLayout.CENTER );
478         addComponentListener( new ComponentAdapter() {
479
480             @Override
481             public void componentResized( final ComponentEvent e ) {
482                 if ( getMainPanel().getCurrentTreePanel() != null ) {
483                     getMainPanel().getCurrentTreePanel().setParametersForPainting( getMainPanel().getCurrentTreePanel()
484                                                                                            .getWidth(),
485                                                                                    getMainPanel().getCurrentTreePanel()
486                                                                                            .getHeight(),
487                                                                                    false );
488                 }
489             }
490         } );
491         if ( getConfiguration().isUseTabbedDisplay() ) {
492             AptxUtil.printAppletMessage( NAME, "using tabbed display" );
493             AptxUtil.addPhylogeniesToTabs( phys,
494                                            new File( phys_url.getFile() ).getName(),
495                                            phys_url.toString(),
496                                            getConfiguration(),
497                                            getMainPanel() );
498         }
499         else {
500             AptxUtil.printAppletMessage( NAME, "not using tabbed display" );
501             AptxUtil.addPhylogenyToPanel( phys, getConfiguration(), getMainPanel() );
502         }
503         validate();
504         setName( NAME );
505         getMainPanel().getControlPanel().showWholeAll();
506         getMainPanel().getControlPanel().showWhole();
507         System.gc();
508         AptxUtil.printAppletMessage( NAME, "successfully initialized" );
509         /* GUILHEM_BEG */
510         getCurrentTreePanel().getControlPanel().getSequenceRelationTypeBox().removeAllItems();
511         for( final SequenceRelation.SEQUENCE_RELATION_TYPE type : getMainPanel().getCurrentPhylogeny()
512                 .getRelevantSequenceRelationTypes() ) {
513             getCurrentTreePanel().getControlPanel().getSequenceRelationTypeBox().addItem( type );
514         }
515         final String default_relation = getParameter( Constants.APPLET_PARAM_NAME_FOR_DEFAULT_SEQUENCE_RELATION_TYPE );
516         if ( default_relation != null ) {
517             getCurrentTreePanel().getControlPanel().getSequenceRelationTypeBox().setSelectedItem( default_relation );
518         }
519         final String default_sequence = getParameter( Constants.APPLET_PARAM_NAME_FOR_DEFAULT_QUERY_SEQUENCE );
520         if ( default_sequence != null ) {
521             getCurrentTreePanel().getControlPanel().getSequenceRelationBox().setSelectedItem( default_sequence );
522             /* GUILHEM_END */
523         }
524         setVisible( true );
525     }
526
527     public void showTextFrame( final String s, final String title ) {
528         checkTextFrames();
529         _textframes.addLast( TextFrame.instantiate( s, title, _textframes ) );
530     }
531
532     @Override
533     public void start() {
534         if ( getMainPanel() != null ) {
535             getMainPanel().validate();
536         }
537         requestFocus();
538         requestFocusInWindow();
539         requestFocus();
540         AptxUtil.printAppletMessage( NAME, "started" );
541     }
542
543     void buildFontSizeMenu() {
544         _font_size_menu = MainFrame.createMenu( MainFrame.FONT_SIZE_MENU_LABEL, getConfiguration() );
545         _font_size_menu.add( _super_tiny_fonts_mi = new JMenuItem( "Super tiny fonts" ) );
546         _font_size_menu.add( _tiny_fonts_mi = new JMenuItem( "Tiny fonts" ) );
547         _font_size_menu.add( _small_fonts_mi = new JMenuItem( "Small fonts" ) );
548         _font_size_menu.add( _medium_fonts_mi = new JMenuItem( "Medium fonts" ) );
549         _font_size_menu.add( _large_fonts_mi = new JMenuItem( "Large fonts" ) );
550         customizeJMenuItem( _super_tiny_fonts_mi );
551         customizeJMenuItem( _tiny_fonts_mi );
552         customizeJMenuItem( _small_fonts_mi );
553         customizeJMenuItem( _medium_fonts_mi );
554         customizeJMenuItem( _large_fonts_mi );
555         _jmenubar.add( _font_size_menu );
556     }
557
558     void buildHelpMenu() {
559         _help_jmenu = MainFrame.createMenu( "Help", getConfiguration() );
560         _help_jmenu.add( _help_item = new JMenuItem( "Help" ) );
561         _help_jmenu.add( _website_item = new JMenuItem( "Archaeopteryx Home" ) );
562         _aptx_ref_item = new JMenuItem( "Archaeopteryx Reference" );
563         _help_jmenu.add( _phyloxml_website_item = new JMenuItem( "phyloXML Home" ) );
564         _help_jmenu.add( _phyloxml_ref_item = new JMenuItem( "phyloXML Reference" ) );
565         _help_jmenu.addSeparator();
566         _help_jmenu.add( _about_item = new JMenuItem( "About" ) );
567         customizeJMenuItem( _help_item );
568         customizeJMenuItem( _website_item );
569         customizeJMenuItem( _phyloxml_website_item );
570         customizeJMenuItem( _aptx_ref_item );
571         customizeJMenuItem( _phyloxml_ref_item );
572         customizeJMenuItem( _about_item );
573         _phyloxml_ref_item.setToolTipText( MainFrame.PHYLOXML_REF_TOOL_TIP );
574         _aptx_ref_item.setToolTipText( MainFrame.APTX_REF_TOOL_TIP );
575         _jmenubar.add( _help_jmenu );
576     }
577
578     void buildOptionsMenu() {
579         _options_jmenu = MainFrame.createMenu( MainFrame.OPTIONS_HEADER, getConfiguration() );
580         _options_jmenu.addChangeListener( new ChangeListener() {
581
582             @Override
583             public void stateChanged( final ChangeEvent e ) {
584                 MainFrame.setOvPlacementColorChooseMenuItem( _overview_placment_mi, getOptions() );
585                 MainFrame.setTextColorChooseMenuItem( _switch_colors_mi, getCurrentTreePanel() );
586                 MainFrame
587                         .setTextMinSupportMenuItem( _choose_minimal_confidence_mi, getOptions(), getCurrentTreePanel() );
588                 MainFrame.setTextForFontChooserMenuItem( _choose_font_mi, MainFrame
589                         .createCurrentFontDesc( getMainPanel().getTreeFontSet() ) );
590                 MainFrame.updateOptionsMenuDependingOnPhylogenyType( getMainPanel(),
591                                                                      _show_scale_cbmi,
592                                                                      _show_branch_length_values_cbmi,
593                                                                      _non_lined_up_cladograms_rbmi,
594                                                                      _uniform_cladograms_rbmi,
595                                                                      _ext_node_dependent_cladogram_rbmi,
596                                                                      _label_direction_cbmi );
597                 MainFrame.setCycleNodeFillMenuItem( _cycle_node_fill_mi, getOptions() );
598                 MainFrame.setCycleNodeShapeMenuItem( _cycle_node_shape_mi, getOptions() );
599                 MainFrame.setTextNodeSizeMenuItem( _choose_node_size_mi, getOptions() );
600             }
601         } );
602         _options_jmenu.add( MainFrame.customizeMenuItemAsLabel( new JMenuItem( MainFrame.DISPLAY_SUBHEADER ),
603                                                                 getConfiguration() ) );
604         _options_jmenu
605                 .add( _ext_node_dependent_cladogram_rbmi = new JRadioButtonMenuItem( MainFrame.NONUNIFORM_CLADOGRAMS_LABEL ) );
606         _options_jmenu.add( _uniform_cladograms_rbmi = new JRadioButtonMenuItem( MainFrame.UNIFORM_CLADOGRAMS_LABEL ) );
607         _options_jmenu
608                 .add( _non_lined_up_cladograms_rbmi = new JRadioButtonMenuItem( MainFrame.NON_LINED_UP_CLADOGRAMS_LABEL ) );
609         _radio_group_1 = new ButtonGroup();
610         _radio_group_1.add( _ext_node_dependent_cladogram_rbmi );
611         _radio_group_1.add( _uniform_cladograms_rbmi );
612         _radio_group_1.add( _non_lined_up_cladograms_rbmi );
613         _options_jmenu.add( _show_overview_cbmi = new JCheckBoxMenuItem( MainFrame.SHOW_OVERVIEW_LABEL ) );
614         _options_jmenu.add( _show_scale_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_SCALE_LABEL ) );
615         _options_jmenu
616                 .add( _show_branch_length_values_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_BRANCH_LENGTH_VALUES_LABEL ) );
617         _options_jmenu.add( _show_confidence_stddev_cbmi = new JCheckBoxMenuItem( MainFrame.SHOW_CONF_STDDEV_LABEL ) );
618         _options_jmenu
619                 .add( _show_default_node_shapes_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_NODE_BOXES_LABEL ) );
620         _options_jmenu
621                 .add( _taxonomy_colorize_node_shapes_cbmi = new JCheckBoxMenuItem( MainFrame.TAXONOMY_COLORIZE_NODE_SHAPES_LABEL ) );
622         _options_jmenu.add( _cycle_node_shape_mi = new JMenuItem( MainFrame.CYCLE_NODE_SHAPE_LABEL ) );
623         _options_jmenu.add( _cycle_node_fill_mi = new JMenuItem( MainFrame.CYCLE_NODE_FILL_LABEL ) );
624         _options_jmenu.add( _choose_node_size_mi = new JMenuItem( MainFrame.CHOOSE_NODE_SIZE_LABEL ) );
625         _options_jmenu.add( _label_direction_cbmi = new JCheckBoxMenuItem( MainFrame.LABEL_DIRECTION_LABEL ) );
626         _options_jmenu
627                 .add( _color_labels_same_as_parent_branch = new JCheckBoxMenuItem( MainFrame.COLOR_LABELS_LABEL ) );
628         _color_labels_same_as_parent_branch.setToolTipText( MainFrame.COLOR_LABELS_TIP );
629         _options_jmenu.add( _abbreviate_scientific_names = new JCheckBoxMenuItem( MainFrame.ABBREV_SN_LABEL ) );
630         _label_direction_cbmi.setToolTipText( MainFrame.LABEL_DIRECTION_TIP );
631         _options_jmenu.add( _screen_antialias_cbmi = new JCheckBoxMenuItem( MainFrame.SCREEN_ANTIALIAS_LABEL ) );
632         _options_jmenu.add( _background_gradient_cbmi = new JCheckBoxMenuItem( MainFrame.BG_GRAD_LABEL ) );
633         if ( getConfiguration().doDisplayOption( Configuration.show_domain_architectures ) ) {
634             _options_jmenu.add( _show_domain_labels = new JCheckBoxMenuItem( MainFrame.SHOW_DOMAIN_LABELS_LABEL ) );
635         }
636         _options_jmenu.add( _choose_minimal_confidence_mi = new JMenuItem( "" ) );
637         _options_jmenu.add( _overview_placment_mi = new JMenuItem( "" ) );
638         _options_jmenu.add( _switch_colors_mi = new JMenuItem( "" ) );
639         _options_jmenu.add( _choose_font_mi = new JMenuItem( "" ) );
640         _options_jmenu.addSeparator();
641         _options_jmenu.add( MainFrame.customizeMenuItemAsLabel( new JMenuItem( MainFrame.SEARCH_SUBHEADER ),
642                                                                 getConfiguration() ) );
643         _options_jmenu
644                 .add( _search_case_senstive_cbmi = new JCheckBoxMenuItem( MainFrame.SEARCH_CASE_SENSITIVE_LABEL ) );
645         _options_jmenu.add( _search_whole_words_only_cbmi = new JCheckBoxMenuItem( MainFrame.SEARCH_TERMS_ONLY_LABEL ) );
646         _options_jmenu
647                 .add( _inverse_search_result_cbmi = new JCheckBoxMenuItem( MainFrame.INVERSE_SEARCH_RESULT_LABEL ) );
648         customizeJMenuItem( _choose_font_mi );
649         customizeJMenuItem( _choose_minimal_confidence_mi );
650         customizeJMenuItem( _switch_colors_mi );
651         customizeJMenuItem( _overview_placment_mi );
652         customizeCheckBoxMenuItem( _label_direction_cbmi,
653                                    getOptions().getNodeLabelDirection() == NODE_LABEL_DIRECTION.RADIAL );
654         customizeCheckBoxMenuItem( _screen_antialias_cbmi, getOptions().isAntialiasScreen() );
655         customizeCheckBoxMenuItem( _background_gradient_cbmi, getOptions().isBackgroundColorGradient() );
656         customizeCheckBoxMenuItem( _show_domain_labels, getOptions().isShowDomainLabels() );
657         customizeCheckBoxMenuItem( _abbreviate_scientific_names, getOptions().isAbbreviateScientificTaxonNames() );
658         customizeCheckBoxMenuItem( _show_default_node_shapes_cbmi, getOptions().isShowDefaultNodeShapes() );
659         customizeCheckBoxMenuItem( _taxonomy_colorize_node_shapes_cbmi, getOptions().isTaxonomyColorizeNodeShapes() );
660         customizeJMenuItem( _cycle_node_shape_mi );
661         customizeJMenuItem( _cycle_node_fill_mi );
662         customizeJMenuItem( _choose_node_size_mi );
663         customizeCheckBoxMenuItem( _color_labels_same_as_parent_branch, getOptions().isColorLabelsSameAsParentBranch() );
664         customizeCheckBoxMenuItem( _search_case_senstive_cbmi, getOptions().isSearchCaseSensitive() );
665         customizeCheckBoxMenuItem( _show_scale_cbmi, getOptions().isShowScale() );
666         customizeRadioButtonMenuItem( _non_lined_up_cladograms_rbmi,
667                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.NON_LINED_UP );
668         customizeRadioButtonMenuItem( _uniform_cladograms_rbmi,
669                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.TOTAL_NODE_SUM_DEP );
670         customizeRadioButtonMenuItem( _ext_node_dependent_cladogram_rbmi,
671                                       getOptions().getCladogramType() == CLADOGRAM_TYPE.EXT_NODE_SUM_DEP );
672         customizeCheckBoxMenuItem( _show_branch_length_values_cbmi, getOptions().isShowBranchLengthValues() );
673         customizeCheckBoxMenuItem( _show_overview_cbmi, getOptions().isShowOverview() );
674         customizeCheckBoxMenuItem( _search_whole_words_only_cbmi, getOptions().isMatchWholeTermsOnly() );
675         customizeCheckBoxMenuItem( _inverse_search_result_cbmi, getOptions().isInverseSearchResult() );
676         customizeCheckBoxMenuItem( _show_confidence_stddev_cbmi, getOptions().isShowConfidenceStddev() );
677         _jmenubar.add( _options_jmenu );
678     }
679
680     void buildToolsMenu() {
681         _tools_menu = MainFrame.createMenu( "Tools", getConfiguration() );
682         _tools_menu.add( _confcolor_item = new JMenuItem( "Colorize Branches Depending on Confidence" ) );
683         customizeJMenuItem( _confcolor_item );
684         _tools_menu.add( _taxcolor_item = new JMenuItem( "Taxonomy Colorize Branches" ) );
685         customizeJMenuItem( _taxcolor_item );
686         _tools_menu.add( _remove_branch_color_item = new JMenuItem( "Delete Branch Colors" ) );
687         _remove_branch_color_item.setToolTipText( "To delete branch color values from the current phylogeny." );
688         customizeJMenuItem( _remove_branch_color_item );
689         _tools_menu.addSeparator();
690         _tools_menu.add( _midpoint_root_item = new JMenuItem( "Midpoint-Root" ) );
691         customizeJMenuItem( _midpoint_root_item );
692         _tools_menu.addSeparator();
693         _tools_menu
694                 .add( _infer_common_sn_names_item = new JMenuItem( "Infer Common Parts of Internal Scientific Names" ) );
695         customizeJMenuItem( _infer_common_sn_names_item );
696         _tools_menu.add( _collapse_species_specific_subtrees = new JMenuItem( "Collapse Species-Specific Subtrees" ) );
697         customizeJMenuItem( _collapse_species_specific_subtrees );
698         _jmenubar.add( _tools_menu );
699     }
700
701     void buildTypeMenu() {
702         _type_menu = MainFrame.createMenu( MainFrame.TYPE_MENU_HEADER, getConfiguration() );
703         _type_menu.add( _rectangular_type_cbmi = new JCheckBoxMenuItem( MainFrame.RECTANGULAR_TYPE_CBMI_LABEL ) );
704         _type_menu.add( _euro_type_cbmi = new JCheckBoxMenuItem( MainFrame.EURO_TYPE_CBMI_LABEL ) );
705         _type_menu.add( _rounded_type_cbmi = new JCheckBoxMenuItem( MainFrame.ROUNDED_TYPE_CBMI_LABEL ) );
706         _type_menu.add( _curved_type_cbmi = new JCheckBoxMenuItem( MainFrame.CURVED_TYPE_CBMI_LABEL ) );
707         _type_menu.add( _triangular_type_cbmi = new JCheckBoxMenuItem( MainFrame.TRIANGULAR_TYPE_CBMI_LABEL ) );
708         _type_menu.add( _convex_type_cbmi = new JCheckBoxMenuItem( MainFrame.CONVEX_TYPE_CBMI_LABEL ) );
709         _type_menu.add( _unrooted_type_cbmi = new JCheckBoxMenuItem( MainFrame.UNROOTED_TYPE_CBMI_LABEL ) );
710         _type_menu.add( _circular_type_cbmi = new JCheckBoxMenuItem( MainFrame.CIRCULAR_TYPE_CBMI_LABEL ) );
711         customizeCheckBoxMenuItem( _rectangular_type_cbmi, false );
712         customizeCheckBoxMenuItem( _triangular_type_cbmi, false );
713         customizeCheckBoxMenuItem( _euro_type_cbmi, false );
714         customizeCheckBoxMenuItem( _rounded_type_cbmi, false );
715         customizeCheckBoxMenuItem( _curved_type_cbmi, false );
716         customizeCheckBoxMenuItem( _convex_type_cbmi, false );
717         customizeCheckBoxMenuItem( _unrooted_type_cbmi, false );
718         customizeCheckBoxMenuItem( _circular_type_cbmi, false );
719         _unrooted_type_cbmi.setToolTipText( MainFrame.USE_MOUSEWHEEL_SHIFT_TO_ROTATE );
720         _circular_type_cbmi.setToolTipText( MainFrame.USE_MOUSEWHEEL_SHIFT_TO_ROTATE );
721         initializeTypeMenu( getOptions() );
722         _jmenubar.add( _type_menu );
723     }
724
725     void buildViewMenu() {
726         _view_jmenu = MainFrame.createMenu( "View", getConfiguration() );
727         _view_jmenu.add( _display_basic_information_item = new JMenuItem( "Display Basic Information" ) );
728         _view_jmenu.addSeparator();
729         _view_jmenu.add( _view_as_XML_item = new JMenuItem( "View as phyloXML" ) );
730         _view_jmenu.add( _view_as_NH_item = new JMenuItem( "View as Newick" ) );
731         _view_jmenu.add( _view_as_NHX_item = new JMenuItem( "View as NHX" ) );
732         _view_jmenu.add( _view_as_nexus_item = new JMenuItem( "View as Nexus" ) );
733         customizeJMenuItem( _display_basic_information_item );
734         customizeJMenuItem( _view_as_NH_item );
735         customizeJMenuItem( _view_as_NHX_item );
736         customizeJMenuItem( _view_as_XML_item );
737         customizeJMenuItem( _view_as_nexus_item );
738         _jmenubar.add( _view_jmenu );
739     }
740
741     void checkTextFrames() {
742         if ( _textframes.size() > 5 ) {
743             try {
744                 if ( _textframes.getFirst() != null ) {
745                     _textframes.getFirst().removeMe();
746                 }
747                 else {
748                     _textframes.removeFirst();
749                 }
750             }
751             catch ( final NoSuchElementException e ) {
752                 // Ignore.
753             }
754         }
755     }
756
757     void customizeCheckBoxMenuItem( final JCheckBoxMenuItem item, final boolean is_selected ) {
758         if ( item != null ) {
759             item.setFont( MainFrame.menu_font );
760             if ( !getConfiguration().isUseNativeUI() ) {
761                 item.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
762                 item.setForeground( getConfiguration().getGuiMenuTextColor() );
763             }
764             item.setSelected( is_selected );
765             item.addActionListener( this );
766         }
767     }
768
769     void customizeJMenuItem( final JMenuItem jmi ) {
770         jmi.setFont( MainFrame.menu_font );
771         if ( !getConfiguration().isUseNativeUI() ) {
772             jmi.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
773             jmi.setForeground( getConfiguration().getGuiMenuTextColor() );
774         }
775         jmi.addActionListener( this );
776     }
777
778     void displayBasicInformation() {
779         if ( ( getMainPanel().getCurrentPhylogeny() != null ) && !getMainPanel().getCurrentPhylogeny().isEmpty() ) {
780             String title = "Basic Information";
781             if ( !ForesterUtil.isEmpty( getMainPanel().getCurrentPhylogeny().getName() ) ) {
782                 title = getMainPanel().getCurrentPhylogeny().getName() + " " + title;
783             }
784             showTextFrame( AptxUtil.createBasicInformation( getMainPanel().getCurrentPhylogeny() ), title );
785         }
786     }
787
788     Configuration getConfiguration() {
789         return _configuration;
790     }
791
792     TreePanel getCurrentTreePanel() {
793         return getMainPanel().getCurrentTreePanel();
794     }
795
796     JCheckBoxMenuItem getlabelDirectionCbmi() {
797         return _label_direction_cbmi;
798     }
799
800     Options getOtions() {
801         return _options;
802     }
803
804     void help( final Map<String, WebLink> weblinks ) {
805         final StringBuilder sb = new StringBuilder();
806         sb.append( "Display options\n" );
807         sb.append( "-------------------\n" );
808         sb.append( "Use the checkboxes to select types of information to display on the tree.\n\n" );
809         sb.append( "Clickable tree nodes\n" );
810         sb.append( "--------------------\n" );
811         sb.append( "Tree nodes can be clicked, the action is determined by the 'click on node to' menu\n" );
812         sb.append( "or by right clicking:\n" );
813         sb.append( "o  Display Node Data -- display information for a node\n" );
814         sb.append( "o  Collapse/Uncollapse -- collapse and uncollapse subtree from clicked node\n" );
815         sb.append( "o  Root/Reroot -- change tree root to clicked node\n" );
816         sb.append( "o  Sub/Super Tree -- toggle between subtree from clicked node and whole tree\n" );
817         sb.append( "o  Swap Descendants -- switch descendant on either side of clicked node\n" );
818         sb.append( "o  Colorize Subtree -- color a subtree\n" );
819         sb.append( "o  Open Sequence Web -- launch a web browser to display sequence information\n" );
820         sb.append( "o  Open Taxonomy Web -- launch a web browser to display taxonomy information\n" );
821         sb.append( "-  there may be additional choices depending on this particular setup\n\n" );
822         sb.append( "Right clicking on a node always displays the information of a node.\n\n" );
823         sb.append( "Zooming\n" );
824         sb.append( "---------\n" );
825         sb.append( "The mouse wheel and the plus and minus keys control zooming.\n" );
826         sb.append( "Mouse wheel+Ctrl changes the text size.\n" );
827         sb.append( "Mouse wheel+Shift controls zooming in vertical direction only.\n" );
828         sb.append( "Use the buttons on the control panel to zoom the tree in and out, horizontally or vertically.\n" );
829         sb.append( "The entire tree can be fitted into the window by clicking the \"F\" button, or by pressing F, Delete, or Home.\n" );
830         sb.append( "The up, down, left, and right keys can be used to move the visible part (if zoomed in).\n" );
831         sb.append( "Up, down, left, and right+Shift can be used to control zooming horizontally and vertically.\n" );
832         sb.append( "Plus and minus keys+Ctrl change the text size; F+Ctrl, Delete+Ctrl, or Home+Ctrl resets it.\n\n" );
833         sb.append( "Quick tree manipulation:\n" );
834         sb.append( "------------------------\n" );
835         sb.append( "Order Subtrees -- order the tree by branch length\n" );
836         sb.append( "Uncollapse All -- uncollapse any and all collapsed branches\n\n" );
837         sb.append( "Memory problems (Java heap space error)\n" );
838         sb.append( "---------------------------------------\n" );
839         sb.append( "Since the Java default memory allocation is quite small, it might by necessary (for trees\n" );
840         sb.append( "with more than approximately 5000 external nodes) to increase the memory which Java can use, with\n" );
841         sb.append( "the '-Xmx' Java command line option. For example:\n" );
842         sb.append( "java -Xms32m -Xmx256m -cp path\\to\\forester.jar org.forester.archaeopteryx.Archaeopteryx\n\n" );
843         if ( ( weblinks != null ) && ( weblinks.size() > 0 ) ) {
844             sb.append( "Active web links\n" );
845             sb.append( "--------------------\n" );
846             for( final String key : weblinks.keySet() ) {
847                 sb.append( " " + weblinks.get( key ).toString() + "\n" );
848             }
849         }
850         sb.append( "\n" );
851         sb.append( "phyloXML\n" );
852         sb.append( "-------------------\n" );
853         sb.append( "Reference: " + Constants.PHYLOXML_REFERENCE + "\n" );
854         sb.append( "Website: " + Constants.PHYLOXML_WEB_SITE + "\n" );
855         sb.append( "Version: " + ForesterConstants.PHYLO_XML_VERSION + "\n" );
856         sb.append( "\n" );
857         sb.append( "For more information: http://www.phylosoft.org/archaeopteryx/\n" );
858         sb.append( "Email: " + Constants.AUTHOR_EMAIL + "\n\n" );
859         TextFrame.instantiate( sb.toString(), "Help", _textframes );
860     }
861
862     void initializeTypeMenu( final Options options ) {
863         setTypeMenuToAllUnselected();
864         try {
865             switch ( options.getPhylogenyGraphicsType() ) {
866                 case CONVEX:
867                     _convex_type_cbmi.setSelected( true );
868                     break;
869                 case CURVED:
870                     _curved_type_cbmi.setSelected( true );
871                     break;
872                 case EURO_STYLE:
873                     _euro_type_cbmi.setSelected( true );
874                     break;
875                 case ROUNDED:
876                     _rounded_type_cbmi.setSelected( true );
877                     break;
878                 case TRIANGULAR:
879                     _triangular_type_cbmi.setSelected( true );
880                     break;
881                 case UNROOTED:
882                     _unrooted_type_cbmi.setSelected( true );
883                     break;
884                 case CIRCULAR:
885                     _circular_type_cbmi.setSelected( true );
886                     break;
887                 default:
888                     _rectangular_type_cbmi.setSelected( true );
889                     break;
890             }
891         }
892         catch ( final NullPointerException np ) {
893             // In all likelihood, this is caused by menu-less display.
894         }
895     }
896
897     void removeAllTextFrames() {
898         for( final TextFrame tf : _textframes ) {
899             if ( tf != null ) {
900                 tf.close();
901             }
902         }
903         _textframes.clear();
904     }
905
906     void setConfiguration( final Configuration configuration ) {
907         _configuration = configuration;
908     }
909
910     void setCurrentExternalNodesDataBuffer( final String s ) {
911         if ( !ForesterUtil.isEmpty( s ) ) {
912             _ext_node_data_buffer = s.trim();
913         }
914         else {
915             _ext_node_data_buffer = "";
916         }
917     }
918
919     void setOptions( final Options options ) {
920         _options = options;
921     }
922
923     void setSelectedTypeInTypeMenu( final PHYLOGENY_GRAPHICS_TYPE type ) {
924         setTypeMenuToAllUnselected();
925         try {
926             switch ( type ) {
927                 case CIRCULAR:
928                     _circular_type_cbmi.setSelected( true );
929                     break;
930                 case CONVEX:
931                     _convex_type_cbmi.setSelected( true );
932                     break;
933                 case CURVED:
934                     _curved_type_cbmi.setSelected( true );
935                     break;
936                 case EURO_STYLE:
937                     _euro_type_cbmi.setSelected( true );
938                     break;
939                 case ROUNDED:
940                     _rounded_type_cbmi.setSelected( true );
941                     break;
942                 case RECTANGULAR:
943                     _rectangular_type_cbmi.setSelected( true );
944                     break;
945                 case TRIANGULAR:
946                     _triangular_type_cbmi.setSelected( true );
947                     break;
948                 case UNROOTED:
949                     _unrooted_type_cbmi.setSelected( true );
950                     break;
951                 default:
952                     throw new IllegalArgumentException( "unknown type: " + type );
953             }
954         }
955         catch ( final NullPointerException np ) {
956             // In all likelihood, this is caused by menu-less display.
957         }
958     }
959
960     void setTypeMenuToAllUnselected() {
961         if ( _convex_type_cbmi != null ) {
962             _convex_type_cbmi.setSelected( false );
963         }
964         if ( _curved_type_cbmi != null ) {
965             _curved_type_cbmi.setSelected( false );
966         }
967         if ( _euro_type_cbmi != null ) {
968             _euro_type_cbmi.setSelected( false );
969         }
970         if ( _rounded_type_cbmi != null ) {
971             _rounded_type_cbmi.setSelected( false );
972         }
973         if ( _triangular_type_cbmi != null ) {
974             _triangular_type_cbmi.setSelected( false );
975         }
976         if ( _rectangular_type_cbmi != null ) {
977             _rectangular_type_cbmi.setSelected( false );
978         }
979         if ( _unrooted_type_cbmi != null ) {
980             _unrooted_type_cbmi.setSelected( false );
981         }
982         if ( _circular_type_cbmi != null ) {
983             _circular_type_cbmi.setSelected( false );
984         }
985     }
986
987     void switchColors() {
988         final TreeColorSet colorset = getMainPanel().getCurrentTreePanel().getTreeColorSet();
989         final ColorSchemeChooser csc = new ColorSchemeChooser( getMainPanel(), colorset );
990         csc.setVisible( true );
991         getMainPanel().setTreeColorSet( colorset );
992     }
993
994     void typeChanged( final Object o ) {
995         updateTypeCheckboxes( getOptions(), o );
996         updateOptions( getOptions() );
997         if ( getCurrentTreePanel() != null ) {
998             final PHYLOGENY_GRAPHICS_TYPE previous_type = getCurrentTreePanel().getPhylogenyGraphicsType();
999             final PHYLOGENY_GRAPHICS_TYPE new_type = getOptions().getPhylogenyGraphicsType();
1000             if ( ( ( previous_type == PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) && ( new_type != PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) )
1001                     || ( ( previous_type == PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) && ( new_type != PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) )
1002                     || ( ( previous_type != PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) && ( new_type == PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) )
1003                     || ( ( previous_type != PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) && ( new_type == PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) ) ) {
1004                 getCurrentTreePanel().getControlPanel().showWhole();
1005             }
1006             if ( getCurrentTreePanel().isPhyHasBranchLengths() && ( new_type != PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) ) {
1007                 getCurrentTreePanel().getControlPanel().setDrawPhylogramEnabled( true );
1008             }
1009             else {
1010                 getCurrentTreePanel().getControlPanel().setDrawPhylogramEnabled( false );
1011             }
1012             getCurrentTreePanel().setPhylogenyGraphicsType( getOptions().getPhylogenyGraphicsType() );
1013             MainFrame.updateScreenTextAntialias( getMainPanel().getTreePanels() );
1014         }
1015     }
1016
1017     void updateOptions( final Options options ) {
1018         options.setAntialiasScreen( ( _screen_antialias_cbmi != null ) && _screen_antialias_cbmi.isSelected() );
1019         options.setBackgroundColorGradient( ( _background_gradient_cbmi != null )
1020                 && _background_gradient_cbmi.isSelected() );
1021         options.setShowDomainLabels( ( _show_domain_labels != null ) && _show_domain_labels.isSelected() );
1022         options.setAbbreviateScientificTaxonNames( ( _abbreviate_scientific_names != null )
1023                 && _abbreviate_scientific_names.isSelected() );
1024         options.setColorLabelsSameAsParentBranch( ( _color_labels_same_as_parent_branch != null )
1025                 && _color_labels_same_as_parent_branch.isSelected() );
1026         options.setShowDefaultNodeShapes( ( _show_default_node_shapes_cbmi != null )
1027                 && _show_default_node_shapes_cbmi.isSelected() );
1028         options.setTaxonomyColorizeNodeShapes( ( _taxonomy_colorize_node_shapes_cbmi != null )
1029                 && _taxonomy_colorize_node_shapes_cbmi.isSelected() );
1030         if ( ( _non_lined_up_cladograms_rbmi != null ) && ( _non_lined_up_cladograms_rbmi.isSelected() ) ) {
1031             options.setCladogramType( CLADOGRAM_TYPE.NON_LINED_UP );
1032         }
1033         else if ( ( _uniform_cladograms_rbmi != null ) && ( _uniform_cladograms_rbmi.isSelected() ) ) {
1034             options.setCladogramType( CLADOGRAM_TYPE.TOTAL_NODE_SUM_DEP );
1035         }
1036         else if ( ( _ext_node_dependent_cladogram_rbmi != null ) && ( _ext_node_dependent_cladogram_rbmi.isSelected() ) ) {
1037             options.setCladogramType( CLADOGRAM_TYPE.EXT_NODE_SUM_DEP );
1038         }
1039         options.setSearchCaseSensitive( ( _search_case_senstive_cbmi != null )
1040                 && _search_case_senstive_cbmi.isSelected() );
1041         if ( ( _show_scale_cbmi != null ) && _show_scale_cbmi.isEnabled() ) {
1042             options.setShowScale( _show_scale_cbmi.isSelected() );
1043         }
1044         if ( _label_direction_cbmi != null ) {
1045             if ( _label_direction_cbmi.isSelected() ) {
1046                 options.setNodeLabelDirection( NODE_LABEL_DIRECTION.RADIAL );
1047             }
1048             else {
1049                 options.setNodeLabelDirection( NODE_LABEL_DIRECTION.HORIZONTAL );
1050             }
1051         }
1052         options.setShowOverview( ( _show_overview_cbmi != null ) && _show_overview_cbmi.isSelected() );
1053         options.setShowConfidenceStddev( ( _show_confidence_stddev_cbmi != null )
1054                 && _show_confidence_stddev_cbmi.isSelected() );
1055         if ( ( _show_branch_length_values_cbmi != null ) && _show_branch_length_values_cbmi.isEnabled() ) {
1056             options.setShowBranchLengthValues( _show_branch_length_values_cbmi.isSelected() );
1057         }
1058         options.setMatchWholeTermsOnly( ( _search_whole_words_only_cbmi != null )
1059                 && _search_whole_words_only_cbmi.isSelected() );
1060         options.setInverseSearchResult( ( _inverse_search_result_cbmi != null )
1061                 && _inverse_search_result_cbmi.isSelected() );
1062         if ( ( _rectangular_type_cbmi != null ) && _rectangular_type_cbmi.isSelected() ) {
1063             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
1064         }
1065         else if ( ( _triangular_type_cbmi != null ) && _triangular_type_cbmi.isSelected() ) {
1066             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.TRIANGULAR );
1067         }
1068         else if ( ( _curved_type_cbmi != null ) && _curved_type_cbmi.isSelected() ) {
1069             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.CURVED );
1070         }
1071         else if ( ( _convex_type_cbmi != null ) && _convex_type_cbmi.isSelected() ) {
1072             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.CONVEX );
1073         }
1074         else if ( ( _euro_type_cbmi != null ) && _euro_type_cbmi.isSelected() ) {
1075             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.EURO_STYLE );
1076         }
1077         else if ( ( _rounded_type_cbmi != null ) && _rounded_type_cbmi.isSelected() ) {
1078             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.ROUNDED );
1079         }
1080         else if ( ( _unrooted_type_cbmi != null ) && _unrooted_type_cbmi.isSelected() ) {
1081             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.UNROOTED );
1082         }
1083         else if ( ( _circular_type_cbmi != null ) && _circular_type_cbmi.isSelected() ) {
1084             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.CIRCULAR );
1085         }
1086     }
1087
1088     void updateTypeCheckboxes( final Options options, final Object o ) {
1089         setTypeMenuToAllUnselected();
1090         ( ( JCheckBoxMenuItem ) o ).setSelected( true );
1091     }
1092
1093     void viewAsNexus() {
1094         if ( ( getMainPanel().getCurrentPhylogeny() != null ) && !getMainPanel().getCurrentPhylogeny().isEmpty() ) {
1095             String title = "Nexus";
1096             if ( !ForesterUtil.isEmpty( getMainPanel().getCurrentPhylogeny().getName() ) ) {
1097                 title = getMainPanel().getCurrentPhylogeny().getName() + " " + title;
1098             }
1099             showTextFrame( getMainPanel().getCurrentPhylogeny().toNexus( getOptions()
1100                                    .getNhConversionSupportValueStyle() ),
1101                            title );
1102         }
1103     }
1104
1105     void viewAsNH() {
1106         if ( ( getMainPanel().getCurrentPhylogeny() != null ) && !getMainPanel().getCurrentPhylogeny().isEmpty() ) {
1107             String title = "New Hampshire";
1108             if ( !ForesterUtil.isEmpty( getMainPanel().getCurrentPhylogeny().getName() ) ) {
1109                 title = getMainPanel().getCurrentPhylogeny().getName() + " " + title;
1110             }
1111             showTextFrame( getMainPanel().getCurrentPhylogeny()
1112                                    .toNewHampshire( false, getOptions().getNhConversionSupportValueStyle() ),
1113                            title );
1114         }
1115     }
1116
1117     void viewAsNHX() {
1118         if ( ( getMainPanel().getCurrentPhylogeny() != null ) && !getMainPanel().getCurrentPhylogeny().isEmpty() ) {
1119             String title = "NHX";
1120             if ( !ForesterUtil.isEmpty( getMainPanel().getCurrentPhylogeny().getName() ) ) {
1121                 title = getMainPanel().getCurrentPhylogeny().getName() + " " + title;
1122             }
1123             showTextFrame( getMainPanel().getCurrentPhylogeny().toNewHampshireX(), title );
1124         }
1125     }
1126
1127     void viewAsXML() {
1128         if ( ( getMainPanel().getCurrentPhylogeny() != null ) && !getMainPanel().getCurrentPhylogeny().isEmpty() ) {
1129             String title = "phyloXML";
1130             if ( !ForesterUtil.isEmpty( getMainPanel().getCurrentPhylogeny().getName() ) ) {
1131                 title = getMainPanel().getCurrentPhylogeny().getName() + " " + title;
1132             }
1133             showTextFrame( getMainPanel().getCurrentPhylogeny().toPhyloXML( 0 ), title );
1134         }
1135     }
1136
1137     private void chooseFont() {
1138         final FontChooser fc = new FontChooser();
1139         fc.setFont( getMainPanel().getTreeFontSet().getLargeFont() );
1140         fc.showDialog( this, "Select the Base Font" );
1141         getMainPanel().getTreeFontSet().setBaseFont( fc.getFont() );
1142     }
1143
1144     private void chooseMinimalConfidence() {
1145         final String s = ( String ) JOptionPane
1146                 .showInputDialog( this,
1147                                   "Please the minimum for confidence values to be displayed.\n" + "[current value: "
1148                                           + getOptions().getMinConfidenceValue() + "]\n",
1149                                   "Minimal Confidence Value",
1150                                   JOptionPane.QUESTION_MESSAGE,
1151                                   null,
1152                                   null,
1153                                   getOptions().getMinConfidenceValue() );
1154         if ( !ForesterUtil.isEmpty( s ) ) {
1155             boolean success = true;
1156             double m = 0.0;
1157             final String m_str = s.trim();
1158             if ( !ForesterUtil.isEmpty( m_str ) ) {
1159                 try {
1160                     m = Double.parseDouble( m_str );
1161                 }
1162                 catch ( final Exception ex ) {
1163                     success = false;
1164                 }
1165             }
1166             else {
1167                 success = false;
1168             }
1169             if ( success && ( m >= 0.0 ) ) {
1170                 getOptions().setMinConfidenceValue( m );
1171             }
1172         }
1173     }
1174
1175     private void customizeRadioButtonMenuItem( final JRadioButtonMenuItem item, final boolean is_selected ) {
1176         if ( item != null ) {
1177             item.setFont( MainFrame.menu_font );
1178             if ( !getConfiguration().isUseNativeUI() ) {
1179                 item.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
1180                 item.setForeground( getConfiguration().getGuiMenuTextColor() );
1181             }
1182             item.setSelected( is_selected );
1183             item.addActionListener( this );
1184         }
1185     }
1186
1187     private MainPanel getMainPanel() {
1188         return _main_panel;
1189     }
1190
1191     private boolean isScreenAntialias() {
1192         return true;
1193     }
1194
1195     private void removeBranchColors() {
1196         if ( getMainPanel().getCurrentPhylogeny() != null ) {
1197             AptxUtil.removeBranchColors( getMainPanel().getCurrentPhylogeny() );
1198         }
1199     }
1200
1201     private void setMainPanel( final MainPanelApplets main_panel ) {
1202         _main_panel = main_panel;
1203     }
1204
1205     private void setupUI() {
1206         try {
1207             if ( getConfiguration().isUseNativeUI() ) {
1208                 UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
1209             }
1210             else {
1211                 UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
1212             }
1213         }
1214         catch ( final UnsupportedLookAndFeelException e ) {
1215             AptxUtil.dieWithSystemError( "UnsupportedLookAndFeelException: " + e.toString() );
1216         }
1217         catch ( final ClassNotFoundException e ) {
1218             AptxUtil.dieWithSystemError( "ClassNotFoundException: " + e.toString() );
1219         }
1220         catch ( final InstantiationException e ) {
1221             AptxUtil.dieWithSystemError( "InstantiationException: " + e.toString() );
1222         }
1223         catch ( final IllegalAccessException e ) {
1224             AptxUtil.dieWithSystemError( "IllegalAccessException: " + e.toString() );
1225         }
1226         catch ( final Exception e ) {
1227             AptxUtil.dieWithSystemError( e.toString() );
1228         }
1229     }
1230
1231     static void setupScreenTextAntialias( final List<TreePanel> treepanels, final boolean antialias ) {
1232         for( final TreePanel tree_panel : treepanels ) {
1233             tree_panel.setTextAntialias();
1234         }
1235     }
1236 }