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