/*****************************************************************************
 * @(#)Ceasar.java
 *
 *  Project Name: Ceasar Encryption
 *
 *  Author: Bryan T. Biedenkapp
 *  Date: 6/11/04
 *
 *****************************************************************************
 *
 *  Class: Ceasar
 *  Version: 1.01
 *
 *****************************************************************************
 *
 * Revision History:
 *
 *	Version 1.01:
 *		- Added FormattedButton to allow for a short and sweet
 *		  formatted output.
 *		- Added key limits! (Thanx, George)
 *
 *	Version 1.00:
 *		- Revision History Started
 *
 *****************************************************************************
 * Project Notes:
 * ...
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.io.*;
import java.net.*;
import java.util.*;
 
public final class Ceasar implements ActionListener
{
    /////////////////////////////////
    // Constants

	private String Program = "Ceasar Encryption";
	private String Version = "Version 1.01";
    
    private JFrame ceasarFrame = new JFrame(Program);
    private Container ceasarContentPane = 
    		ceasarFrame.getContentPane();

    private JLabel keyLabel;
    private JLabel textLabel;
    
    private JTextField keyField;
    private JTextField textField;
    
    private JRadioButton encryptRadio;
    private JRadioButton decryptRadio;
   
    private JButton goButton;
    private JButton resetButton;
    private JButton aboutButton;
    private JButton formattedButton;

	private int key;												// Our Encryption Key
	private char 													// Character Set
		 alpha[] = { ' ', '.', ',', '!', '?', '<', '>', '|', '[', ']',  //
					 '{', '}', '-', '+', '=', '_', '(', ')', '*', '&',  // 
					 '^', '%', '$', '#', '@', '!', '`', '~', ':', ';',  // 
					 '"', '\'','1/', '\\', 'a', 'b', 'c', 'd', 'e', 'f', //
					 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',  //
					 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',  // 
					 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',  // 
					 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',  //
					 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3',  //
					 '4', '5', '6', '7', '8', '9', '0' };
    /////////////////////////////////

    /////////////////////////////////
    // Functions of Ceasar class

	public Ceasar()
	{
		System.out.println(Program + " " + Version);
		System.out.println("(c)Copyright Bryan T. Biedenkapp., 2004");

		run();
	}
	
	public void run()
	{
		// Set our layout, and setup all our buttons and fields
		// with the proper labels.
		ceasarContentPane.setLayout(new BorderLayout(5, 5));
		
		goButton = new JButton("Go");
		resetButton = new JButton("Reset");
		aboutButton = new JButton("About");
		formattedButton = new JButton("Formatted Output");
		
		keyLabel = new JLabel("Key (Make number > 10 and < 20)");
		textLabel = new JLabel("Text");
		
		keyField = new JTextField();
		textField = new JTextField(30);
		
		encryptRadio = new JRadioButton("Encrypt", true);
		decryptRadio = new JRadioButton("Decrypt", false);
		
		// Set the key label, text label, key field
		// and text field in to specified panels.
		JPanel p1 = new JPanel(new BorderLayout());
		JPanel p1a = new JPanel(new BorderLayout());
		p1a.add(keyLabel, BorderLayout.NORTH);
		p1a.add(keyField, BorderLayout.SOUTH);

		JPanel p1b = new JPanel(new BorderLayout());
		p1b.add(textLabel, BorderLayout.NORTH);
		p1b.add(textField, BorderLayout.SOUTH);
		p1.add(p1a, BorderLayout.NORTH);
		p1.add(p1b, BorderLayout.SOUTH);
	
		// Add the panels to the content pane
		ceasarContentPane.add(p1, BorderLayout.CENTER);
		
		// Setup our button group for our radio buttons
		ButtonGroup bg = new ButtonGroup();
		bg.add(encryptRadio);
		bg.add(decryptRadio);

		// Set the go button, reset button, about button
		// encrypt radio, and decrypt radio button's into
		// specified panels
		JPanel p2 = new JPanel(new BorderLayout());

		JPanel p2a = new JPanel(new BorderLayout());
		JPanel p2a1 = new JPanel(new BorderLayout());
		p2a1.add(goButton, BorderLayout.WEST);
		p2a1.add(resetButton, BorderLayout.CENTER);
		p2a1.add(aboutButton, BorderLayout.EAST);
		p2a.add(p2a1, BorderLayout.CENTER);
		p2.add(p2a, BorderLayout.NORTH);

		JPanel p2b = new JPanel(new BorderLayout());
		p2b.add(encryptRadio, BorderLayout.WEST);	
		p2b.add(decryptRadio, BorderLayout.CENTER);
		p2b.add(formattedButton, BorderLayout.EAST);
		formattedButton.setEnabled(false);
		p2.add(p2b, BorderLayout.SOUTH); 

		// Add the panels to the content pane
		ceasarContentPane.add(p2, BorderLayout.SOUTH);
		
		// Set the buttons to have action listeners
		goButton.addActionListener(this);
		resetButton.addActionListener(this);
		aboutButton.addActionListener(this);
		formattedButton.addActionListener(this);
		
		// Setup our window listner in case we decide to close
		// the window with the "X"!
		ceasarFrame.addWindowListener(
			new WindowAdapter()
			{
				public void windowClosing(WindowEvent event)
				{
    		  		System.exit(0);
				}
			}
		);

		// Pack, and show the frame
		ceasarFrame.pack();
		ceasarFrame.setResizable(false);
		
		ceasarFrame.show();
	}

    public void actionPerformed(ActionEvent e) 
    {
		if(e.getActionCommand().equals("Go"))
		{
			// Preform the encryption or decryption depending on
			// the state of the radio buttons, if for some REALLY
			// extremly retarted reason the radio buttons are not set
			// do the default thing and encrypt.
			// By the way, DO NOT DO ANYTHING IF THE KEY FIELD
			// OR TEXT FIELD'S ARE EMPTY!!
			if( !keyField.getText().equals("") )
			{
				Integer keyVal = new Integer(keyField.getText());
				key = keyVal.intValue();
				
				if( key <= 10 )
				{
					key = 10;
				}
				
				if( key >= 20 )
				{
					key = 20;
				}	
				
				if( !textField.getText().equals("") )
				{
					// Check the radio button states and do the stated task
					// once done set to the opposite radio button!
					if( encryptRadio.isSelected() )
					{
					  textField.setText(encrypt(textField.getText()));
		
					  encryptRadio.setSelected(false);
					  decryptRadio.setSelected(true);
					  
					  formattedButton.setEnabled(true);
					} 
					else if( decryptRadio.isSelected() )
					{
					  textField.setText(decrypt(textField.getText()));
		
					  encryptRadio.setSelected(true);
					  decryptRadio.setSelected(false);
					  
					  formattedButton.setEnabled(false);
					}
					else
					{
					  textField.setText(encrypt(textField.getText()));
		
					  encryptRadio.setSelected(false);
					  decryptRadio.setSelected(true);
					  
					  formattedButton.setEnabled(true);
					}
				}
			}
		}	  
		else if(e.getActionCommand().equals("Reset"))
		{
			// Reset's the text fields and radio buttons
			// to default states.
			keyField.setText("");
			textField.setText("");
			
			encryptRadio.setSelected(true);
			decryptRadio.setSelected(false);
			
			formattedButton.setEnabled(false);
		}
		else if(e.getActionCommand().equals("About"))
		{
			// Pop-up the Message Box with our about information
			JOptionPane.showMessageDialog(ceasarContentPane, Program + " " + Version + "\n(c)Copyright Bryan T. Biedenkapp., 2004\n\nProgram created in Mrs. Bucaria\'s 2003/2004\nAP Computer Science Class", Program + " - About", JOptionPane.INFORMATION_MESSAGE);
		}
		else if(e.getActionCommand().equals("Formatted Output"))
		{
			// Pop-up the Message Box with our formatted output information
			JOptionPane.showMessageDialog(ceasarContentPane, textField.getText() + ", Key = " + keyField.getText(), Program + " - Formatted Output", JOptionPane.INFORMATION_MESSAGE);
		}
    }

	/*
	 *  Function Description:
	 *		Encrypts the input string using the "Ceasarian method" and then
	 *		returns the encrypted string.
	 *
	 *	Arguments:
	 *		message - String to be encrypted
	 *
	 *	Returns:
	 *		crypto, Type String - Encrypted message
	 *
	 *	Notes:
	 *		May be buggy, lag off on using this for a while.
	 *
	 */	
	public String encrypt(String message)
	{
		String crypto = new String();
		char messCrypt[] = message.toCharArray();
		
		for( int x = 0; x < messCrypt.length; x++ )
		{
			for( int y = 0; y < alpha.length; y++ )
			{
				if( messCrypt[x] == alpha[y] )
				{
					if( (y + key > alpha.length) )
					{
						crypto += messCrypt[x];
					}
					else crypto += alpha[y + key];
				}
			}
		}
		
		return crypto;
	}

	/*
	 *  Function Description:
	 *		Decrypts the input string using the "Ceasarian method" and then
	 *		returns the decrypted string.
	 *
	 *	Arguments:
	 *		message - String to be decrypted
	 *
	 *	Returns:
	 *		crypto, Type String - Decrypted message
	 *
	 *	Notes:
	 *		May be buggy, lag off on using this for a while.
	 *
	 */	
	public String decrypt(String message)
	{
		String decrypto = new String();
		char messCrypt[] = message.toCharArray();
		
		for( int x = 0; x < messCrypt.length; x++ )
		{
			for( int y = 0; y < alpha.length; y++ )
			{
				if( messCrypt[x] == alpha[y] )
				{
					if( (y - key < 0) )
					{
						decrypto += messCrypt[x];
					}
					else decrypto += alpha[y - key];
				}
			}
		
		}
		return decrypto;
	}
	
	public static void main(String [] args)
	{
		new Ceasar();
	}
    /////////////////////////////////
}
