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

package siprp.clientes;

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

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

/**
 *
 * @author fpalma
 */
public class PerfilProtocoloPanel extends JPanel
	implements ControllableComponent, ItemListener
{
	protected JCheckBox consultasCheck;
	protected JCheckBox examesCheck;
	protected CheckBoxPanel tiposCheck[];
	
	protected IDObject grupos[];
	protected IDObject tipos[][];
	
	/** Creates a new instance of PerfilProtocoloPanel */
	public PerfilProtocoloPanel( IDObject grupos[], IDObject tipos[][] )
	{
		this.grupos = grupos;
		this.tipos = tipos;
		setupComponents();
	}
	
	private void setupComponents()
	{
		consultasCheck = new JCheckBox( "Consultas" );
		examesCheck = new JCheckBox( "Exames" );
		examesCheck.addItemListener( this );
		tiposCheck = new CheckBoxPanel[ grupos.length ];
		
		GridBagLayout gridbag = new GridBagLayout();
		setLayout( gridbag );
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.insets = new Insets( 1, 1, 1, 1 );
		
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weighty = 0;
		constraints.weightx = 1;
		constraints.gridheight = 1;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		gridbag.setConstraints( consultasCheck, constraints );
		gridbag.setConstraints( examesCheck, constraints );
		
		add( consultasCheck );
		add( examesCheck );
		
		constraints.fill = GridBagConstraints.BOTH;
		constraints.weighty = grupos.length > 0 ? 1.0 / grupos.length : 0.0;
		for( int n = 0; n < grupos.length; n++ )
		{
			tiposCheck[ n ] = setupComponentsGrupo( n );
			gridbag.setConstraints( tiposCheck[ n ], constraints );
			add( tiposCheck[ n ] );
			tiposCheck[ n ].setEnabled( false );
		}
	}
	
	private CheckBoxPanel setupComponentsGrupo( int indice )
	{
		String desc = grupos[ indice ].toString();
		CheckBoxPanel panel = new CheckBoxPanel( tipos[ indice ] );
		panel.setBorder( BorderFactory.createTitledBorder(
							BorderFactory.createEtchedBorder(), desc ) );
		return panel;
	}
	
	public void fill( Object data )
	{ 
		clear();
		if( data != null )
		{
			Vector list = ( Vector ) data;
			boolean consultasExames[] = (boolean[]) list.elementAt( 0 );
			Integer tiposEscolhidos[] = ( Integer [] ) list.elementAt( 1 );
			consultasCheck.setSelected( consultasExames[ 0 ] );
			examesCheck.setSelected( consultasExames[ 1 ] );
			for( int n = 0; n < tiposCheck.length; n++ )
			{
				tiposCheck[ n ].fill( tiposEscolhidos );
			}
		}
	}
	
	public Object save()
	{
		boolean consultasExames[] = new boolean[ 2 ];
		consultasExames[ 0 ] = consultasCheck.isSelected();
		consultasExames[ 1 ] = examesCheck.isSelected();
		Vector tiposEscolhidos = new Vector();
		if( consultasExames[ 1 ] )
		{
			for( int n = 0; n < tiposCheck.length; n++ )
			{
				tiposEscolhidos.addAll( Arrays.asList( tiposCheck[ n ].getSelected() ) );
			}
		}
		Vector list = new Vector();
		list.add( consultasExames );
		list.add( tiposEscolhidos.toArray( new Integer[ tiposEscolhidos.size() ] ) );
		return list;
	}
	
	public void setEnabled( boolean enable )
	{
		consultasCheck.setEnabled( enable );
		examesCheck.setEnabled( enable );
		for( int n = 0; n < tiposCheck.length; n++ )
		{
			tiposCheck[ n ].setEnabled( enable && examesCheck.isSelected() );
		}
	}
	
	public void clear()
	{
		consultasCheck.setSelected( false );
		examesCheck.setSelected( false );
		for( int n = 0; n < tiposCheck.length; n++ )
		{
			tiposCheck[ n ].clear();
		}
	}
	
	public void itemStateChanged( ItemEvent e )
	{
		Object source = e.getSource();
		if( source.equals( examesCheck ) )
		{
			for( int n = 0; n < tiposCheck.length; n++ )
			{
				tiposCheck[ n ].setEnabled( examesCheck.isEnabled() && examesCheck.isSelected() );
			}
		}
	}
}
