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