/**
*	Basic output window
*/

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Output extends JPanel {

	private JTextArea outText = new JTextArea(10, 10);
	private JScrollPane outPane = new JScrollPane(outText);
	private JButton btnClear = new JButton("Clear Text");
	
	public Output() {
		setLayout(new BorderLayout());
		setBorder(BorderFactory.createTitledBorder(" Output "));
		add(outPane, BorderLayout.CENTER);
		JPanel p = new JPanel();
		p.add(btnClear);
		add(p, BorderLayout.SOUTH);
		btnClear.addActionListener( new
			ActionListener() {
				public void actionPerformed(ActionEvent e) {
					outText.setText("");
					outText.requestFocus();
				}
			}
		);
	}
	
	public void show(String str) {
		outText.append(str);		
	}
	
	//-----------------------------------------------------------------
	// Main Test/Debug code
	//
	
	public static void main(String[] args) {
		JFrame testWin = new JFrame("Test Window");
		
		testWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container contents = testWin.getContentPane();
		final Output display = new Output();
		
		JPanel sample = new JPanel();
		final JTextField input = new JTextField(5);
		JLabel lblA = new JLabel("Type and press <enter>");
		sample.add(lblA);
		sample.add(input);
		input.addActionListener( new TextFieldAction(display, input) );
		/*
		// The following is an alternative to creating a new class
		input.addActionListener( new
			ActionListener() {
				public void actionPerformed(ActionEvent e) {
					display.show( input.getText() + "\n" );
					input.setText("");
					input.requestFocus();
				}
			}
		);
		*/
		
		contents.add(display, BorderLayout.CENTER);
		contents.add(sample, BorderLayout.NORTH );
		
		testWin.setSize(400, 350);
		testWin.show();
	}
	
	/**
	*	This class is used to handle events for the input text field.
	*	The class must be static because I used it above in a static context.
	*/
	private static class TextFieldAction implements ActionListener {
		Output myDisplay;
		JTextField input;
		
		public TextFieldAction(Output disp, JTextField in) {
			myDisplay = disp;
			input = in;
		}
		
		public void actionPerformed(ActionEvent e) {
			myDisplay.show( input.getText() + "\n" );
			input.setText("");
			input.requestFocus();
		}
	
	}
}