// Copyright 1996, Marimba Inc. All Rights Reserved.


// @(#)TableRow.java, 1.6, 12/05/96





package marimba.gui;





import java.awt.*;


import java.util.*;





import marimba.persist.*;


import marimba.util.*;


import marimba.io.*;





/**


 * A row descriptor.


 *


 * @author	Klaas Waslander


 * @version 	1.6, 12/05/96


 */


public class TableRow extends TableColumn implements Sortable {


    /**


     * A row stores its data.


     * @see #getData


     */


    public Vector  data = new Vector();





    /**


     * Constructor for constructing a


     * default row with no data.


     */


    public TableRow() {


	this("", 0, 50, 0, new Vector());


    }





    /**


     * Constructor which takes all possible arguments.


     */


    public TableRow(String label, int pos, int size, int displayIndex, Vector data) {


	super(label, pos, size, displayIndex);


	if (data == null) {


	    data = new Vector();


	}


	this.data = data;


    }





    /**


     * Get the properties of this TableColumn.


     */


    public void getProperties(PropertyList list) {


	super.getProperties(list);





	// persistify data


	Object  items[] = new Object[data.size()];


	data.copyInto(items);


	list.setObjectArray("data", data.size(), items);





	// persistify java.lang objects, since setObjectArray does not do that yet


	for (int i = 0; i < data.size(); i++) {


	    Object  item = data.elementAt(i);


	    if (item instanceof Boolean) {


		list.setString("__boolean__"+i, ((Boolean)item).toString(), null);


	    } else if (item instanceof Character) {


		list.setString("__character__"+i, ((Character)item).toString(), null);


	    } else if (item instanceof Double) {


		list.setString("__double__"+i, ((Double)item).toString(), null);


	    } else if (item instanceof Float) {


		list.setString("__float__"+i, ((Float)item).toString(), null);


	    } else if (item instanceof Integer) {


		list.setString("__integer__"+i, ((Integer)item).toString(), null);


	    } else if (item instanceof Long) {


		list.setString("__long__"+i, ((Long)item).toString(), null);


	    } else if (item instanceof String) {


		list.setString("__string__"+i, (String)item, null);


	    }


	}


    }





    /**


     * Set the properties of this widget.


     */


    public void setProperties(PropertyList list) {


	super.setProperties(list);





	// get the persistified data


	data.removeAllElements();


	Object  items[] = list.getObjectArray("data", null);


	for (int i = 0; i < items.length; i++) {


	    data.addElement(items[i]);


	}





	// get java.lang objects


	for (int i = 0; i < data.size(); i++) {


	    Object  item = data.elementAt(i);


	    int  objectType = 0;


	    if (item == null) {


		item = list.getString("__boolean__"+i, null);


		objectType++;


	    }


	    if (item == null) {


		item = list.getString("__character__"+i, null);


		objectType++;


	    }


	    if (item == null) {


		item = list.getString("__double__"+i, null);


		objectType++;


	    }


	    if (item == null) {


		item = list.getString("__float__"+i, null);


		objectType++;


	    }


	    if (item == null) {


		item = list.getString("__integer__"+i, null);


		objectType++;


	    }


	    if (item == null) {


		item = list.getString("__long__"+i, null);


		objectType++;


	    }


	    if (item == null) {


		item = list.getString("__string__"+i, null);


		objectType++;


	    }





	    switch (objectType) {


		case 1: data.setElementAt(new Boolean((String)item), i); break;


		case 2: data.setElementAt(new Character(((String)item).charAt(0)), i); break;


		case 3: data.setElementAt(new Double((String)item), i); break;


		case 4: data.setElementAt(new Float((String)item), i); break;


		case 5: data.setElementAt(new Integer((String)item), i); break;


		case 6: data.setElementAt(new Long((String)item), i); break;


		case 7: data.setElementAt((String)item, i); break;


	    }


	}


    }





    /**


     * Get the data in this row.


     * @see #data


     */


    public Vector getData() {


	return data;


    }





    /**


     * Compare this row to another row based on the given column index.


     * The comparing is done using strings: widgets are compared using


     * their method 'getText'.


     */


    public int compareTo(Sortable other, Object columnIndex) {


	int  result;





	Vector  otherData = ((TableRow)other).getData();


	int  colNumber = ((Integer)columnIndex).intValue();





	Object  thisItem = (colNumber < data.size()) ? data.elementAt(colNumber) : null;


	Object  otherItem = (colNumber < otherData.size()) ? otherData.elementAt(colNumber) : null;





	if (thisItem == null) {


	    result = -1;


	} else if (otherItem == null) {


	    result = 1;


	} else {


	    // get strings representing both items


	    String  thisStr = null;


	    String  otherStr = null;


	    if (thisItem instanceof String) {


		thisStr = (String)thisItem;


	    } else if (thisItem instanceof Widget) {


		thisStr = ((Widget)thisItem).getText();


	    }


	    if (otherItem instanceof String) {


		otherStr = (String)otherItem;


	    } else if (otherItem instanceof Widget) {


		otherStr = ((Widget)otherItem).getText();


	    }





	    // return appropriate result if one of the two is null


	    if (thisStr == null) {


		result = -1;


	    } else if (otherStr == null) {


		result = 1;


	    } else {


		result = thisStr.compareTo(otherStr);


	    }


	}





	return result;


    }


}


