import java.awt.*;
import java.awt.event.*;

/** RadioGroup extends CheckboxGroup,
 * to provide one-of-group functionality to a wider group of controls,
 * presently Checkbox and CheckImageButton.
 *
 * RadioGroup completely overrides CheckboxGroup,
 * by reimplementing all methods that are specific to Checkbox,
 * and providing similar methods that are more general.
 *
 * Should replace all type Object here by some CheckLike interface,
 * as a marker that setState and getState methods are supported,
 * to be implemented by Checkbox, CheckboxMenuItem, CheckImageButton, etc.
 * But that interface is not worth much,
 * unless Checkbox and CheckboxMenuItem declare it.
 * @author Morris Hirsch IPD Inc
 */

public class RadioGroup extends CheckboxGroup {

    private Object latest = null;

/** Deprecated */

    public Checkbox getCurrent () {
	return getSelectedCheckbox ();
    }

/** Required as extension of CheckboxGroup but not recommended */

    public Checkbox getSelectedCheckbox () {
	if (null == latest)
	    return null;
	if (latest instanceof Checkbox)
	    return (Checkbox)latest;
	return null;
    }

/** Best but caller must (cast) the returned Object,
 * and must know all the possible flavors,
 * in order to call any methods on it.
 * <PRE>
 *   Object myObj = RG.getSelected ();
 *   if (myObj instanceof StarMarker)
 *      ((StarMarker)myObj).setState (false);
 *   else if (myObj instanceof Checkbox)
 *      ((Checkbox)myObj).setState (false);
 *   else if (myObj instanceof CheckImageButton)
 *      ((CheckImageButton)myObj).setState (false);
 * </PRE> */

    public Object getSelected () {
	return latest;
    }

/** Deprecated */

    public void setCurrent (Checkbox next) {
	setSelectedCheckbox (next);
    }

/** Required as extension of CheckboxGroup but not recommended */

    public void setSelectedCheckbox (Checkbox next) {
	setSelected (next);
    }

/** Best but lacks compile-time restriction on calling parameter.
 * @param next -- the next Selected Object,
 * or null to clear the RadioGroup.
 * @throws IllegalArgumentException,
 * if next does not support setState and getState.  */

    public void setSelected (Object next) {

	if ((null != next)
	 && !(next instanceof StarMarker)
	 && !(next instanceof Checkbox)
	 && !(next instanceof CheckImageButton))
	     throw new IllegalArgumentException ("next: "+next);

/* Hold latest in prev and update latest to next right away,
 * because when we call setState (false) to turn off the old one,
 * it will in turn call getCurrent or getSelected,
 * to see if it is the current choice,
 * which would appear to be true if we did not first update latest,
 * and would cause it to refuse to turn off. */

        Object prev = latest;
	latest = next;

	if ((null != prev)
	 && (next != prev)) {
	     if (prev instanceof StarMarker)
	        ((StarMarker)prev).setState (false);

	     else if (prev instanceof Checkbox)
	        ((Checkbox)prev).setState (false);

	     else if (prev instanceof CheckImageButton)
	        ((CheckImageButton)prev).setState (false);

             else
		 throw new IllegalArgumentException ("prev: "+prev);
        }
    }

    public String toString () {
	if (null != latest)
	    return "RadioGroup: "+latest.toString ();
	else
	    return "RadioGroup: null";
    }

}
/* <IMG SRC="/cgi-bin/counter">*/
