package siprp.medicina.processo.mail;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.colorchooser.AbstractColorChooserPanel;

import com.evolute.utils.ui.CustomJDialog;

public class ColorChooserDialog extends CustomJDialog
	implements ActionListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	protected JColorChooser colorChooser;
	protected JButton okButton;
	protected JButton cancelButton;
	
	protected Color initialColor;
	protected Color selectedColor;
	
	public ColorChooserDialog( JFrame owner, Color color )
	{
		super( owner, true );
		initialColor = color;
		setupComponents();
		if( owner != null )
		{
			centerSuper();
		}
		else
		{
			center();
		}
	}
	
	private void setupComponents()
	{
		setTitle( "Escolha a cor" );
		colorChooser = new JColorChooser( initialColor );
		
		colorChooser.setChooserPanels( new AbstractColorChooserPanel[]{ colorChooser.getChooserPanels()[ 0 ] } );
		colorChooser.setPreviewPanel( new JPanel() );
		okButton = new JButton( "OK" );
		cancelButton = new JButton( "Cancelar" );
		
		setLayout( new BorderLayout() );
		add( colorChooser, BorderLayout.CENTER );
		JPanel buttonPanel = new JPanel();
		add( buttonPanel, BorderLayout.SOUTH );
		
		buttonPanel.setLayout( new FlowLayout( FlowLayout.CENTER ) );
		buttonPanel.add( okButton );
		buttonPanel.add( cancelButton );
		
		okButton.addActionListener( this );
		cancelButton.addActionListener( this );
		pack();
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
		if( source.equals( okButton ) )
		{
			selectedColor = colorChooser.getColor();
			close();
		}
		else
		{
			close();
		}
	}
	
	public void close()
	{
		SwingUtilities.invokeLater( new Runnable(){
			public void run()
			{
				setVisible( false );
				dispose();
			}
		} );
	}
	
	public Color getColor()
	{
		return selectedColor;
	}
}
