small clean-up.
[jalview.git] / forester / java / src / org / forester / archaeopteryx / ArchaeopteryxE.java
index 7dbed25..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"
@@ -306,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" ) );