package org.cmdline.sudoku;

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

public class ColorChooser extends javax.swing.JPanel {
	java.util.Vector selected = new java.util.Vector();
	
	public ColorChooser() {
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        setLayout(gbl);
        
        ActionListener color_listener = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                selectColor(((JButton)evt.getSource()).getBackground());
            }
        };
        
        Color[] colors = new Color[12];
        colors[0] = Color.white;
        colors[1] = Color.black;
        
        colors[2] = Color.blue;
        colors[3] = Color.cyan;
        colors[4] = Color.gray;
        colors[5] = Color.green;
        colors[6] = Color.lightGray;
        colors[7] = Color.magenta;
        colors[8] = Color.orange;
        colors[9] = Color.pink;
        colors[10] = Color.red;
        colors[11] = Color.yellow;
        
        // Reserved - shouldn't be included but give the display some symmetry
        selected.addElement(Color.white);
        selected.addElement(Color.black);
        selected.addElement(Color.gray);
        
        c.gridheight = 1;
        c.gridwidth = 1;
        c.fill = c.NONE;
        c.weightx = 1.0;
        c.weighty = 1.0;
        
        for(int i=0; i<3; i++) {
            for(int j=0; j<4; j++) {
                c.gridx=j;
                c.gridy=i;
                
                JButton button = new ColorButton("");
                Dimension dim = new Dimension(25,25);
                button.setSize(dim);
                button.setPreferredSize(dim);
                button.setMinimumSize(dim);
                button.setBackground(colors[j+i*4]);
                gbl.setConstraints(button,c);
                add(button);
                button.addActionListener(color_listener);
            }
        }
        
    }
    
    public void reset() {
    	selected.clear();
    	selectColor(Color.black);
    }
    
    protected Color selectedColor = Color.black;
    public void selectColor(Color newColor) {
    	if (selected.contains(newColor)) return;	// Can't reuse a color
        Color oldColor = selectedColor;
        selectedColor = newColor;
        SolvingSudokuBoard.setCurrColor(newColor);
        firePropertyChange("selectedColor",oldColor, newColor);
    }

}

class ColorButton extends JButton {

	public ColorButton(String title) { super(title); }
	
	public void paintComponent(Graphics g) {
		Color b = getBackground();
		setBackground(Color.white);
		super.paintComponent(g);
		setBackground(b);
		g.setColor(b);
		int w = getWidth();
		int h = getHeight();
		g.fillRect(7,7,w-14,h-14);
	}
	
}