in progress
[jalview.git] / forester / java / src / org / forester / archaeopteryx / ArchaeopteryxE.java
index 9e483b7..7ba34f4 100644 (file)
@@ -7,6 +7,7 @@ import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.ComponentAdapter;
 import java.awt.event.ComponentEvent;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
 import java.net.URL;
@@ -25,11 +26,14 @@ import javax.swing.UnsupportedLookAndFeelException;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
 
+import org.apache.commons.codec.binary.Base64;
+import org.forester.archaeopteryx.AptxUtil.GraphicsExportType;
 import org.forester.archaeopteryx.Options.CLADOGRAM_TYPE;
 import org.forester.archaeopteryx.Options.NODE_LABEL_DIRECTION;
 import org.forester.archaeopteryx.Options.PHYLOGENY_GRAPHICS_TYPE;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.data.SequenceRelation;
+import org.forester.util.ForesterConstants;
 import org.forester.util.ForesterUtil;
 
 // Use like this:
@@ -98,7 +102,6 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
     private JMenuItem            _choose_font_mi;
     private JMenuItem            _switch_colors_mi;
     JCheckBoxMenuItem            _label_direction_cbmi;
-    private JCheckBoxMenuItem    _show_node_boxes_cbmi;
     private JCheckBoxMenuItem    _show_scale_cbmi;
     private JCheckBoxMenuItem    _search_case_senstive_cbmi;
     private JCheckBoxMenuItem    _search_whole_words_only_cbmi;
@@ -109,6 +112,12 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
     private JMenuItem            _collapse_species_specific_subtrees;
     private JMenuItem            _overview_placment_mi;
     private ButtonGroup          _radio_group_1;
