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