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


// @(#)DropDownMenu.java, 1.20, 12/17/96





package marimba.gui;





import java.awt.*;


import java.util.*;





/**


 * A simple drop down menu.


 *


 * @author	Klaas Waslander


 * @version 	1.20, 12/17/96


 */


class DropDownMenu extends PopupMenu {


    /**


     * The choicewidget that uses this DropDownMenu.


     * The menu sets the current value of this choiceWidget


     * and invokes the w.action() method after that.


     */


    public ChoiceWidget  w;





    /**


     * Constructor with the given choiceWidget.


     * @param autoSize	if false, the width of the choicewidget is used.


     */


    DropDownMenu(ChoiceWidget w, boolean autoSize) {


	this(w, autoSize, 0);


    }





    /**


     * Constructor with the given choiceWidget and the maximum number


     * of visible items.


     * @param autoSize	if false, the width of the choicewidget is used.


     */


    DropDownMenu(ChoiceWidget w, boolean autoSize, int maxVisible) {


	this.w = w;


	lineMode = SOLID;


	this.maxVisible = maxVisible;





	int width = 20;


	FontMetrics fm = getFontMetrics(w.font);


	String choices[] = w.getChoices();


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


	    add(choices[i]);


	    int strw = fm.stringWidth(choices[i]);


	    if (strw + 20 > width) {


		width = strw + 20;


	    }


	}


	if ((width > w.width) || !autoSize) {


	    width = w.width;


	}


	layout();


	popup(w, 0, w.height, width);


    }





    /**


     * Select next item, do not loop around when


     * the last item has been reached.


     * @see #current


     */


    public void nextItem() {


	int current = indexOf(this.current);


	if (current < getItemCount() - 1) {


	    super.nextItem();


	}


    }





    /**


     * Select previous item, do not loop around when


     * the first item has been reached.


     * @see #current


     */


    public void previousItem() {


	int  current = indexOf(this.current);


	if (current > 0) {


	    super.previousItem();


	}


    }





    /**


     * Handle mouse events.


     */


    public boolean handleEvent(Event evt) {


	switch (evt.id) {


	  case Event.ACTION_EVENT: {


	    Widget  cont = container.content;


	    for (int i = 0 ; i < cont.nwidgets ; i++) {


		if (evt.target == cont.widgets[i]) {


		    w.setValue(i);


		    w.action();


		    break;


		}


	    }


	    return true;


	  }





	  case Event.MOUSE_DRAG:


	    owner.postEvent(evt);


	    break;





	  case Event.MOUSE_UP:


	    // make sure that the button of dropdownlist is not down


	    ((DropDownTextBoxWidget)owner).setDown(false);


	    break;


	}


	return super.handleEvent(evt);


    }


}


