diff --git a/projects/samskivert/src/java/com/samskivert/swing/CommandButton.java b/projects/samskivert/src/java/com/samskivert/swing/CommandButton.java new file mode 100644 index 00000000..7f63e8b7 --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/swing/CommandButton.java @@ -0,0 +1,93 @@ +// +// $Id: CommandButton.java,v 1.1 2003/01/29 22:20:14 ray Exp $ + +package com.samskivert.swing; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.JButton; + +import com.samskivert.swing.event.CommandEvent; + +/** + * A button that fires CommandEvents when it is actioned. + */ +public class CommandButton extends JButton +{ + /** + * Set the argument of the CommandEvents which we generate. + */ + public void setActionArgument (Object arg) + { + _argument = arg; + } + + /** + * Get the argument that we'll use when we generate CommandEvents. + */ + public Object getActionArgument () + { + return _argument; + } + + // documentation inherited + protected void fireActionPerformed (ActionEvent event) + { + Object[] listeners = listenerList.getListenerList(); + ActionEvent e = null; + // Process the listeners last to first, notfiying + // those that are interested in this event + for (int ii = listeners.length - 2; ii >= 0; ii -= 2) { + if (listeners[ii] == ActionListener.class) { + // Lazily create the event: + if (e == null) { + e = createEventToFire(event); + } + ((ActionListener) listeners[ii + 1]).actionPerformed(e); + } + } + } + + /** + * Create the event to fire from the prototype. + * If this method were broken out in AbstractButton, we wouldn't even + * have had to override fireActionPerformed(). + */ + protected ActionEvent createEventToFire (ActionEvent proto) + { + String actionCommand = proto.getActionCommand(); + if (actionCommand == null) { + actionCommand = getActionCommand(); + } + + Object arg = (proto instanceof CommandEvent) + ? arg = ((CommandEvent) proto).getArgument() + : null; + + // if we were passed an actionevent, or if it was a + // command event with a null arg.. + if (arg == null) { + // use ours + arg = getActionArgument(); + } + + if (arg == null) { + // just create a plain actionEvent + return new ActionEvent(CommandButton.this, + ActionEvent.ACTION_PERFORMED, + actionCommand, + proto.getWhen(), + proto.getModifiers()); + } else { + // if we found an arg somewhere, create a commandevent + return new CommandEvent(CommandButton.this, + actionCommand, + arg, + proto.getWhen(), + proto.getModifiers()); + } + } + + /** The CommandEvent argument for our actions. */ + protected Object _argument; +} diff --git a/projects/samskivert/src/java/com/samskivert/swing/event/CommandEvent.java b/projects/samskivert/src/java/com/samskivert/swing/event/CommandEvent.java index acf08e05..9d4648f9 100644 --- a/projects/samskivert/src/java/com/samskivert/swing/event/CommandEvent.java +++ b/projects/samskivert/src/java/com/samskivert/swing/event/CommandEvent.java @@ -1,5 +1,5 @@ // -// $Id: CommandEvent.java,v 1.3 2003/01/03 23:07:33 mdb Exp $ +// $Id: CommandEvent.java,v 1.4 2003/01/29 22:20:14 ray Exp $ package com.samskivert.swing.event; @@ -19,6 +19,14 @@ public class CommandEvent extends ActionEvent _argument = argument; } + public CommandEvent ( + Object source, String command, Object argument, + long when, int modifiers) + { + super(source, ActionEvent.ACTION_PERFORMED, command, when, modifiers); + _argument = argument; + } + /** * Returns the argument provided to the command event at construct * time.