// Base64Image.java
//
// Created: February 2006
//
// This file is a component of the BioMoby project.
// Copyright Martin Senger (martin.senger@gmail.com).
//

package org.biomoby.service.dashboard.renderers;

import org.biomoby.shared.MobyException;
import org.biomoby.service.dashboard.data.DataContainer;

import org.apache.axis.encoding.Base64;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;

import java.io.File;

/**
 * Decode a byte array from a Base64 encoded Biomoby data types, and
 * convert the resulting byte array to an image. <p>
 *
 * It can handle several Biomoby data types containing encoded images,
 * but there is no automatic way (at least I do not know about any)
 * how to detect which Biomoby objects have images, and
 * where. Therefore, whenever a new Biomoby image data type appears,
 * somebody must change here {@link #canHandle} method. Note, however,
 * that you do not need to add anything here if a new Biomoby data
 * types just inherits from those that are already here - those will
 * be found. <p>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: Base64Image.java,v 1.3 2006/02/20 05:51:11 senger Exp $
 */

public class Base64Image
    extends AbstractMobyRenderer {

//    private static org.apache.commons.logging.Log log =
//	org.apache.commons.logging.LogFactory.getLog (Base64Image.class);

    /*********************************************************************
     *
     ********************************************************************/
    public Base64Image() {
        super ("Image", "images/image.gif");
    }

    /*********************************************************************
     *
     ********************************************************************/
    public Base64Image (String name, String iconName) {
        super (name, iconName);
    }

    /*********************************************************************
     *
     ********************************************************************/
    public boolean canHandle (String criterion, Object value) {

	return (
		criterion.equals (MOBY_TYPE) &&
		isSubclassOfKnown (value.toString())
		);
    }

    /*********************************************************************
     *
     ********************************************************************/
    public JComponent getComponent (DataContainer data)
	throws MobyException {

	// deal with collections
        if (data.getData().getClass().isArray())
	    return new TabCollectionRenderer (this).getComponent (data);

	// the real job
	try {
	    String encoded = traverseToString (data.getData());
	    byte[] imageData = Base64.decode (encoded);
            ImageIcon theImage = new ImageIcon (imageData);
 	    return new JScrollPane (new JLabel (theImage));

	} catch (Error e) {
	    throw new MobyException
		("Problem in renderer '" + getName() + "'.", e);
	}
    }

    /**************************************************************************
     *
     **************************************************************************/
    public boolean save2File (DataContainer data, File file)
	throws MobyException {

	// deal with collections
        if (data.getData().getClass().isArray())
	    return new TabCollectionRenderer (this).save2File (data, file);

	String encoded = traverseToString (data.getData());
	byte[] imageData = Base64.decode (encoded);
	return super.save2File (new DataContainer (imageData), file);
    }

}
