8755c6720189197086dff02a29961790b431255e
[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, getConfiguration().isValidatePhyloXmlAgainstSchema() );
791             }
792             catch ( final Exception e ) {
793                 ForesterUtil.printErrorMessage( NAME, e.toString() );
794                 e.printStackTrace();
795                 JOptionPane.showMessageDialog( this,
796                                                NAME + ": Failed to read phylogenies: " + "\nException: " + e,
797                                                "Failed to read phylogenies",
798                                                JOptionPane.ERROR_MESSAGE );
799             }
800         }
801         if ( ( phys == null ) || ( phys.length < 1 ) ) {
802             ForesterUtil.printErrorMessage( NAME, "phylogenies from [" + phys_url + "] are null or empty" );
803             JOptionPane.showMessageDialog( this,
804                                            NAME + ": phylogenies from [" + phys_url + "] are null or empty",
805                                            "Failed to read phylogenies",
806                                            JOptionPane.ERROR_MESSAGE );
807             return;
808         }
809         else {
810             AptxUtil.printAppletMessage( NAME, "loaded " + phys.length + " phylogenies from: " + phys_url );
811         }
812         setVisible( false );
813         setMainPanel( new MainPanelApplets( getConfiguration(), this ) );
814         _jmenubar = new JMenuBar();
815         if ( !getConfiguration().isHideControlPanelAndMenubar() ) {
816             if ( !getConfiguration().isUseNativeUI() ) {
817                 _jmenubar.setBackground( getConfiguration().getGuiMenuBackgroundColor() );
818             }
819             buildToolsMenu();
820             buildViewMenu();
821             buildFontSizeMenu();
822             buildOptionsMenu();
823             buildTypeMenu();
824             buildHelpMenu();
825             setJMenuBar( _jmenubar );
826         }
827         final Container contentpane = getContentPane();
828         contentpane.setLayout( new BorderLayout() );
829         contentpane.add( getMainPanel(), BorderLayout.CENTER );
830         addComponentListener( new ComponentAdapter() {
831
832             @Override
833             public void componentResized( final ComponentEvent e ) {
834                 if ( getMainPanel().getCurrentTreePanel() != null ) {
835                     getMainPanel().getCurrentTreePanel().setParametersForPainting( getMainPanel().getCurrentTreePanel()
836                                                                                            .getWidth(),
837                                                                                    getMainPanel().getCurrentTreePanel()
838                                                                                            .getHeight(),
839                                                                                    false );
840                 }
841             }
842         } );
843         if ( getConfiguration().isUseTabbedDisplay() ) {
844             AptxUtil.printAppletMessage( NAME, "using tabbed display" );
845             AptxUtil.addPhylogeniesToTabs( phys,
846                                            new File( phys_url.getFile() ).getName(),
847                                            phys_url.toString(),
848                                            getConfiguration(),
849                                            getMainPanel() );
850         }
851         else {
852             AptxUtil.printAppletMessage( NAME, "not using tabbed display" );
853             AptxUtil.addPhylogenyToPanel( phys, getConfiguration(), getMainPanel() );
854         }
855         validate();
856         setName( NAME );
857         getMainPanel().getControlPanel().showWholeAll();
858         getMainPanel().getControlPanel().showWhole();
859         System.gc();
860         AptxUtil.printAppletMessage( NAME, "successfully initialized" );
861         /* GUILHEM_BEG */
862         getCurrentTreePanel().getControlPanel().getSequenceRelationTypeBox().removeAllItems();
863         for( final SequenceRelation.SEQUENCE_RELATION_TYPE type : getMainPanel().getCurrentPhylogeny()
864                 .getRelevantSequenceRelationTypes() ) {
865             getCurrentTreePanel().getControlPanel().getSequenceRelationTypeBox().addItem( type );
866         }
867         final String default_relation = getParameter( Constants.APPLET_PARAM_NAME_FOR_DEFAULT_SEQUENCE_RELATION_TYPE );
868         if ( default_relation != null ) {
869             getCurrentTreePanel().getControlPanel().getSequenceRelationTypeBox().setSelectedItem( default_relation );
870         }
871         final String default_sequence = getParameter( Constants.APPLET_PARAM_NAME_FOR_DEFAULT_QUERY_SEQUENCE );
872         if ( default_sequence != null ) {
873             getCurrentTreePanel().getControlPanel().getSequenceRelationBox().setSelectedItem( default_sequence );
874             /* GUILHEM_END */
875         }
876         setVisible( true );
877     }
878
879     void initializeTypeMenu( final Options options ) {
880         setTypeMenuToAllUnselected();
881         try {
882             switch ( options.getPhylogenyGraphicsType() ) {
883                 case CONVEX:
884                     _convex_type_cbmi.setSelected( true );
885                     break;
886                 case CURVED:
887                     _curved_type_cbmi.setSelected( true );
888                     break;
889                 case EURO_STYLE:
890                     _euro_type_cbmi.setSelected( true );
891                     break;
892                 case ROUNDED:
893                     _rounded_type_cbmi.setSelected( true );
894                     break;
895                 case TRIANGULAR:
896                     _triangular_type_cbmi.setSelected( true );
897                     break;
898                 case UNROOTED:
899                     _unrooted_type_cbmi.setSelected( true );
900                     break;
901                 case CIRCULAR:
902                     _circular_type_cbmi.setSelected( true );
903                     break;
904                 default:
905                     _rectangular_type_cbmi.setSelected( true );
906                     break;
907             }
908         }
909         catch ( final NullPointerException np ) {
910             // In all likelihood, this is caused by menu-less display.
911         }
912     }
913
914     private boolean isScreenAntialias() {
915         return true;
916     }
917
918     private void removeBranchColors() {
919         if ( getMainPanel().getCurrentPhylogeny() != null ) {
920             AptxUtil.removeBranchColors( getMainPanel().getCurrentPhylogeny() );
921         }
922     }
923
924     void setConfiguration( final Configuration configuration ) {
925         _configuration = configuration;
926     }
927
928     private void setMainPanel( final MainPanelApplets main_panel ) {
929         _main_panel = main_panel;
930     }
931
932     void setOptions( final Options options ) {
933         _options = options;
934     }
935
936     void setSelectedTypeInTypeMenu( final PHYLOGENY_GRAPHICS_TYPE type ) {
937         setTypeMenuToAllUnselected();
938         try {
939             switch ( type ) {
940                 case CIRCULAR:
941                     _circular_type_cbmi.setSelected( true );
942                     break;
943                 case CONVEX:
944                     _convex_type_cbmi.setSelected( true );
945                     break;
946                 case CURVED:
947                     _curved_type_cbmi.setSelected( true );
948                     break;
949                 case EURO_STYLE:
950                     _euro_type_cbmi.setSelected( true );
951                     break;
952                 case ROUNDED:
953                     _rounded_type_cbmi.setSelected( true );
954                     break;
955                 case RECTANGULAR:
956                     _rectangular_type_cbmi.setSelected( true );
957                     break;
958                 case TRIANGULAR:
959                     _triangular_type_cbmi.setSelected( true );
960                     break;
961                 case UNROOTED:
962                     _unrooted_type_cbmi.setSelected( true );
963                     break;
964                 default:
965                     throw new IllegalArgumentException( "unknown type: " + type );
966             }
967         }
968         catch ( final NullPointerException np ) {
969             // In all likelihood, this is caused by menu-less display.
970         }
971     }
972
973     void setTypeMenuToAllUnselected() {
974         if ( _convex_type_cbmi != null ) {
975             _convex_type_cbmi.setSelected( false );
976         }
977         if ( _curved_type_cbmi != null ) {
978             _curved_type_cbmi.setSelected( false );
979         }
980         if ( _euro_type_cbmi != null ) {
981             _euro_type_cbmi.setSelected( false );
982         }
983         if ( _rounded_type_cbmi != null ) {
984             _rounded_type_cbmi.setSelected( false );
985         }
986         if ( _triangular_type_cbmi != null ) {
987             _triangular_type_cbmi.setSelected( false );
988         }
989         if ( _rectangular_type_cbmi != null ) {
990             _rectangular_type_cbmi.setSelected( false );
991         }
992         if ( _unrooted_type_cbmi != null ) {
993             _unrooted_type_cbmi.setSelected( false );
994         }
995         if ( _circular_type_cbmi != null ) {
996             _circular_type_cbmi.setSelected( false );
997         }
998     }
999
1000     private void setupUI() {
1001         try {
1002             if ( getConfiguration().isUseNativeUI() ) {
1003                 UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
1004             }
1005             else {
1006                 UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
1007             }
1008         }
1009         catch ( final UnsupportedLookAndFeelException e ) {
1010             AptxUtil.dieWithSystemError( "UnsupportedLookAndFeelException: " + e.toString() );
1011         }
1012         catch ( final ClassNotFoundException e ) {
1013             AptxUtil.dieWithSystemError( "ClassNotFoundException: " + e.toString() );
1014         }
1015         catch ( final InstantiationException e ) {
1016             AptxUtil.dieWithSystemError( "InstantiationException: " + e.toString() );
1017         }
1018         catch ( final IllegalAccessException e ) {
1019             AptxUtil.dieWithSystemError( "IllegalAccessException: " + e.toString() );
1020         }
1021         catch ( final Exception e ) {
1022             AptxUtil.dieWithSystemError( e.toString() );
1023         }
1024     }
1025
1026     @Override
1027     public void start() {
1028         if ( getMainPanel() != null ) {
1029             getMainPanel().validate();
1030         }
1031         requestFocus();
1032         requestFocusInWindow();
1033         requestFocus();
1034         AptxUtil.printAppletMessage( NAME, "started" );
1035     }
1036
1037     void switchColors() {
1038         final TreeColorSet colorset = getMainPanel().getCurrentTreePanel().getTreeColorSet();
1039         final ColorSchemeChooser csc = new ColorSchemeChooser( getMainPanel(), colorset );
1040         csc.setVisible( true );
1041         getMainPanel().setTreeColorSet( colorset );
1042     }
1043
1044     void typeChanged( final Object o ) {
1045         updateTypeCheckboxes( getOptions(), o );
1046         updateOptions( getOptions() );
1047         if ( getCurrentTreePanel() != null ) {
1048             final PHYLOGENY_GRAPHICS_TYPE previous_type = getCurrentTreePanel().getPhylogenyGraphicsType();
1049             final PHYLOGENY_GRAPHICS_TYPE new_type = getOptions().getPhylogenyGraphicsType();
1050             if ( ( ( previous_type == PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) && ( new_type != PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) )
1051                     || ( ( previous_type == PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) && ( new_type != PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) )
1052                     || ( ( previous_type != PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) && ( new_type == PHYLOGENY_GRAPHICS_TYPE.UNROOTED ) )
1053                     || ( ( previous_type != PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) && ( new_type == PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) ) ) {
1054                 getCurrentTreePanel().getControlPanel().showWhole();
1055             }
1056             if ( getCurrentTreePanel().isPhyHasBranchLengths() && ( new_type != PHYLOGENY_GRAPHICS_TYPE.CIRCULAR ) ) {
1057                 getCurrentTreePanel().getControlPanel().setDrawPhylogramEnabled( true );
1058             }
1059             else {
1060                 getCurrentTreePanel().getControlPanel().setDrawPhylogramEnabled( false );
1061             }
1062             getCurrentTreePanel().setPhylogenyGraphicsType( getOptions().getPhylogenyGraphicsType() );
1063             MainFrame.updateScreenTextAntialias( getMainPanel().getTreePanels() );
1064         }
1065     }
1066
1067     void updateOptions( final Options options ) {
1068         options.setAntialiasScreen( ( _screen_antialias_cbmi != null ) && _screen_antialias_cbmi.isSelected() );
1069         options.setBackgroundColorGradient( ( _background_gradient_cbmi != null )
1070                 && _background_gradient_cbmi.isSelected() );
1071         options.setShowDomainLabels( ( _show_domain_labels != null ) && _show_domain_labels.isSelected() );
1072         options.setAbbreviateScientificTaxonNames( ( _abbreviate_scientific_names != null )
1073                 && _abbreviate_scientific_names.isSelected() );
1074         options.setColorLabelsSameAsParentBranch( ( _color_labels_same_as_parent_branch != null )
1075                 && _color_labels_same_as_parent_branch.isSelected() );
1076         options.setShowDefaultNodeShapes( ( _show_default_node_shapes_cbmi != null )
1077                 && _show_default_node_shapes_cbmi.isSelected() );
1078         options.setTaxonomyColorizeNodeShapes( ( _taxonomy_colorize_node_shapes_cbmi != null )
1079                 && _taxonomy_colorize_node_shapes_cbmi.isSelected() );
1080         if ( ( _non_lined_up_cladograms_rbmi != null ) && ( _non_lined_up_cladograms_rbmi.isSelected() ) ) {
1081             options.setCladogramType( CLADOGRAM_TYPE.NON_LINED_UP );
1082         }
1083         else if ( ( _uniform_cladograms_rbmi != null ) && ( _uniform_cladograms_rbmi.isSelected() ) ) {
1084             options.setCladogramType( CLADOGRAM_TYPE.TOTAL_NODE_SUM_DEP );
1085         }
1086         else if ( ( _ext_node_dependent_cladogram_rbmi != null ) && ( _ext_node_dependent_cladogram_rbmi.isSelected() ) ) {
1087             options.setCladogramType( CLADOGRAM_TYPE.EXT_NODE_SUM_DEP );
1088         }
1089         options.setSearchCaseSensitive( ( _search_case_senstive_cbmi != null )
1090                 && _search_case_senstive_cbmi.isSelected() );
1091         if ( ( _show_scale_cbmi != null ) && _show_scale_cbmi.isEnabled() ) {
1092             options.setShowScale( _show_scale_cbmi.isSelected() );
1093         }
1094         if ( _label_direction_cbmi != null ) {
1095             if ( _label_direction_cbmi.isSelected() ) {
1096                 options.setNodeLabelDirection( NODE_LABEL_DIRECTION.RADIAL );
1097             }
1098             else {
1099                 options.setNodeLabelDirection( NODE_LABEL_DIRECTION.HORIZONTAL );
1100             }
1101         }
1102         options.setShowOverview( ( _show_overview_cbmi != null ) && _show_overview_cbmi.isSelected() );
1103         options.setShowConfidenceStddev( ( _show_confidence_stddev_cbmi != null )
1104                 && _show_confidence_stddev_cbmi.isSelected() );
1105         if ( ( _show_branch_length_values_cbmi != null ) && _show_branch_length_values_cbmi.isEnabled() ) {
1106             options.setShowBranchLengthValues( _show_branch_length_values_cbmi.isSelected() );
1107         }
1108         options.setMatchWholeTermsOnly( ( _search_whole_words_only_cbmi != null )
1109                 && _search_whole_words_only_cbmi.isSelected() );
1110         options.setInverseSearchResult( ( _inverse_search_result_cbmi != null )
1111                 && _inverse_search_result_cbmi.isSelected() );
1112         if ( ( _rectangular_type_cbmi != null ) && _rectangular_type_cbmi.isSelected() ) {
1113             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.RECTANGULAR );
1114         }
1115         else if ( ( _triangular_type_cbmi != null ) && _triangular_type_cbmi.isSelected() ) {
1116             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.TRIANGULAR );
1117         }
1118         else if ( ( _curved_type_cbmi != null ) && _curved_type_cbmi.isSelected() ) {
1119             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.CURVED );
1120         }
1121         else if ( ( _convex_type_cbmi != null ) && _convex_type_cbmi.isSelected() ) {
1122             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.CONVEX );
1123         }
1124         else if ( ( _euro_type_cbmi != null ) && _euro_type_cbmi.isSelected() ) {
1125             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.EURO_STYLE );
1126         }
1127         else if ( ( _rounded_type_cbmi != null ) && _rounded_type_cbmi.isSelected() ) {
1128             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.ROUNDED );
1129         }
1130         else if ( ( _unrooted_type_cbmi != null ) && _unrooted_type_cbmi.isSelected() ) {
1131             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.UNROOTED );
1132         }
1133         else if ( ( _circular_type_cbmi != null ) && _circular_type_cbmi.isSelected() ) {
1134             options.setPhylogenyGraphicsType( PHYLOGENY_GRAPHICS_TYPE.CIRCULAR );
1135         }
1136     }
1137
1138     void updateTypeCheckboxes( final Options options, final Object o ) {
1139         setTypeMenuToAllUnselected();
1140         ( ( JCheckBoxMenuItem ) o ).setSelected( true );
1141     }
1142
1143     void displayBasicInformation() {
1144         if ( ( getMainPanel().getCurrentPhylogeny() != null ) && !getMainPanel().getCurrentPhylogeny().isEmpty() ) {
1145             String title = "Basic Information";
1146             if ( !ForesterUtil.isEmpty( getMainPanel().getCurrentPhylogeny().getName() ) ) {
1147                 title = getMainPanel().getCurrentPhylogeny().getName() + " " + title;
1148             }
1149             showTextFrame( AptxUtil.createBasicInformation( getMainPanel().getCurrentPhylogeny() ), title );
1150         }
1151     }
1152
1153     void viewAsNexus() {
1154         if ( ( getMainPanel().getCurrentPhylogeny() != null ) && !getMainPanel().getCurrentPhylogeny().isEmpty() ) {
1155             String title = "Nexus";
1156             if ( !ForesterUtil.isEmpty( getMainPanel().getCurrentPhylogeny().getName() ) ) {
1157                 title = getMainPanel().getCurrentPhylogeny().getName() + " " + title;
1158             }
1159             showTextFrame( getMainPanel().getCurrentPhylogeny().toNexus( getOptions()
1160                                    .getNhConversionSupportValueStyle() ),
1161                            title );
1162         }
1163     }
1164
1165     void viewAsNH() {
1166         if ( ( getMainPanel().getCurrentPhylogeny() != null ) && !getMainPanel().getCurrentPhylogeny().isEmpty() ) {
1167             String title = "New Hampshire";
1168             if ( !ForesterUtil.isEmpty( getMainPanel().getCurrentPhylogeny().getName() ) ) {
1169                 title = getMainPanel().getCurrentPhylogeny().getName() + " " + title;
1170             }
1171             showTextFrame( getMainPanel().getCurrentPhylogeny()
1172                                    .toNewHampshire( false, getOptions().getNhConversionSupportValueStyle() ),
1173                            title );
1174         }
1175     }
1176
1177     void viewAsNHX() {
1178         if ( ( getMainPanel().getCurrentPhylogeny() != null ) && !getMainPanel().getCurrentPhylogeny().isEmpty() ) {
1179             String title = "NHX";
1180             if ( !ForesterUtil.isEmpty( getMainPanel().getCurrentPhylogeny().getName() ) ) {
1181                 title = getMainPanel().getCurrentPhylogeny().getName() + " " + title;
1182             }
1183             showTextFrame( getMainPanel().getCurrentPhylogeny().toNewHampshireX(), title );
1184         }
1185     }
1186
1187     void viewAsXML() {
1188         if ( ( getMainPanel().getCurrentPhylogeny() != null ) && !getMainPanel().getCurrentPhylogeny().isEmpty() ) {
1189             String title = "phyloXML";
1190             if ( !ForesterUtil.isEmpty( getMainPanel().getCurrentPhylogeny().getName() ) ) {
1191                 title = getMainPanel().getCurrentPhylogeny().getName() + " " + title;
1192             }
1193             showTextFrame( getMainPanel().getCurrentPhylogeny().toPhyloXML( 0 ), title );
1194         }
1195     }
1196
1197     public void showTextFrame( final String s, final String title ) {
1198         checkTextFrames();
1199         _textframes.addLast( TextFrame.instantiate( s, title, _textframes ) );
1200     }
1201
1202     void checkTextFrames() {
1203         if ( _textframes.size() > 5 ) {
1204             try {
1205                 if ( _textframes.getFirst() != null ) {
1206                     _textframes.getFirst().removeMe();
1207                 }
1208                 else {
1209                     _textframes.removeFirst();
1210                 }
1211             }
1212             catch ( final NoSuchElementException e ) {
1213                 // Ignore.
1214             }
1215         }
1216     }
1217
1218     void removeAllTextFrames() {
1219         for( final TextFrame tf : _textframes ) {
1220             if ( tf != null ) {
1221                 tf.close();
1222             }
1223         }
1224         _textframes.clear();
1225     }
1226
1227     static void setupScreenTextAntialias( final List<TreePanel> treepanels, final boolean antialias ) {
1228         for( final TreePanel tree_panel : treepanels ) {
1229             tree_panel.setTextAntialias();
1230         }
1231     }
1232 }