
import java.awt.*;
import java.applet.*;
import java.util.*;

/**
 *  Use this applet when you can't figure out a "Word Jumble".
 *  Just enter the characters from the Jumble, and press the calculate button.
 *  This applet determines all of the possible words from the characters
 *  you provide. (Warning: Six characters creates 720 possible word
 *  combinations; 7 characters creates over 5,000 combinations.)
 *
 *  @author Developer's Daily (http://www.devdaily.com)
 *  @version 1.0
 */
public class JumbleApplet extends Applet {

    /**
     *  The "calc" button was clicked; determine what characters were entered
     *  by the user, and run the calculations.
     */
	void calcButton_Clicked(Event event) {

		//{{CONNECTION
		// Clear the text for TextArea
		outputTextArea.setText("");
		//}}
		
        statusLabel.setText("Calculating possibilities ...");
		String s = textField1.getText();
		int numChars = s.length();
		
		//--------------------------------------------------------//
		// create the current "chars" array.                      //
		// this is the array of characters that the user enters.  //
		//--------------------------------------------------------//

		chars = new char[numChars];     // size "chars" to be as large as the string
	
		for (int i=0; i<numChars; i++) {
		    chars[i] = s.charAt(i);
		}    
		
        //-----------------------------------------//
        // okay, run the actual calculations here  //
        //-----------------------------------------//

	    jumbleCalc.setPossibleChars(chars);              // define array of possible characters

	    int vectorSize = 1;
	    for (int i=numChars; i==1; i--) {
	        vectorSize = vectorSize * i;
	    }
	    Vector vectorOfStrings = new Vector(vectorSize);

	    jumbleCalc.runCalcs(vectorOfStrings);         // run the calculations
		
        if (vectorOfStrings.size() > 0 ) {
		    for (int i=0; i<vectorOfStrings.size(); i++) {
		        String tmp = (String) vectorOfStrings.elementAt(i);
		        outputTextArea.appendText(tmp + "\n");
		    }
		} else {
		        outputTextArea.setText("oops - that didn't work\n");
		}
		
        statusLabel.setText("Enter your Jumble characters");

	}


    /**
     *  Initialize all of the necessary applet objects.
     */
	public void init() {
		super.init();

		//{{INIT_CONTROLS
		setLayout(null);
		addNotify();
		resize(294,304);
		setBackground(new Color(16777215));
		textField1 = new java.awt.TextField();
		textField1.reshape(120,24,156,19);
		add(textField1);
		label1 = new java.awt.Label("Jumble Characters:");
		label1.reshape(12,24,108,22);
		label1.setFont(new Font("Dialog", Font.PLAIN, 12));
		add(label1);
		calcButton = new java.awt.Button("Calculate!");
		calcButton.reshape(12,60,81,20);
		calcButton.setFont(new Font("Dialog", Font.PLAIN, 12));
		add(calcButton);
		outputTextArea = new java.awt.TextArea();
		outputTextArea.setEditable(false);
		outputTextArea.reshape(120,60,156,193);
		outputTextArea.setFont(new Font("Dialog", Font.PLAIN, 12));
		outputTextArea.setBackground(new Color(12632256));
		add(outputTextArea);
		statusLabel = new java.awt.Label("Enter your Jumble characters");
		statusLabel.reshape(12,264,262,18);
		statusLabel.setFont(new Font("Dialog", Font.PLAIN, 12));
		statusLabel.setForeground(new Color(8421504));
		add(statusLabel);
		//}}

		jumbleCalc = new ThreadedJumbleCalc();

	}

    /**
     *  Simple applet GUI event handling.
     */
	public boolean handleEvent(Event event) {
		if (event.target == calcButton && event.id == Event.ACTION_EVENT) {
			calcButton_Clicked(event);
			return true;
		}
		return super.handleEvent(event);
	}

	//{{DECLARE_CONTROLS
	java.awt.TextField textField1;
	java.awt.Label label1;
	java.awt.Button calcButton;
	java.awt.TextArea outputTextArea;
	java.awt.Label statusLabel;
	//}}
	
	char[] chars;
	
	ThreadedJumbleCalc jumbleCalc;
}
