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;
import java.io.File;
import java.util.Scanner;
import javax.swing.JOptionPane;

/**
 *This Example illustrates the OpenHashTable class
 *@author Bill Kraynek
 */


public class OpenHashTableExample {
    final int WIDTH = 100;
    
    
    public OpenHashTableExample() throws IOException  {
        Scanner in = new Scanner(new File(JOptionPane.showInputDialog("Enter File Name","src/OpenHashTableExample.java")));
        String file = "";
        while( in.hasNext() ) {
            file += in.nextLine() + "\n";
        } // end while
        Set<String> words = new OpenHashTable<String>();
        getWords(words,file);
        new Output((OpenHashTable<String>)words);
    } // end constructor
    
    /**
     * Parse the String file for 'words' and store unique words in the OpenHashTable words
     * @param words is the OpenHashTable
     * @param file the input file as a String
     */
    void getWords(Set<String> words, String file) {
        Scanner parser = new Scanner(file);
        parser.useDelimiter("[^a-zA-Z]+");
        while( parser.hasNext() ) {
            words.add(parser.next());
        } // end while
    } // end getWords
    
    class Output {
        Output(OpenHashTable<String> words) {
            JFrame aFrame = new JFrame();
            aFrame.setSize(800,600);
            aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JTextArea outArea = new JTextArea(20,100);
            JScrollPane outPane = new JScrollPane(outArea);
            aFrame.add(outPane);
            aFrame.setVisible(true);
            outArea.setText("   The " + words.size() + " words:\n" + hashTableToString(words));
            outArea.append("\n" + words.numberInEachBucket());
        } // end constructor
    } // end Output
    
    String hashTableToString(OpenHashTable<String> hashSet) {
        final int WIDTH = 100;
        String out = "    ";
        int width = 0;
        for( String w : hashSet ) {
            width += w.length();
            out += w;
            if( width < WIDTH ) {
                out += " ";
            } else {
                out += "\n    ";
                width = 0;
            } // end if
        } // end for
        return out;
    } // end hashTableToString
    
    public static void main(String args[]) throws IOException {
        new OpenHashTableExample();
    } // end main
    
} // end OpenHashTableExample
