/*
 * EmpresaPanel.java
 *
 * Created on 12 de Maio de 2004, 19:08
 */

package siprp.clientes;

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

import com.evolute.utils.*;
import com.evolute.utils.data.*;
import com.evolute.utils.dataui.*;
import com.evolute.utils.documents.*;
import com.evolute.utils.jdo.*;
import com.evolute.utils.tables.*;
import com.evolute.utils.ui.*;
import com.evolute.utils.ui.calendar.*;
import com.evolute.utils.ui.text.*;

import siprp.*;
import siprp.data.*;

/**
 *
 * @author  fpalma
 */
public class EmpresaPanel extends JPanel
	implements ControllableComponent, ListSelectionListener
{
	private JDOProvider JDO;
	
	private FichaDataProvider provider;
	private EmpresaData empresa;
	private ComponentsHashtable components;

	private Integer id;
	
	private JTextField designacaoSocialText;
	private JTextField moradaText;
	private JTextField codigoPostalText;
	private JTextField localText;
	private JTextField distritoText;
	private JTextField concelhoText;
	private JTextField codigo1Text;
	private JTextField codigo2Text;
	private JTextField codigo3Text;
//	private JTextField perfil1Text;
//	private JTextField perfil2Text;
	private JComboBox servicosCombo;
	private JTextField precoHigieneText;
	private JTextField precoMedicinaText;
	private JTextField modalidadePagamentoText;
	private ContactoPanel contacto1Panel;
	private ContactoPanel contacto2Panel;
	private JCalendarPanel envioPropostaPanel;
	private JCalendarPanel aceitacaoPropostaPanel;
	private JCalendarPanel inicioContratoPanel;
	private JTextField duracaoContratoText;
	private JCalendarPanel cancelamentoPanel;
	private JCalendarPanel envioContratoPanel;
	private JCalendarPanel recepcaoContratoPanel;
	private JCalendarPanel envioIdictPanel;
	private JCalendarPanel relatorioAnualPanel;
	private JTextField caeText;
	private JTextField actividadeText;
	private JTextField contribuinteText;
	private JTextField segSocialText;
	private JPanel marcacoesPanel;
	private JPanel listaMarcacoesPanel;
	private BaseTable marcacoesTable;
	private MarcacaoEmpresaPanel marcacaoPanel;
	private CardLayout marcacoesCardLayout;
	private ProtocoloPanel protocoloPanel;
//	private UserPanel userPanel;
	
	private BaseTable estabelecimentosTable;
	private VectorTableModel estabelecimentosModel;
	
	private boolean webAware;
	
	private Vector listSelectionListeners;
	
	/** Creates a new instance of EmpresaPanel */
	public EmpresaPanel()
		throws Exception
	{
		webAware = ( ( Boolean ) Singleton.getInstance( SingletonConstants.WEB_AWARE ) ).booleanValue();
		provider = (FichaDataProvider)FichaDataProvider.getProvider();
		listSelectionListeners = new Vector();
		JDO = ( JDOProvider ) Singleton.getInstance( Singleton.DEFAULT_JDO_PROVIDER );
		setupComponents();
		setupComponentsHashtable();
	}
	
	private void setupComponents()
		throws Exception
	{
		String codeFormat = ( String ) Singleton.getInstance( SingletonConstants.CODIGO_EMPRESA_FORMAT );
		String formatElements[] = codeFormat.split( " " );
		int formatFieldLengths[] = new int[ formatElements.length ];
		String fieldSeparators[] = new String[ formatElements.length ];
		int n1 = 0;
		int n2 = 0;
		for( ; n1 < formatElements.length; n1++ )
		{
			try
			{
				formatFieldLengths[ n2 ] = Integer.parseInt( formatElements[ n1 ] );
				n2++;
			}
			catch( NumberFormatException ex )
			{
				fieldSeparators[ n2 - 1 ] = formatElements[ n1 ];
			}
		}
		int countFields = n2;
		
		JLabel designacaoSocialLabel = new JLabel( "Designa\u00e7\u00e3o Social" );
		designacaoSocialText = new StateTextField();
		JLabel codigoLabel = new JLabel( "C\u00f3digo" );
		JPanel codigoPanel = new JPanel();
		codigo1Text = new StateTextField();
		if( countFields > 0 && formatFieldLengths[ 0 ] > 0 )
		{
			codigo1Text.setDocument( new MaximumLengthDocument( formatFieldLengths[ 0 ] ) );
		}
		codigo2Text = new StateTextField();
		if( countFields > 1 && formatFieldLengths[ 1 ] > 0 )
		{
			codigo2Text.setDocument( new MaximumLengthDocument( formatFieldLengths[ 1 ] ) );
		}
		codigo3Text = new StateTextField();
		if( countFields > 2 && formatFieldLengths[ 2 ] > 0 )
		{
			codigo3Text.setDocument( new MaximumLengthDocument( formatFieldLengths[ 2 ] ) );
		}
//		JLabel perfisLabel = new JLabel( "Perfis (A/B)" );
//		perfil1Text = new JTextField();
//		JLabel perfil2Label = new JLabel( "/" );
//		perfil2Text = new JTextField();
		servicosCombo = new JComboBox();
		servicosCombo.setPreferredSize( new Dimension( 120, 20 ) );
		
		JLabel precoLabel = new JLabel( "Pre\u00e7os" );
		JLabel precoHigieneLabel = new JLabel( " Higiene" );
		precoHigieneText = new JTextField();
		precoHigieneText.setDocument( new DoubleDocument( 15, true ) );
		JLabel higieneEuroLabel = new JLabel( "\u20ac" );
		JLabel precoMedicinaLabel = new JLabel( " Medicina" );
		precoMedicinaText = new JTextField();
		precoMedicinaText.setDocument( new DoubleDocument( 15, true ) );
		JLabel medicinaEuroLabel = new JLabel( "\u20ac" );
		JLabel modalidadePagamentoLabel = new JLabel( "Periodicidade" );
		modalidadePagamentoText = new JTextField();
		
		JLabel moradaLabel = new JLabel( "Morada" );
		moradaText = new StateTextField();
		JLabel codigoPostalLabel = new JLabel( "C\u00f3digo Postal" );
		codigoPostalText = new StateTextField();
		codigoPostalText.setPreferredSize( new Dimension( 50, 20 ) );
		codigoPostalText.setDocument( new MaximumLengthDocument( 8 ) );
		JLabel localLabel = new JLabel( "Localidade" );
		localText = new StateTextField();
		JLabel distritoLabel = new JLabel( "Distrito" );
		distritoText = new JTextField();
		JLabel concelhoLabel = new JLabel( "Concelho" );
		concelhoText = new JTextField();
		contacto1Panel = new ContactoPanel();
		contacto1Panel.setBorder( BorderFactory.createEtchedBorder() );
		contacto2Panel = new ContactoPanel();
		contacto2Panel.setBorder( BorderFactory.createEtchedBorder() );
		JLabel envioPropostaLabel = new JLabel( "Proposta" );
		envioPropostaPanel = new JCalendarPanel( null );
		JLabel aceitacaoPropostaLabel = new JLabel( "Aceita\u00e7\u00e3o" );
		aceitacaoPropostaPanel = new JCalendarPanel( null );
		JLabel inicioContratoLabel = new JLabel( "In\u00edcio" );
		inicioContratoPanel = new JCalendarPanel( null );
		JLabel duracaoContratoLabel = new JLabel( "Dura\u00e7\u00e3o (meses)" );
		duracaoContratoText = new JTextField();
		duracaoContratoText.setDocument( new IntegerDocument( 5, false ) );
		JLabel cancelamentoLabel = new JLabel( "Cancelamento" );
		cancelamentoPanel = new JCalendarPanel( null );
		JLabel envioContratoLabel = new JLabel( "Envio Contrato" );
		envioContratoPanel = new JCalendarPanel( null );
		JLabel recepcaoContratoLabel = new JLabel( "Recep\u00e7\u00e3o" );
		recepcaoContratoPanel = new JCalendarPanel( null );
		JLabel envioIdictLabel = new JLabel( "Mod. 1360" );
		envioIdictPanel = new JCalendarPanel( null );
		JLabel relatorioAnualLabel = new JLabel( "Relat\u00f3rio Anual" );
		relatorioAnualPanel = new JCalendarPanel( null );
		JTabbedPane tabbedPane = new JTabbedPane();
		JLabel caeLabel = new JLabel( "CAE" );
		caeText = new JTextField();
		JLabel actividadeLabel = new JLabel( " - " );
		actividadeText = new JTextField();
		JLabel contribuinteLabel = new JLabel( "Contribuinte" );
		contribuinteText = new JTextField();
		JLabel segSocialLabel = new JLabel( "Seguran\u00e7a Social" );
		segSocialText = new JTextField();
		JPanel moradaPanel = new JPanel();
		marcacoesPanel = new JPanel();
		protocoloPanel = new ProtocoloPanel();
//		userPanel = new UserPanel();
				
		JScrollPane estabelecimentosScroll = new JScrollPane();
		estabelecimentosScroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
		estabelecimentosModel = new VectorTableModel( new String[]{ "Estabelecimentos" } );
		estabelecimentosTable = new BaseTable( estabelecimentosModel );
		estabelecimentosTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
		estabelecimentosTable.getSelectionModel().addListSelectionListener( this );
		estabelecimentosScroll.setViewportView( estabelecimentosTable );
		
		JPanel dadosEmpresaPanel = new JPanel();
		JPanel contactosPanel = new JPanel();
		JScrollPane contactosScroll = new JScrollPane();
		contactosScroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
		contactosScroll.setViewportView( contactosPanel );
		contactosScroll.setBorder( BorderFactory.createTitledBorder( 
									BorderFactory.createEtchedBorder(), "Contactos" ) );
		JPanel formalidadesPanel = new JPanel();
		
		JPanel pad;
		
		JPanel upperPanel = new JPanel();
		GridBagLayout gridbag = new GridBagLayout();
		upperPanel.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 = 0;
		constraints.gridwidth = 1;
		gridbag.setConstraints( designacaoSocialLabel, constraints );
		upperPanel.add( designacaoSocialLabel );
		
		constraints.weightx = 0.9;
		constraints.gridwidth = 5;
		gridbag.setConstraints( designacaoSocialText, constraints );
		upperPanel.add( designacaoSocialText );
		
		constraints.weightx = 0;
		constraints.gridwidth = 1;
		gridbag.setConstraints( codigoLabel, constraints );
		upperPanel.add( codigoLabel );
		
		constraints.weightx = 0.1;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		gridbag.setConstraints( codigoPanel, constraints );
		upperPanel.add( codigoPanel );
		
		constraints.weightx = 0;
		constraints.gridwidth = 1;
//		gridbag.setConstraints( moradaLabel, constraints );
//		upperPanel.add( moradaLabel );
		gridbag.setConstraints( caeLabel, constraints );
		upperPanel.add( caeLabel );
		
		constraints.weightx = 0.2;
		constraints.gridwidth = 1;
		gridbag.setConstraints( caeText, constraints );
		upperPanel.add( caeText );
		
		constraints.weightx = 0;
		constraints.gridwidth = 1;
//		gridbag.setConstraints( moradaLabel, constraints );
//		upperPanel.add( moradaLabel );
		gridbag.setConstraints( actividadeLabel, constraints );
		upperPanel.add( actividadeLabel );
		
		constraints.weightx = 0.6;
		constraints.gridwidth = 1;
		gridbag.setConstraints( actividadeText, constraints );
		upperPanel.add( actividadeText );
		
		constraints.weightx = 0;
		constraints.gridwidth = 1;
//		gridbag.setConstraints( codigoPostalLabel, constraints );
//		upperPanel.add( codigoPostalLabel );
		gridbag.setConstraints( contribuinteLabel, constraints );
		upperPanel.add( contribuinteLabel );
		
		constraints.weightx = 0.1;
		constraints.gridwidth = 1;
		gridbag.setConstraints( contribuinteText, constraints );
		upperPanel.add( contribuinteText );
		
		constraints.weightx = 0;
		constraints.gridwidth = 2;
//		gridbag.setConstraints( localLabel, constraints );
//		upperPanel.add( localLabel );
		gridbag.setConstraints( segSocialLabel, constraints );
		upperPanel.add( segSocialLabel );
		
		constraints.weightx = 0.1;
		constraints.gridwidth = 1;
		gridbag.setConstraints( segSocialText, constraints );
		upperPanel.add( segSocialText );
		
		
		gridbag = new GridBagLayout();
		dadosEmpresaPanel.setLayout( gridbag );
		constraints = new GridBagConstraints();
		constraints.insets = new Insets( 1, 1, 1, 1 );
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weighty = 0;
		constraints.gridheight = 1;

		contactosPanel.setLayout( new GridLayout( 1, 2 ) );
		contactosPanel.add( contacto1Panel );
		contactosPanel.add( contacto2Panel );
		
		gridbag = new GridBagLayout();
		formalidadesPanel.setLayout( gridbag );
		constraints.weighty = 0;
		constraints.gridheight = 1;
		
		constraints.weightx = 0;
		constraints.gridwidth = 1;
		gridbag.setConstraints( envioPropostaLabel, constraints );
		formalidadesPanel.add( envioPropostaLabel );
		
		constraints.weightx = 0.3;
		gridbag.setConstraints( envioPropostaPanel, constraints );
		formalidadesPanel.add( envioPropostaPanel );
		
		constraints.weightx = 0;
		gridbag.setConstraints( inicioContratoLabel, constraints );
		formalidadesPanel.add( inicioContratoLabel );
		
		constraints.weightx = 0.2;
		constraints.gridwidth = 1;
//		JPanel perfisPanel = new JPanel();
		gridbag.setConstraints( inicioContratoPanel, constraints );
		formalidadesPanel.add( inicioContratoPanel );
				
		constraints.weightx = 0.4;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		gridbag.setConstraints( precoLabel, constraints );
		formalidadesPanel.add( precoLabel );
		
		constraints.weightx = 0;
		constraints.gridwidth = 1;
		gridbag.setConstraints( aceitacaoPropostaLabel, constraints );
		formalidadesPanel.add( aceitacaoPropostaLabel );
		
		constraints.weightx = 0.3;
		constraints.gridwidth = 1;
		gridbag.setConstraints( aceitacaoPropostaPanel, constraints );
		formalidadesPanel.add( aceitacaoPropostaPanel );
		
		constraints.weightx = 0;
		gridbag.setConstraints( duracaoContratoLabel, constraints );
		formalidadesPanel.add( duracaoContratoLabel );
		
		constraints.weightx = 0.2;
		constraints.gridwidth = 1;
//		JPanel perfisPanel = new JPanel();
		gridbag.setConstraints( duracaoContratoText, constraints );
		formalidadesPanel.add( duracaoContratoText );
		
		constraints.weightx = 0;
		constraints.gridwidth = 1;
		gridbag.setConstraints( precoHigieneLabel, constraints );
		formalidadesPanel.add( precoHigieneLabel );
		
		constraints.weightx = 0.4;
		constraints.gridwidth = 1;
		gridbag.setConstraints( precoHigieneText, constraints );
		formalidadesPanel.add( precoHigieneText );
		
		constraints.weightx = 0;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		gridbag.setConstraints( higieneEuroLabel, constraints );
		formalidadesPanel.add( higieneEuroLabel );

		constraints.weightx = 0;
		constraints.gridwidth = 1;
		gridbag.setConstraints( envioContratoLabel, constraints );
		formalidadesPanel.add( envioContratoLabel );
		
		constraints.weightx = 0.3;
		gridbag.setConstraints( envioContratoPanel, constraints );
		formalidadesPanel.add( envioContratoPanel );
		
		constraints.weightx = 0;
		gridbag.setConstraints( cancelamentoLabel, constraints );
		formalidadesPanel.add( cancelamentoLabel );
		
		constraints.weightx = 0.2;
		constraints.gridwidth = 1;
//		JPanel perfisPanel = new JPanel();
		gridbag.setConstraints( cancelamentoPanel, constraints );
		formalidadesPanel.add( cancelamentoPanel );
		
		constraints.weightx = 0;
		constraints.gridwidth = 1;
		gridbag.setConstraints( precoMedicinaLabel, constraints );
		formalidadesPanel.add( precoMedicinaLabel );
		
		constraints.weightx = 0.4;
		constraints.gridwidth = 1;
		gridbag.setConstraints( precoMedicinaText, constraints );
		formalidadesPanel.add( precoMedicinaText );
		
		constraints.weightx = 0;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		gridbag.setConstraints( medicinaEuroLabel, constraints );
		formalidadesPanel.add( medicinaEuroLabel );
		
		constraints.weightx = 0;
		constraints.gridwidth = 1;
		gridbag.setConstraints( recepcaoContratoLabel, constraints );
		formalidadesPanel.add( recepcaoContratoLabel );
		
		constraints.weightx = 0.3;
		constraints.gridwidth = 1;
		gridbag.setConstraints( recepcaoContratoPanel, constraints );
		formalidadesPanel.add( recepcaoContratoPanel );
		
		constraints.weightx = 0.2;
		constraints.gridwidth = 2;
		gridbag.setConstraints( servicosCombo, constraints );
		formalidadesPanel.add( servicosCombo );
		
		constraints.weightx = 0;
		constraints.gridwidth = 1;
		gridbag.setConstraints( modalidadePagamentoLabel, constraints );
		formalidadesPanel.add( modalidadePagamentoLabel );
		
		constraints.weightx = 0.4;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		gridbag.setConstraints( modalidadePagamentoText, constraints );
		formalidadesPanel.add( modalidadePagamentoText );
		
		constraints.weightx = 0;
		constraints.gridwidth = 1;
		gridbag.setConstraints( envioIdictLabel, constraints );
		formalidadesPanel.add( envioIdictLabel );
		
		constraints.weightx = 0.3;
		gridbag.setConstraints( envioIdictPanel, constraints );
		formalidadesPanel.add( envioIdictPanel );
		
		constraints.weightx = 0;
		constraints.gridwidth = 1;
		gridbag.setConstraints( relatorioAnualLabel, constraints );
		formalidadesPanel.add( relatorioAnualLabel );
		
		constraints.weightx = 0.2;
		constraints.gridwidth = 1;
		gridbag.setConstraints( relatorioAnualPanel, constraints );
		formalidadesPanel.add( relatorioAnualPanel );
		
		constraints.weightx = 0.4;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		pad = new JPanel();
		gridbag.setConstraints( pad, constraints );
		formalidadesPanel.add( pad );
		
//		constraints.weightx = 0.5;
//		constraints.gridwidth = GridBagConstraints.REMAINDER;
//		pad = new JPanel();
//		gridbag.setConstraints( pad, constraints );
//		formalidadesPanel.add( pad );
		
		setLayout( new BorderLayout() );
		add( upperPanel, BorderLayout.NORTH );
		JPanel lowerPanel = new JPanel();
		add( lowerPanel, BorderLayout.CENTER );
		
		gridbag = new GridBagLayout();
		lowerPanel.setLayout( gridbag );
		constraints.fill = GridBagConstraints.BOTH;
		constraints.insets = new Insets( 0, 0, 0, 0 );
		constraints.weighty = 1;
		constraints.gridheight = GridBagConstraints.REMAINDER;
		
		constraints.weightx = 0.55;
		constraints.gridwidth = 1;
//		lowerPanel.setLayout( new BorderLayout() );
		//lowerPanel.add( dadosEmpresaPanel );
		//lowerPanel.add( new JPanel() );
		gridbag.setConstraints( tabbedPane, constraints );
		lowerPanel.add( tabbedPane );
//		lowerPanel.add( tabbedPane, BorderLayout.CENTER );
		tabbedPane.add( "Contactos", contactosPanel );
		tabbedPane.add( "Morada", moradaPanel );
		tabbedPane.add( "Formalidades", formalidadesPanel );
		tabbedPane.add( "Protocolo", protocoloPanel );
		tabbedPane.add( "Tarefas", marcacoesPanel );
		
//		if( webAware )
//		{
//			tabbedPane.add( "Utilizador da P\u00e1gina", userPanel );
//		}
		
		constraints.weightx = 0.45;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		gridbag.setConstraints( estabelecimentosScroll, constraints );
		lowerPanel.add( estabelecimentosScroll );
//		lowerPanel.add( estabelecimentosScroll, BorderLayout.EAST );
		
		gridbag = new GridBagLayout();
		codigoPanel.setLayout( gridbag );
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.insets = new Insets( 1, 0, 1, 2 );
		constraints.weighty = 1;
		constraints.gridheight = GridBagConstraints.REMAINDER;
		
		if( countFields > 0 )
		{
			constraints.weightx = 0.2;
			constraints.gridwidth = 1;
			gridbag.setConstraints( codigo1Text, constraints );
			codigoPanel.add( codigo1Text );
			
			if( fieldSeparators[ 0 ] != null )
			{
				constraints.weightx = 0;
				JLabel divLabel = new JLabel( fieldSeparators[ 0 ] );
				gridbag.setConstraints( divLabel, constraints );
				codigoPanel.add( divLabel );
			}
		}
		
		if( countFields > 1 )
		{
			constraints.weightx = 0.5;
			gridbag.setConstraints( codigo2Text, constraints );
			codigoPanel.add( codigo2Text );

			if( fieldSeparators[ 1 ] != null )
			{
				constraints.weightx = 0;
				JLabel divLabel = new JLabel( fieldSeparators[ 1 ] );
				gridbag.setConstraints( divLabel, constraints );
				codigoPanel.add( divLabel );
			}
//			constraints.weightx = 0;
//			JLabel divLabel = new JLabel( "/" );
//			gridbag.setConstraints( divLabel, constraints );
//			codigoPanel.add( divLabel );
		}
		
		if( countFields > 2 )
		{
			constraints.weightx = 0.2;
//			constraints.gridwidth = GridBagConstraints.REMAINDER;
			gridbag.setConstraints( codigo3Text, constraints );
			codigoPanel.add( codigo3Text );
			
			if( fieldSeparators[ 2 ] != null )
			{
				constraints.weightx = 0;
				JLabel divLabel = new JLabel( fieldSeparators[ 2 ] );
				gridbag.setConstraints( divLabel, constraints );
				codigoPanel.add( divLabel );
			}
		}
		
		
//		gridbag = new GridBagLayout();
//		perfisPanel.setLayout( gridbag );
//		constraints.fill = GridBagConstraints.HORIZONTAL;
//		constraints.insets = new Insets( 1, 0, 1, 2 );
//		constraints.weighty = 1;
//		constraints.gridheight = GridBagConstraints.REMAINDER;
//		
//		constraints.weightx = 0.5;
//		constraints.gridwidth = 1;
//		gridbag.setConstraints( perfil1Text, constraints );
//		perfisPanel.add( perfil1Text );
//		
//		constraints.weightx = 0;
//		constraints.gridwidth = 1;
//		gridbag.setConstraints( perfil2Label, constraints );
//		perfisPanel.add( perfil2Label );
//		
//		constraints.weightx = 0.5;
//		//constraints.gridwidth = GridBagConstraints.REMAINDER;
//		gridbag.setConstraints( perfil2Text, constraints );
//		perfisPanel.add( perfil2Text );

		
		gridbag = new GridBagLayout();
		moradaPanel.setLayout( gridbag );
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weighty = 0;
		constraints.gridheight = 1;
		
		constraints.weightx = 0;
		constraints.gridwidth = 1;
		gridbag.setConstraints( moradaLabel, constraints );
		moradaPanel.add( moradaLabel );
		
		constraints.weightx = 1;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		gridbag.setConstraints( moradaText, constraints );
		moradaPanel.add( moradaText );
		
		constraints.weightx = 0;
		constraints.gridwidth = 1;
		gridbag.setConstraints( codigoPostalLabel, constraints );
		moradaPanel.add( codigoPostalLabel );
		
		constraints.weightx = 0.3;
		gridbag.setConstraints( codigoPostalText, constraints );
		moradaPanel.add( codigoPostalText );
		
		constraints.weightx = 0.7;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		pad = new JPanel();
		gridbag.setConstraints( pad, constraints );
		moradaPanel.add( pad );
		
		constraints.weightx = 0;
		constraints.gridwidth = 1;
		gridbag.setConstraints( localLabel, constraints );
		moradaPanel.add( localLabel );
		
		constraints.weightx = 1;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		gridbag.setConstraints( localText, constraints );
		moradaPanel.add( localText );
		
		constraints.weightx = 0;
		constraints.gridwidth = 1;
		gridbag.setConstraints( distritoLabel, constraints );
		moradaPanel.add( distritoLabel );
		
		constraints.weightx = 1;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		gridbag.setConstraints( distritoText, constraints );
		moradaPanel.add( distritoText );
		
		constraints.weightx = 0;
		constraints.gridwidth = 1;
		gridbag.setConstraints( concelhoLabel, constraints );
		moradaPanel.add( concelhoLabel );
		
		constraints.weightx = 1;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		gridbag.setConstraints( concelhoText, constraints );
		moradaPanel.add( concelhoText );
		
		
		listaMarcacoesPanel = new JPanel();
		listaMarcacoesPanel.setLayout( new GridLayout( 1, 1 ) );
		VectorTableModel marcacoesModel = new VectorTableModel( new String[]{ "Data", "Realizada", "Texto" } );
		marcacoesTable = new BaseTable( marcacoesModel );
		marcacoesTable.setToolTipColumn( 2 );
		marcacoesTable.fixColumnWidth( 0, 150 );
		marcacoesTable.fixColumnWidth( 1, 80 );
		marcacoesTable.setNonResizableNorReordable();
		JScrollPane marcacoesScp = new JScrollPane( marcacoesTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
													JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
		listaMarcacoesPanel.add( marcacoesScp );
		
		marcacoesCardLayout = new CardLayout();
		marcacaoPanel = new MarcacaoEmpresaPanel( this );
		marcacoesPanel.setLayout( marcacoesCardLayout );
		marcacoesPanel.add( MarcacaoEmpresaPanel.LIST_PANEL, listaMarcacoesPanel );
		marcacoesPanel.add( MarcacaoEmpresaPanel.EDIT_PANEL, marcacaoPanel );
		
		marcacoesTable.getSelectionModel().addListSelectionListener( new ListSelectionListener(){
			public void valueChanged( ListSelectionEvent e )
			{
				int selected = marcacoesTable.getSelectedRow();
				if( selected == -1 || e.getValueIsAdjusting() )
				{
					return;
				}
				Object row = ( (VectorTableModel)marcacoesTable.getModel() ).getRowAt( selected );
				marcacoesTable.clearSelection();
				marcacaoPanel.setEmpresa( empresa );
				if( row != null && row instanceof MarcacaoEmpresaData )
				{
					marcacaoPanel.fill( row );
				}
				else
				{
					marcacaoPanel.fill( null );
				}
				marcacoesCardLayout.show( marcacoesPanel, MarcacaoEmpresaPanel.EDIT_PANEL );
			}
		} );
		
		servicosCombo.addItem( new MappableObject( new Integer( 1 ), "SHST" ) );
		servicosCombo.addItem( new MappableObject( new Integer( 2 ), "Medicina do Trabalho" ) );
		servicosCombo.addItem( new MappableObject( new Integer( 3 ), "Higiene e Seguran\u00e7a" ) );
		servicosCombo.addItem( new MappableObject( new Integer( 4 ), "Forma\u00e7\u00e3o" ) );
		servicosCombo.addItem( new MappableObject( new Integer( 5 ), "Estudos Especificos HS" ) );
		servicosCombo.addItem( new MappableObject( new Integer( 6 ), "Sinal\u00e9tica" ) );
		
		new CopyPasteHandler( designacaoSocialText );
		new CopyPasteHandler( moradaText );
		new CopyPasteHandler( codigoPostalText );
		new CopyPasteHandler( localText );
		new CopyPasteHandler( distritoText );
		new CopyPasteHandler( concelhoText );
		new CopyPasteHandler( codigo1Text );
		new CopyPasteHandler( codigo2Text );
		new CopyPasteHandler( codigo3Text );
//		new CopyPasteHandler( perfil1Text );
//		new CopyPasteHandler( perfil2Text );
		new CopyPasteHandler( precoHigieneText );
		new CopyPasteHandler( precoMedicinaText );
		new CopyPasteHandler( modalidadePagamentoText );
		new CopyPasteHandler( caeText );
		new CopyPasteHandler( actividadeText );
		new CopyPasteHandler( contribuinteText );
		new CopyPasteHandler( segSocialText );
	}
	
	private void setupComponentsHashtable()
	{
		components = new ComponentsHashtable();
		components.putComponent( EmpresaData.DESIGNACAO_SOCIAL, designacaoSocialText );
		components.putComponent( EmpresaData.MORADA, moradaText );
		components.putComponent( EmpresaData.CODIGO_POSTAL, codigoPostalText );
		components.putComponent( EmpresaData.LOCALIDADE, localText );
		components.putComponent( EmpresaData.DISTRITO, distritoText );
		components.putComponent( EmpresaData.CONCELHO, concelhoText );
		components.putComponent( EmpresaData.DATA_PROPOSTA, envioPropostaPanel );
		components.putComponent( EmpresaData.DATA_ACEITACAO, aceitacaoPropostaPanel );
		components.putComponent( EmpresaData.INICIO_CONTRATO, inicioContratoPanel );
		components.putComponent( EmpresaData.DURACAO, duracaoContratoText );
		components.putComponent( EmpresaData.DATA_CANCELAMENTO, cancelamentoPanel );
		components.putDummy( EmpresaData.PERFIL_1 );
		components.putDummy( EmpresaData.PERFIL_2);
		components.putComponent( EmpresaData.DATA_ENVIO_CONTRATO, envioContratoPanel );
		components.putComponent( EmpresaData.DATA_RECEPCAO_CONTRATO, recepcaoContratoPanel );
		components.putComponent( EmpresaData.DATA_ENVIO_IDICT, envioIdictPanel );
		components.putComponent( EmpresaData.DATA_RELATORIO_ANUAL, relatorioAnualPanel );
		components.putComponent( EmpresaData.CODIGO_1, codigo1Text );
		components.putComponent( EmpresaData.CODIGO_2, codigo2Text );
		components.putComponent( EmpresaData.CODIGO_3, codigo3Text );
		components.putComponent( EmpresaData.CAE, caeText );
		components.putComponent( EmpresaData.ACTIVIDADE, actividadeText );
		components.putComponent( EmpresaData.CONTRIBUINTE, contribuinteText );
		components.putComponent( EmpresaData.SEGURANCA_SOCIAL, segSocialText );
		components.putComponent( EmpresaData.CONTACTO_1, contacto1Panel );
		components.putComponent( EmpresaData.CONTACTO_2, contacto2Panel );
		components.putComponent( EmpresaData.SERVICOS, servicosCombo );
		components.putComponent( EmpresaData.PRECO_HIGIENE, precoHigieneText );
		components.putComponent( EmpresaData.PRECO_MEDICINA, precoMedicinaText );
		components.putComponent( EmpresaData.PERIODICIDADE, modalidadePagamentoText );
		components.putDummy( EmpresaData.SERVICO_SAUDE_TIPO );
		components.putDummy( EmpresaData.SERVICO_SAUDE_DESIGNACAO );
		components.putDummy( EmpresaData.SERVICO_HIGIENE_TIPO );
		components.putDummy( EmpresaData.SERVICO_HIGIENE_DESIGNACAO );
		components.putDummy( EmpresaData.DESIGNACAO_SOCIAL_PLAIN );
	}
	
	public void clear()
	{
		String names[] = (String[])components.keySet().toArray( new String[0] );
		ComponentController.clear( names, components );
		estabelecimentosModel.clearAll();
		protocoloPanel.clear();
		clearMarcacoes();
		empresa = null;
	}
	
	public void fill(Object value)
	{
		clear();
		
		if( value == null || ( ( Object[] )value )[ 0 ] == null )
		{
//			userPanel.setEmpresaID( null );
			return;
		}
		empresa = ( EmpresaData ) ( ( Object [] )value )[ 0 ];
		Integer elementosProtocolo[][] = ( Integer [][] ) ( ( Object [] )value )[ 1 ];
		
		String names[] = ( String[] ) components.keySet().toArray( new String[ components.size() ] );
		ComponentController.fill( names, empresa.getHashData(), components );
		id = (Integer) empresa.get( EmpresaData.ID );
		IDObject estabelecimentos[];
		try
		{
			estabelecimentos = provider.getAllEstabelecimentosForEmpresa( id );
		}
		catch( Exception ex )
		{
			DialogException.showExceptionMessage( ex, "Erro a carregar a lista de estabelecimentos", true );
			estabelecimentos = new IDObject[0];
		}
		reloadMarcacoes();
		Vector v = new Vector( Arrays.asList( estabelecimentos ) );
		estabelecimentosModel.setValues( v );
		
		Vector dadosProtocolo[] = new Vector[]{ new Vector(), new Vector() };
		boolean analisesExamesA[] = new boolean[2];
		analisesExamesA[ 0 ] = "y".equals( empresa.get( EmpresaData.A_CONSULTAS ) );
		analisesExamesA[ 1 ] = "y".equals( empresa.get( EmpresaData.A_EXAMES ) );
		dadosProtocolo[ 0 ].add( analisesExamesA );
		dadosProtocolo[ 0 ].add( elementosProtocolo[ 0 ] );
		boolean analisesExamesB[] = new boolean[2];
		analisesExamesB[ 0 ] = "y".equals( empresa.get( EmpresaData.B_CONSULTAS ) );
		analisesExamesB[ 1 ] = "y".equals( empresa.get( EmpresaData.B_EXAMES ) );
		dadosProtocolo[ 1 ].add( analisesExamesB );
		dadosProtocolo[ 1 ].add( elementosProtocolo[ 1 ] );
		protocoloPanel.fill( dadosProtocolo );
//		if( webAware )
//		{
//			userPanel.setEmpresaID( (Integer) empresa.get( EmpresaData.ID ) );
//			userPanel.setEnabled( id != null );
//		}
	}
	
	public Object save()
	{
		if( empresa == null )
		{
			empresa = new EmpresaData();
		}
			
		String names[] = ( String[] ) components.keySet().toArray( new String[ components.size() ] );
		Hashtable hash = empresa.getHashData();
		ComponentController.save( names, hash, components );
		empresa.setHashData( hash );
		Vector dadosProtocolo[] = ( Vector[] ) protocoloPanel.save();
		boolean analisesExamesA[] = ( boolean [] ) dadosProtocolo[ 0 ].elementAt( 0 );
		empresa.set( EmpresaData.A_CONSULTAS, analisesExamesA[ 0 ] ? "y" : "n" );
		empresa.set( EmpresaData.A_EXAMES, analisesExamesA[ 1 ] ? "y" : "n" );
		boolean analisesExamesB[] = ( boolean [] ) dadosProtocolo[ 1 ].elementAt( 0 );
		empresa.set( EmpresaData.B_CONSULTAS, analisesExamesB[ 0 ] ? "y" : "n" );
		empresa.set( EmpresaData.B_EXAMES, analisesExamesB[ 1 ] ? "y" : "n" );
		Integer elementosProtocolo[][] = 
			new Integer[][]{ ( Integer [] ) dadosProtocolo[ 0 ].elementAt( 1 ), 
								( Integer [] ) dadosProtocolo[ 1 ].elementAt( 1 ) };
		return new Object[] { empresa, elementosProtocolo };
	}
	
	public void setEnabled( boolean enable )
	{
		super.setEnabled( enable );
		String names[] = (String[])components.keySet().toArray( new String[0] );
		ComponentController.setEnabled( names, enable, components );
		protocoloPanel.setEnabled( enable );
		//estabelecimentosTable.setEnabled( enable );
	}
	
	public void addListSelectionListener( ListSelectionListener listener )
	{
		listSelectionListeners.add( listener );
	}
	
	public void removeListSelectionListener( ListSelectionListener listener )
	{
		listSelectionListeners.remove( listener );
	}
	
	public void valueChanged( ListSelectionEvent e )
	{
		for( int n = 0; n < listSelectionListeners.size(); n++ )
		{
			ListSelectionEvent newEvent = 
				new ListSelectionEvent( this, e.getFirstIndex(), e.getLastIndex(), e.getValueIsAdjusting() );
			( (ListSelectionListener) listSelectionListeners.elementAt( n ) ).valueChanged( newEvent );
		}
	}
	
	public Integer getSelectedEstabelecimento()
	{
		int selected = estabelecimentosTable.getSelectedRow();
		if( selected == -1 )
		{
			return null;
		}
		return ( (IDObject) estabelecimentosModel.getRowAt( selected ) ).getID();
	}
	
	public void reload()
	{
		int selected = estabelecimentosTable.getSelectedRow();
		fill( empresa );
		if( selected != -1 )
		{
			estabelecimentosTable.setRowSelectionInterval( selected, selected );
		}
	}
	
	public JTable getEstabelecimentosTable()
	{
		return estabelecimentosTable;
	}
	
	public void setSelectedEstabelecimento( EstabelecimentoData estab )
	{
		IDObject lista[] = (IDObject [])estabelecimentosModel.getValues().toArray( new IDObject[ 0 ] );
		Integer id = (Integer)estab.get( EstabelecimentoData.ID );
		for( int n = 0; n < lista.length; n++ )
		{
			if( lista[ n ].getID().equals( id ) )
			{
				estabelecimentosTable.setRowSelectionInterval( n, n );
				return;
			}
		}
		estabelecimentosTable.clearSelection();
	}
	
	protected void reloadMarcacoes()
	{
		try
		{
			Collection marcacoesCollection =
				JDO.listLoad( MarcacaoEmpresa.class, new Object[]{ empresa.get( EmpresaData.ID ) },
								new String []{ "empresa.id" }, new String[]{ "data descending" } );
			Vector marcacoes = new Vector();
			marcacoes.add( new ColumnizedObject(){
				public Object getValue( int col )
				{
					switch( col )
					{
						case 0: case 1:
							return "";
						case 2:
							return "NOVA TAREFA...";
					}
					return null;
				}
			} );
			if( marcacoesCollection != null )
			{
				marcacoes.addAll( marcacoesCollection );
			}
			marcacoesTable.clearSelection();
			( (VectorTableModel) marcacoesTable.getModel() ).setValues( marcacoes );
		}
		catch( Exception ex )
		{
			DialogException.showExceptionMessage( ex, "Erro a carregar as Marca\u00e7\u00f5es", true );
		}
		showMarcacoes();
	}
	
	protected void showMarcacoes()
	{
		marcacoesCardLayout.show( marcacoesPanel, MarcacaoEmpresaPanel.LIST_PANEL );
	}
	
	protected void clearMarcacoes()
	{
		( (VectorTableModel) marcacoesTable.getModel() ).clearAll();
	}
}
