// ListenerFrame.java
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;

/*
 Demonstrates bringing up a frame with a bunch of controls in it.
 Demonstrates using buttons and sliders with  annonymous inner class listeners.
 
 The Widget class displays a number using a large font, and responds to the
 increment() and setCount() messages.
*/
public class ListenerFrame extends JFrame {
	private Widget center;
	private Widget north;
	
	public ListenerFrame() {
		super("ListenerFrame");
		
		JComponent container = (JComponent) getContentPane();
		
		// Put in a border layout
		container.setLayout(new BorderLayout());
		
		// Put a couple widgets directly in the border layout
		north = new Widget(100, 100);
		container.add(north, BorderLayout.NORTH);
		
		center = new Widget(100, 100);
		container.add(center, BorderLayout.CENTER);
		
		
		JButton button;
		
		// Put a button in the west, and connect it to the center
		button = new JButton("Increment");
		container.add(button, BorderLayout.WEST);
		button.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					// note: we can access ivars of our "outer" object
					center.increment();
				}
			}
		);
		
		
		// Create a vertical box, put it in the east, put things in it
		Box box = Box.createVerticalBox();
		container.add(box, BorderLayout.EAST);
		
		box.add(new JLabel("Box with things in it"));
		button = new JButton("Clear North");
		box.add(button);
		button.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					north.setCount(0);
				}
			}
		);
		button = new JButton("Clear Center");
		box.add(button);
		button.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					center.setCount(0);
				}
			}
		);
		
		// Create a vertical box, put it in the south
		// Put panels in it which group things
		box = Box.createVerticalBox();
		container.add(box, BorderLayout.SOUTH);
		
		JPanel panel = new JPanel();
		box.add(panel);
		panel.add(new JLabel("North slider"));
		
		// note: store slider as "final" variable so inner class
		// can see it
		final JSlider slider = new JSlider(0, 100, 0);	// (min, max, current)
		panel.add(slider);
		
		slider.addChangeListener(
			new ChangeListener() {
				public void stateChanged(ChangeEvent e) {
					// note: can access final stack var "slider"
					north.setCount(slider.getValue());
				}
			}
		);
		
		
		panel = new JPanel();
		box.add(panel);
		panel.add(new JLabel("Center slider"));
		JSlider slider2 = new JSlider(0, 100, 0);
		panel.add(slider2);
		
		slider2.addChangeListener(
			new ChangeListener() {
				public void stateChanged(ChangeEvent e) {
					JSlider s = (JSlider) e.getSource();
					center.setCount(s.getValue());
				}
			}
		);
		
		button = new JButton("Beep");
		box.add(button);
		button.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Toolkit.getDefaultToolkit().beep();
				}
			}
		);
		
		// Put in the XComponent we used as an earlier example
		final XComponent xcomp = new XComponent(200,200);
		box.add(xcomp);
		JSlider slider3 = new JSlider(0, 100, 0);
		box.add(slider3);
		slider3.addChangeListener(
			new ChangeListener() {
				public void stateChanged(ChangeEvent e) {
					// note: another way to get a pointer to the source
					JSlider s = (JSlider) e.getSource();
					xcomp.setCount(s.getValue());
				}
			}
		);

		
		// Quit on window close
		addWindowListener(
			new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					System.exit(0);
				}
			}
		);
		// in 1.2 this works: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		pack();
		setVisible(true);
		
	}
	
	public static void main(String[] args) {
		new ListenerFrame();
	}	
}
