small clean-up.
[jalview.git] / forester / java / src / org / forester / archaeopteryx / ArchaeopteryxE.java
index d6d842b..1793013 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;
@@ -28,9 +29,12 @@ import javax.swing.event.ChangeListener;
 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.archaeopteryx.Util.GraphicsExportType;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.data.SequenceRelation;
+import org.forester.util.ForesterConstants;
 import org.forester.util.ForesterUtil;
+import org.apache.commons.codec.binary.Base64;
 
 // Use like this:
 // <applet archive="forester.jar"
@@ -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,11 @@ 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;
 
     @Override
     public void actionPerformed( final ActionEvent e ) {
@@ -186,11 +194,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() );
@@ -249,6 +263,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();
         }
@@ -290,6 +310,67 @@ 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 ) {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        try{
+            Util.writePhylogenyToGraphicsByteArrayOutputStream( baos, 
+                                                                _main_panel.getWidth(),
+                                                                _main_panel.getHeight(),
+                                                                this.getCurrentTreePanel(),
+                                                                getCurrentTreePanel().getControlPanel(),
+                                                                GraphicsExportType.valueOf(format),
+                                                                getOptions());            
+        }catch(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;
+        }
+        byte[] bytes = baos.toByteArray();
+        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 +412,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 +425,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,7 +441,15 @@ 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_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( _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 ) );
@@ -389,13 +481,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().isShowNodeBoxes() );
         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() );
@@ -894,7 +990,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 );
         }