// Joseph Bergin
// Pace University
// May, 1996, November, 1997


import java.awt.*;
import java.awt.event.*;

/**
* Provides a dialog box prompt for user input of long and double values.  The dialog
* is modal and does not return (unless specifically cancelled) until the user enters a 
* valid value of the right type. <p>
* 
* To use a Prompter, create one, passing it a Frame (NOT null).  
* You then just call getLong, getDouble... supplying a prompt string that informs
* the user what is required.  A dialog box will be put up with a button labeled
* with the type of data required.  The user pushes this button when data have been
* added to the Reply field.  The dialog will not accept illegal values. 
* The cancel button will cancel the function and will return default values (zero). 
* <p> Sources: <a href = Prompter.java> Prompter.java </a> 
*/

public class Prompter
{	/** Create a new Prompter.
	* @param parent The Prompter's parent frame.  Must not be null.
	*/
	public Prompter(Frame parent)
	{	itsFrame = parent;
	}
	
	/** Prompt for and return a long value.
	* @param prompt A string to be shown in the dialog box to help the user.
	* @return the long entered by the user. 
	*/
	public long getLong(String prompt)
	{	InputDialog longDialog = new InputDialog(itsFrame, prompt, "Long");
		longDialog.requestFocus();
		longDialog.setVisible(true);
		long value = longValue;
		longValue = 0;
		return value;
	}
	
	/** Prompt for and return a double value.
	* @param prompt A string to be shown in the dialog box to help the user.
	* @return the double entered by the user. 
	*/	
	public double getDouble(String prompt)
	{	InputDialog doubleDialog = new InputDialog(itsFrame, prompt, "Double");
		doubleDialog.requestFocus();
		doubleDialog.setVisible(true);
		double value = doubleValue;
		doubleValue = 0.0;
		return value;
	}
	
	private void resetValues(long v, double fv)
	{	longValue = v;
		doubleValue = fv;
	} 

	private Frame itsFrame;
	private long longValue = 0;
	private double doubleValue = 0.0;
	
	// inner
	private class InputDialog extends Dialog
	{	public InputDialog(Frame parent, String prompt, String tag)
		{	super(parent, "Input Required", true);
			Panel tempPanel = new Panel();
			tempPanel.setLayout(new GridLayout(2,2));
			tempPanel.add(new Label(""));
			tempPanel.add(new TextField(prompt, prompt.length()));
			tempPanel.add(new Label("    Reply"));
			tempPanel.add(response = new TextField(""));
			add("Center", tempPanel);
			tempPanel = new Panel();
			Button b;
			tempPanel.add(b = new Button(tag));
			ActionListener a;
			b.addActionListener(a = new GotItListener(Prompter.this));
			response.addActionListener(a);
			tempPanel.add(b = new Button("Cancel"));
			b.addActionListener(new CancelListener(Prompter.this));
			add("South", tempPanel);
			setSize(300, 80);
			setLocation(100,100);
			if( tag.equals("Double") || tag.equals("double"))wantlong = false;
			setResizable(false);
		}
		
		boolean wantlong = true;
		TextField response;	
		
		public void requestFocus()
		{	response.requestFocus();
		}
		
		class GotItListener implements ActionListener
		{	GotItListener(Prompter prompter){this.prompter = prompter;}
			Prompter prompter;
			
			public void actionPerformed(ActionEvent e)
			{	boolean done = false;
				String raw = response.getText().trim();
				if(wantlong)
				{	try
						{	prompter.resetValues(Long.parseLong(raw), 0.0);
							InputDialog.this.setVisible(false);
							InputDialog.this.dispose();
						}
						catch(NumberFormatException ex){ return;}
				}
				else
				{	try
						{	prompter.resetValues(0, Double.valueOf(raw).doubleValue());
							InputDialog.this.setVisible(false);
							InputDialog.this.dispose();
						}
						catch(NumberFormatException ex){ return;}
				}
			}
		}

		class CancelListener implements ActionListener
		{	CancelListener(Prompter prompter){this.prompter = prompter;}
			Prompter prompter;
			
			public void actionPerformed(ActionEvent e)
			{	InputDialog.this.setVisible(false);
				InputDialog.this.dispose();
				prompter.resetValues(0, 0.0);
			}
		}
		
	}

}

