Index
NetLibsCommand.java


/*
 *  "Net Libs" by Copyright (C) Michael Benson - 7/27/97
 *
 *  Loosely based on "Mac Libs" which is loosely based on the old
 *  "Mad Libs" books for creating stories from forms.
 */

package NetLibs;

import java.awt.*;
import java.applet.Applet;
import java.util.*;
import java.io.*;
import java.net.*;

import COM.bensoft.base.*;
import COM.bensoft.widgets.*;


/**
 * NetLibsCommand takes care of the multicasting to event
 * listeners for various commands in NetLibs.  I admit, for an applet 
 * as simple as NetLibs, this may be overkill.  But, I wanted to put
 * this mechanism in place for future applications.
 *  
 * @author	Michael Benson
 */
public class NetLibsCommand implements NetLibsConst
{
	private NetLibs					_applet;
	private BensEventMulticaster	_commandListeners;

	/**
	 * NetLibsCommands constructor.  Create multicasters
	 * for various listeners in NetLibs and handle delagating
	 * the commands.
	 *
	 * @param	applet	the NetLibs applet.
	 */
	public NetLibsCommand (NetLibs applet)
	{
		_applet = applet;
		_commandListeners = new BensEventMulticaster();
	}
	
	/**
	 * Determine which command has been issued, call the command
	 * listeners, then do the command.
	 *
	 * @param	command		the command index.
	 * @see		NetLibsConst
	 */
	public synchronized void doCommand(int command)
	{
		boolean		okay = true;
	
		// Let all listeners know which command is in progress:
		if (_commandListeners.hasListeners()) {
			Enumeration e = _commandListeners.elements();
			while (e.hasMoreElements()) {
				okay = ((CommandListener)e.nextElement()).commandSelected(command);
			}
		}
		
		// Do the command:
		if (okay) {
			switch (command) {
			case CMD_AUTO_GENERATE_STORY:
				_applet.autoGenerateStory();
				break;
			case CMD_PROMPT_GENERATE_STORY:
				_applet.promptGenerateStory();
				break;
			default:
				// No action for the default case.
			}
		}
	}
	
	/**
	 * Add a "command" listener.  Used when someone wants to know
	 * that a certain command has been selected.  (For enabling/disabling
	 * buttons, etc.)
	 *
	 * @param	listener	the listener to add.
	 */
	public void addCommandListener(CommandListener listener)
	{
		_commandListeners.addListener(listener);
	}

	/**
	 * Remove a "command" listener.
	 *
	 * @param	listener	the listener to remove.
	 */
	public void removeCommandListener(CommandListener listener)
	{
		_commandListeners.removeListener(listener);
	}
}