/*
 * ObservacoesDialog.java
 *
 * Created on 13 de Maio de 2007, 13:13
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package siprp.medicina.processo.detalhes;

import com.evolute.utils.ui.CustomJDialog;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

/**
 *
 * @author Frederico
 */
public class ObservacoesDialog extends CustomJDialog
	implements ActionListener
{
	protected JTextArea observacoesText;
	protected JButton okButton;
	protected JButton cancelButton;
	
	protected boolean ok = false;
	
	public static void main( String args[] )
	{
		ObservacoesDialog dialog = new ObservacoesDialog( null );
		dialog.editarObservacao( "ISto e a observba" );
		System.exit( 0 );
	}
	
	/** Creates a new instance of ObservacoesDialog */
	public ObservacoesDialog( JFrame owner )
	{
		super( owner, true );
		setupComponents();
		
		if( owner != null )
		{
			centerSuper();
		}
		else
		{
			center();
		}
	}
	
	private void setupComponents()
	{
		setTitle( "Coment\u00e1rio" );
		setSize( 400, 200 );
		observacoesText = new JTextArea();
		observacoesText.setLineWrap( true );
		observacoesText.setWrapStyleWord( true );
		JScrollPane scp = new JScrollPane( observacoesText, 
			JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
		okButton = new JButton( "Ok" );
		okButton.addActionListener( this );
		cancelButton = new JButton( "Cancelar" );
		cancelButton.addActionListener( this );
		JPanel buttonPanel = new JPanel();
		
		setLayout( new BorderLayout() );
		add( scp, BorderLayout.CENTER );
		add( buttonPanel, BorderLayout.SOUTH );
		
		buttonPanel.setLayout( new GridLayout( 1, 2 ) );
		buttonPanel.add( okButton );
		buttonPanel.add( cancelButton );
	}
	
	public void actionPerformed( ActionEvent e )
	{
		Object source = e.getSource();
		if( source.equals( okButton ) )
		{
			ok = true;
			close();
		}
		else
		{
			ok = false;
			close();
		}
	}
	
	public String editarObservacao( String observacao )
	{
		observacoesText.setText( observacao );
		setVisible( true );
		return ok ? observacoesText.getText() : null;
	}
	
	public void close()
	{
		SwingUtilities.invokeLater( new Runnable(){
			public void run()
			{
				setVisible( false );
				dispose();
			}
		} );
	}
}
