import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/*
 * Created on Nov 18, 2004. 
 *
 * This applet illustrates the use of StringTokenizer class.
 */

/**
 * @author Aditya Mathur
 *
 */
public class TokenizerIllustrator extends Applet implements ActionListener {
	
	JButton tokenize=new JButton("Tokenize");
	JButton done=new JButton("Done");
	
	JTextArea message=new JTextArea(3,20);
	
	JTextArea stringInput=new JTextArea ("Type a string here", 4, 30);
	JTextField delimiter=new JTextField("Type token delimiter string here.", 10);

	JTextArea tokenOutput=new JTextArea(5, 15);
	
	
	JFrame myFrame=new JFrame("String Tokenizer");
	JPanel p=new JPanel(new GridLayout(2,1));
	
	public void init(){
		
		
		tokenize.addActionListener(this);
		done.addActionListener(this);
		
		// Set colors  and fonts.
		tokenOutput.setBackground(Color.orange);
		p.setBackground(Color.blue);
		stringInput.setBackground(Color.blue);
		stringInput.setForeground(Color.yellow);
		
		message.setFont(new Font("SansSerif", Font.BOLD, 20));
		message.setText("Welcome to CS 177 /178 Programming with Multimedia Objects\n");
		message.append("Fall 2004");
		message.append("String Tokenizer Illustrator");
		
		p.setBackground(Color.blue);
		p.add(tokenize); p.add(done);
		
		this.add(new JTextField("This area is useless."));
		this.add(new JTextField("Close this applet when done"));
		this.setBackground(Color.orange);
		
		// Add panel and widgets to the frame.
		
		myFrame.getContentPane().add(BorderLayout.WEST, p);
		myFrame.getContentPane().add(BorderLayout.NORTH, message);
		myFrame.getContentPane().add(BorderLayout.SOUTH, stringInput); 
		myFrame.getContentPane().add(BorderLayout.CENTER,tokenOutput);
		myFrame.getContentPane().add(BorderLayout.EAST,delimiter);
		
//		 Set some properties of the frame (resizability, color, and size).
		
		myFrame.isResizable(); // Makes the frame resizable.
		myFrame.setBounds(140, 140, 800, 300); // Sets the top left corner coordinbaties to 140, 140 and size to 800x300 pixels.
		
		myFrame.getContentPane().setBackground(Color.orange);
	
		myFrame.show();
		
	}
	
	// Process translate request.
	
	public void actionPerformed (ActionEvent e){
	
		
		Object source=e.getSource();
		
		
		// Fetch string and  the delimiter
		
		if(source==done){
			myFrame.dispose();
		}
		else{  // Otherwise this must be the Tokenize  button click. Process the request.
			processTokenize();
		}
	
	} // End of actionPerformed()
		
	
	// Process convert request.
	
		public void processTokenize(){
			

			String stringTyped;
			
			stringTyped=stringInput.getText(); // Get string typed by user.
			stringInput.setText("You typed: "+stringTyped);
			
			String separator=getSeparator();
			convertToTokens(stringTyped, separator); // This and the next statement can be combined into one statement. Can you do it?
			
			
		} // End of processConvert()

	
	// Get token separator.
	
	public String getSeparator(){
		String d=delimiter.getText();
		
		delimiter.setText("Delimiter: "+d);
		return(d);
	
	}

// Convert the input string to token separated by delimiter.

public void  convertToTokens(String stringTyped, String delimiter){
	
	
	StringTokenizer st=new StringTokenizer(stringTyped, delimiter); 

	tokenOutput.setText("");  // Clear  the token output area.
	
	// Extract and display token until there are no more tokens.
	
	while(st.hasMoreTokens()){
		 tokenOutput.append(st.nextToken()+"\n");
	}
	


} // End of convertToTokens()


	
}
	
	
  
