/*
 * ProcessoUpperPanel.java
 *
 * Created on February 1, 2006, 6:48 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package siprp.medicina.processo;

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import com.evolute.utils.dataui.*;
import com.evolute.utils.tables.*;
import com.evolute.utils.ui.*;
import siprp.medicina.MedicinaDataProvider;

/**
 *
 * @author fpalma
 */
public class ProcessoUpperPanel extends JPanel
		implements ListSelectionListener, ControllableComponent
{
	protected BaseTable empresasTable;
	protected VectorTableModel empresasModel;
	protected BaseTable estabelecimentosTable;
	protected VectorTableModel estabelecimentosModel;
	protected BaseTable trabalhadoresTable;
	protected VectorTableModel trabalhadoresModel;
	
	protected MedicinaDataProvider provider;
	
	protected final Vector listeners;
	
	protected boolean editing = false;
	
	/**
	 * Creates a new instance of ProcessoUpperPanel
	 */
	public ProcessoUpperPanel()
		throws Exception
	{
		provider = ( MedicinaDataProvider ) MedicinaDataProvider.getProvider();
		listeners = new Vector();
		setupComponents();
	}
	
	private void setupComponents()
		throws Exception
	{
		empresasModel = new VectorTableModel( new String[]{ "empresas" } );
		empresasTable = new BaseTable( empresasModel );
		empresasTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
		empresasTable.setNonResizableNorReordable();
		JScrollPane empresasScroll = 
				new JScrollPane( empresasTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
									JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
		empresasTable.getSelectionModel().addListSelectionListener( this );
		estabelecimentosModel = new VectorTableModel( new String[]{ "estabelecimentos" } );
		estabelecimentosTable = new BaseTable( estabelecimentosModel );
		estabelecimentosTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
		estabelecimentosTable.setNonResizableNorReordable();
		JScrollPane estabelecimentosScroll = 
				new JScrollPane( estabelecimentosTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
									JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
		estabelecimentosTable.getSelectionModel().addListSelectionListener( this );
		trabalhadoresModel = new VectorTableModel( new String[]{ "trabalhadores" } );
		trabalhadoresTable = new BaseTable( trabalhadoresModel );
		trabalhadoresTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
		trabalhadoresTable.setNonResizableNorReordable();
		JScrollPane trabalhadoresScroll = 
				new JScrollPane( trabalhadoresTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
									JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
		trabalhadoresTable.getSelectionModel().addListSelectionListener( this );
		
		GridBagLayout gridbag = new GridBagLayout();
		setLayout( gridbag );
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.insets = new Insets( 1, 1, 1, 1 );
		constraints.fill = GridBagConstraints.BOTH;
		constraints.gridwidth = 1;
		constraints.gridheight = 1;
		constraints.weightx = 0.3;
		constraints.weighty = 1;
		
		gridbag.setConstraints( empresasScroll, constraints );
		
		gridbag.setConstraints( estabelecimentosScroll, constraints );
		
		constraints.weightx = 0.4;
		constraints.gridheight = GridBagConstraints.REMAINDER;
		gridbag.setConstraints( trabalhadoresScroll, constraints );
		
		add( empresasScroll );
		add( estabelecimentosScroll );
		add( trabalhadoresScroll );
		
		ColumnizedMappable empresas[] = provider.getAllEmpresas();
		Vector values = empresasModel.getValues();
		values.addAll( Arrays.asList( empresas ) );
		empresasModel.setValues( values );
	}
	
	public void valueChanged( ListSelectionEvent e )
	{
		Object source = e.getSource();
		if( e.getValueIsAdjusting() )
		{
			return;
		}
		if( source.equals( empresasTable.getSelectionModel() ) )
		{
			carregarEstabelecimentos();
		}
		else if( source.equals( estabelecimentosTable.getSelectionModel() ) )
		{
			carregarTrabalhadores();
		}
		else if( source.equals( trabalhadoresTable.getSelectionModel() ) )
		{
			notifyListeners( e );
		}
	}
	
	protected void carregarEstabelecimentos()
	{
		estabelecimentosTable.clearSelection();
		int selected = empresasTable.getSelectedRow();
		estabelecimentosModel.clearAll();
		if( selected > -1 )
		{
			try
			{
				Integer empresaID = ( ( ColumnizedMappable ) empresasModel.getRowAt( selected ) ).getID();
				ColumnizedMappable estabelecimentos[] = provider.getAllEstabelecimentosForEmpresa( empresaID);
				Vector values = estabelecimentosModel.getValues();
				values.addAll( Arrays.asList( estabelecimentos ) );
				estabelecimentosModel.setValues( values );
			}
			catch( Exception ex )
			{
				DialogException.showExceptionMessage( ex, "Erro a carregar os estabelecimentos.", true );
				estabelecimentosModel.clearAll();
			}
		}
	}
	
	protected void carregarTrabalhadores()
	{
		trabalhadoresTable.clearSelection();
		int selected = estabelecimentosTable.getSelectedRow();
		trabalhadoresModel.clearAll();
		if( selected > -1 )
		{
			try
			{
				Integer estabelecimentoID = ( ( ColumnizedMappable ) estabelecimentosModel.getRowAt( selected ) ).getID();
				ColumnizedMappable trabalhadores[] = provider.getAllTrabalhadoresForEstabelecimento( estabelecimentoID );
				Vector values = trabalhadoresModel.getValues();
				values.addAll( Arrays.asList( trabalhadores ) );
				trabalhadoresModel.setValues( values );
			}
			catch( Exception ex )
			{
				DialogException.showExceptionMessage( ex, "Erro a carregar trabalhadores.", true );
				trabalhadoresModel.clearAll();
			}
		}
	}
	
	protected void notifyListeners( ListSelectionEvent e )
	{
		for( int n = 0; n < listeners.size(); n++ )
		{
			ListSelectionEvent event = 
					new ListSelectionEvent( this, e.getFirstIndex(), e.getLastIndex(), e.getValueIsAdjusting() );
			( ( ListSelectionListener ) listeners.elementAt( n ) ).valueChanged( event );
		}
	}
	
	public void addListSelectionListener( ListSelectionListener listener )
	{
		listeners.add( listener );
	}
	
	public void removeSelectionListener( ListSelectionListener listener )
	{
		listeners.remove( listener );
	}
	
	public void clear()
	{
//		empresasTable.clearSelection();
	}
	
	public void fill( Object value )
	{
		if( value == null )
		{
			clear();
		}
		Integer ids[] = ( Integer [] ) value;
		for( int n = 0; n < empresasTable.getRowCount(); n++ )
		{
			if( ( ( ColumnizedMappable ) empresasModel.getRowAt( n ) ).getID().equals( ids[ 0 ] ) )
			{
				empresasTable.setRowSelectionInterval( n, n );
				break;
			}
		}
		
		for( int n = 0; n < estabelecimentosTable.getRowCount(); n++ )
		{
			if( ( ( ColumnizedMappable ) estabelecimentosModel.getRowAt( n ) ).getID().equals( ids[ 1 ] ) )
			{
				estabelecimentosTable.setRowSelectionInterval( n, n );
				break;
			}
		}
		
		for( int n = 0; n < trabalhadoresTable.getRowCount(); n++ )
		{
			if( ( ( ColumnizedMappable ) trabalhadoresModel.getRowAt( n ) ).getID().equals( ids[ 2 ] ) )
			{
				trabalhadoresTable.setRowSelectionInterval( n, n );
				break;
			}
		}
	}
	
	public Object save()
	{
		int empresaSelected = empresasTable.getSelectedRow();
		int estabelecimentoSelected = estabelecimentosTable.getSelectedRow();
		int trabalhadorSelected = trabalhadoresTable.getSelectedRow();
		return new Integer[]{
				empresaSelected == -1 ? null : ( ( ColumnizedMappable ) empresasModel.getRowAt( empresaSelected ) ).getID(),
				estabelecimentoSelected == -1 ? null : ( ( ColumnizedMappable ) estabelecimentosModel.getRowAt( estabelecimentoSelected ) ).getID(),
				trabalhadorSelected == -1 ? null : ( ( ColumnizedMappable ) trabalhadoresModel.getRowAt( trabalhadorSelected ) ).getID()
			};
	}
	
	public void setEnabled( boolean enable )
	{
		editing = !enable;
		empresasTable.setEnabled( enable );
		estabelecimentosTable.setEnabled( enable );
		trabalhadoresTable.setEnabled( enable );
	}
}
