/*
	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("Activities");
		add(popup);
		
		// Add menu items to the menu. 
		MenuItem item = new ColorMenuItem("Cyan", new MenuShortcut(KeyEvent.VK_1), new Color(200, 255, 255));
		popup.add(item);

		item = new ColorMenuItem("Yellow", new MenuShortcut(KeyEvent.VK_2), Color.yellow);
		popup.add(item);
		
		popup.add(new UserColorMenuItem());

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

		// Add components to the main window.		
		add("North", userText);
		add("Center", userInput);
		AddButton addButton = new AddButton();
		add("South", addButton);
		userInput.addActionListener(addButton); 
		// The button will be the listener for the field. (Sharing listeners)
		userInput.requestFocus();
		
		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);
	FontChoice fonts = new FontChoice();
	
	// Inner Classes
	
	class ColorMenuItem extends MenuItem implements ActionListener
	{	public ColorMenuItem(String label, MenuShortcut s, Color c)
		{	super(label, s);
			color = c;
			addActionListener(this);
		}
		
		public void actionPerformed(ActionEvent e)
		{	setBackground(color); 
			repaint();
		}	
		
		private Color color;
	}
	
	class UserColorMenuItem extends MenuItem implements ActionListener // Menu UserColor
	{	public UserColorMenuItem()
		{	super("UserColor", new MenuShortcut(KeyEvent.VK_3));
			addActionListener(this);
		}
		
			public void actionPerformed(ActionEvent e)
		{	userColorDialog.setVisible(true);
			red.requestFocus();
		} 
	}

	class AddButton extends Button implements ActionListener // Add button for main window.
	{	public AddButton()
		{	super("Add It");
			addActionListener(this);
		}
	
		public void actionPerformed(ActionEvent e)
		{	String s = userInput.getText();
			userText.append(s + '\n');
			userInput.setText("");
		}
	}

	class FontChoice extends Choice implements ItemListener 
	{	public FontChoice()
		{	super();
			addItemListener(this);
		}
	
		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)
		{	int x = e.getX(), y = e.getY();
			if(e.isPopupTrigger()) popup.show(AllenApplet.this, x, y);
		}
	}

}