+    private JCheckBoxMenuItem    _show_default_node_shapes_cbmi;
+    private JMenuItem            _cycle_node_shape_mi;
+    private JMenuItem            _cycle_node_fill_mi;
+    private JMenuItem            _choose_node_size_mi;
+    private JCheckBoxMenuItem    _taxonomy_colorize_node_shapes_cbmi;
+    private JCheckBoxMenuItem    _show_confidence_stddev_cbmi;
 
     @Override
     public void actionPerformed( final ActionEvent e ) {
@@ -186,11 +195,17 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         else if ( o == _choose_minimal_confidence_mi ) {
             chooseMinimalConfidence();
         }
+        else if ( o == _choose_node_size_mi ) {
+            MainFrame.chooseNodeSize( getOptions(), this );
+        }
         else if ( o == _overview_placment_mi ) {
             MainFrame.cycleOverview( getOptions(), getCurrentTreePanel() );
         }
-        else if ( o == _show_node_boxes_cbmi ) {
-            updateOptions( getOptions() );
+        else if ( o == _cycle_node_fill_mi ) {
+            MainFrame.cycleNodeFill( getOptions(), getCurrentTreePanel() );
+        }
+        else if ( o == _cycle_node_shape_mi ) {
+            MainFrame.cycleNodeShape( getOptions(), getCurrentTreePanel() );
         }
         else if ( o == _non_lined_up_cladograms_rbmi ) {
             updateOptions( getOptions() );
@@ -222,6 +237,9 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         else if ( o == _show_branch_length_values_cbmi ) {
             updateOptions( getOptions() );
         }
+        else if ( o == _show_confidence_stddev_cbmi ) {
+            updateOptions( getOptions() );
+        }
         else if ( o == _label_direction_cbmi ) {
             updateOptions( getOptions() );
         }
@@ -249,6 +267,12 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         else if ( o == _color_labels_same_as_parent_branch ) {
             updateOptions( getOptions() );
         }
+        else if ( o == _show_default_node_shapes_cbmi ) {
+            updateOptions( getOptions() );
+        }
+        else if ( o == _taxonomy_colorize_node_shapes_cbmi ) {
+            updateOptions( getOptions() );
+        }
         else if ( o == _about_item ) {
             MainFrame.about();
         }
@@ -257,7 +281,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         }
         else if ( o == _website_item ) {
             try {
-                Util.openWebsite( Constants.APTX_WEB_SITE, true, this );
+                AptxUtil.openWebsite( Constants.APTX_WEB_SITE, true, this );
             }
             catch ( final IOException e1 ) {
                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
@@ -265,7 +289,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         }
         else if ( o == _phyloxml_website_item ) {
             try {
-                Util.openWebsite( Constants.PHYLOXML_WEB_SITE, true, this );
+                AptxUtil.openWebsite( Constants.PHYLOXML_WEB_SITE, true, this );
             }
             catch ( final IOException e1 ) {
                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
@@ -273,7 +297,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         }
         else if ( o == _aptx_ref_item ) {
             try {
-                Util.openWebsite( Constants.APTX_REFERENCE_URL, true, this );
+                AptxUtil.openWebsite( Constants.APTX_REFERENCE_URL, true, this );
             }
             catch ( final IOException e1 ) {
                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
@@ -281,7 +305,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         }
         else if ( o == _phyloxml_ref_item ) {
             try {
-                Util.openWebsite( Constants.PHYLOXML_REFERENCE_URL, true, this );
+                AptxUtil.openWebsite( Constants.PHYLOXML_REFERENCE_URL, true, this );
             }
             catch ( final IOException e1 ) {
                 ForesterUtil.printErrorMessage( Constants.PRG_NAME, e1.toString() );
@@ -290,6 +314,68 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         repaint();
     }
 
+    /**
+     * This method returns the current phylogeny as a string in the chosen format
+     * 
+     * @param format must be NH, NHX, NEXUS or PHYLOXML
+     * @return the phylogeny string
+     * @author Herve Menager
+     */
+    public String getCurrentPhylogeny( final String format ) {
+        removeTextFrame();
+        if ( ( getMainPanel().getCurrentPhylogeny() == null ) || getMainPanel().getCurrentPhylogeny().isEmpty()
+                || ( getMainPanel().getCurrentPhylogeny().getNumberOfExternalNodes() > 10000 ) ) {
+            return new String();
+        }
+        switch ( ForesterConstants.PhylogeneticTreeFormats.valueOf( format ) ) {
+            case NH:
+                return getMainPanel().getCurrentPhylogeny().toNewHampshire();
+            case NHX:
+                return getMainPanel().getCurrentPhylogeny().toNewHampshireX();
+            case NEXUS:
+                return getMainPanel().getCurrentPhylogeny().toNexus();
+            case PHYLOXML:
+                return getMainPanel().getCurrentPhylogeny().toPhyloXML( -1 );
+            default:
+                break;
+        }
+        return new String();
+    }
+
+    /**
+     * This method returns a view of the current phylogeny in a chosen 
+     * graphics format, base64-encoded in a string so that in can be used
+     * from javascript.
+     * 
+     * @param format must be GraphicsExportType (gif, jpg, pdf, png, tif, bmp)
+     * @return the phylogeny string
+     * @author Herve Menager
+     */
+    public String getCurrentPhylogenyGraphicsAsBase64EncodedString( final String format ) {
+        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        try {
+            AptxUtil.writePhylogenyToGraphicsByteArrayOutputStream( baos,
+                                                                    _main_panel.getWidth(),
+                                                                    _main_panel.getHeight(),
+                                                                    getCurrentTreePanel(),
+                                                                    getCurrentTreePanel().getControlPanel(),
+                                                                    GraphicsExportType.valueOf( format ),
+                                                                    getOptions() );
+        }
+        catch ( final IOException ioe ) {
+            ForesterUtil.printErrorMessage( NAME, ioe.toString() );
+            ioe.printStackTrace();
+            JOptionPane.showMessageDialog( this,
+                                           NAME + ": Failed to generate graphics: " + "\nException: " + ioe,
+                                           "Failed to generate graphics",
+                                           JOptionPane.ERROR_MESSAGE );
+            return null;
+        }
+        final byte[] bytes = baos.toByteArray();
+        final String dataImg = Base64.encodeBase64String( bytes );
+        return dataImg;
+    }
+
     void buildFontSizeMenu() {
         _font_size_menu = MainFrame.createMenu( MainFrame.FONT_SIZE_MENU_LABEL, getConfiguration() );
         _font_size_menu.add( _super_tiny_fonts_mi = new JMenuItem( "Super tiny fonts" ) );
@@ -331,7 +417,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
 
             @Override
             public void stateChanged( final ChangeEvent e ) {
-                MainFrame.setOvPlacementColorChooseMenuItem( _overview_placment_mi, getCurrentTreePanel() );
+                MainFrame.setOvPlacementColorChooseMenuItem( _overview_placment_mi, getOptions() );
                 MainFrame.setTextColorChooseMenuItem( _switch_colors_mi, getCurrentTreePanel() );
                 MainFrame
                         .setTextMinSupportMenuItem( _choose_minimal_confidence_mi, getOptions(), getCurrentTreePanel() );
@@ -344,6 +430,9 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
                                                                      _uniform_cladograms_rbmi,
                                                                      _ext_node_dependent_cladogram_rbmi,
                                                                      _label_direction_cbmi );
+                MainFrame.setCycleNodeFillMenuItem( _cycle_node_fill_mi, getOptions() );
+                MainFrame.setCycleNodeShapeMenuItem( _cycle_node_shape_mi, getOptions() );
+                MainFrame.setTextNodeSizeMenuItem( _choose_node_size_mi, getOptions() );
             }
         } );
         _options_jmenu.add( MainFrame.customizeMenuItemAsLabel( new JMenuItem( MainFrame.DISPLAY_SUBHEADER ),
@@ -357,11 +446,18 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         _radio_group_1.add( _ext_node_dependent_cladogram_rbmi );
         _radio_group_1.add( _uniform_cladograms_rbmi );
         _radio_group_1.add( _non_lined_up_cladograms_rbmi );
-        _options_jmenu.add( _show_node_boxes_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_NODE_BOXES_LABEL ) );
+        _options_jmenu.add( _show_overview_cbmi = new JCheckBoxMenuItem( MainFrame.SHOW_OVERVIEW_LABEL ) );
         _options_jmenu.add( _show_scale_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_SCALE_LABEL ) );
         _options_jmenu
                 .add( _show_branch_length_values_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_BRANCH_LENGTH_VALUES_LABEL ) );
-        _options_jmenu.add( _show_overview_cbmi = new JCheckBoxMenuItem( MainFrame.SHOW_OVERVIEW_LABEL ) );
+        _options_jmenu.add( _show_confidence_stddev_cbmi = new JCheckBoxMenuItem( MainFrame.SHOW_CONF_STDDEV_LABEL ) );
+        _options_jmenu
+                .add( _show_default_node_shapes_cbmi = new JCheckBoxMenuItem( MainFrame.DISPLAY_NODE_BOXES_LABEL ) );
+        _options_jmenu
+                .add( _taxonomy_colorize_node_shapes_cbmi = new JCheckBoxMenuItem( MainFrame.TAXONOMY_COLORIZE_NODE_SHAPES_LABEL ) );
+        _options_jmenu.add( _cycle_node_shape_mi = new JMenuItem( MainFrame.CYCLE_NODE_SHAPE_LABEL ) );
+        _options_jmenu.add( _cycle_node_fill_mi = new JMenuItem( MainFrame.CYCLE_NODE_FILL_LABEL ) );
+        _options_jmenu.add( _choose_node_size_mi = new JMenuItem( MainFrame.CHOOSE_NODE_SIZE_LABEL ) );
         _options_jmenu.add( _label_direction_cbmi = new JCheckBoxMenuItem( MainFrame.LABEL_DIRECTION_LABEL ) );
         _options_jmenu
                 .add( _color_labels_same_as_parent_branch = new JCheckBoxMenuItem( MainFrame.COLOR_LABELS_LABEL ) );
@@ -389,13 +485,17 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         customizeJMenuItem( _choose_minimal_confidence_mi );
         customizeJMenuItem( _switch_colors_mi );
         customizeJMenuItem( _overview_placment_mi );
-        customizeCheckBoxMenuItem( _show_node_boxes_cbmi, getOptions().getDefaultNodeShape() );
         customizeCheckBoxMenuItem( _label_direction_cbmi,
                                    getOptions().getNodeLabelDirection() == NODE_LABEL_DIRECTION.RADIAL );
         customizeCheckBoxMenuItem( _screen_antialias_cbmi, getOptions().isAntialiasScreen() );
         customizeCheckBoxMenuItem( _background_gradient_cbmi, getOptions().isBackgroundColorGradient() );
         customizeCheckBoxMenuItem( _show_domain_labels, getOptions().isShowDomainLabels() );
         customizeCheckBoxMenuItem( _abbreviate_scientific_names, getOptions().isAbbreviateScientificTaxonNames() );
+        customizeCheckBoxMenuItem( _show_default_node_shapes_cbmi, getOptions().isShowDefaultNodeShapes() );
+        customizeCheckBoxMenuItem( _taxonomy_colorize_node_shapes_cbmi, getOptions().isTaxonomyColorizeNodeShapes() );
+        customizeJMenuItem( _cycle_node_shape_mi );
+        customizeJMenuItem( _cycle_node_fill_mi );
+        customizeJMenuItem( _choose_node_size_mi );
         customizeCheckBoxMenuItem( _color_labels_same_as_parent_branch, getOptions().isColorLabelsSameAsParentBranch() );
         customizeCheckBoxMenuItem( _search_case_senstive_cbmi, getOptions().isSearchCaseSensitive() );
         customizeCheckBoxMenuItem( _show_scale_cbmi, getOptions().isShowScale() );
@@ -409,6 +509,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         customizeCheckBoxMenuItem( _show_overview_cbmi, getOptions().isShowOverview() );
         customizeCheckBoxMenuItem( _search_whole_words_only_cbmi, getOptions().isMatchWholeTermsOnly() );
         customizeCheckBoxMenuItem( _inverse_search_result_cbmi, getOptions().isInverseSearchResult() );
+        customizeCheckBoxMenuItem( _show_confidence_stddev_cbmi, getOptions().isShowConfidenceStddev() );
         _jmenubar.add( _options_jmenu );
     }
 
@@ -543,7 +644,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
 
     @Override
     public void destroy() {
-        Util.printAppletMessage( NAME, "going to be destroyed " );
+        AptxUtil.printAppletMessage( NAME, "going to be destroyed " );
         removeTextFrame();
         if ( getMainPanel() != null ) {
             getMainPanel().terminate();
@@ -577,15 +678,15 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
     @Override
     public void init() {
         final String config_filename = getParameter( Constants.APPLET_PARAM_NAME_FOR_CONFIG_FILE_URL );
-        Util.printAppletMessage( NAME, "URL for configuration file is: " + config_filename );
-        final Configuration configuration = new Configuration( config_filename, true, true );
+        AptxUtil.printAppletMessage( NAME, "URL for configuration file is: " + config_filename );
+        final Configuration configuration = new Configuration( config_filename, true, true, true );
         setConfiguration( configuration );
         setOptions( Options.createInstance( configuration ) );
         setupUI();
         URL phys_url = null;
         Phylogeny[] phys = null;
         final String phys_url_string = getParameter( Constants.APPLET_PARAM_NAME_FOR_URL_OF_TREE_TO_LOAD );
-        Util.printAppletMessage( NAME, "URL for phylogenies is " + phys_url_string );
+        AptxUtil.printAppletMessage( NAME, "URL for phylogenies is " + phys_url_string );
         // Get URL to tree file
         if ( phys_url_string != null ) {
             try {
@@ -601,7 +702,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         // Load the tree from URL
         if ( phys_url != null ) {
             try {
-                phys = Util.readPhylogeniesFromUrl( phys_url, getConfiguration().isValidatePhyloXmlAgainstSchema() );
+                phys = AptxUtil.readPhylogeniesFromUrl( phys_url, getConfiguration().isValidatePhyloXmlAgainstSchema() );
             }
             catch ( final Exception e ) {
                 ForesterUtil.printErrorMessage( NAME, e.toString() );
@@ -621,7 +722,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
             return;
         }
         else {
-            Util.printAppletMessage( NAME, "loaded " + phys.length + " phylogenies from: " + phys_url );
+            AptxUtil.printAppletMessage( NAME, "loaded " + phys.length + " phylogenies from: " + phys_url );
         }
         setVisible( false );
         setMainPanel( new MainPanelApplets( getConfiguration(), this ) );
@@ -655,23 +756,23 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
             }
         } );
         if ( getConfiguration().isUseTabbedDisplay() ) {
-            Util.printAppletMessage( NAME, "using tabbed display" );
-            Util.addPhylogeniesToTabs( phys,
-                                       new File( phys_url.getFile() ).getName(),
-                                       phys_url.toString(),
-                                       getConfiguration(),
-                                       getMainPanel() );
+            AptxUtil.printAppletMessage( NAME, "using tabbed display" );
+            AptxUtil.addPhylogeniesToTabs( phys,
+                                           new File( phys_url.getFile() ).getName(),
+                                           phys_url.toString(),
+                                           getConfiguration(),
+                                           getMainPanel() );
         }
         else {
-            Util.printAppletMessage( NAME, "not using tabbed display" );
-            Util.addPhylogenyToPanel( phys, getConfiguration(), getMainPanel() );
+            AptxUtil.printAppletMessage( NAME, "not using tabbed display" );
+            AptxUtil.addPhylogenyToPanel( phys, getConfiguration(), getMainPanel() );
         }
         validate();
         setName( NAME );
         getMainPanel().getControlPanel().showWholeAll();
         getMainPanel().getControlPanel().showWhole();
         System.gc();
-        Util.printAppletMessage( NAME, "successfully initialized" );
+        AptxUtil.printAppletMessage( NAME, "successfully initialized" );
         /* GUILHEM_BEG */
         getCurrentTreePanel().getControlPanel().getSequenceRelationTypeBox().removeAllItems();
         for( final SequenceRelation.SEQUENCE_RELATION_TYPE type : getMainPanel().getCurrentPhylogeny()
@@ -731,7 +832,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
 
     private void removeBranchColors() {
         if ( getMainPanel().getCurrentPhylogeny() != null ) {
-            Util.removeBranchColors( getMainPanel().getCurrentPhylogeny() );
+            AptxUtil.removeBranchColors( getMainPanel().getCurrentPhylogeny() );
         }
     }
 
@@ -828,19 +929,19 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
             }
         }
         catch ( final UnsupportedLookAndFeelException e ) {
-            Util.dieWithSystemError( "UnsupportedLookAndFeelException: " + e.toString() );
+            AptxUtil.dieWithSystemError( "UnsupportedLookAndFeelException: " + e.toString() );
         }
         catch ( final ClassNotFoundException e ) {
-            Util.dieWithSystemError( "ClassNotFoundException: " + e.toString() );
+            AptxUtil.dieWithSystemError( "ClassNotFoundException: " + e.toString() );
         }
         catch ( final InstantiationException e ) {
-            Util.dieWithSystemError( "InstantiationException: " + e.toString() );
+            AptxUtil.dieWithSystemError( "InstantiationException: " + e.toString() );
         }
         catch ( final IllegalAccessException e ) {
-            Util.dieWithSystemError( "IllegalAccessException: " + e.toString() );
+            AptxUtil.dieWithSystemError( "IllegalAccessException: " + e.toString() );
         }
         catch ( final Exception e ) {
-            Util.dieWithSystemError( e.toString() );
+            AptxUtil.dieWithSystemError( e.toString() );
         }
     }
 
@@ -852,7 +953,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         requestFocus();
         requestFocusInWindow();
         requestFocus();
-        Util.printAppletMessage( NAME, "started" );
+        AptxUtil.printAppletMessage( NAME, "started" );
     }
 
     void switchColors() {
@@ -894,7 +995,10 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
                 && _abbreviate_scientific_names.isSelected() );
         options.setColorLabelsSameAsParentBranch( ( _color_labels_same_as_parent_branch != null )
                 && _color_labels_same_as_parent_branch.isSelected() );
-        options.setShowNodeBoxes( ( _show_node_boxes_cbmi != null ) && _show_node_boxes_cbmi.isSelected() );
+        options.setShowDefaultNodeShapes( ( _show_default_node_shapes_cbmi != null )
+                && _show_default_node_shapes_cbmi.isSelected() );
+        options.setTaxonomyColorizeNodeShapes( ( _taxonomy_colorize_node_shapes_cbmi != null )
+                && _taxonomy_colorize_node_shapes_cbmi.isSelected() );
         if ( ( _non_lined_up_cladograms_rbmi != null ) && ( _non_lined_up_cladograms_rbmi.isSelected() ) ) {
             options.setCladogramType( CLADOGRAM_TYPE.NON_LINED_UP );
         }
@@ -918,6 +1022,8 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
             }
         }
         options.setShowOverview( ( _show_overview_cbmi != null ) && _show_overview_cbmi.isSelected() );
+        options.setShowConfidenceStddev( ( _show_confidence_stddev_cbmi != null )
+                && _show_confidence_stddev_cbmi.isSelected() );
         if ( ( _show_branch_length_values_cbmi != null ) && _show_branch_length_values_cbmi.isEnabled() ) {
             options.setShowBranchLengthValues( _show_branch_length_values_cbmi.isSelected() );
         }
@@ -971,7 +1077,7 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
                 || ( getMainPanel().getCurrentPhylogeny().getNumberOfExternalNodes() > 10000 ) ) {
             return;
         }
-        _textframe = TextFrame.instantiate( getMainPanel().getCurrentPhylogeny().toNewHampshire( false ) );
+        _textframe = TextFrame.instantiate( getMainPanel().getCurrentPhylogeny().toNewHampshire() );
     }
 
     void viewAsNHX() {