// UpperLowerCaseConverter.java
// Converts text to upper and lower case and tokens to proper case
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class UpperLowerCaseConverter extends JApplet implements ActionListener {
	private JLabel labelIn, labelLower, labelUpper, labelProper;
	private JTextField stuffIn, lowerOut, upperOut;
	private JTextArea outputArea;
	private StringTokenizer tokens;

	public void init( ) {
		Container c = getContentPane( );
		c.setLayout( new FlowLayout( ) );
		labelIn = new JLabel( "Enter a line of text ");
		c.add( labelIn );

		stuffIn = new JTextField( 30 );
		stuffIn.addActionListener( this );
		c.add( stuffIn );

		labelLower = new JLabel( "Text in lower case" );
		c.add( labelLower );
		lowerOut = new JTextField( 30 );
		c.add( lowerOut );

		labelUpper = new JLabel( "Text in upper case" );
		c.add( labelUpper );
		upperOut = new JTextField( 30 );
		c.add( upperOut );
		
		labelProper = new JLabel( "Text in proper case" );
		c.add( labelProper );
		outputArea = new JTextArea( 10, 30 );
		outputArea.setEditable( false );
		c.add( outputArea );
	}

	public void actionPerformed( ActionEvent e ) {
		// convert text to String and output in lower and upper cases
		String stuff = stuffIn.getText( );
		lowerOut.setText( stuff.toLowerCase( ) );
		upperOut.setText( stuff.toUpperCase( ) );

		// convert text to String and then to String tokens
		String stuffToTokenize = stuffIn.getText( );
		StringTokenizer tokens = new StringTokenizer( stuffToTokenize );
		outputArea.setText( "" );  // clears text area
		
		outputArea.append( "Number of tokens:  " + tokens.countTokens( ) + "\nThe tokens in proper case are:\n" );
		
		// output tokens converting first letter to upper case, remainder to lower case
		while ( tokens.hasMoreTokens( ) ) {
			String currToken = tokens.nextToken( );
			outputArea.append( currToken.substring( 0, 1).toUpperCase( ) + currToken.substring( 1 ).toLowerCase( ) + "\n" );
		}
	}
}