package usinggui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import gui.*;
import inheritance.*;


/**
 * A dialog box that is used to enter a TextMessage
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class TextMessageDialog extends    JDialog
			       implements ActionListener
{
    private boolean        shouldReturnValue;
    private JButton        cancelButton, okButton;
    private JTextArea      ta;
    private SliderPanel    sp;

    /**
     * Constructor
     *
     * @param frame  The owner
     */
    public TextMessageDialog(Frame frame)
    {
	super(frame, true); // true so it is modal
	Container           contentPane;
	JPanel              southPanel;
	JScrollPane         scrollPane;


	shouldReturnValue = false;
	setTitle("Enter a priority and message:");

	contentPane = getContentPane();
	contentPane.setLayout(new BorderLayout());


	ta = new JTextArea();
	scrollPane = new JScrollPane(ta,
			 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
			 JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	contentPane.add(scrollPane, BorderLayout.CENTER);

	sp = new SliderPanel(0, 10, 1);
	contentPane.add(sp, BorderLayout.NORTH);

	southPanel = new JPanel();

	  southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

	  okButton = new JButton("OK");
	  okButton.setMnemonic('O');
	  okButton.addActionListener(this);

	  cancelButton = new JButton("Cancel");
	  cancelButton.setMnemonic('C');
	  cancelButton.addActionListener(this);

	  southPanel.add(okButton);
	  southPanel.add(cancelButton);

	contentPane.add(southPanel, BorderLayout.SOUTH);

	setSize(400,200);
    }




    /**
     * Get the newly-entered TextMessage
     *
     * @return  A new TextMessage
     */
    public TextMessage getTextMessage()
    {
	TextMessage    message;

	message = null;
	if (shouldReturnValue) 
        {
	    message = new TextMessage(ta.getText() + "  " + sp.getValue());
	}

	return message;
    }



    /**
     * Handle action events (required by ActionListener)
     *
     * @param evt   The event
     */
    public void actionPerformed(ActionEvent evt)
    {
	boolean   done;
	String    command;

	done = false;
	command = evt.getActionCommand();

	if (command.equals("OK")) 
        {
	    shouldReturnValue = true;
	    done = true;
	} 
        else if (command.equals("Cancel")) 
        {
	    done = true;
	}

	if (done) setVisible(false);
    }
}
