// Written by Brad Simonin.
// This Java Applet captures information from various objects and passes the 
// information to a Java Servlet which then loads the data into an Access 97 
// database using the JDBC:ODBC Bridge.


import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
import java.util.*;


public class LaunchBradsWindow extends Applet implements ActionListener
{
	
	private Frame aboutFrame = new Frame("About");
	private Scrollbar scrollVertical;
	private Scrollbar scrollHorizontal;
	private TextField txtName;
	private TextField txtOccupation;
	private TextField txtCity;
	private Choice txtState;
	private TextField txtEmail;
	private TextField txtWebPage;
	private TextArea txtComments;
	private TextArea Status;
	private Toolkit theKit;
	private URL url;
	private String TheState;
	private Button Submit;
	private MenuBar MyMenuBar;
	private Menu menuFile;
	private MenuItem About;
	private MenuItem Exit;
	private Font MyFont = new Font("TimesRoman",Font.BOLD,12);
	private boolean InstanceCreated = false;
	private boolean FirstTime = true;
	private MessageBox MissingInfo;
	private MessageBox Completed;
	private BradsGuestBookFrame TheWindow;
	private Button ShowFrame;
	
	public void init()
	{
		ShowFrame = new Button("Sign my Guest Book");
		ShowFrame.addActionListener(this);
		ShowFrame.setActionCommand("Show");
		ShowFrame.addMouseListener(new MouseHandler());
		ShowFrame.setBackground(Color.magenta);
		ShowFrame.setForeground(Color.black);
		add(ShowFrame);
		if (FirstTime = true)
		TheWindow = new BradsGuestBookFrame("Brads GuestBook using Java Applet to Servlet");
	} //end init method
	
