// UpperLowerCaseConverter2.java
// Converts text to upper and lower case and tokens to proper case
// APPLET CODE="UpperLowerCaseConverter2" HEIGHT=500 WIDTH=400
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class UpperLowerCaseConverter2 extends JApplet implements ActionListener {
	private JLabel labelIn, labelLower, labelUpper, labelProper;
	private JTextField stuffIn, lowerOut, upperOut;
	private JTextArea properOut;
	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 );
		properOut = new JTextArea( 1, 30 );
		properOut.setEditable( false );
		c.add( properOut );
	}

	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 );
		properOut.setText( "" );  // clears text area
		
		// output tokens converting first letter to upper case, remainder to lower case
		while ( tokens.hasMoreTokens( ) ) {
			String currToken = tokens.nextToken( );
			properOut.append( currToken.substring( 0, 1).toUpperCase( ) + currToken.substring( 1 ).toLowerCase( ) + " " );
		}
	}
}