summaryrefslogtreecommitdiff
path: root/javax/swing/DefaultButtonModel.java
diff options
context:
space:
mode:
authorAndrew John Hughes <gnu_andrew@member.fsf.org>2005-01-14 10:24:02 +0000
committerAndrew John Hughes <gnu_andrew@member.fsf.org>2005-01-14 10:24:02 +0000
commitc61f399b1d3c471a8e459a4a2be645f95560f088 (patch)
tree14e7f5759d2cded647d22e019435a770b8ed69e5 /javax/swing/DefaultButtonModel.java
parent451c55a31fbc6b949f7609dd90932bb2a0d91a19 (diff)
downloadclasspath-c61f399b1d3c471a8e459a4a2be645f95560f088.tar.gz
2005-01-14 Andrew John Hughes <gnu_andrew@member.fsf.org>
* Merge of September 2004 HEAD patches to generics branch.
Diffstat (limited to 'javax/swing/DefaultButtonModel.java')
-rw-r--r--javax/swing/DefaultButtonModel.java267
1 files changed, 142 insertions, 125 deletions
diff --git a/javax/swing/DefaultButtonModel.java b/javax/swing/DefaultButtonModel.java
index e9a1356e8..4c7c24772 100644
--- a/javax/swing/DefaultButtonModel.java
+++ b/javax/swing/DefaultButtonModel.java
@@ -1,4 +1,4 @@
-/* DefaultButtonModel.java --
+/* DefaultButtonModel.java --
Copyright (C) 2002, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -44,87 +44,103 @@ import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.io.Serializable;
import java.util.EventListener;
-
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;
+
/**
* The purpose of this class is to model the dynamic state of an abstract
* button. The concrete button type holding this state may be a a "toggle"
* button (checkbox, radio button) or a "push" button (menu button, button).
- *
- * If the model is disabled, only the "selected" property can be changed.
- * An attempt to change the "armed", "rollover" or "pressed" properties
- * while the model is disabled will be blocked.
- *
- * Any successful (non-blocked) change to the model's properties will
- * trigger the firing of a ChangeEvent.
- *
- * Any change to the "selected" property will trigger the firing of an
- * ItemEvent in addition to ChangeEvent. This is true whether the model is
- * enabled or not.
- *
- * One other state change is special: the transition from "enabled, armed
- * and pressd" to "enabled, armed and not-pressed". This is considered the
- * "trailing edge" of a successful mouse click, and therefore fires an
- * ActionEvent in addition to a ChangeEvent.
+ * If the model is disabled, only the "selected" property can be changed. An
+ * attempt to change the "armed", "rollover" or "pressed" properties while
+ * the model is disabled will be blocked. Any successful (non-blocked) change
+ * to the model's properties will trigger the firing of a ChangeEvent. Any
+ * change to the "selected" property will trigger the firing of an ItemEvent
+ * in addition to ChangeEvent. This is true whether the model is enabled or
+ * not. One other state change is special: the transition from "enabled,
+ * armed and pressd" to "enabled, armed and not-pressed". This is considered
+ * the "trailing edge" of a successful mouse click, and therefore fires an
+ * ActionEvent in addition to a ChangeEvent. In all other respects this class
+ * is just a container of boolean flags.
*
- * In all other respects this class is just a container of boolean flags.
- *
- * @author Graydon Hoare (graydon@redhat.com)
+ * @author Graydon Hoare (graydon_at_redhat.com)
*/
public class DefaultButtonModel implements ButtonModel, Serializable
{
+ /** DOCUMENT ME! */
static final long serialVersionUID = -5342609566534980231L;
- /** Indicates that the button is <em>partially</em> committed to being
- pressed, but not entirely. This usually happens when a user has pressed
- but not yet released the mouse button. */
+ /**
+ * Indicates that the button is <em>partially</em> committed to being
+ * pressed, but not entirely. This usually happens when a user has pressed
+ * but not yet released the mouse button.
+ */
public static final int ARMED = 1;
- /** State constant indicating that the button is enabled. Buttons cannot
- be pressed or selected unless they are enabled. */
+ /**
+ * State constant indicating that the button is enabled. Buttons cannot be
+ * pressed or selected unless they are enabled.
+ */
public static final int ENABLED = 8;
- /** State constant indicating that the user is holding down the button.
- When this transitions from true to false, an ActionEvent may be fired,
- depending on the value of the "armed" property.*/
+ /**
+ * State constant indicating that the user is holding down the button. When
+ * this transitions from true to false, an ActionEvent may be fired,
+ * depending on the value of the "armed" property.
+ */
public static final int PRESSED = 4;
- /** State constant indicating that the mouse is currently positioned over
- the button. */
+ /**
+ * State constant indicating that the mouse is currently positioned over the
+ * button.
+ */
public static final int ROLLOVER = 16;
- /** State constant indicating that the button is selected. This constant
- is only meaningful for toggle-type buttons (radio buttons,
- checkboxes). */
+ /**
+ * State constant indicating that the button is selected. This constant is
+ * only meaningful for toggle-type buttons (radio buttons, checkboxes).
+ */
public static final int SELECTED = 2;
- /** Represents the "state properties" (armed, enabled, pressed, rollover
- and selected) by a bitwise combination of integer constants. */
+ /**
+ * Represents the "state properties" (armed, enabled, pressed, rollover and
+ * selected) by a bitwise combination of integer constants.
+ */
protected int stateMask = ENABLED;
- /** List of ItemListeners, ChangeListeners, and ActionListeners
- registered on this model. */
- protected EventListenerList listenerList = new EventListenerList();;
+ /**
+ * List of ItemListeners, ChangeListeners, and ActionListeners registered on
+ * this model.
+ */
+ protected EventListenerList listenerList = new EventListenerList();
+ ;
- /** The single ChangeEvent this model (re)uses to call its
- ChangeListeners. */
+ /** The single ChangeEvent this model (re)uses to call its ChangeListeners. */
protected ChangeEvent changeEvent = new ChangeEvent(this);
- /** The group this model belongs to. Only one button in a group may be
- selected at any given time. */
+ /**
+ * The group this model belongs to. Only one button in a group may be
+ * selected at any given time.
+ */
protected ButtonGroup group;
-
- /** The key code (one of {@link java.awt.event.KeyEvent} VK_*) used to
- press this button via a keyboard interface. */
+
+ /**
+ * The key code (one of {@link java.awt.event.KeyEvent} VK_) used to press
+ * this button via a keyboard interface.
+ */
protected int mnemonic = KeyEvent.VK_UNDEFINED;
- /** The string used as the "command" property of any ActionEvent this
- model sends. */
+ /**
+ * The string used as the "command" property of any ActionEvent this model
+ * sends.
+ */
protected String actionCommand;
+ /**
+ * Creates a new DefaultButtonModel object.
+ */
public DefaultButtonModel()
{
}
@@ -135,10 +151,10 @@ public class DefaultButtonModel implements ButtonModel, Serializable
*
* @return <code>null</code>
*/
- public Object[] getSelectedObjects()
- {
- return null;
- }
+ public Object[] getSelectedObjects()
+ {
+ return null;
+ }
/**
* Returns a specified class of listeners.
@@ -151,10 +167,10 @@ public class DefaultButtonModel implements ButtonModel, Serializable
{
return listenerList.getListeners(listenerType);
}
-
+
/**
- * Add an ActionListener to the model. Usually only called to subscribe
- * an AbstractButton's listener to the model.
+ * Add an ActionListener to the model. Usually only called to subscribe an
+ * AbstractButton's listener to the model.
*
* @param l The listener to add
*/
@@ -162,10 +178,10 @@ public class DefaultButtonModel implements ButtonModel, Serializable
{
listenerList.add(ActionListener.class, l);
}
-
+
/**
- * Remove an ActionListener to the model. Usually only called to
- * unsubscribe an AbstractButton's listener to the model.
+ * Remove an ActionListener to the model. Usually only called to unsubscribe
+ * an AbstractButton's listener to the model.
*
* @param l The listener to remove
*/
@@ -185,8 +201,8 @@ public class DefaultButtonModel implements ButtonModel, Serializable
}
/**
- * Add an ItemListener to the model. Usually only called to subscribe
- * an AbstractButton's listener to the model.
+ * Add an ItemListener to the model. Usually only called to subscribe an
+ * AbstractButton's listener to the model.
*
* @param l The listener to add
*/
@@ -196,8 +212,8 @@ public class DefaultButtonModel implements ButtonModel, Serializable
}
/**
- * Remove an ItemListener to the model. Usually only called to
- * unsubscribe an AbstractButton's listener to the model.
+ * Remove an ItemListener to the model. Usually only called to unsubscribe
+ * an AbstractButton's listener to the model.
*
* @param l The listener to remove
*/
@@ -217,8 +233,8 @@ public class DefaultButtonModel implements ButtonModel, Serializable
}
/**
- * Add a ChangeListener to the model. Usually only called to subscribe
- * an AbstractButton's listener to the model.
+ * Add a ChangeListener to the model. Usually only called to subscribe an
+ * AbstractButton's listener to the model.
*
* @param l The listener to add
*/
@@ -228,8 +244,8 @@ public class DefaultButtonModel implements ButtonModel, Serializable
}
/**
- * Remove a ChangeListener to the model. Usually only called to
- * unsubscribe an AbstractButton's listener to the model.
+ * Remove a ChangeListener to the model. Usually only called to unsubscribe
+ * an AbstractButton's listener to the model.
*
* @param l The listener to remove
*/
@@ -258,61 +274,60 @@ public class DefaultButtonModel implements ButtonModel, Serializable
public void fireItemStateChanged(ItemEvent e)
{
ItemListener[] ll = getItemListeners();
-
+
for (int i = 0; i < ll.length; i++)
ll[i].itemStateChanged(e);
}
/**
* Inform each ActionListener in the {@link listenerList} that an
- * ActionEvent has occurred. This happens in response to the any change
- * to the {@link stateMask} field which makes the enabled, armed and
- * pressed properties all simultaneously <code>true</code>.
+ * ActionEvent has occurred. This happens in response to the any change to
+ * the {@link stateMask} field which makes the enabled, armed and pressed
+ * properties all simultaneously <code>true</code>.
*
* @param e The ActionEvent to fire
*/
public void fireActionPerformed(ActionEvent e)
{
ActionListener[] ll = getActionListeners();
-
+
for (int i = 0; i < ll.length; i++)
ll[i].actionPerformed(e);
}
/**
- * Inform each ChangeListener in the {@link listenerList} that a
- * ChangeEvent has occurred. This happens in response to the any change
- * to a property of the model.
- *
- * @param event The ChangeEvent to fire
+ * Inform each ChangeListener in the {@link listenerList} that a ChangeEvent
+ * has occurred. This happens in response to the any change to a property
+ * of the model.
*/
public void fireStateChanged()
{
ChangeListener[] ll = getChangeListeners();
-
+
for (int i = 0; i < ll.length; i++)
ll[i].stateChanged(changeEvent);
}
/**
- * Helper method to fire a ChangeEvent with the model as the event's
- * source.
+ * Helper method to fire a ChangeEvent with the model as the event's source.
+ *
+ * @param stateflag DOCUMENT ME!
+ * @param b DOCUMENT ME!
*/
protected void changeState(int stateflag, boolean b)
- {
+ {
int oldstate = stateMask;
int newstate;
if (b)
newstate = oldstate | stateflag;
else
- newstate = oldstate & ~stateflag;
+ newstate = oldstate & ~ stateflag;
if (oldstate == newstate)
return;
- if ((stateflag != SELECTED)
- && (stateflag != ENABLED)
+ if ((stateflag != SELECTED) && (stateflag != ENABLED)
&& (stateMask & ENABLED) == 0)
return;
@@ -320,35 +335,38 @@ public class DefaultButtonModel implements ButtonModel, Serializable
fireStateChanged();
- if ((oldstate & SELECTED) == 0
- && (newstate & SELECTED) == SELECTED)
- fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
- null, ItemEvent.SELECTED));
+ if ((oldstate & SELECTED) == 0 && (newstate & SELECTED) == SELECTED)
+ {
+ fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
+ null, ItemEvent.SELECTED));
+ if (group != null)
+ group.setSelected(this, true);
+ }
- else if ((oldstate & SELECTED) == SELECTED
- && (newstate & SELECTED) == 0)
- fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
- null, ItemEvent.DESELECTED));
-
- else if (((oldstate & ARMED) == ARMED && (oldstate & PRESSED) == PRESSED)
- &&
- ((newstate & ARMED) == ARMED && (newstate & PRESSED) == 0))
+ else if ((oldstate & SELECTED) == SELECTED && (newstate & SELECTED) == 0)
{
- fireActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
- actionCommand));
+ fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
+ null, ItemEvent.DESELECTED));
+ if (group != null)
+ group.setSelected(this, false);
}
- }
-
+
+ else if (((oldstate & ARMED) == ARMED && (oldstate & PRESSED) == PRESSED)
+ && ((newstate & ARMED) == ARMED && (newstate & PRESSED) == 0))
+ fireActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
+ actionCommand));
+ }
+
/**
* Get the value of the model's "armed" property.
- *
- * @return The current "armed" property
+ *
+ * @return The current "armed" property
*/
public boolean isArmed()
{
return (stateMask & ARMED) == ARMED;
}
-
+
/**
* Set the value of the model's "armed" property.
*
@@ -365,9 +383,9 @@ public class DefaultButtonModel implements ButtonModel, Serializable
* @return The current "enabled" property.
*/
public boolean isEnabled()
- {
+ {
return (stateMask & ENABLED) == ENABLED;
- }
+ }
/**
* Set the value of the model's "enabled" property.
@@ -385,9 +403,9 @@ public class DefaultButtonModel implements ButtonModel, Serializable
* @param p The new "pressed" property
*/
public void setPressed(boolean p)
- {
+ {
changeState(PRESSED, p);
- }
+ }
/**
* Get the value of the model's "pressed" property.
@@ -409,7 +427,6 @@ public class DefaultButtonModel implements ButtonModel, Serializable
changeState(ROLLOVER, r);
}
-
/**
* Set the value of the model's "selected" property.
*
@@ -436,9 +453,9 @@ public class DefaultButtonModel implements ButtonModel, Serializable
* @return The current "rollover" property
*/
public boolean isRollover()
- {
+ {
return (stateMask & ROLLOVER) == ROLLOVER;
- }
+ }
/**
* Get the value of the model's "mnemonic" property.
@@ -446,7 +463,7 @@ public class DefaultButtonModel implements ButtonModel, Serializable
* @return The current "mnemonic" property
*/
public int getMnemonic()
- {
+ {
return mnemonic;
}
@@ -459,15 +476,15 @@ public class DefaultButtonModel implements ButtonModel, Serializable
{
if (mnemonic != key)
{
- mnemonic = key;
- fireStateChanged();
+ mnemonic = key;
+ fireStateChanged();
}
}
-
+
/**
- * Set the value of the model's "actionCommand" property. This property
- * is used as the "command" property of the {@link ActionEvent} fired
- * from the model.
+ * Set the value of the model's "actionCommand" property. This property is
+ * used as the "command" property of the {@link ActionEvent} fired from the
+ * model.
*
* @param s The new "actionCommand" property.
*/
@@ -475,11 +492,11 @@ public class DefaultButtonModel implements ButtonModel, Serializable
{
if (actionCommand != s)
{
- actionCommand = s;
- fireStateChanged();
+ actionCommand = s;
+ fireStateChanged();
}
- }
-
+ }
+
/**
* Returns the current value of the model's "actionCommand" property.
*
@@ -491,9 +508,9 @@ public class DefaultButtonModel implements ButtonModel, Serializable
}
/**
- * Set the value of the model's "group" property. The model is said to be
- * a member of the {@link ButtonGroup} held in its "group" property, and
- * only one model in a given group can have their "selected" property be
+ * Set the value of the model's "group" property. The model is said to be a
+ * member of the {@link ButtonGroup} held in its "group" property, and only
+ * one model in a given group can have their "selected" property be
* <code>true</code> at a time.
*
* @param g The new "group" property
@@ -502,8 +519,8 @@ public class DefaultButtonModel implements ButtonModel, Serializable
{
if (group != g)
{
- group = g;
- fireStateChanged();
+ group = g;
+ fireStateChanged();
}
}