// TabCollectionRenderer.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.tulsoft.tools.gui.SwingUtils;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

/**
 * 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 in a tabbed
 * pane. <p>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: TabCollectionRenderer.java,v 1.2 2006/02/20 05:51:11 senger Exp $
 */

public class TabCollectionRenderer
    extends CollectionRenderer {

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

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

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

	JPanel p = new JPanel (new GridBagLayout());
	JTabbedPane tabs = new JTabbedPane();

	Object[] collection = (Object[])data.getData();
	for (int i = 0; i < collection.length; i++) {
	    tabs.addTab (""+(i+1),
			 renderer.getComponent (new DataContainer (collection[i])));
	}
	p.add (tabs);
	SwingUtils.addComponent
	    (p, tabs,  0, 0, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.NORTHWEST, 1.0, 1.0);
	return p;
    }

}
