package extensions.awt; import java.awt.*; import java.util.*; /** * Delegate Choice action event. * * @see java.awt.Choice * @see extensions.awt.ActionProtocol * @version 1.0.2 * @author John Webster Small */ public class ChoiceAction extends Choice implements ActionProtocol { private ActionProtocol ap; /** * Add multiple items at once. * @param items items separated by '|', e.g. "one|two|three" */ public void addItems(String items) { StringTokenizer i = new StringTokenizer(items,"|"); while (i.hasMoreTokens()) addItem(i.nextToken()); } /** Return width of maximum length item. */ public int maxCols() { int max = 0; for (int i = 0; i < countItems(); i++) if (max < getItem(i).length()) max = getItem(i).length(); return max; } /** Construct a ChoiceAction (i.e. a choice drop down) * with the action to be performed when this item is * selected. The action is performed automatically * only if this ChoiceAction appears within an * FrameExtended or DialogExtended window. * For example: *
*
* import java.awt.*;
* import extensions.awt.*;
*
* public class Main extends FrameExtended
* implements ActionProtocol
* {
* ChoiceAction choices;
* TextFieldPanel choice;
* public final void action
* (Component forward, Event evt, Object what)
* { choice.getField().setText((String)what); }
* Main()
* {
* super("ChoiceAction Demo",FrameExtended.EXIT_ON_DESTROY);
* add("Center",choices = new ChoiceAction("one|two|three",this));
* add("South",choice = new TextFieldPanel("Choice: ",choices.maxCols()));
* resize(250,100);
* show();
* }
* public static void main(String args[]) { new Main(); }
* }
*
*
* Notice that in the above example that neither the frame's
* handleEvent() or action() methods had to be overrided
* to process the button clicked event!
*
* @param ap action to be performed, i.e. run()
* @see extensions.awt.FrameExtended
* @see extensions.awt.DialogExtended
* @see extensions.awt.ActionProtocol
* @see extensions.awt.MenuItemAction
*/
public ChoiceAction(ActionProtocol ap)
{
super();
this.ap = ap;
}
/**
* Construct a ChoiceAction.
* @param items initial choices
* @param ap action to be performed, i.e. run()
* @see #addItems
* @see extensions.awt.ActionProtocol
*/
public ChoiceAction(String items, ActionProtocol ap)
{
super();
this.ap = ap;
addItems(items);
}
/** Forward the choice selection action event on to
* the action for further processing by the application
* model rather than the GUI. This method is called
* automatically by FrameExtended and DialogExtended.
* @param forward the component forwarding action event
* @param evt the event that caused the action
* @param what the action
* @see extensions.awt.ActionProtocol
* @see extensions.awt.FrameExtended#handleEvent
* @see extensions.awt.DialogExtended#handleEvent
*/
public final void action(Component forward, Event evt, Object what)
{ ap.action(forward,evt,what); }
}