/*
 * MarcacoesHigieneUpperPanel.java
 *
 * Created on February 2, 2006, 12:40 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package siprp.higiene.marcacoes;

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

import com.evolute.utils.*;
import com.evolute.utils.dataui.*;
import com.evolute.utils.tables.*;
import com.evolute.utils.ui.*;

import siprp.higiene.*;

/**
 *
 * @author fpalma
 */
public class MarcacoesHigieneUpperPanel extends JPanel
		implements ListSelectionListener, ControllableComponent
{
	protected BaseTable empresasTable;
	protected VectorTableModel empresasModel;
	protected BaseTable estabelecimentosTable;
	protected VectorTableModel estabelecimentosModel;
	
	protected HigieneDataProvider provider;
	
	protected final Vector listeners;
	
	/** Creates a new instance of MedicinaUpperPanel */
	public MarcacoesHigieneUpperPanel()
		throws Exception
	{
		provider = ( HigieneDataProvider ) HigieneDataProvider.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 );
		
		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 = GridBagConstraints.REMAINDER;
		constraints.weightx = 0.5;
		constraints.weighty = 1;
		
		gridbag.setConstraints( empresasScroll, constraints );
		
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		gridbag.setConstraints( estabelecimentosScroll, constraints );
		
		add( empresasScroll );
		add( estabelecimentosScroll );
		
		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() ) )
		{
			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 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;
			}
		}
	}
	
	public Object save()
	{
		int empresaSelected = empresasTable.getSelectedRow();
		int estabelecimentoSelected = estabelecimentosTable.getSelectedRow();
		return new Integer[]{
				empresaSelected == -1 ? null : ( ( ColumnizedMappable ) empresasModel.getRowAt( empresaSelected ) ).getID(),
				estabelecimentoSelected == -1 ? null : ( ( ColumnizedMappable ) estabelecimentosModel.getRowAt( estabelecimentoSelected ) ).getID()
			};
	}
	
	public void setEnabled( boolean enable )
	{
		empresasTable.setEnabled( enable );
		estabelecimentosTable.setEnabled( enable );
	}
}
