/*************** * * Copyright (c) AT&T DARICS, 1996 -- All Rights Reserved * * PROJECT: JavaWorld * MODULE: Tips & Tricks * FILE: MutuallyExclusiveMenu.java * * AUTHOR: Andrzej Porebski, May 10, 1997 * * DESCRIPTION: * This file implements the menu that allows only one menu item * selected at a time. * ************/ import java.awt.Menu; import java.awt.MenuItem; import java.awt.CheckboxMenuItem; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; public class MutuallyExclusiveMenu extends Menu implements ItemListener { /** * Holds currently active item - can be null if none of the items * are active */ CheckboxMenuItem checked; /** * The listener to receive ItemEvents when a new item is selected - can * be null. */ ItemListener listener; /** * Basic constructor - calls Menu's constructor with label */ public MutuallyExclusiveMenu (String label) { super(label); } /** * This function activates the menu item passed in as parameter. If * there is an active item in the menu already, deactivate it first. * I decided to make this function protected, but you can easily modify * it so that it is public and you can call it to activate an item * programmatically. */ protected void setChecked(CheckboxMenuItem item) { if (checked != null) checked.setState(false); checked = item; } /** * This function adds menu items to our menu. If menu item is not * a CheckboxMenuItem, we don't add it at all. If it is, we first * register ourselves for notification of ItemEvent from this * item so that we can react to item selection, then we call parent's * add() to really add it to our menu. If item we are adding is checked, * than we better make sure we record it appropriately - this is * accomplished by a call to setChecked(). */ public MenuItem add (MenuItem item) { if(item instanceof CheckboxMenuItem) { CheckboxMenuItem item_ = (CheckboxMenuItem) item; item_.addItemListener(this); super.add(item); if(item_.getState()) setChecked(item_); } return item; } /** * Just another add() function that overwrites the Menu's add(String) * function. We are create a new CheckboxMenu item with the label * and call our own add(MenuItem) function. */ public void add(String label) { add(new CheckboxMenuItem(label)); } /** * This following two functions register and unregister an ItemListener * for notification upon new item selection event. */ public void addItemListener(ItemListener l) { listener = l; } public void removeItemListener() { listener = null; } /** * This function is called by each menu item that the user selects. * We want to see if this item is already selected first and * we can do it by comparing it to class variable 'checked'. If it is * already active, we must set it to checked again because the user's * click really unchecked it. If this item is not already selected, we * must uncheck the currently checked item referenced by 'checked', * save the reference to the newly selected item in 'checked' and * notify the listener (if one registered) via itemStateChanged() * function call. */ public void itemStateChanged(ItemEvent e) { CheckboxMenuItem item = (CheckboxMenuItem) e.getItemSelectable(); if (item == checked) item.setState(true); else { if (checked != null) checked.setState(false); checked = item; if (listener != null) listener.itemStateChanged(e); } } }