import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.TextField;

import javax.swing.*;

/**
 * @author Michael Norton (original setListener method)
 * @author Nancy Harris (for 2013 exam)
 * @version 1.0
 * 
 */
public class RecursiveColors extends JFrame {
	private final int WINDOW_WIDTH = 500; // Window width
	private final int WINDOW_HEIGHT = 500; // Window height
	private JPanel panelBig;
	private JPanel panel11;
	private JPanel panel12;
	private JPanel panel13;

	/**
	 * Constructor
	 */
	public RecursiveColors() {
		// Set the title bar text.
		setTitle("RecursiveColor-FinalExam");

		// Set the size of the window.
		setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

		// Specify an action for the close button.
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		panelBig = new JPanel();

		// Add a GridLayout manager to the content pane.
		panelBig.setLayout(new GridLayout(1, 3, 10, 10));

		// Add the panel to the content pane
		buildPanel11();
		panelBig.add(panel11); // Goes into row 1, column 1

		buildPanel12();
		panelBig.add(panel12); // Goes into row 1, column 2

		buildPanel13();
		panelBig.add(panel13); // Goes into row 1, column 3

		add(panelBig);
		reColor(panelBig, Color.YELLOW, Color.RED);
		// Display the window.
		pack();
		setVisible(true);
	}

	/**
	 * builds the first panel
	 */
	private void buildPanel11() {
		panel11 = new JPanel();
		JTextField text = new JTextField();
		text.setText("I am a text field");
		panel11.add(text);
		text = new JTextField();
		text.setText("I am another text field");
		panel11.add(text);
	}

	/**
	 * Builds the 2nd panel
	 */
	private void buildPanel12() {
		panel12 = new JPanel();
		JTextField text = new JTextField();
		text.setText("More text");
		panel12.add(text);
		JButton aButton = new JButton("Button");
		panel12.add(aButton);
	}

	/**
	 * Builds the third panel
	 * 
	 */
	private void buildPanel13() {
		JButton button1, button2, button3;
		JTextField area1, area2;
		JPanel panel4;

		panel13 = new JPanel();
		panel13.setLayout(new GridLayout(2, 2));

		button1 = new JButton("1,1");
		panel13.add(button1);

		button2 = new JButton("1,2");
		panel13.add(button2);

		button3 = new JButton("2,1");
		panel13.add(button3);

		panel4 = new JPanel();
		panel4.setLayout(new BorderLayout());
		area1 = new JTextField("Hello");
		area2 = new JTextField("Goodbye");
		panel4.add(area1, BorderLayout.NORTH);
		panel4.add(area2, BorderLayout.SOUTH);

		panel13.add(panel4);
	}

	/**
	 * The main method creates an instance of the PatchworkSampler class,
	 * causing it to display its window.
	 */

	public static void main(String[] args) {
		RecursiveColors rc = new RecursiveColors();
	}

	/**
	 * Recursively color all JTextFields and JButtons 
	 * on a Graphical Form. 
	 * 
	 * @param the
	 *            root JPanel
	 */
	private void reColor(JPanel panel, Color bColor, Color tColor) {
		Component[] panelItems = panel.getComponents();
		for (int i = 0; i < panelItems.length; i++) {
			if (panelItems[i] instanceof JTextField) {
				panelItems[i].setBackground(tColor);
			} 
			else if (panelItems[i] instanceof JButton) {
				panelItems[i].setBackground(bColor);
				((JButton) panelItems[i]).setBorderPainted(false);
				((JButton) panelItems[i]).setOpaque(true);
			} 
			else 
			{ 
				// You fill in the rest

			}

		}
	}
}
