Index
PromptWindow.java


/*
 *  "Net Libs" by Copyright (C) Michael Benson - 7/27/97
 *
 *	PromptWindow - opens a window with word prompts for a story.
 */

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.*;


/**
 * PromptWindow implements the class which puts up a prompt
 * window for the user to type in words for the story.
 *  
 * @author	Michael Benson
 */
public class PromptWindow extends Frame implements NetLibsConst
{
	private static final int		_PROMPT_HEIGHT = 25;
	private static final int		_PWIN_WIDTH = 400;
	private static final String		_extraSpace = " ";
	private static final String		_showStory = "Show Story";
	private static final String		_cancelStory = "Cancel";
	private	String					_storyName;
	private	NetLibs					_applet;
	private	Panel					_p1;

	/**
	 * PromptWindow constructor.  Creates the prompt window, but
	 * does not display it.  That's up to whoever calls this.
	 *
	 * @param	applet		the parent applet (used for code base).
	 * @param	prompts		the list of prompt strings.
	 * @param	storyName	name of the story template.
	 */
	public PromptWindow (NetLibs applet, Vector prompts, String storyName)
	{
		super("Fill in the blanks...");

		_applet = applet;
		_storyName = storyName;
		
		int pwinHeight = (prompts.size() + 1) * _PROMPT_HEIGHT;
		resize(_PWIN_WIDTH, pwinHeight);
		_p1 = new Panel();
		setLayout(new BorderLayout());
		add("North", new Label(_extraSpace, Label.CENTER));
		add("West", new Label(_extraSpace, Label.CENTER));
		add("East", new Label(_extraSpace, Label.CENTER));
		Panel p2 = new Panel();
		p2.add(new Button(_cancelStory));
		p2.add(new Button(_showStory));
		add("South", p2);
		add("Center", _p1);
		
		// Arrange prompts and text fields in a 2 by n grid:
		_p1.setLayout(new GridLayout(prompts.size() + 1, 2));
		for (int i = 0;  i < prompts.size();  i++) {
			_p1.add(new Label((String)(prompts.elementAt(i))));
			_p1.add(new TextField());
		}
	}

	/**
	 * Overrides the action method in Component to check for
	 * button clicks.
	 *
	 * @param	evt		the event that caused the action.
	 * @param	arg		the action.
	 * @return	true if the event has been handled; false if not.
	 * @see		java.awt.Component#action()
	 */
	public boolean action(Event evt, Object arg)
	{
		// "Show Story" button clicked:
		if (_showStory.equals(arg)) {
			Vector		strings = new Vector();
			TextField	text;
			Label		prompt;
			String		str;
			String		aoran;
			
			// Get the strings out of the window before closing:
			int numFields = _p1.countComponents() / 2;
			for (int i = 0;  i < numFields;  i++) {
				prompt = (Label)(_p1.getComponent(i * 2));
				text = (TextField)(_p1.getComponent((i * 2) + 1));
				str = text.getText();
				
				// Determine if we need to capitalize the word:
				if (Character.isUpperCase(prompt.getText().charAt(0))) {
					str = BensUtils.capitalizePhrase(str);
				}
				strings.addElement(str);
			}
			
			// Close the window:
			dispose();
			
			// Substitute the words into the story:
			_applet.substituteWords(_storyName, strings);
			_applet.getNetLibsCommand().doCommand(CMD_STORY_GENERATED);
		}
		// "Cancel Story" button clicked:
		else if (_cancelStory.equals(arg)) {
			// Close the window:
			dispose();
			_applet.getNetLibsCommand().doCommand(CMD_STORY_GENERATED);
		}
		else {
			return false;
		}
		
		return true;
	}

}