/*
 * MarcacoesHigieneWindow.java
 *
 * Created on January 31, 2006, 11:32 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package siprp.higiene.mapa;

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

import com.evolute.utils.documents.*;
import com.evolute.utils.tables.*;
import com.evolute.utils.ui.*;
import com.evolute.utils.ui.window.*;

import siprp.higiene.*;

/**
 *
 * @author fpalma
 */
public class MapaHigieneWindow extends EditorWindow
	implements ActionListener
{
	public final static String TITLE = "Mapa de Higiene e Seguran\u00e7a";
	
	private final static int iPermissionArray[][] =
		new int[][]{ { } };
	
	protected MapaHigienePanel mainPanel;
	protected JTextField anoText;
	protected JButton actualizarButton;
	
	protected HigieneDataProvider provider;
	
	public static void main( String args[] )
		throws Exception
	{
		
		JFrame frm = new MapaHigieneWindow();
		frm.setVisible( true );
	}
	
	/** Creates a new instance of MarcacoesHigieneWindow */
	public MapaHigieneWindow()	
		throws Exception
	{
		super( iPermissionArray );
		provider = ( HigieneDataProvider ) HigieneDataProvider.getProvider();
		setupComponents();
		setExtendedState(getExtendedState() | MAXIMIZED_BOTH);
	}
	
	private void setupComponents()
		throws Exception
	{
		setTitle( TITLE );
		JPanel upperPanel = new JPanel();
		mainPanel = new MapaHigienePanel();
		getContentPane().setLayout( new BorderLayout() );
		getContentPane().add( upperPanel, BorderLayout.NORTH );
		getContentPane().add( mainPanel, BorderLayout.CENTER );
		
		upperPanel.setLayout( new FlowLayout( FlowLayout.LEFT ) );
		upperPanel.add( new TecnicosPanel() );
		JLabel anoLabel = new JLabel( "Ano" );
		anoText = new JTextField();
		anoText.setPreferredSize( new Dimension( 50, 20 ) );
		anoText.setDocument( new YearDocument() );
		anoText.addActionListener( this );
		actualizarButton = new JButton( "Actualizar" );
		actualizarButton.addActionListener( this );
		
		Calendar cal = Calendar.getInstance();
		anoText.setText( "" + cal.get( Calendar.YEAR ) );
		
		JPanel pesquisaPanel = new JPanel();
		upperPanel.add( pesquisaPanel );
		pesquisaPanel.setLayout( new FlowLayout( FlowLayout.CENTER ) );
		pesquisaPanel.add( anoLabel );
		pesquisaPanel.add( anoText );
		pesquisaPanel.add( actualizarButton );
		
//		Vector data[][] = new Vector[ 31 ][ 12 ];
//		for( int r = 0; r < 31; r++ )
//		{
//			for( int c = 0; c < 12; c++ )
//			{
//				data[ r ][ c ] = new Vector();
//			}	
//		}
////		Vector example = new Vector();
////		example.add( new Object[]{ "Empresa1", "Estabelecimento1", "Pai Natal", "9:30", Color.red } );
//		data[ 20 ][ 10 ].add( new Object[]{ new Integer( 21 ) } );
//		data[ 20 ][ 10 ].add( new Object[]{ "Empresa1", "Estabelecimento1", "Pai Natal", Color.red } );
//		data[ 20 ][ 10 ].add( new Object[]{ "Empresa1", "Estabelecimento1", "Pai Natal", Color.green } );
//		data[ 20 ][ 10 ].add( new Object[]{ "Empresa1", "Estabelecimento1", "Pai Natal", Color.yellow } );
//		data[ 20 ][ 10 ].add( new Object[]{ "Empresa1", "Estabelecimento1", "Pai Natal", Color.white } );
//		mainPanel.fill( data );
	}
	
	public void actualizar()
	{
		String anoStr = anoText.getText();
		if( anoStr.length() == 0 )
		{
			mainPanel.clear();
		}
		else
		{
			Integer ano = new Integer( anoStr );
			try
			{
				mainPanel.fill( provider.getMapaAnual( ano ) );
			}
			catch( Exception ex )
			{
				DialogException.showExceptionMessage( ex, "Erro a carregar dados", true );
				mainPanel.clear();
			}
		}
	}
	
	public void actionPerformed( ActionEvent e )
	{
		Object source = e.getSource();
		if( source.equals( actualizarButton ) || source.equals( anoText ) )
		{
			actualizar();
		}
	}
}