	public void actionPerformed( ActionEvent e )
	{
		String ButtonPushed;
		ButtonPushed = e.getActionCommand().toString();
		
		if (ButtonPushed == "Show" && InstanceCreated == false)
		{
			TheWindow = new BradsGuestBookFrame("Brads GuestBook using Java Applet to Servlet");
		}	
	} //end actionPerformed method
	
	
	public class BradsGuestBookFrame extends Frame implements ActionListener
	{
		public BradsGuestBookFrame(String Title) 
		{
			super(Title);
		
			Panel TextPanel = new Panel();
			Panel ButtonPanel = new Panel();
			
			MyMenuBar = new MenuBar();
			menuFile = new Menu("File");
			menuFile.setFont(MyFont);
			About = new MenuItem("About");
			About.addActionListener(this);
			About.setActionCommand("About");
			About.setFont(MyFont);
			Exit = new MenuItem("Exit");
			Exit.addActionListener(this);
			Exit.setActionCommand("Exit");
			Exit.setFont(MyFont);
			
			menuFile.add(About);
			menuFile.add(Exit);
			MyMenuBar.add(menuFile);
			setMenuBar(MyMenuBar);
		
			Label lblStatus = new Label("Status:");
			lblStatus.setForeground(Color.yellow);
			lblStatus.setSize(10,10);
			lblStatus.setFont(MyFont);
			Status = new TextArea(1,35);
			Status.setEditable(false);
			Status.setBackground(Color.yellow);
			Status.setForeground(Color.black);
			Status.setText("Fill out the form below and press Submit");
			
			Label lblName = new Label("Name:");
			lblName.setForeground(Color.white);
			lblName.setSize(10,10);
			lblName.setFont(MyFont);
			txtName = new TextField(30);
			txtName.setBackground(Color.white);
			txtName.setForeground(Color.black);
		
			Label lblOccupation = new Label("Occupation:");
			lblOccupation.setForeground(Color.white);
			lblOccupation.setSize(10,10);
			lblOccupation.setFont(MyFont);
			txtOccupation = new TextField(30);
			txtOccupation.setBackground(Color.white);
			txtOccupation.setForeground(Color.black);
		
			Label lblCity = new Label("City:");
			lblCity.setForeground(Color.white);
			lblCity.setSize(10,10);
			lblCity.setFont(MyFont);
			txtCity = new TextField(30);
			txtCity.setBackground(Color.white);
			txtCity.setForeground(Color.black);
		
			Label lblState = new Label("State:");
			lblState.setForeground(Color.white);
			lblState.setSize(10,10);
			lblState.setFont(MyFont);
			txtState = new Choice();
			txtState.addItemListener(new ChoiceHandler());
			txtState.setBackground(Color.white);
			txtState.setForeground(Color.black);
			txtState.add("Pick One");
			txtState.add("None/Other");
			txtState.add("Alabama");
			txtState.add("Alaska");
			txtState.add("Arizona");
			txtState.add("Arkansas");
			txtState.add("California");
			txtState.add("Colorado");
			txtState.add("Connecticut");
			txtState.add("Delaware");
			txtState.add("District of Columbia");
			txtState.add("Florida");
			txtState.add("Georgia");
			txtState.add("Hawaii");
			txtState.add("Idaho");
			txtState.add("Illinois");
			txtState.add("Indiana");
			txtState.add("Iowa");
			txtState.add("Kansas");
			txtState.add("Kentucky");
			txtState.add("Louisiana");
			txtState.add("Maine");
			txtState.add("Maryland");
			txtState.add("Massachusetts");
			txtState.add("Michigan");
			txtState.add("Minnesota");
			txtState.add("Mississippi");
			txtState.add("Missouri");
			txtState.add("Montana");
			txtState.add("Nebraska");
			txtState.add("Nevada");
			txtState.add("New Hampshire");
			txtState.add("New Jersey");
			txtState.add("New Mexico");
			txtState.add("New York");
			txtState.add("North Carolina");
			txtState.add("North Dakota");
			txtState.add("Ohio");
			txtState.add("Oklahoma");
			txtState.add("Oregon");
			txtState.add("Pennsylvania");
			txtState.add("Rhode Island");
			txtState.add("South Carolina");
			txtState.add("South Dakota");
			txtState.add("Tennessee");
			txtState.add("Texas");
			txtState.add("Utah");
			txtState.add("Vermont");
			txtState.add("Virginia");
			txtState.add("Washington");
			txtState.add("West Virginia");
			txtState.add("Wisconsin");
			txtState.add("Wyoming");	
			TheState = txtState.getItem(0).toString();
						
			Label lblEmail = new Label("E-Mail:");
			lblEmail.setForeground(Color.white);
			lblEmail.setSize(10,10);
			lblEmail.setFont(MyFont);
			txtEmail = new TextField(30);
			txtEmail.setBackground(Color.white);
			txtEmail.setForeground(Color.black);
			
			Label lblWebPage = new Label("Web Page:");
			lblWebPage.setForeground(Color.white);
			lblWebPage.setSize(10,10);
			lblWebPage.setFont(MyFont);
			txtWebPage = new TextField(30);
			txtWebPage.setBackground(Color.white);
			txtWebPage.setForeground(Color.black);
		
			Label lblComments = new Label("Thoughts or Comments:");
			lblComments.setForeground(Color.white);
			lblComments.setSize(10,10);
			lblComments.setFont(MyFont);
			txtComments = new TextArea(5,60);
			txtComments.setBackground(Color.white);
			txtComments.setForeground(Color.black);
		
			Submit = new Button("Submit");
			Submit.addActionListener(this);
			Submit.setActionCommand("Submit");
			Submit.addMouseListener(new MouseHandler());
			Submit.setBackground(Color.green);
			Submit.setForeground(Color.black);
		
			Button Clear = new Button("Clear");
			Clear.addActionListener(this);
			Clear.setActionCommand("Clear");
			Clear.addMouseListener(new MouseHandler());
			Clear.setBackground(Color.green);
			Clear.setForeground(Color.black);
		
			Button Quit = new Button("Quit");
			Quit.addActionListener(this);
			Quit.setActionCommand("Quit");
			Quit.addMouseListener(new MouseHandler());
			Quit.setBackground(Color.green);
			Quit.setForeground(Color.black);
		
			theKit = getToolkit();
			Dimension wndsize = theKit.getScreenSize();
			setBounds(0, 0, // Position 
				wndsize.width, wndsize.height);    // Size
		
			GridBagLayout layout = new GridBagLayout();
			GridBagConstraints constraints = new GridBagConstraints();
			constraints.anchor = constraints.NORTHWEST;
			constraints.fill = constraints.NONE;
			constraints.insets = new Insets(5,0,0,0);
			TextPanel.setLayout(layout);
			
		
			constraints.gridx = 0;
			constraints.gridy = 0;
			layout.setConstraints(lblStatus, constraints);
			TextPanel.add(lblStatus);
			
			constraints.gridx = 1;
			constraints.gridy = 0;
			layout.setConstraints(Status, constraints);
			TextPanel.add(Status);
			
			constraints.gridx = 0;
			constraints.gridy = 1;
			layout.setConstraints(lblName, constraints);
			TextPanel.add(lblName);
			
			constraints.gridx = 1;
			constraints.gridy = 1;
			layout.setConstraints(txtName, constraints);
			TextPanel.add(txtName);
			
			constraints.gridx = 0;
			constraints.gridy = 2;
			layout.setConstraints(lblOccupation, constraints);
			TextPanel.add(lblOccupation);
			
			constraints.gridx = 1;
			constraints.gridy = 2;
			layout.setConstraints(txtOccupation, constraints);
			TextPanel.add(txtOccupation);
			
			constraints.gridx = 0;
			constraints.gridy = 3;
			layout.setConstraints(lblCity, constraints);
			TextPanel.add(lblCity);
			
			constraints.gridx = 1;
			constraints.gridy = 3;
			layout.setConstraints(txtCity, constraints);
			TextPanel.add(txtCity);
			
			constraints.gridx = 2;
			constraints.gridy = 3;
			layout.setConstraints(lblState, constraints);
			TextPanel.add(lblState);
			
			constraints.gridx = 3;
			constraints.gridy = 3;
			layout.setConstraints(txtState, constraints);
			TextPanel.add(txtState);
			
			constraints.gridx = 0;
			constraints.gridy = 4;
			layout.setConstraints(lblEmail, constraints);
			TextPanel.add(lblEmail);
			
			constraints.gridx = 1;
			constraints.gridy = 4;
			layout.setConstraints(txtEmail, constraints);
			TextPanel.add(txtEmail);
			
			constraints.gridx = 0;
			constraints.gridy = 5;
			layout.setConstraints(lblWebPage, constraints);
			TextPanel.add(lblWebPage);
			
			constraints.gridx = 1;
			constraints.gridy = 5;
			layout.setConstraints(txtWebPage, constraints);
			TextPanel.add(txtWebPage);
			
			constraints.gridx = 0;
			constraints.gridy = 6;
			layout.setConstraints(lblComments, constraints);
			TextPanel.add(lblComments);
			
			constraints.gridx = 0;
			constraints.gridy = 7;
			constraints.gridwidth = 3;
			layout.setConstraints(txtComments, constraints);
			TextPanel.add(txtComments);
		
			ButtonPanel.add(Submit);
			ButtonPanel.add(Clear);
			ButtonPanel.add(Quit);
			
			constraints.gridx = 1;
			constraints.gridy = 8;
			layout.setConstraints(ButtonPanel, constraints);
			TextPanel.add(ButtonPanel);
			
			setLayout(new BorderLayout());
			
			add(TextPanel, BorderLayout.CENTER);
			setBackground(Color.blue);
			requestFocus();
			txtName.requestFocus();
			showStatus(Title);
			String TheType;
			TheType = "Main";
			addWindowListener(new WindowHandler(TheType));
			setVisible(true);
			InstanceCreated = true;
		} //end BradsGuestBookFrame constructor
	
