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