/*
 * ProtocoloPanel.java
 *
 * Created on January 27, 2006, 12:23 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package siprp.clientes;

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

import com.evolute.utils.data.*;
import com.evolute.utils.dataui.*;

/**
 *
 * @author fpalma
 */
public class ProtocoloPanel extends JPanel
	implements ControllableComponent
{
	protected ClientesDataProvider provider;
	
	protected IDObject grupos[];
	protected IDObject tipos[][];
	
	protected PerfilProtocoloPanel perfisPanels[];
	
	/** Creates a new instance of ProtocoloPanel */
	public ProtocoloPanel()
		throws Exception
	{
		provider = ( ClientesDataProvider ) ClientesDataProvider.getProvider();
		IDObject gruposReais[] = provider.getAllGruposProtocoloReais();
		Vector gruposAux = new Vector( Arrays.asList( gruposReais ) );
		gruposAux.add( new MappableObject( new Integer( 0 ), "Outros" ) );
		grupos = ( IDObject[] ) gruposAux.toArray( new IDObject[ gruposReais.length + 1 ] );
		
		IDObject gruposFalsos[] = provider.getAllGruposProtocoloFalsos();
		
		Hashtable tiposHash = provider.getAllTiposElementosProtocoloByGrupo();
		tipos = new IDObject[ grupos.length ][];
		for( int n = 0; n < gruposReais.length; n++ )
		{
			Vector tiposGrupo = ( Vector ) tiposHash.get( gruposReais[ n ].getID() );
			tipos[ n ] = ( IDObject[] ) tiposGrupo.toArray( new IDObject[ tiposGrupo.size() ] );
		}
		Vector outros = new Vector();
		for( int n = 0; n < gruposFalsos.length; n++ )
		{
			Vector tiposGrupo = ( Vector ) tiposHash.get( gruposFalsos[ n ].getID() );
			outros.addAll( tiposGrupo );
		}
		tipos[ gruposReais.length ] = ( IDObject[] ) outros.toArray( new IDObject[ outros.size() ] );
		setupComponents();
	}
	
	private void setupComponents()
	{
		perfisPanels = new PerfilProtocoloPanel[ 2 ];
		
		setLayout( new GridLayout( 1, 2 ) );
		
		perfisPanels[ 0 ] = new PerfilProtocoloPanel( grupos, tipos );
		JScrollPane perfilAScp = 
			new JScrollPane( perfisPanels[ 0 ], JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
								JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		perfilAScp.setBorder( BorderFactory.createTitledBorder(
								BorderFactory.createEtchedBorder(), "Perfil A" ) );
		add( perfilAScp );
		
		perfisPanels[ 1 ] = new PerfilProtocoloPanel( grupos, tipos );
		JScrollPane perfilBScp = 
			new JScrollPane( perfisPanels[ 1 ], JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
								JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		perfilBScp.setBorder( BorderFactory.createTitledBorder(
								BorderFactory.createEtchedBorder(), "Perfil B" ) );
		add( perfilBScp );
	}
	
	public void fill( Object data )
	{
		Vector lists[] = ( Vector [] ) data;
		perfisPanels[ 0 ].fill( lists[ 0 ] );
		perfisPanels[ 1 ].fill( lists[ 1 ] );
	}
	
	public Object save()
	{
		Vector lists[] = new Vector[ 2 ];
		lists[ 0 ] = ( Vector ) perfisPanels[ 0 ].save();
		lists[ 1 ] = ( Vector ) perfisPanels[ 1 ].save();
		return lists;
	}
	
	public void setEnabled( boolean enable )
	{
		perfisPanels[ 0 ].setEnabled( enable );
		perfisPanels[ 1 ].setEnabled( enable );
	}
	
	public void clear()
	{
		perfisPanels[ 0 ].clear();
		perfisPanels[ 1 ].clear();
	}
}
