package ch.werner_randelshofer.gui.model;/*Copyright (C) 1999 Werner Randelshoferwerner.randelshofer@mythen.chhttp://www.mythen.ch/w.randelshofer/    Permission to use this release of DefaultButtonModel is herebygranted without fee provided that the complete copyrightnotice and this permission notice appear in all copiesand in supporting documentation.*/import java.awt.Component;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Event;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import ch.werner_randelshofer.gui.event.ChangeEvent;import ch.werner_randelshofer.gui.event.ChangeListener;import ch.werner_randelshofer.gui.event.EventListenerList;/**Default button model.@author	Werner Randelshofer, Staldenmattweg 2, CH-6405 Immensee, Switzerland.@version	1.0	20.02.1999	Created.*/public class DefaultButtonModelimplements ButtonModel	{	private boolean isEnabled_  = true, isPressed_, isSelected_, isArmed_;	private EventListenerList listenerList_ = new EventListenerList();	private String actionCommand_;	private ChangeEvent changeEvent_;		/** Enables or disables the button. */	public void setEnabled(boolean b)		{		if (b != isEnabled_)			{			isEnabled_ = b;			fireStateChanged();			}		}		/** Indicates if the button can be selected or pressed by an input device	(such as a mouse pointer). */	public boolean isEnabled()		{ return isEnabled_; }		/** Sets the button to pressed or unpressed. */	public void setPressed(boolean b)		{		if (b != isPressed_)			{			isPressed_ = b;			if (b == false)				{				fireActionPerformed(					new ActionEvent(this,ActionEvent.ACTION_PERFORMED,getActionCommand())					);				}			fireStateChanged();			} 		}	/** Indicates if the button has been pressed. */	public boolean isPressed()		{ return isPressed_; }		/** Selects or deselects the button. */	public void setSelected(boolean b)		{		if (b != isSelected_)			{			isSelected_ = b;			fireItemStateChanged(				new ItemEvent(this,ItemEvent.ITEM_STATE_CHANGED,this,							b ?  ItemEvent.SELECTED : ItemEvent.DESELECTED)							);			fireStateChanged();			}		}	/** Arms or unarms the button. */	public void setArmed(boolean b)		{		if (b != isArmed_)			{			isArmed_ = b;			fireStateChanged();			}		}		/** Indicates if the button is armed. */	public boolean isArmed()		{ return isArmed_; }			/** Indicates if the button has been selected. */	public boolean isSelected()		{ return isSelected_; }	/** Sets the action command of the button. */	public void setActionCommand(String command)		{ actionCommand_ = command; }	/** Gets the action command of the button. */	public String getActionCommand()		{ return actionCommand_; }	/** Adds an action listener to the button. */	public void addActionListener(ActionListener l)		{ listenerList_.add(ActionListener.class,l); }	/** Removes an action listener from the button. */	public void removeActionListener(ActionListener l)		{ listenerList_.remove(ActionListener.class,l); }		/** Adds a change listener to the button. */	public void addChangeListener(ChangeListener l)		{ listenerList_.add(ChangeListener.class,l); }	/** Removes a change listener from the button. */	public void removeChangeListener(ChangeListener l)		{ listenerList_.remove(ChangeListener.class,l); }	/** Adds an item listener to the button. */	public void addItemListener(ItemListener l)		{ listenerList_.add(ItemListener.class,l); }	/** Removes an item listener from the button. */	public void removeItemListener(ItemListener l)		{ listenerList_.remove(ItemListener.class,l); }		/*	 * Notify all listeners that have registered interest for	 * notification on this event type.  The event instance 	 * is lazily created using the parameters passed into 	 * the fire method.	 *	 * @param e the ActionEvent to deliver to listeners	 * @see EventListenerList	 */	protected void fireActionPerformed(ActionEvent e)		{		// Guaranteed to return a non-null array		Object[] listeners = listenerList_.getListenerList();		// Process the listeners last to first, notifying		// those that are interested in this event		for (int i = listeners.length-2; i>=0; i-=2)			{			if (listeners[i]==ActionListener.class)				{				// Lazily create the event:				// if (changeEvent == null)				// changeEvent = new ChangeEvent(this);				((ActionListener)listeners[i+1]).actionPerformed(e);				}			}		}	/*	 * Notify all listeners that have registered interest for	 * notification on this event type.  The event instance 	 * is lazily created using the parameters passed into 	 * the fire method.	 *	 * @param e the ItemEvent to deliver to listeners	 * @see EventListenerList	 */	protected void fireItemStateChanged(ItemEvent e)		{		// Guaranteed to return a non-null array		Object[] listeners = listenerList_.getListenerList();		// Process the listeners last to first, notifying		// those that are interested in this event		for (int i = listeners.length-2; i>=0; i-=2)			{			if (listeners[i]==ItemListener.class)				{				// Lazily create the event:				// if (changeEvent == null)				// changeEvent = new ChangeEvent(this);				((ItemListener)listeners[i+1]).itemStateChanged(e);				}			}		}	/*	 * Notify all listeners that have registered interest for	 * notification on this event type.  The event instance 	 * is lazily created using the parameters passed into 	 * the fire method.	 *	 * @see EventListenerList	 */	protected void fireStateChanged()		{		// Guaranteed to return a non-null array		Object[] listeners = listenerList_.getListenerList();		// Process the listeners last to first, notifying		// those that are interested in this event		for (int i = listeners.length-2; i>=0; i-=2)			{			if (listeners[i]==ChangeListener.class)				{				// Lazily create the event:				if (changeEvent_ == null)					changeEvent_ = new ChangeEvent(this);				((ChangeListener)listeners[i+1]).stateChanged(changeEvent_);				}			}		}	/** Overriden to return null. */	public Object[] getSelectedObjects()		{ return null; }	}