		public void actionPerformed( ActionEvent e )
		{  
			String ButtonPushed;
			String Name;
			String Occupation;
			String City;
			String State;
			String Email;
			String WebPage;
			String Comments;
			ButtonPushed = e.getActionCommand().toString();
			
			if (ButtonPushed == "About")
			{
				aboutFrame.addWindowListener(new WindowHandler("AboutFrame"));
			
				String aboutText = "Programmed by Bradley K. Simonin \n\n" +
					"This Java Applet is a client/server system that \n" +
					"communicates with a Java Servlet. The Java Applet \n" + 
					"is the client graphical user interface and the \n" +
					"Java Servlet loads the data into an Access 97 \n" +
					"database using the JDBC:ODBC Bridge. \n";
				
				TextArea showAbout = new TextArea(5,10);
				showAbout.setText(aboutText);
				aboutFrame.add(showAbout);	
				aboutFrame.setSize(300,200);
				aboutFrame.setResizable(false);
				aboutFrame.setBackground(Color.red);
				aboutFrame.setForeground(Color.white);
				aboutFrame.setVisible(true);
			}
			if (ButtonPushed == "Submit")
			{
				Name = txtName.getText().trim();
				Occupation = txtOccupation.getText().trim();
				City = txtCity.getText().trim();
				State = TheState.trim();
				Email = txtEmail.getText().trim();
				WebPage = txtWebPage.getText().trim();
				Comments = txtComments.getText().trim();
				
				if (Name.equals(""))
				{
					MissingInfo = new MessageBox(this, "Warning!", "You must enter a name", 170);
					txtName.requestFocus();
					return;
				}
				if (State.equals("") || State.equals("Pick One"))
				{
					MissingInfo = new MessageBox(this, "Warning!", "You must choose a State", 170);
					txtState.requestFocus();
					return;
				}
				
				Submit.setEnabled(false);
				ProcessForm(Name, Occupation, City, State, Email, WebPage, Comments);
				Submit.setEnabled(true);
				
				if (Status.getText().indexOf("Thank you") != -1)
				{
					String CompletedMessage = Status.getText().trim().toString();
					int LenMessage = CompletedMessage.length() + 10;
					txtName.setText("");
					txtOccupation.setText("");
					txtCity.setText("");
					txtEmail.setText("");
					txtWebPage.setText("");
					txtComments.setText("");
					txtState.select(0);
					TheState = txtState.getItem(0).toString();
				}	
			} //end Submit button
			
			if (ButtonPushed == "Clear")
			{
				Status.setText("Fill out the form below and press Submit");
				txtName.setText("");
				txtOccupation.setText("");
				txtCity.setText("");
				txtEmail.setText("");
				txtWebPage.setText("");
				txtComments.setText("");
				txtState.select(0);
				TheState = txtState.getItem(0).toString();
			}
			
			if (ButtonPushed == "Quit" || ButtonPushed == "Exit")
			{
				setVisible(false);
				dispose();
				InstanceCreated = false;
				FirstTime = false;
				System.exit(0);
			}	
		} //end actionPerformed method
		
		
		private void ProcessForm(String TheName, String TheOccupation, 
								String TheCity, String TheState, 
								String TheEmail, String TheWebPage,
								String TheComments) 
		{		
				String PostData = "";
				Status.requestFocus();
				Status.setText("Executing Servlet. . .");
				
				try 
				{
					URL action = new URL("http://128.123.20.93/servlet/GuestBookAppletToServlet");
					URLConnection url = action.openConnection();
					url.setUseCaches(false);
					url.setDoInput(true);
					url.setDoOutput(true);
					url.setRequestProperty("Content-Type",
										   "application/x-www-form-urlencoded");
					PostData = "strName=";
					PostData += URLEncoder.encode(TheName);
					
					if (!TheOccupation.equals(""))
					{
						PostData += "&strCompanyOccupation=";
						PostData += URLEncoder.encode(TheOccupation);
					}
					if (!TheCity.equals(""))
					{
						PostData += "&strCity=";
						PostData += URLEncoder.encode(TheCity);
					}
					if (!TheState.equals(""))
					{
						PostData += "&strState=";
						PostData += URLEncoder.encode(TheState);
					}
					if (!TheEmail.equals(""))
					{
						PostData += "&strEmail=";
						PostData += URLEncoder.encode(TheEmail);
					}
					if (!TheWebPage.equals(""))
					{
						PostData += "&strWebSite=";
						PostData += URLEncoder.encode(TheWebPage);
					}
					if (!TheComments.equals(""))
					{
						PostData += "&strComment=";
						PostData += URLEncoder.encode(TheComments);
					}
							
					DataOutputStream out = new DataOutputStream(url.getOutputStream());
					out.writeBytes(PostData);
					out.flush();
					out.close();
					
					BufferedReader in = new BufferedReader(new InputStreamReader(url.getInputStream() ));
					Status.requestFocus();
					Status.setText("");
					String ReturnFromServlet = null;
					while((ReturnFromServlet = in.readLine()) != null)
					{
						Status.setText(ReturnFromServlet + "\n");
					}
					
					in.close();
				}
				catch (MalformedURLException BadURL)
				{
					Status.setText("BadURL in Applet: " + BadURL.getMessage());
				}
				catch (Exception BadExp)
				{
					Status.setText("BadExp in Applet: " + BadExp.getMessage());
				}
				
		} //end ProcessName method
		
	}  //end BradsGuestBookFrame inner class
	

