// BoxCollectionRenderer.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 javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.BoxLayout;

import java.awt.Component;

/**
 * This is not a normal renderer that is loaded and used through
 * SPI. It is explicitly called if a service result is a collection
 * but no renderer is here to process it as a collection, but there is
 * one or more renderer(s) that can process individual elements of
 * such collection. <p>
 *
 * Instance of this class gets such renderer and a whole collection,
 * and iterates over it - putting resulting components vertically
 * below each other. <p>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: BoxCollectionRenderer.java,v 1.2 2006/02/20 05:51:11 senger Exp $
 */

public class BoxCollectionRenderer
    extends CollectionRenderer {

    /*********************************************************************
     *
     ********************************************************************/
    public BoxCollectionRenderer (Renderer renderer) {
	super (renderer);
    }

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

	Object obj = data.getData();
	if (! obj.getClass().isArray())
	    throw new MobyException ("BoxCollectionRenderer not given a collection.");

	JPanel p = new JPanel();
	p.setLayout (new BoxLayout (p, BoxLayout.Y_AXIS));

	Object[] collection = (Object[])obj;
	for (int i = 0; i < collection.length; i++) {
	    JComponent comp =
		renderer.getComponent (new DataContainer (collection[i]));
	    comp.setAlignmentX (Component.LEFT_ALIGNMENT);
	    p.add (comp);
	}

	return p;
    }

}
