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

public class UpperLowerCaseConverter3 extends JApplet implements ActionListener {
	private JLabel labelIn, labelLower, labelUpper, labelProper;
	private JTextField stuffIn, lowerOut, upperOut;
	private JTextField 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 );
		lowerOut.setEditable( false );
		c.add( lowerOut );

		labelUpper = new JLabel( "Text in upper case" );
		c.add( labelUpper );

		upperOut = new JTextField( 30 );
		upperOut.setEditable( false );
		c.add( upperOut );

		labelProper = new JLabel( "Text in proper case" );
		c.add( labelProper );

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