/*
 * MarcacaoEmpresaPanel.java
 *
 * Created on 25 de Junho de 2004, 20:32
 */

package siprp.clientes;

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

import com.evolute.utils.*;
import com.evolute.utils.dataui.*;
import com.evolute.utils.toolbar.*;
import com.evolute.utils.tables.*;
import com.evolute.utils.ui.*;
import com.evolute.utils.ui.calendar.*;
import com.evolute.utils.ui.text.*;

import siprp.data.*;

/**
 *
 * @author  fpalma
 */
public class MarcacaoEmpresaPanel extends JPanel
	implements ControllableComponent, DocumentListener, ActionListener, ChangeListener
{
	protected static final String LIST_PANEL = "LIST_PANEL";
	protected static final String EDIT_PANEL = "EDIT_PANEL";
	
	private ComponentsHashtable components;
	private EmpresaPanel owner;
	
	private JCalendarPanel calendarPanel;
	private JTextArea text;
	private JCheckBox realizadaCheck;
	private JButton saveButton;
	private JButton cancelButton;

	private EmpresaData empresa;
	private boolean changed = false;
	private boolean filling = false;
	
	private MarcacaoEmpresaData marcacao;
	
	/** Creates a new instance of MarcacaoEmpresaPanel */
	public MarcacaoEmpresaPanel( EmpresaPanel owner )
	{
		this.owner = owner;
		setupComponents();
		setupComponentsHashtable();
	}
	
	private void setupComponents()
	{
		calendarPanel = new JCalendarPanel( null );
//		calendarPanel.addDocumentListener( this );
		text = new JTextArea();
		new CopyPasteHandler( text );
//		text.getDocument().addDocumentListener( this );
		text.setLineWrap( true );
		text.setWrapStyleWord( true );
		JScrollPane textScp = new JScrollPane( text, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
													JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
		realizadaCheck = new JCheckBox( "Realizada" );
		saveButton = new JButton( "Guardar" );
		saveButton.addActionListener( this );
		cancelButton = new JButton( "Voltar" );
		cancelButton.addActionListener( this );
		
		
		GridBagLayout gridbag = new GridBagLayout();
		setLayout( gridbag );
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.insets = new Insets( 1, 1, 1, 1 );
		constraints.fill = GridBagConstraints.HORIZONTAL;
		
		constraints.weightx = 0;
		constraints.gridwidth = 1;
		constraints.weighty = 0;
		constraints.gridheight = 1;
		calendarPanel.setPreferredSize( new Dimension( 130, 30 ) );
		gridbag.setConstraints( calendarPanel, constraints );
		add( calendarPanel );
		
		gridbag.setConstraints( realizadaCheck, constraints );
		add( realizadaCheck );
		
		constraints.weightx = 1;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		JPanel buttonPanel = new JPanel();
		gridbag.setConstraints( buttonPanel, constraints );
		add( buttonPanel );
		
		constraints.fill = GridBagConstraints.BOTH;
		constraints.weighty = 1;
		constraints.gridheight = GridBagConstraints.REMAINDER;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		constraints.weightx = 1;
		gridbag.setConstraints( textScp, constraints );
		add( textScp );
		
		buttonPanel.setLayout( new FlowLayout( FlowLayout.RIGHT ) );
		initButtons();
		buttonPanel.add( cancelButton );
		buttonPanel.add( saveButton );
	}
	
	private void setupComponentsHashtable()
	{
		components = new ComponentsHashtable();
		components.putComponent( MarcacaoEmpresaData.DATA, calendarPanel );
		components.putComponent( MarcacaoEmpresaData.TEXTO, text );
		components.putComponent( MarcacaoEmpresaData.REALIZADA, realizadaCheck );
		components.putDummy( MarcacaoEmpresaData.EMPRESA );
	}
	
	public void clear()
	{
		String names[] = (String[])components.keySet().toArray( new String[0] );
		ComponentController.clear( names, components );
		marcacao = null;
	}
	
	public void fill(Object value)
	{
		filling = true;
		clear();
		initButtons();
		marcacao = (MarcacaoEmpresaData)value;
		if( marcacao == null )
		{
			filling = false;
			return;
		}
		String names[] = marcacao.getFieldNames();
		ComponentController.fill( names, marcacao.getHashData(), components );
		filling = false;
	}
	
	public Object save()
	{
		if( marcacao == null )
		{
			marcacao = new MarcacaoEmpresaData();
		}
			
		String names[] = marcacao.getFieldNames();
		Hashtable hash = new Hashtable();
	
		ComponentController.save( names, hash, components );
		hash.put( MarcacaoEmpresaData.EMPRESA, empresa );
		marcacao.setHashData( hash );
	System.out.println( "EMPRESA:: " + marcacao.get( MarcacaoEmpresaData.EMPRESA ) );
		return marcacao;
	}
	
	public void setEnabled(boolean enable)
	{
		super.setEnabled( enable );
		String names[] = (String[])components.keySet().toArray( new String[0] );
		ComponentController.setEnabled( names, enable, components );
	}
	
	public void changedUpdate(javax.swing.event.DocumentEvent documentEvent)
	{
		startEditedState();
	}
	
	public void insertUpdate(javax.swing.event.DocumentEvent documentEvent)
	{
		startEditedState();
	}
	
	public void removeUpdate(javax.swing.event.DocumentEvent documentEvent)
	{
		startEditedState();
	}
	
	private void startEditedState()
	{
		if( !filling )
		{
			calendarPanel.removeChangeListener( this );
			text.getDocument().removeDocumentListener( this );
			realizadaCheck.removeChangeListener( this );
			changed = true;
			saveButton.setEnabled( true );
			changeButton();
		}
	}
	
	private void initButtons()
	{
		saveButton.setEnabled( false );
		calendarPanel.addChangeListener( this );
		realizadaCheck.addChangeListener( this );
		text.getDocument().addDocumentListener( this );
		ClassLoader cl = new EVUtilsImageLib().getClass().getClassLoader();
		Icon normal = new ImageIcon( cl.getResource( "buttons/save_normal.gif" ) );
		Icon rollover = new ImageIcon( cl.getResource( "buttons/save_rollover.gif" ) );
		Icon pressed = new ImageIcon( cl.getResource( "buttons/save_pressed.gif" ) );
		Icon disabled = new ImageIcon( cl.getResource( "buttons/save_disabled.gif" ) );
		saveButton.setIcon( normal );
		saveButton.setRolloverIcon( rollover );
		saveButton.setPressedIcon( pressed );
		saveButton.setDisabledIcon( disabled );
		saveButton.setText( null );
		saveButton.setBorderPainted( false );
		saveButton.setRequestFocusEnabled( false );
		saveButton.setMargin( new Insets( 0, 0, 0, 0 ) );
		saveButton.setToolTipText( "Gravar" );
		
		normal = new ImageIcon( cl.getResource( "buttons/shiftl_normal.gif" ) );
		rollover = new ImageIcon( cl.getResource( "buttons/shiftl_rollover.gif" ) );
		pressed = new ImageIcon( cl.getResource( "buttons/shiftl_pressed.gif" ) );
		disabled = new ImageIcon( cl.getResource( "buttons/shiftl_disabled.gif" ) );
		cancelButton.setIcon( normal );
		cancelButton.setRolloverIcon( rollover );
		cancelButton.setPressedIcon( pressed );
		cancelButton.setDisabledIcon( disabled );
		cancelButton.setText( null );
		cancelButton.setBorderPainted( false );
		cancelButton.setRequestFocusEnabled( false );
		cancelButton.setMargin( new Insets( 0, 0, 0, 0 ) );
		cancelButton.setToolTipText( "Voltar \u00e0 lista" );
		
	}
	
	private void changeButton()
	{
		ClassLoader cl = new EVUtilsImageLib().getClass().getClassLoader();
		Icon normal = new ImageIcon( cl.getResource( "buttons/cancel_normal.gif" ) );
		Icon rollover = new ImageIcon( cl.getResource( "buttons/cancel_rollover.gif" ) );
		Icon pressed = new ImageIcon( cl.getResource( "buttons/cancel_pressed.gif" ) );
		Icon disabled = new ImageIcon( cl.getResource( "buttons/cancel_disabled.gif" ) );
		cancelButton.setIcon( normal );
		cancelButton.setRolloverIcon( rollover );
		cancelButton.setPressedIcon( pressed );
		cancelButton.setDisabledIcon( disabled );
		cancelButton.setToolTipText( "Cancelar" );
	}
	
	public void actionPerformed( ActionEvent e )
	{
		Object source = e.getSource();
		if( source.equals( saveButton ) )
		{
			if( calendarPanel.getDate() == null )
			{
				JOptionPane.showMessageDialog( this, "Tem de escolher uma data", "Erro...", JOptionPane.ERROR_MESSAGE );
				return;
			}
			MarcacaoEmpresaData marc = ( MarcacaoEmpresaData ) save();
			
			try
			{
				marc.save();
			}
			catch( Exception ex )
			{
				DialogException.showExceptionMessage( ex, "Erro a gravar", true );
				return;
			}
			owner.reloadMarcacoes();
		}
		else if( source.equals( cancelButton ) )
		{
			owner.showMarcacoes();
		}
		
	}
	
	public void setEmpresa( EmpresaData empresa )
	{
		this.empresa = empresa;
	}
	
	public void stateChanged(javax.swing.event.ChangeEvent changeEvent)
	{
		startEditedState();
	}
	
}
