package apps.pint;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

import java.util.*;

import cnrg.itx.ds.*;

public class JAutoTextField extends JTextField implements KeyListener {
	private Auto theList;
	public static void main(String args[]) {
		//Test struct

		Auto a = new Auto();
		a.insert("aaa");
		a.insert("aa");
		a.insert("bbb");
		a.insert("abb");
		System.out.println(a.complete("a"));
		System.out.println(a.complete("aa"));
		System.out.println(a.complete("ab"));
		System.out.println(a.complete("c"));
		System.out.println(a.complete("b"));
		
		
	}
	
	public JAutoTextField() {
		super();
		theList = new Auto();
		this.addKeyListener(this);
		
	}
	
	public JAutoTextField(String s) {
		super(s);
		theList = new Auto();
		this.addKeyListener(this);
	}
	
	public void populate(Vector v) {
		for (Enumeration e = v.elements();e.hasMoreElements();) {
			String st = ((UserProperty)e.nextElement()).getUserID().toString();
			theList.insert(st);
		}
	}
	
	
	
	/* ** key listener	 */
	public void keyTyped(KeyEvent ke) {}

	public void keyPressed(KeyEvent ke) {}

	public void keyReleased(KeyEvent ke) {
		int kc = ke.getKeyCode();
		if (!ke.isActionKey()	&& kc!=ke.VK_DELETE 
								&& kc!=ke.VK_BACK_SPACE 
								&& kc!=ke.VK_SHIFT 
								&& kc!= ke.VK_ALT 
								&& kc!=ke.VK_CONTROL) {
			String startChars = this.getText();
			String finalString = theList.complete(startChars);
			if (!finalString.equals("")) {
				this.setText(finalString);
				this.setSelectionStart(startChars.length());
				this.setSelectionEnd(finalString.length());
			}
		}
	}
}
	
	class Auto {
		public String text = "";
		public Hashtable next = new Hashtable();
		public String firstMatch = "";
		public void Auto() {
		}
		
		public void insert(String s) {
			insert(s,s) ;
				}
							
		public void insert(String st, String whole) {
			if (!st.equals("")) {
				Character key = new Character(st.charAt(0));
				if (next.containsKey(key)) { //if already in our hashtable, pass on to next
					((Auto)next.get(key)).insert(st.substring(1),whole);
				} else {
					//we need a new entry
					Auto a = new Auto();
					next.put(key,a);//add to our hashtable
					a.text = ""+st.charAt(0);
					a.firstMatch = whole;
					//a.insert(st.substring(1),whole);
				}
			}
		}
		public String complete(String st) {
			String toAdd = "";
			Auto currentNode = this; 
			int currentIndex = 0;
			Character ch;
			boolean match = false;
			boolean deep = false;
			boolean empty = false;
			
			while (
				   (!currentNode.next.isEmpty()) && //we're not at a leaf (stop at leaf)
				   (currentIndex < st.length()) && //we're not looking deeper into the tree than our string length
				   (currentNode.next.containsKey(ch = new Character(st.charAt(currentIndex))))  ) { //the string is in the tree
				currentNode = (Auto)currentNode.next.get(ch); //get the node corresponding to the next character
				currentIndex++;
				
			}
			toAdd = currentNode.firstMatch;

			match = toAdd.startsWith(st);
			
			
			return ((toAdd.length() > st.length()) && match)?toAdd:""; // if we don't match, return ""
		}
	}

