Allow to retrieve a snapshot of the edited phylogenetic tree using javascript code...
authorherve.menager@gmail.com <herve.menager@gmail.com@ca865154-3058-d1c3-3e42-d8f55a55bdbd>
Mon, 10 Oct 2011 10:02:57 +0000 (10:02 +0000)
committerherve.menager@gmail.com <herve.menager@gmail.com@ca865154-3058-d1c3-3e42-d8f55a55bdbd>
Mon, 10 Oct 2011 10:02:57 +0000 (10:02 +0000)
forester/.classpath
forester/java/build.xml
forester/java/resources/commons-codec-1.5.jar [new file with mode: 0644]
forester/java/src/org/forester/archaeopteryx/ArchaeopteryxE.java
forester/java/src/org/forester/archaeopteryx/Util.java

index 9899c02..8f3eef3 100644 (file)
@@ -3,5 +3,6 @@
        <classpathentry kind="src" path="java/src"/>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
        <classpathentry kind="lib" path="java/resources/itextpdf-5.1.0.jar"/>
+       <classpathentry kind="lib" path="java/resources/commons-codec-1.5.jar"/>
        <classpathentry kind="output" path="bin"/>
 </classpath>
index 93ad138..1492570 100644 (file)
@@ -5,6 +5,7 @@
        <property name="classes.dir" value="classes" />
        <property name="jars.dir" value="resources" />
        <property name="itextjar" value="${jars.dir}/itextpdf-5.1.0.jar" />
+       <property name="commonscodecjar" value="${jars.dir}/commons-codec-1.5.jar" />
        <property name="forester_jar" value="forester.jar" />
        <property name="archaeopteryx_applets_jar" value="archaeopteryx_applets.jar" />
 
@@ -33,7 +34,7 @@
 
        <target name="compile_applets_only" description="Compiles the Task" depends="clean">
                <mkdir dir="${classes.dir}" />
-               <javac source="1.5" target="1.5" debug="false" srcdir="${src.dir}" destdir="${classes.dir}" optimize="on" verbose="false" classpath="${itextjar}">
+               <javac source="1.5" target="1.5" debug="false" srcdir="${src.dir}" destdir="${classes.dir}" optimize="on" verbose="false" classpath="${itextjar};${commonscodecjar}">
                        <compilerarg value="-Xlint:deprecation" />
                </javac> 
        </target>
        </target>
 
        <target name="jar_applets_only" description="JARs the classes files" depends="compile_applets_only,copy_resources">
+               <unjar src="${commonscodecjar}" dest="${classes.dir}">
+        <patternset>
+            <include name="**/*.class" />
+            <include name="**/*.afm" />
+            <include name="**/*.txt" />
+            <exclude name="**/*Test*.class" />
+        </patternset>
+    </unjar>
                <jar level="9" destfile="${archaeopteryx_applets_jar}" basedir="${classes.dir}" excludes="org/forester/application/** 
                                  org/forester/development/**
                                  org/forester/evoinference/**
diff --git a/forester/java/resources/commons-codec-1.5.jar b/forester/java/resources/commons-codec-1.5.jar
new file mode 100644 (file)
index 0000000..e9013fe
Binary files /dev/null and b/forester/java/resources/commons-codec-1.5.jar differ
index 51f09b0..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,10 +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"
@@ -335,6 +338,39 @@ public class ArchaeopteryxE extends JApplet implements ActionListener {
         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" ) );
index f9b7064..9682e34 100644 (file)
@@ -32,6 +32,7 @@ import java.awt.GraphicsEnvironment;
 import java.awt.Rectangle;
 import java.awt.RenderingHints;
 import java.awt.image.BufferedImage;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
@@ -851,6 +852,71 @@ public final class Util {
         return msg;
     }
 
+    final static String writePhylogenyToGraphicsByteArrayOutputStream( final ByteArrayOutputStream baos,
+                                                                       int width,
+                                                                       int height,
+                                                                       final TreePanel tree_panel,
+                                                                       final ControlPanel ac,
+                                                                       final GraphicsExportType type,
+                                                                       final Options options ) throws IOException{
+        if ( !options.isGraphicsExportUsingActualSize() ) {
+            if ( options.isGraphicsExportVisibleOnly() ) {
+                throw new IllegalArgumentException( "cannot export visible rectangle only without exporting in actual size" );
+            }
+            tree_panel.setParametersForPainting( options.getPrintSizeX(), options.getPrintSizeY(), true );
+            tree_panel.resetPreferredSize();
+            tree_panel.repaint();
+        }
+        final RenderingHints rendering_hints = new RenderingHints( RenderingHints.KEY_RENDERING,
+                                                                   RenderingHints.VALUE_RENDER_QUALITY );
+        rendering_hints.put( RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY );
+        if ( options.isAntialiasPrint() ) {
+            rendering_hints.put( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON );
+            rendering_hints.put( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
+        }
+        else {
+            rendering_hints.put( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF );
+            rendering_hints.put( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF );
+        }
+        final Phylogeny phylogeny = tree_panel.getPhylogeny();
+        if ( ( phylogeny == null ) || phylogeny.isEmpty() ) {
+            return "";
+        }
+        Rectangle visible = null;
+        if ( !options.isGraphicsExportUsingActualSize() ) {
+            width = options.getPrintSizeX();
+            height = options.getPrintSizeY();
+        }
+        else if ( options.isGraphicsExportVisibleOnly() ) {
+            visible = tree_panel.getVisibleRect();
+            width = visible.width;
+            height = visible.height;
+        }
+        final BufferedImage buffered_img = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB );
+        Graphics2D g2d = buffered_img.createGraphics();
+        g2d.setRenderingHints( rendering_hints );
+        int x = 0;
+        int y = 0;
+        if ( options.isGraphicsExportVisibleOnly() ) {
+            g2d = ( Graphics2D ) g2d.create( -visible.x, -visible.y, visible.width, visible.height );
+            g2d.setClip( null );
+            x = visible.x;
+            y = visible.y;
+        }
+        tree_panel.paintPhylogeny( g2d, false, true, width, height, x, y );
+        ImageIO.write( buffered_img, type.toString(), baos );
+        g2d.dispose();
+        System.gc();
+        if ( !options.isGraphicsExportUsingActualSize() ) {
+            tree_panel.getMainPanel().getControlPanel().showWhole();
+        }
+        String msg = baos.toString();
+        if ( ( width > 0 ) && ( height > 0 ) ) {
+            msg += " [size: " + width + ", " + height + "]";
+        }
+        return msg;
+    }
+    
     final static void writeToTiff( final File file, final BufferedImage image ) throws IOException {
         // See: http://log.robmeek.com/2005/08/write-tiff-in-java.html
         ImageWriter writer = null;