import java.io.BufferedReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Set;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.Container;

/**
 *This Example illustrates the ClosedHashTable class
 *@author Bill Kraynek
*/


public class ClosedHashTableExample {
	final String DELIMITERS = " :;,&$%@^[]?_#{}|().+-/*=<>!\t\n\r\'\"\\";
	final int WIDTH = 100;
	

    /**
     * Parse the String file for 'words' and store unique words in the OpenHashTable words
     * @param words is the ClosedHashTable
     * @param file the input file as a String
     */         
     void getWords(Set words, String file) {
     	StringTokenizer parser = new StringTokenizer(file,DELIMITERS);
     	while( parser.hasMoreTokens() ) {
     		words.add(parser.nextToken());
     	} // end while
     } // end getWords
     
    public ClosedHashTableExample() throws IOException {
    	BufferedReader in = new BufferedReader(OpenFiles.getNameAndOpenInput());
    	String file = "";
    	String aLine;
    	while( (aLine = in.readLine()) != null ) {
    		file = file + aLine + '\n';
    	} // end while
      	Set words = new ClosedHashTable();
      	getWords(words,file);
      	new Output((ClosedHashTable)words);
    } // end main
    
    class Output {
    	Output(ClosedHashTable words) {
    		JFrame aFrame = new JFrame();
    		aFrame.setSize(800,600);
    		aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Container content = aFrame.getContentPane();
    		JTextArea outArea = new JTextArea(20,100);
    		JScrollPane outPane = new JScrollPane(outArea);
    		content.add(outPane);
    		aFrame.setVisible(true);
    		outArea.setText("   The " + words.size() + " words:\n" + hashTableToString(words));
    		outArea.append("\n" + words.buckets());
    	} // end constructor
    } // end Output
    
    public String hashTableToString(ClosedHashTable H) {
    	final int WIDTH = 100;
     	String out = "    ";
	 	int width = 0;
	 	Iterator i = H.iterator();
	 	while( i.hasNext() ) {
	 		String w = i.next().toString();
	    	width += w.length();
	    	out += w;
	    	if( width < WIDTH ) {
	    		out += " ";
	    	} else {
	    		out += "\n    ";
	    		width = 0;
	    	} // end if
	    } // end while
	    return out;
    } // end hasTableToString

    public static void main(String args[]) throws IOException {
    	new OpenHashTableExample();
    } // end main  


} // end ClosedHashTableExample
