package jas2.util.multibuttontoolbar;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import jas2.util.OnScreenPopupMenu;

/**
 * A MultiButtonGroup manages which of the AbstractButtons in it is visible
 * at any given time.
 * 
 * @author Peter Armstrong
 * @author Tony Johnson
 */
public class MultiButtonGroup {
	//
	//constructors
	//
	
	/**
	 * Creates a MultiButtonGroup with one component button, which is set to
	 * be the visible button.  This ensures that there is always exactly one
	 * button visible (as opposed to a ButtonGroup, in which there can be
	 * either 0 or 1 buttons selected.
	 */
	public MultiButtonGroup(AbstractButton b) {
		m_buttons = new Vector();
		m_visibleButtonIndex = 0;
		add(b);
		m_mouseAdapter = new MouseAdapter() {
			private int menuItemIndex = 0;
			public void mouseClicked(MouseEvent me) {
				if (SwingUtilities.isRightMouseButton(me)) {
					JPopupMenu thePopupMenu = new OnScreenPopupMenu();
					Enumeration e = getElements();
					AbstractButton theButton;
					MultiButtonMenuItem theMenuItem;
					Icon theIcon;
					String theText;
					ButtonGroup theMenuItemGroup = new ButtonGroup();
					menuItemIndex = 0;
					while (e.hasMoreElements()) {
						theButton = (AbstractButton)e.nextElement();
						theMenuItem = new MultiButtonMenuItem(menuItemIndex);
						theIcon = theButton.getIcon();
						theText = theButton.getText();
						if (theIcon != null)
							theMenuItem.setIcon(theIcon);
						if (theText != null)
							theMenuItem.setText(theText);
						if (isVisibleButton(theButton)) {
							theMenuItem.setEnabled(false);
						} else {
							theMenuItem.setEnabled(true);
						}
						theMenuItem.addActionListener(
							new ActionListener() {
								public void actionPerformed(ActionEvent e) {
									int temp = ((MultiButtonMenuItem)e.getSource()).getIndex();
									m_multiToolBar.replace(
										getVisibleButton(false),
										getButton(true, temp));
									m_visibleButtonIndex = temp;
									getButton(true, temp).requestFocus();
									getButton(true, temp).doClick();
								}
							});
						thePopupMenu.add(theMenuItem);
						theMenuItemGroup.add(theMenuItem);
						menuItemIndex++;
					}
					thePopupMenu.show(me.getComponent(), me.getX(), me.getY());
				}
			}
		};
	}
	
	//
	//public methods
	//
	
	/**
	 * Adds the button to the group.
	 */
	public void add(AbstractButton b) {
		m_buttons.addElement(b);
	}

	/**
	 * Removes the button from the group.  Removing the only button is
	 * forbidden.  Removing the visible button (which is allowed if there is
	 * more than one button in the group) causes the first button in the
	 * MultiButtonGroup to become the visible button.
	 */
	public void remove(AbstractButton b) {
		if (m_buttons.size() < 2) {
			return;
		}
		if (isVisibleButton(b)) {
			m_visibleButtonIndex = 0;
		}
		m_buttons.removeElement(b);
	}

	/**
	 * Return the visible button.
	 * 
	 * @param needMouseListener true if the visible button is being put in a
	 * toolbar that will enable it to pop up a menu that allows the visible
	 * button to be switched, false otherwise
	 */
	public AbstractButton getVisibleButton(boolean needMouseListener) {
		AbstractButton theButton =
			(AbstractButton)m_buttons.elementAt(m_visibleButtonIndex);
		if (needMouseListener) {
			theButton.addMouseListener(m_mouseAdapter);
		}
		return theButton;
	}

	/**
	 * Return the button at the index.
	 * 
	 * @param needMouseListener true if the visible button is being put in a
	 * toolbar that will enable it to pop up a menu that allows the visible
	 * button to be switched, false otherwise
	 */
	private AbstractButton getButton(boolean needMouseListener, int index) {
		AbstractButton theButton =
			(AbstractButton)m_buttons.elementAt(index);
		if (needMouseListener) {
			theButton.addMouseListener(m_mouseAdapter);
		}
		return theButton;
	}
	
	/**
	 * Sets the button to be the visible button.  Note that there is no
	 * setButtonVisible method since one button must be selected at all times,
	 * which means that the visible property should not be thought of as
	 * something that can be turned on/off, but that can only be transferred.
	 */
	public void setVisibleButton(AbstractButton b) {
		int newIndex = m_buttons.indexOf(b);
		if (newIndex != -1) {
			m_visibleButtonIndex = newIndex;
		}
	}
	
	/**
	 * Returns whether the button is the visible button.
	 */
	public boolean isVisibleButton(AbstractButton b) {
		return (m_buttons.elementAt(m_visibleButtonIndex) == b);
	}

	/**
	 * Return all the buttons that are participating in this group.
	 */
	public Enumeration getElements() {
		return m_buttons.elements();
	}
	
	public void setPositionInToolBar(int pos) {
		m_toolbarPositionIndex = pos;
	}
	
	public void setToolBar(MultiToolBar tb) {
		m_multiToolBar = tb;
	}
	
	//
	//member variables
	//
	private Vector m_buttons;
		//the buttons in the MultiButtonGroup
	private int m_visibleButtonIndex;
		//the index of the visible button in the MultiButtonGroup
	private int m_toolbarPositionIndex;
		//the index of the MultiButtonGroup in the toolbar
	private MultiToolBar m_multiToolBar;
		//the MultiToolBar that this MultiButtonGroup is in
	private MouseAdapter m_mouseAdapter;
		//the MouseAdaptor that is attached to every button that is returned by
		//getVisibleButton()
	
	//
	//non-anonymous inner classes
	//
	private class MultiButtonMenuItem extends JMenuItem {
		public MultiButtonMenuItem(int i) {
			super();
			index = i;
		}
		public int getIndex() {
			return index;
		}
		private int index;
	};
}

 

Ads help maintain this website.