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