/** Palindrome
 @version 0.3a
 Copyright (c) 1997-2008, @author Bruno Andrighetto
 All Rights Reserved

This applet allows the user to enter in a string, and tests that string to see if it
 is a palindrome.

*/

// Palindrome tester in Java
// 21-05-08
// version 0.3a
//		- changes to work with modern JVMs
// 19-07-97
// version 0.3
//		- removed output stmt to improve performance
//		- uses drawString to report result
//		- added status line msg: 'Checking input ...'
// version 0.2
//		- ignores case, spaces and various punctuation marks
// version 0.1
//		- first, classic version (refer CCS Ada prac 2)

import java.awt.*;
import java.applet.Applet;

public class Palindrome extends Applet
{
	// create some panels:
	Panel Panel1 = new Panel();
	Panel Panel2 = new Panel();
	Panel Panel3 = new Panel();

	String inputString;
	Label inputLabel;
	TextField inputField;
	Label resultLabel;
	TextField resultField;
	boolean isPalindrome = true;
	String ignoreTheseCharacters = new String(" .,:;'!?-");
	private Font outputFont;

	public void init()
	{
		setBackground(Color.white);

		// create a layout for the panels
		setLayout(new BorderLayout());
		add("North", Panel1);
		add("Center", Panel2);
		add("South", Panel3);

		inputLabel = new Label("Enter a string:");
		inputField = new TextField(50);
		inputField.setText("A man, a plan, a canal: Panama");
		resultLabel = new Label("Result:");
		//resultField = new TextField(24);
		//resultField.setEditable(false);
		Panel1.add(inputLabel);
		Panel2.add(inputField);
		//Panel2.add(resultLabel);
		//Panel2.add(resultField);
		Panel3.add(new Button("Test for Palindrome"));

		outputFont = new Font("Helvetica", Font.BOLD, 18);
	}

	public void paint( Graphics g ) {
		showStatus("  Checking input ...");
		Graphics gr = Panel2.getGraphics();
		gr.setColor(Color.white);
		gr.fillRect(0, 0, size().width, size().height);
		gr.setColor(Color.red);
		gr.setFont(outputFont);
		if (isPalindrome) {
			gr.setColor(Color.green);
			gr.drawString("  Input string IS a palindrome  ", 135, 72);
			// resultField.setText("Input string IS a palindrome");
		}
		else {
			gr.setColor(Color.red);
			gr.drawString("Input string is NOT a palindrome", 135, 72);
			// resultField.setText("Input string is NOT a palindrome");
		}
		gr.setColor(Color.black);
		showStatus("  A very simple applet that tests for palindromes");
	}


	public boolean action(Event event, Object ob)
	{
		inputString = new String(inputField.getText()).toUpperCase();
		if ((event.target == inputField) || ("Test for Palindrome".equals(ob)))
		{
			if (inputString == null)
				isPalindrome = true;
			else
				isPalindrome = palindrome(inputString);
			repaint();
		}
		return true;
	}

	public boolean palindrome(String s)
	{
		int len = s.length();

		// System.out.println("Testing substring: " + s);

		// 0 or 1 character => palindrome
		if (len <= 1)
			return(true);
		else
		{
			// first strip off punctuation, spaces
			if (ignoreTheseCharacters.indexOf((int) s.charAt(0)) != -1)
				return(palindrome(s.substring(1, len)));
			if (ignoreTheseCharacters.indexOf((int) s.charAt(len-1)) != -1)
				return(palindrome(s.substring(0, len-1)));

			// recursively check that first and last characters of substring are the same
			if (s.charAt(0) == s.charAt(len-1))
				return(palindrome(s.substring(1, len-1)));
			else
				return(false);
		}
	}
	
}
