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


// @(#)PopupMenuItemWidget.java, 1.15, 12/10/96





package marimba.gui;





import java.awt.FontMetrics;


import java.awt.Graphics;


import java.awt.Event;


import java.awt.Dimension;





/**


 * An item in a popup menu.


 *


 * @author	Arthur van Hoff


 * @version 	1.15, 12/10/96


 */


public class PopupMenuItemWidget extends Widget {


    /**


     * A popupMenuItem can be selected.


     * @see #select


     */


    public boolean  selected;





    /**


     * The label of this popupMenuItem


     * @see #getText


     * @see #setText


     */


    public String  label;





    /**


     * Create a PopupMenuItemWidget with the given label.


     */


    public PopupMenuItemWidget(String label) {


	if (label.startsWith("*")) {


	    label = label.substring(1);


	    disable();


	}


	this.label = label;


	if ("-".equals(label)) {


	    disable();


	}


    }





    /**


     * Create a PopupMenuItemWidget with the given name and label.


     */


    public PopupMenuItemWidget(String name, String label) {


	this(label);


	setName(name);


    }





    /**


     * Get the popup menu to which this item belongs.


     */


    public PopupMenu getMenu() {


	return (PopupMenu)parent.parent.parent;


    }





    /**


     * Figure out the preferredSize.


     */


    public Dimension preferredSize() {


	if ("-".equals(label)) {


	    return new Dimension(0, 4);


	}


	FontMetrics fm = getFontMetrics(font);


	return new Dimension(fm.stringWidth(label) + 20, fm.getHeight() + 3);


    }





    /**


     * Select the item.


     * @see #selected


     */


    public void select(boolean selected) {


	if ((selected != this.selected) && !disabled) {


	    this.selected = selected;


	    repaint();


	}


    }





    /**


     * Get the value.


     * @see #getText


     */


    public Object getValue() {


	return getText();


    }





    /**


     * Set the value.


     * @see #setText


     */


    public void setValue(Object obj) {


	setText(String.valueOf(obj));


    }





    /**


     * Get the label.


     * @see #label


     * @see #getValue


     */


    public String getText() {


	return label;


    }





    /**


     * Set the label.


     * @see #label


     * @see #setValue


     */


    public void setText(String label) {


	if (label == null) {


	    label = "";


	}


	if (!this.label.equals(label)) {


	    this.label = label;


	    getMenu().invalidate();


	    repaint();


	}


    }





    /**


     * Paint the item.


     */


    public void paint(Graphics g) {


	if ("-".equals(label)) {


	    g.setColor(background);


	    Bevel.drawRect(g, 1, 1, width-2, 2, false, 1);


	} else {


	    FontMetrics fm = getFontMetrics(font);





 	    if (disabled) {


		g.setColor(Bevel.getBrighter(background, true));


		g.drawString(label, 11, fm.getAscent()+2);


		g.setColor(Bevel.getDarker(background, true));


		g.drawString(label, 10, fm.getAscent()+1);


	    } else {


		if (selected) {


		    g.setColor(getSelBackground());


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


		    if (getMenu().itemFocus) {


			g.setColor(getSelFocusColor());


			Dash.drawFocusRect(g, 0, 0, width,height);


		    }


		    g.setColor(getSelForeground());


		} else {


		    g.setColor(foreground);


		}


		g.drawString(label, 10, fm.getAscent()+1);


	    }


	}


    }





    /**


     * Debugging.


     */


    public void paramString(StringBuffer buf) {


	super.paramString(buf);


	buf.append(",label=");


	buf.append(label);


	if (disabled) {


	    buf.append(",disabled");


	}


    }


}


