/*
 * Created on Apr 6, 2003
 *
 * To change this generated comment go to 
 * Window>Preferences>Java>Code Generation>Code Template
 */
package colorpicker;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;


/**
 * @author Christoph Denzler
 */
public class ColorPicker extends JFrame {
	private ColorModel color      = new ColorModel();
	
	private JScrollBar redslider  = new JScrollBar(SwingConstants.HORIZONTAL);
	private JScrollBar greenslider= new JScrollBar(SwingConstants.HORIZONTAL);
	private JScrollBar blueslider = new JScrollBar(SwingConstants.HORIZONTAL);
	private JTextField redvalue   = new JTextField("0", 3);
	private JTextField greenvalue = new JTextField("0", 3);
	private JTextField bluevalue  = new JTextField("0", 3);
	private JLabel redlabel       = new JLabel("0");
	private JLabel greenlabel     = new JLabel("0");
	private JLabel bluelabel      = new JLabel("0");
	private JButton darker        = new JButton("Darker");
	private JButton brighter      = new JButton("Brighter");
	private JMenuBar menuBar      = new JMenuBar();
	private JMenu fileMenu        = new JMenu("File");
	private JMenuItem exit      = new JMenuItem("Exit");
	private JMenu attrMenu        = new JMenu("Attributes");
	private JMenuItem red       = new JMenuItem("red");
	private JMenuItem green     = new JMenuItem("green");
	private JMenuItem blue      = new JMenuItem("blue");
	private JMenuItem yellow    = new JMenuItem("yellow");
	private JMenuItem cyan      = new JMenuItem("cyan");
	private JMenuItem magenta   = new JMenuItem("magenta");
	private JMenuItem black     = new JMenuItem("black");
	private JMenuItem white     = new JMenuItem("white");
	private JPanel application    = new JPanel();
	private JPanel colorPanel     = new JPanel();
	
	public static void main(String[] args) {
		ColorPicker cp = new ColorPicker();
		cp.setVisible(true);
	}

	/**
	 * @throws java.awt.HeadlessException
	 */
	public ColorPicker() throws HeadlessException {
		super();
		
		// set component properties
		// Menu
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		red.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				color.setColor(Color.RED);
			}
		});
		
		green.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				color.setColor(Color.GREEN);
			}
		});
		
		blue.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				color.setColor(Color.BLUE);
			}
		});
		
		yellow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				color.setColor(Color.YELLOW);
			}
		});
		
		magenta.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				color.setColor(Color.MAGENTA);
			}
		});
		
		cyan.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				color.setColor(Color.CYAN);
			}
		});
		
		white.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				color.setColor(Color.WHITE);
			}
		});
		
		black.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				color.setColor(Color.BLACK);
			}
		});
		
		ColorAdjustor adjustor = new ColorAdjustor();
		color.addColorListener(adjustor);
		
		redslider.setBackground(Color.RED);
		redslider.setValues(0, 10, 0, 265);
		redslider.getModel().addChangeListener(new ChangeListener () {
			public void stateChanged(ChangeEvent e) {
				color.setRed(((BoundedRangeModel)e.getSource()).getValue());
			}
		});
		
		greenslider.setBackground(Color.GREEN);
		greenslider.setValues(0, 10, 0, 265);
		greenslider.getModel().addChangeListener(new ChangeListener () {
			public void stateChanged(ChangeEvent e) {
				color.setGreen(((BoundedRangeModel)e.getSource()).getValue());
			}
		});
		
		blueslider.setBackground(Color.BLUE);
		blueslider.setValues(0, 10, 0, 265);
		blueslider.getModel().addChangeListener(new ChangeListener () {
			public void stateChanged(ChangeEvent e) {
				color.setBlue(((BoundedRangeModel)e.getSource()).getValue());
			}
		});
		
		redvalue.addActionListener(new ActionListener () {
			public void actionPerformed(ActionEvent e) {
				try {
					int col = Integer.decode(redvalue.getText()).intValue();
					if (col >= 0 && col <= 255) color.setRed(col);
				} catch (NumberFormatException nfe) {
					// not nice
				}
			}
		});
		
		greenvalue.addActionListener(new ActionListener () {
			public void actionPerformed(ActionEvent e) {
				try {
					int col = Integer.decode(redvalue.getText()).intValue();
					if (col >= 0 && col <= 255) color.setGreen(col);
				} catch (NumberFormatException nfe) {
					// not nice
				}
			}
		});
		
		bluevalue.addActionListener(new ActionListener () {
			public void actionPerformed(ActionEvent e) {
				try {
					int col = Integer.decode(redvalue.getText()).intValue();
					if (col >= 0 && col <= 255) color.setBlue(col);
				} catch (NumberFormatException nfe) {
					// not nice
				}
			}
		});
		
		darker.addActionListener(new ActionListener () {
			public void actionPerformed(ActionEvent e) {
				color.setDarker();
			}
		});
		
		brighter.addActionListener(new ActionListener () {
			public void actionPerformed(ActionEvent e) {
				color.setBrighter();
			}
		});
		
		colorPanel.setBorder(new LineBorder(Color.BLACK));
		colorPanel.setBackground(color.getColor());
		
		
		// place components components
		redslider.setBounds(0, 2, 150, 20);
		greenslider.setBounds(0, 27, 150, 20);
		blueslider.setBounds(0, 52, 150, 20);
		redvalue.setBounds(160, 2, 50, 20);
		greenvalue.setBounds(160, 27, 50, 20);
		bluevalue.setBounds(160, 52, 50, 20);
		redlabel.setBounds(215, 2, 50, 20);
		greenlabel.setBounds(215, 27, 50, 20);
		bluelabel.setBounds(215, 52, 50, 20);
		colorPanel.setBounds(10, 82, 120, 120);
		darker.setBounds(140, 112, 90, 25);
		brighter.setBounds(140, 147, 90, 25);
		this.setSize(240, 255);
		
		// compose menu
		this.setJMenuBar(menuBar);
		menuBar.add(fileMenu);
			fileMenu.add(exit);
		menuBar.add(attrMenu);
			attrMenu.add(black);
			attrMenu.add(white);
			attrMenu.add(red);
			attrMenu.add(green);
			attrMenu.add(blue);
			attrMenu.add(yellow);
			attrMenu.add(cyan);
			attrMenu.add(magenta);

		// compose GUI	
		this.getContentPane().add(application);
		application.setLayout(null);
		application.add(redslider);
		application.add(greenslider);
		application.add(blueslider);
		application.add(redvalue);
		application.add(greenvalue);
		application.add(bluevalue);
		application.add(redlabel);
		application.add(greenlabel);
		application.add(bluelabel);
		application.add(colorPanel);
		application.add(darker);
		application.add(brighter);
	}
	
	private class ColorAdjustor implements ColorListener {
		public void colorChanged(ColorEvent e) {
			int red = e.getRed();
			int green = e.getGreen();
			int blue = e.getBlue();
			
			colorPanel.setBackground(e.getColor());
			redslider.setValue(red);
			greenslider.setValue(green);
			blueslider.setValue(blue);
			redlabel.setText(Integer.toString(red,16).toUpperCase());
			greenlabel.setText(Integer.toString(green,16).toUpperCase());
			bluelabel.setText(Integer.toString(blue,16).toUpperCase());
			redvalue.setText(Integer.toString(red));
			greenvalue.setText(Integer.toString(green));
			bluevalue.setText(Integer.toString(blue));
		}
	}

}
