import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.KeyboardFocusManager;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class ClientReferenceSnippet {
		
	private FocusAdapter focusListener = new FocusAdapter() {
		public void focusGained(FocusEvent event) {
			System.out.println("FocusAdapter.focusGained: "+event.getSource());
		}
		public void focusLost(FocusEvent event) {
			System.out.println("FocusAdapter.focusLost: "+event.getSource());
		}
	};

	public static void main(String args[]) {
		new ClientReferenceSnippet().start();
	}

	public void start() {
		GridBagConstraints c = new GridBagConstraints();
		JFrame frame = new JFrame("ClientReferenceSnippet");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel mainPane = new JPanel(new GridBagLayout());
		frame.add(mainPane);

		JTextField textFiel1 = new JTextField(30);
		textFiel1.addFocusListener(focusListener);
		JLabel label1 = new JLabel("TextField1, ohne Rahmen:");
		c.gridx=0; c.gridy=0; c.fill=GridBagConstraints.NONE; c.anchor=GridBagConstraints.NORTHWEST; c.insets=new Insets(2, 2, 2, 2);
		mainPane.add(label1, c);
		c.gridx=1; c.fill=GridBagConstraints.HORIZONTAL;
		mainPane.add(textFiel1, c);
		
		JCheckBox checkBox2 = new JCheckBox();
		checkBox2.addFocusListener(focusListener);
		JLabel label2 = new JLabel("TextField1, mit Rahmen:");
		c.gridx=0; c.gridy=1; c.fill=GridBagConstraints.NONE;
		mainPane.add(label2, c);
		c.gridx=1; c.fill=GridBagConstraints.HORIZONTAL;
		mainPane.add(checkBox2, c);
		System.out.println(checkBox2.getUIClassID());
	
		String[] columnNames = {"First Name",
                        "Last Name",
                        "Sport",
                        "# of Years",
                        "Vegetarian"};
		Object[][] data = {
		    {"Mary", "Campione",
		     "Snowboarding", new Integer(5), new Boolean(false)},
		    {"Alison", "Huml",
		     "Rowing", new Integer(3), new Boolean(true)},
		    {"Kathy", "Walrath",
		     "Knitting", new Integer(2), new Boolean(false)},
		    {"Sharon", "Zakhour",
		     "Speed reading", new Integer(20), new Boolean(true)},
		    {"Philip", "Milne",
		     "Pool", new Integer(10), new Boolean(false)}
		};
		JTable table3 = new JTable(data, columnNames);
		table3.setPreferredScrollableViewportSize(new Dimension(500, 70));
		table3.addFocusListener(focusListener);
		JScrollPane scrollPane3 = new JScrollPane(table3);
		JLabel label3 = new JLabel("Table, ScrolPane, Rahmen:");
		c.gridx=0; c.gridy=2; c.fill=GridBagConstraints.NONE; c.anchor=GridBagConstraints.NORTHWEST;
		mainPane.add(label3, c);
		c.gridx=1; c.fill=GridBagConstraints.BOTH; c.anchor=GridBagConstraints.CENTER;
		mainPane.add(scrollPane3, c);
		
		JButton button1 = new JButton("Button 1");
		button1.addFocusListener(focusListener);
		JButton button2 = new JButton("Button 2");
		button2.addFocusListener(focusListener);
		JPanel pane4 = new JPanel(new FlowLayout());
		pane4.add(button1);
		pane4.add(button2);
		c.gridx=1; c.gridy=3; c.fill=GridBagConstraints.NONE;
		mainPane.add(pane4, c);
		
		KeyboardFocusManager focusMgr = KeyboardFocusManager.getCurrentKeyboardFocusManager();
		focusMgr.addPropertyChangeListener("focusOwner", new PropertyChangeListener(){

			public void propertyChange(PropertyChangeEvent evt) {
				System.out.println(new ReflectionToStringBuilder(evt, ToStringStyle.MULTI_LINE_STYLE).toString());
				evt.getSource(); 	// This is the widget getting the focus.
									// How do we find the matching server side half object? 
			}});
		
		frame.setVisible(true);
		frame.pack();
	}

}
