/*
 * UserPanel.java
 *
 * Created on 8 de Novembro de 2004, 17:03
 */

package siprp.clientes;

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

import com.evolute.utils.ui.*;
import com.evolute.utils.ui.text.*;
/**
 *
 * @author  fpalma
 */
public class UserPanel extends JPanel
	implements DocumentListener, FocusListener, ActionListener
{
	private UserDataProvider provider;
	
	private JTextField userNameText;
	private JTextField passwordText;
	private JButton applyButton;
	private JButton cancelButton;
	
	private boolean passwordEdited = false;
	private boolean hasPassword = false;
	private boolean error = false;
	private Integer empresaID;
	
//	public static void main( String args[] )
//	{
//		JFrame frame = new JFrame();
//		frame.setSize( 300, 200 );
//		frame.getContentPane().setLayout( new GridLayout( 1, 1 ) );
//		frame.getContentPane().add( new UserPanel() );
//		frame.show();
//	}
	
	/** Creates a new instance of UserPanel */
	public UserPanel()
		throws Exception
	{
		provider = ( UserDataProvider ) UserDataProvider.getProvider();
		setupComponents();
	}
	
	private void setupComponents()
	{
		JLabel userNameLabel = new JLabel( "Utilizador" );
		userNameText = new JTextField();
		JLabel passwordLabel = new JLabel( "Senha" );
		passwordText = new JTextField();
		applyButton = new JButton( "Actualizar" );
		cancelButton = new JButton( "Cancelar" );
		
		userNameText.setPreferredSize( new Dimension( 200, 20 ) );
		passwordText.setPreferredSize( new Dimension( 200, 20 ) );
		passwordText.setText( "<N\u00e3o Definida>" );
		
		GridBagLayout gridbag = new GridBagLayout();
		setLayout( gridbag );
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.insets = new Insets( 1, 1, 1, 1 );
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weighty = 0;
		constraints.gridheight = 1;
		
		constraints.weightx = 1;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		gridbag.setConstraints( userNameLabel, constraints );
		gridbag.setConstraints( passwordLabel, constraints );
		gridbag.setConstraints( userNameText, constraints );
		gridbag.setConstraints( passwordText, constraints );
		JPanel buttonPanel = new JPanel();
		gridbag.setConstraints( buttonPanel, constraints );
		
		add( userNameLabel );
		add( userNameText );
		add( passwordLabel );
		add( passwordText );
		add( buttonPanel );
		
		buttonPanel.setLayout( new FlowLayout( FlowLayout.RIGHT ) );
		buttonPanel.add( applyButton );
		buttonPanel.add( cancelButton );
		
		passwordText.addFocusListener( this );
		passwordText.getDocument().addDocumentListener( this );
		
		applyButton.addActionListener( this );
		cancelButton.addActionListener( this );
		
		new CopyPasteHandler( userNameText );
		new CopyPasteHandler( passwordText );
	}
	
	public void changedUpdate(javax.swing.event.DocumentEvent documentEvent)
	{
		passwordEdited = true;
	}
	
	public void insertUpdate(javax.swing.event.DocumentEvent documentEvent)
	{
		passwordEdited = true;
	}
	
	public void removeUpdate(javax.swing.event.DocumentEvent documentEvent)
	{
		passwordEdited = true;
	}
	
	public void focusGained(java.awt.event.FocusEvent focusEvent)
	{
		if( !passwordEdited )
		{
			passwordText.getDocument().removeDocumentListener( this );
			passwordText.setText( "" );
			passwordText.getDocument().addDocumentListener( this );
		}
	}
	
	public void focusLost(java.awt.event.FocusEvent focusEvent)
	{
	}
	
	public void actionPerformed(java.awt.event.ActionEvent actionEvent)
	{
		try
		{
			Object source = actionEvent.getSource();
			if( applyButton.equals( source ) )
			{
				if( save() )
				{
					hasPassword = true;
				}
				refresh();
			}
			else if( cancelButton.equals( source ) )
			{
				reload();
				refresh();
			}
		}
		catch( Exception ex )
		{
			DialogException.showExceptionMessage( ex, "Erro na liga\u00e7\u00e3o \u00e0 Base de Dados WEB", true );
		}
	}
	
	private void refresh()
	{
		passwordText.getDocument().removeDocumentListener( this );
		if( !passwordEdited )
		{
			if( hasPassword )
			{
				passwordText.setText( "<Definida>" );
			}
			else
			{
				passwordText.setText( "<N\u00e3o Definida>" );
			}
		}
		passwordText.getDocument().addDocumentListener( this );
	}
	
	public void setEmpresaID( Integer empresaID )
	{
		try
		{
			userNameText.setText( "" );
			passwordText.setText( "" );
			passwordEdited = false;
			hasPassword = false;
			this.empresaID = empresaID;
//			if( empresaID == null )
//			{
//				return;
//			}
			reload();
			refresh();
			error = false;
		}
		catch( Exception ex )
		{
			DialogException.showExceptionMessage( ex, "Erro na liga\u00e7\u00e3o \u00e0 Base de Dados WEB", true );
			userNameText.setText( "Erro na liga\u00e7\u00e3o" );
			setEnabled( false );
			error = true;
		}
	}
	
	private boolean save()
		throws Exception
	{
		if( !provider.checkNewName( userNameText.getText(), empresaID ) )
		{
			JOptionPane.showMessageDialog( null, "O Utilizador " + userNameText.getText() + " j\u00e1 est\u00e1 em uso por outra entidade." );
			return false;
		}
		provider.saveUser( userNameText.getText(), passwordText.getText(), empresaID, !hasPassword );
		passwordEdited = false;
		return true;
	}
	
	private boolean reload()
		throws Exception
	{
		String userName = provider.getUserName( empresaID );
		if( userName != null )
		{
			userNameText.setText( userName );
			hasPassword = true;
		}
		passwordEdited = false;
		return true;
	}
	
	public void setEnabled( boolean enable )
	{
		userNameText.setEnabled( enable && !error );
		passwordText.setEnabled( enable && !error );
		applyButton.setEnabled( enable && !error );
		cancelButton.setEnabled( enable && !error );
	}
}
