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


// @(#)OptionWidget.java, 1.18, 10/24/96





package marimba.gui;





import java.awt.*;





import marimba.persist.*;





/**


 * An option widget. It represents a single choice within


 * a limited set of mutually exclusive choices: only one


 * option in a group of option Widgets can be set.


 *


 * @author	Klaas Waslander


 * @version 	1.18, 10/24/96


 */


public class OptionWidget extends CheckBoxWidget {


    /**


     * Constructor.


     */


    public OptionWidget() {


	label = "Option";


    }





    /**


     * Paint the option box and also the focus rectangle if there is no label.


     */


    public void paintButton(Graphics g) {


	FontMetrics fm = g.getFontMetrics();


	int siz = Math.max(12, Math.min(20, fm.getAscent()));


	int lx = (align == RIGHT) ? width-(siz+5) : 5;


	int ly = (height - fm.getHeight())/2 + fm.getAscent() - (siz-2);





	g.setColor(parent.background);


	if (disabled) {


	    Bevel.drawOptionBorder(g, lx, ly, siz+1, siz+1);


	} else {


	    Bevel.fillOptionBorder(g, lx, ly, siz+1, siz+1, down ? background : hilite);


	}





	if (value) {


	    g.setColor(Color.black);


	    g.fillArc(lx+4, ly+4, siz-7, siz-7, 0, 360);


	}


	if (hasFocus() && label.length() == 0) {


	    g.setColor(getFocusColor());


	    Dash.drawFocusRect(g, lx - 1, ly - 1, siz + 2, siz + 2);


	}


    }   





    /**


     * Goto the specified OptionWidget in the same group.


     */


    public void gotoOption(OptionWidget option) {


	if (option != null  &&  option != this  &&  group.equals(option.group)) {


	    option.setValue(true);


	    option.requestFocus();


	    option.action();


	}


    }





    /**


     * Handle arrowkey events.


     */


    public boolean handleEvent(Event evt) {


	switch (evt.id) {


	    case Event.KEY_ACTION:


		switch (evt.key) {


		    case Event.LEFT:


		    case Event.UP:


			gotoOption((OptionWidget)getPrevious());


			return true;





		    case Event.RIGHT:


		    case Event.DOWN:


			gotoOption((OptionWidget)getNext());


			return true;


	    }


	    break;


	}


	return super.handleEvent(evt);


    }





    /**


     * An optionWidget is interested in the focus, if it is not disabled


     * and: it is alone or currently has the value.


     */


    public boolean focusInterest() {


	return !disabled && (value || groupCount() == 1);


    }


}


