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

package org.biomoby.service.dashboard;

import java.util.Enumeration;
import java.util.Vector;
import java.util.Map;
import java.util.HashMap;

/**
 * A swing JTable that collects names of services that are going to be
 * deployed, and names of their implementtaion classes. <p>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: DeploymentTable.java,v 1.3 2005/12/20 20:00:41 senger Exp $
 */

public class DeploymentTable
    extends CommonDataTable {

    // indeces of the used columns
    public final static int COL_SERVICE   = DeploymentTableModel.COL_SERVICE;
    public final static int COL_CLASSNAME = DeploymentTableModel.COL_CLASSNAME;

    /*********************************************************************
     * Default constructor.
     ********************************************************************/
    public DeploymentTable() {
	super();
	tableModel = new DeploymentTableModel();
	setModel (tableModel);
	createItself();
    }

    /*********************************************************************
     * Add data representing one row to this table.
     ********************************************************************/
    public void addData (String serviceName, String className) {
	((DeploymentTableModel)tableModel).addData (serviceName, className);
    }

    /*********************************************************************
     * Return all data currently available in the table. The keys are
     * service names, the values are class names (representing classes
     * that will implement these services).
     ********************************************************************/
    public Map getData() {
	Vector v = tableModel.getData();
	Map result = new HashMap (v.size() + 1);
	for (Enumeration en = v.elements(); en.hasMoreElements(); ) {
	    TableDataBag bag = (TableDataBag)en.nextElement();
	    result.put (bag.serviceName, bag.className);
	}
	return result;
    }

    /*********************************************************************
     * Ignored.
     ********************************************************************/
    public void setData (org.biomoby.shared.MobyData[] newData) { }

    /**************************************************************************
     *
     * Container for data for each row. Just two strings...
     *
     **************************************************************************/
    protected class TableDataBag {
	public String serviceName = "";
	public String className = "";
	public TableDataBag (String serviceName, String className) {
	    this.serviceName = serviceName;
	    this.className = className;
	}
    }

    /**************************************************************************
     *
     * Data model for this table...
     *
     **************************************************************************/
    protected class DeploymentTableModel
	extends CommonDataTableModel {

	public final static int COL_SERVICE   = 1;
	public final static int COL_CLASSNAME = 2;

	public DeploymentTableModel() {
	    columnToolTips = new String[] {
		"Click in this column to remove service",
		"Add here services by selecting them in the service tree",
		"Click to edit service's implementation class name",
	    };
	    columnNames = new String[] {
		"Remove",
		"Service name",
		"Implemented by class"
	    };
	    columnClasses = new Class[] {
		Integer.class,
		String.class,
		String.class
	    };
	}

	/**************************************************************************
	 * Ignored.
	 **************************************************************************/
	public void setData (org.biomoby.shared.MobyData[] newData) { }

	/*********************************************************************
	 *
	 ********************************************************************/
	public void addData (String serviceName, String className) {

	    // ignore duplicates
	    for (Enumeration en = data.elements(); en.hasMoreElements(); ) {
		String sName = ((TableDataBag)en.nextElement()).serviceName;
		if (sName.equals (serviceName))
		    return;
	    }

	    TableDataBag bag = new TableDataBag (serviceName, className);
	    data.addElement (bag);
	    int rowCount = getRowCount();
	    fireTableRowsInserted (rowCount - 1, rowCount - 1);
  	    fireTableDataChanged();
	}

	/**************************************************************************
	 * Ignored.
	 **************************************************************************/
	public void addEmptyData() { }

	public Object getValueAt (int row, int col) {
	    try {
		TableDataBag d = (TableDataBag)data.elementAt (row);
		switch (col) {
		case COL_BUTTON:    return new Integer (row);
		case COL_SERVICE:   return d.serviceName;
		case COL_CLASSNAME: return d.className;
		}
	    } catch (Exception e) { }
	    return "";
	}

	public void setValueAt (Object value, int row, int col) {
	    if (value == null) return;
	    try {
		TableDataBag d = (TableDataBag)data.elementAt (row);
		switch (col) {
		case COL_SERVICE:
		    d.serviceName = value.toString();
		    break;
		case COL_CLASSNAME:
		    d.className = value.toString();
		    break;
		}
		fireTableCellUpdated (row, col);
	    } catch (Exception e) { }
	}

	public boolean isCellEditable (int row, int col) {
	    return true;
// 	    return (col != COL_SERVICE);
	}
    }

}