	class MessageBox extends Dialog implements ActionListener
	{
		private Label TheMessage;
		private Button OKay;
		private Toolkit theKit;
		
		public MessageBox(Frame parent, String title, String MessageString, int width)
		{
			super(parent, title, true); //this is a modal dialog box
			theKit = getToolkit();
			Dimension wndsize = theKit.getScreenSize();
			setBounds(wndsize.width/4, wndsize.height/4,     // Position 
							 width, 120);    // Size
			Panel ButtonPanel = new Panel();
			setResizable(false);
			TheMessage = new Label(MessageString, Label.CENTER);
			OKay = new Button("OK");
			OKay.addActionListener(this);
			OKay.setActionCommand("OK");
			OKay.addMouseListener(new MouseHandler());
			OKay.setBackground(Color.green);
			OKay.setForeground(Color.black);
			TheMessage.setForeground(Color.red);
			TheMessage.setFont(MyFont);
			
			add(TheMessage,BorderLayout.CENTER);
			ButtonPanel.add(OKay);
			add(ButtonPanel, BorderLayout.SOUTH);
			setBackground(Color.white);
			String TheType;
			TheType = "Dialog";
			addWindowListener(new WindowHandler(TheType));
			setVisible(true);
		} //end MessageBox constructor
	
		
		public void actionPerformed(ActionEvent e)
		{
			String ButtonPushed;
			ButtonPushed = e.getActionCommand().toString();
		
			if (ButtonPushed == "OK")
			{
				setVisible(false);
				dispose();
			}
		} //end actionPerformed method
	} //end MessageBox inner class
	
	
	
