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


// @(#)MenuButtonWidget.java, 1.23, 12/10/96





package marimba.gui;





import java.awt.FontMetrics;


import java.awt.Graphics;


import java.awt.Event;


import java.util.*;





import marimba.persist.*;





/**


 * A menu button widget.


 *


 * @author	Arthur van Hoff


 * @version 	1.23, 12/10/96


 */


public class MenuButtonWidget extends CommandButtonWidget {


    /**


     * The menu used for this menuButton.


     * @see #getMenu


     * @see #setMenu


     */


    public PopupMenu  menu;





    /**


     * Create a menuButton with 5 default items.


     */


    public MenuButtonWidget() {


	menu = new PopupMenu();


	menu.setOwner(this);


	menu.add("One");


	menu.add("Two");


	menu.add("-");


	menu.add("*Three");


	menu.add("Four");


    }





    /**


     * Create a menu with the specified items.


     */


    public MenuButtonWidget(int nitems, String items[]) {


	menu = new PopupMenu();


	menu.setOwner(this);


	for (int index = 0; index < nitems; index++) {


	    menu.add(items[index]);


	}


    }





    /**


     * Get the properties of this widget.


     */


    public void getProperties(PropertyList list) {


	super.getProperties(list);


	StringBuffer buf = new StringBuffer();


	for (int i = 0 ; i < menu.getItemCount() ; i++) {


	    if (i > 0) {


		buf.append(',');


	    }


	    PopupMenuItemWidget item = menu.getItem(i);


	    if (item.disabled && !item.label.equals("-")) {


		buf.append("*");


	    }


	    buf.append(item.label);


	}


	list.setString("items", buf.toString(), "");


    }





    /**


     * Set the properties of this widget.


     */


    public void setProperties(PropertyList list) {


	super.setProperties(list);





	String str = list.getString("items", "");


	menu = new PopupMenu();


	menu.setOwner(this);


	for (StringTokenizer t = new StringTokenizer(str, ",") ; t.hasMoreTokens() ; ) {


	    menu.add(t.nextToken());


	}


    }





    /**


     * Get the menu.


     * @see #menu


     */


    public PopupMenu getMenu() {


	return menu;


    }





    /**


     * Set the menu.


     * @see #menu


     */


    public void setMenu(PopupMenu menu) {


	this.menu = menu;


	menu.setOwner(this);


    }





    /**


     * Paint the button label.


     */


    public void paintLabel(Graphics g, String str, int x, int y) {


	FontMetrics fm = g.getFontMetrics();


	g.drawString(str, x - 6, y);





	x += fm.stringWidth(str) - 3;


	y -= 7;





	// draw the down arrow


	for (int i = 0 ; i < 4 ; i++) {


	    g.drawLine(x + i, y + i, x + 6 - i, y + i);


	}


    }





    /**


     * Popup the dropDownMenu, setting some properties to make


     * it appear the way it should.


     */


    public void popupMenu() {


	down = true;


	repaint();


	menu.setBackground(background);


	menu.setForeground(foreground);


	menu.popup(this, 0, height);


	menu.requestFocus();


    }





    /**


     * Handle events.


     */


    public boolean handleEvent(Event evt) {


	if (!disabled) {


	    switch (evt.id) {


		case Event.GOT_FOCUS:


		    down = false;


		    repaint();


		    return true;





		case Event.MOUSE_DOWN:


		    popupMenu();


		    return false;





		case Event.MOUSE_DRAG:


		    requestFocus();


		    return true;





		case Event.MOUSE_UP:


		    down = false;


		    repaint();


		    return true;





		case Event.KEY_PRESS:


		    switch (evt.key) {


			case '\n':


			case ' ':


			    popupMenu();


			    menu.select(0);


			    return true;


		    }


		    break;





		case Event.KEY_RELEASE:


		    switch (evt.key) {


			case ' ':


			    // button has to stay down when spacebar is released


			    return true;


		    }


		    break;


	    }


	}


	return super.handleEvent(evt);


    }





    /**


     * Perform the default action.


     */


    public void action() {


        down = true;


        repaint();


        menu.popup(this, 0, height);


        menu.select(0);


        menu.requestFocus();


    }


}


