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


// @(#)ListItemWidget.java, 1.29, 12/17/96





package marimba.gui;





import java.awt.*;





/**


 * An Item in a ListWidget. It takes care displaying


 * multiple columns as described in the ListWidget.


 *


 * @author	Arthur van Hoff


 * @author	Klaas Waslander


 * @version 	1.29, 12/17/96


 */


public class ListItemWidget extends Widget {


    /**


     * A ListItem can be selected.


     * @see #isSelected


     * @see #select()


     */


    public boolean  selected;





    /**


     * Get the ListWidget for this item.


     */


    public ListWidget getListWidget() {


	Widget p = this.parent;


	return ((p != null) && ((p = p.parent) != null)) ? (ListWidget)p : null;


    }





    /**


     * Get the X position of a given column.


     */


    public int getColumnX(int col) {


	ListWidget list = getListWidget();


	return (list != null) ? list.cols[col].x : 0;


    }





    /**


     * Get the width of a given column.


     */


    public int getColumnWidth(int col) {


	ListWidget list = getListWidget();


	return (list != null) ? list.cols[col].width : 0;


    }





    /**


     * The key by which this widget is sorted.


     */


    public String key() {


	return null;


    }





    /**


     * Check if this ListItem is selected.


     * @see #selected


     */


    public boolean isSelected() {


	return selected;


    }





    /**


     * Select this item.


     * @see #selected


     */


    public void select() {


	select(true);


    }





    /**


     * Select or unselect this item.


     * @see #selected


     */


    public void select(boolean sel) {


	ListWidget list = getListWidget();


	if (list != null) {


	    if (sel != selected) {


		synchronized (list) {


		    if (sel && list.exclusive) {


			list.clearSelection();


		    }


		    selected = sel;


		    repaint();


		}


		list.postEvent(new Event(list, sel ? Event.LIST_SELECT : Event.LIST_DESELECT, this));


	    }


	}


    }





    /**


     * Initialize the item.


     */


    public void init() {


	ListWidget list = getListWidget();


	if (list != null) {


	    setFont(list.font);


	    FontMetrics fm = getFontMetrics(font);


	    reshape(0, y, parent.width, fm.getHeight());


	}


    }





    /**


     * Destroy the item.


     */


    public void destroy() {


	ListWidget list = getListWidget();


	if (selected && (list != null)) {


	    list.postEvent(new Event(list, Event.LIST_DESELECT, this));


	}


    }





    /**


     * Draw a column of this item. The x,y position is


     * the baseline on the left most edge of the item.


     */


    public void paint(Graphics g, int col, int x, int y) {


	//g.drawString("abc", x, y);


    }





    /**


     * Paint the selection.


     */


    public void paintSelection(Graphics g) {


	g.fillRect(0, 0, width, height);


    }





    /**


     * Paint the item and all of its columns.


     */


    public void paint(Graphics g) {


	ListWidget list = getListWidget();


	if (list != null) {


	    if (selected && !list.disabled) {


		g.setColor(getSelBackground());


		paintSelection(g);


		g.setColor(getSelForeground());


	    } else {


		g.setColor(foreground);


	    }


	    FontMetrics fm = g.getFontMetrics();


	    int ascent = fm.getAscent();


	    int  ypos = (height - fm.getHeight()) / 2 + ascent;





	    if (list.ncols == 0) {


		paint(g, 0, 0, ypos);


	    } else {


		for (int i = 0 ; i < list.ncols ; i++) {


		    paint(g, i, list.cols[i].x, ypos);


		}


	    }


	}


    }


 


    /**


     * Find a column given an x position.


     */


    int x2col(ListWidget list, int x) {


	for (int c = 0 ; c < list.ncols ; c++) {


	    if (x < list.cols[c].x) {


		return Math.max(0, c-1);


	    }


	}


	return list.ncols-1;


    }





    /**


     * Handle an event for a particular column.


     */


    public boolean handleEvent(Event evt, int col) {


	return false;


    }





    /**


     * Handle events.


     */


    public boolean handleEvent(Event evt) {


	ListWidget list = getListWidget();


	if (list!=null && !list.disabled) {


	    switch (evt.id) {


		case Event.MOUSE_DOWN:


		    if (!handleEvent(evt, x2col(list, evt.x))) {


			if (!selected) {


			    select();


			    if (evt.clickCount > 1) {


				action();


			    }


			} else if (evt.clickCount > 1) {


			    action();


			}


			return false;


		    }


		    // let list detect mouse-down, if nothing is done above


		    list.postEvent(new Event(this, Event.MOUSE_DOWN, null));


		    return true;





		case Event.MOUSE_DRAG:


		    return handleEvent(evt, x2col(list, evt.x));


		


		case Event.MOUSE_UP:


		    return handleEvent(evt, x2col(list, evt.x));


	    }


	}


	return false;


    }





    /**


     * The user has double clicked on this item.


     */


    public void action() {


	ListWidget list = getListWidget();


	if (list != null) {


	    list.postEvent(new Event(list, Event.ACTION_EVENT, this));


	}


    }





    /**


     * Debugging.


     */


    public void paramString(StringBuffer buf) {


	super.paramString(buf);


	String key = key();


	if (key != null) {


	    buf.append(",key=");


	    buf.append(key());


	}


	if (selected) {


	    buf.append(",selected");


	}


    }


}