	class MouseHandler extends MouseAdapter
	{
		Cursor handCursor = new Cursor(Cursor.HAND_CURSOR);
		Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
		
		public void mouseEntered(MouseEvent e)
		{
			e.getComponent().setCursor(handCursor);
		}
		
		public void mouseExited(MouseEvent e)
		{
			e.getComponent().setCursor(defaultCursor);
		}							  
	} //end MouseHandler inner class
	
	
	class ChoiceHandler implements ItemListener
	{
		public void itemStateChanged(ItemEvent e)
		{
			TheState = e.getItem().toString();
		}								  
	} //end ChoiceHandler inner class
	
	
	class WindowHandler implements WindowListener
	{	
		protected String WindowType;
		
		public WindowHandler(String Type)
		{
			WindowType = Type;
		}
		
		public void windowClosing (WindowEvent e)
		{
			if (e.getID() == WindowEvent.WINDOW_CLOSING)
			{			
				e.getWindow().setVisible(false);
				e.getWindow().dispose();
				if (WindowType.equals("Main"))
				{
					InstanceCreated = false;
					System.exit(0);
				}
			}
		} //end windowClosing method
		public void windowOpened (WindowEvent e){}
		public void windowClosed (WindowEvent e){}
		public void windowIconified (WindowEvent e){}
		public void windowDeiconified (WindowEvent e){}
		public void windowActivated (WindowEvent e){}
		public void windowDeactivated (WindowEvent e){}
	} //end WindowHandler inner class
	
} //end LaunchBradsWindow class
