// PlainText.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.CommonConsole;
import org.biomoby.service.dashboard.data.DataContainer;

import javax.swing.JComponent;

/**
 * Display any String as a plain text in a text area. It includes
 * displaying raw, unformatted XML data. <p>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: PlainText.java,v 1.4 2006/02/20 05:51:11 senger Exp $
 */

public class PlainText
    extends AbstractRenderer {

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

    /*********************************************************************
     *
     ********************************************************************/
    public PlainText() {
        super ("Plain text", "images/plaintext.gif");
    }

    /*********************************************************************
     *
     ********************************************************************/
    protected PlainText (String name, String iconFileName) {
	super (name, iconFileName);
    }

    /*********************************************************************
     * It can handle Strings. <p>
     *
     * It can actually handle more - because it uses toString() method
     * on the incoming data - but it is not always the way we want to
     * present data, so this method is more conservative in saying
     * what it can handle. <p>
     ********************************************************************/
    public boolean canHandle (String criterion, Object value) {

	return (
	    ( criterion.equals (CLASS_NAME) &&
	      ((String)value).equals (String.class.getName()) )
	    ||
	    ( criterion.equals (CLASS) &&
	      (Class)value == (String.class) )
	    ||
	    ( criterion.equals (DATA_CONTAINER) &&
	      ((DataContainer)value).getData() instanceof String )
	    ||
	    ( criterion.equals (DATA) &&
	      (value instanceof String ) )
	    );
    }

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

	CommonConsole area = new CommonConsole() {
		public boolean hasCleanButton() { return false; }
		public boolean hasSaveButton() {return false; }
		public boolean hasAppendModeSwitcher() {return false; }
		public boolean hasVerboseModeSwitcher() {return false; }
	    };

	try {
	    if (data.getData().getClass().isArray()) {
		// I deal with collections myself here because I do
		// not want to have each String as a standalone
		// JComponent - as would CollectionRenderer did
		area.setAppendMode (true);
		Object[] coll = (Object[])data.getData();
		for (int i = 0; i < coll.length; i++)
		    area.setText (coll[i].toString());
	    } else {
		area.setText ( data.getData().toString() );
	    }
	} catch (Error e) {
	    throw new MobyException
		("Problem in renderer '" + getName() + "'.", e);
	}
	return area;
    }

}
