package jbgui;

import java.awt.Frame;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.BorderLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.AdjustmentEvent;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JScrollBar;

public class ColorSliderDialog extends JDialog
{	/** Create a new color slider dialog.  The dialog will accept an rgb color from
		the user and when the "Do It" button is pressed the color of the component named
		in the initialization will be changed to this new color.
	*/
	public ColorSliderDialog(Frame frame, JComponent component)
	{	super(frame, "Color Choice",false);
		this.component = component;
		setSize(250, 125);
		setResizable(false);
		getContentPane().add(dialogPanel, BorderLayout.CENTER);
		dialogPanel.setLayout(new GridLayout(3, 2, 0, 2));
		dialogPanel.add(redLabel);
		dialogPanel.add(red);
		red.setValues(0, 0, 0, 255);
		red.setBackground(Color.red);
		dialogPanel.add(greenLabel);
		dialogPanel.add(green);
		green.setValues(0, 0, 0, 255);
		green.setBackground(Color.green);
		dialogPanel.add(blueLabel);
		dialogPanel.add(blue);	
		blue.setValues(0, 0, 0, 255);
		blue.setBackground(Color.blue);
		JPanel buttonPanel = new JPanel(); // Layout the buttons.  
		getContentPane().add(buttonPanel, BorderLayout.SOUTH);
		buttonPanel.add( new YesButton());
		buttonPanel.add( new NoButton());	
		getContentPane().add(colorPanel, BorderLayout.EAST);
		colorPanel.setBackground(Color.black);
	}
	
	public Color getColor()
	{	return currentColor;
	}
	
	ColorPanel colorPanel = new ColorPanel();
	JPanel dialogPanel = new JPanel(); // Layout the fields and labels
	JScrollBar red = new ColorScroller();
	JScrollBar green = new ColorScroller();
	JScrollBar blue = new ColorScroller();
	JLabel redLabel = new JLabel("Red 0");
	JLabel greenLabel = new JLabel ("Green 0");
	JLabel blueLabel = new JLabel ("Blue 0");
	JComponent component;
	Color currentColor = Color.black;
		
	private class ColorPanel extends JPanel
	{	public Dimension getMinimumSize()
		{	return new Dimension(25, 50);
		}
	}
	
	private class YesButton extends JButton implements ActionListener // Yes button for UserColor dialog.
	{	public YesButton()
		{	super( "Do It");
			addActionListener(this);
		}
		
		public void actionPerformed(ActionEvent e)
		{	int r = red.getValue();
			int g = green.getValue();
			int b = blue.getValue();
			redLabel.setText("Red " + r);
			greenLabel.setText("Green " + g);
			blueLabel.setText("Blue " + b);
			currentColor = new Color(r, g, b);
			if(component != null)
			{	component.setBackground(currentColor);
				component.getParent().setBackground(currentColor);	
				component.repaint();
			}
		}
	}

	private class NoButton extends JButton implements ActionListener // No button for UserColor dialog.
	{	public NoButton()
		{	super( "Cancel");
			addActionListener(this);
		}
		
		public void actionPerformed(ActionEvent e)
		{	ColorSliderDialog.this.setVisible(false);
		}
	}
	
	private class ColorScroller extends JScrollBar implements AdjustmentListener
	{	public ColorScroller()
		{	super(HORIZONTAL);
			addAdjustmentListener(this);
		}
	
		public void adjustmentValueChanged(AdjustmentEvent e)
		{	int r = red.getValue();
			int g = green.getValue();
			int b = blue.getValue();
			redLabel.setText("Red " + r);
			greenLabel.setText("Green " + g);
			blueLabel.setText("Blue " + b);
			colorPanel.setBackground(new Color(r, g, b));
			colorPanel.repaint();
		}
	}
	
}