/*
	Joseph Bergin, Nov. 1997
*/

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class AllenApplet extends Applet
{	public void init() 
	{	resize(460, 280);
		setBackground(Color.yellow); 
		//setLayout(new BorderLayout());
		Component ac = this;
		while(!(ac instanceof Frame)) ac = ac.getParent();
		Frame myFrame = (Frame)ac;

		// Create a new popup menu to display our options
		popup = new PopupMenu("Actiities");
		add(popup);
		
		// Add menu items to the menu.  Give each an ActionListener for its action.
		MenuItem item = new MenuItem("Cyan", new MenuShortcut(KeyEvent.VK_1));
		popup.add(item);
		item.addActionListener(new CyanListener());

		item = new MenuItem("Yellow", new MenuShortcut(KeyEvent.VK_2));
		popup.add(item);
		item.addActionListener(new YellowListener());
		
		item = new MenuItem("UserColor", new MenuShortcut(KeyEvent.VK_3));
		popup.add(item);
		item.addActionListener(new UserColorListener());

		userColorDialog = new ColorSliderDialog(myFrame, this);
		userColorDialog.setLocation(50, 50);

		// Add components to the main window.		
		add("North", userText);
		add("Center", userInput);
		Button okButton = new Button("Add It");
		add("South", okButton);
		OkListener ok = new OkListener();
		okButton.addActionListener(ok);
		userInput.addActionListener(ok);
		userInput.requestFocus();
		
		fonts.addItemListener(new FontListener());
		fonts.addItem("Plain");
		fonts.addItem("Bold");
		fonts.addItem("Italic");
		add(fonts); 
		
		userText.setFont(new Font("Serif", Font.PLAIN, 12));
		userText.setEditable(false);
		
		// Handle mouse clicks in our window.  
		addMouseListener(new MouseWatcher());
		
		// Handle menu shortcuts in our popup menu.  
		addKeyListener(new KeyWatcher());
	}
	
	Dialog userColorDialog;
	TextArea userText = new TextArea(15, 60);
	TextField userInput = new TextField(60);
	PopupMenu popup;
	TextField red = new TextField(6);
	TextField green = new TextField(6);
	TextField blue = new TextField(6);
	Choice fonts = new Choice();
	
	// Inner Classes
	
	class CyanListener implements ActionListener // Menu Cyan
	{	public void actionPerformed(ActionEvent e)
		{	setBackground(new Color(200, 255, 255)); //pale Cyan (RGB)
			repaint();
		}
	}

	class YellowListener implements ActionListener // Menu Yellow...
	{	public void actionPerformed(ActionEvent e)
		{	setBackground(Color.yellow);
			repaint();
		}
	}

	class UserColorListener implements ActionListener // Menu UserColor
	{	public void actionPerformed(ActionEvent e)
		{	userColorDialog.setVisible(true);
			red.requestFocus();
		} 
	}

	class OkListener implements ActionListener // OK button for main window.
	{	public void actionPerformed(ActionEvent e)
		{	String s = userInput.getText();
			userText.append(s + '\n');
			userInput.setText("");
		}
	}

	class FontListener implements ItemListener // Listener for font choice
	{	public void itemStateChanged(ItemEvent e)
		{	int i = fonts.getSelectedIndex();
			Font f = userText.getFont();
			switch(i)
			{	case 0: f = new Font("Serif", Font.PLAIN, 12); break;
				case 1: f = new Font("Serif", Font.BOLD, 12); break;
				case 2: f = new Font("Serif", Font.ITALIC, 12); break;
			}
			userText.setFont(f);
		}
	}
	
	class KeyWatcher extends KeyAdapter
	{	public void keyTyped(KeyEvent e)
		{	char c = e.getKeyChar();
			boolean meta = e.isMetaDown();
			if (!meta) return;
			switch((int)c)
			{	case KeyEvent.VK_1: setBackground(new Color(200, 255, 255)); repaint(); break;
				case KeyEvent.VK_2: setBackground(Color.yellow); repaint(); break;
				case KeyEvent.VK_3: userColorDialog.setVisible(true); red.requestFocus(); break;
			}
		}
	}

	class MouseWatcher extends MouseAdapter // Mouse click handler.
	{	public void mouseClicked(MouseEvent e)
		{	Graphics g = getGraphics();
			int x = e.getX(), y = e.getY();
			if(e.getModifiers() == InputEvent.BUTTON3_MASK) // Right button (Ctrl click on Mac).
			{	Font font = new Font("Serif", Font.ITALIC, 10);
				g.setFont(font);
				g.setColor(Color.red);
				g.drawString("Right Click at:(" + x + "," + y + ")", x, y);
			}
			else // Other buttons.
				g.drawString("Left Click", x, y);
			g.dispose();
		}
		
		public void mousePressed(MouseEvent e)
		{	Graphics g = getGraphics();
			int x = e.getX(), y = e.getY();
			if(e.isPopupTrigger()) popup.show(AllenApplet.this, x, y);
			g.dispose();
		}
	}

}

