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

package siprp.higiene.marcacoes;


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

import com.evolute.utils.*;
import com.evolute.utils.jdo.*;
import com.evolute.utils.tables.*;
import com.evolute.utils.ui.*;
import com.evolute.utils.ui.button.*;
import com.evolute.utils.ui.text.*;

import siprp.*;
import siprp.data.*;
import siprp.higiene.*;

/**
 *
 * @author fpalma
 */
public class TecnicoEditor extends CustomJDialog
	implements ActionListener, ListSelectionListener
//	, ItemListener
{
	static
	{
		TecnicoHSTData.registerOperationHandlerForClass( DisableDeleteHandler.INSTANCE, TecnicoHSTData.class );
	}
	
	private JDOProvider JDO;
	private HigieneDataProvider provider;
	private BaseTable table;
	private VectorTableModel model;
	private JButton okButton;
	private JButton cancelarButton;
	private JButton novoButton;
	private JTextField nomeText;
	private JTextField iniciaisText;
//	private JComboBox corCombo;
	private Integer id;
	private boolean isNew = false;
	private BetterButtonGroup corGroup;
	private JRadioButton cores[];
	
	/** Creates a new instance of MedicoEditor */
	public TecnicoEditor( JFrame owner )
		throws Exception
	{
		super( owner, true );
		provider = (HigieneDataProvider)HigieneDataProvider.getProvider();
		JDO = ( JDOProvider ) Singleton.getInstance( Singleton.DEFAULT_JDO_PROVIDER );
		setupComponents();
	}
    
	private void setupComponents()
		throws Exception
	{
		setSize( 600, 300 );
		setResizable( false );
		setTitle( "Criar/Editar T\u00e9cnico" );
		centerSuper();
		
		model = new VectorTableModel( new String[]{ "Nome", "Iniciais", "Cor" } );
		table = new BaseTable( model );
		table.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
		table.setTableCellRenderer( new TecnicoRenderer() );
		JScrollPane scp = new JScrollPane();
		scp.setMinimumSize( new Dimension( 300, 200 ) );
		scp.setMaximumSize( new Dimension( 300, 200 ) );
		scp.setPreferredSize( new Dimension( 300, 200 ) );
		scp.setSize( new Dimension( 300, 200 ) );
		scp.setViewportView( table );
		scp.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
		scp.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
		table.fixColumnWidth( 0, 200 );
		table.setNonResizableNorReordable();
		table.getSelectionModel().addListSelectionListener( this );
		Object obj[][] = provider.getAllTecnicosHST();
		Vector tecnicosHST = new Vector();
		for( int n = 0; n < obj.length; n++ )
		{
			tecnicosHST.add( new ColumnizedObjectArray( obj[ n ], true ) );
		}
		model.setValues( tecnicosHST );
		
		okButton = new JButton( "OK" );
		cancelarButton = new JButton( "Cancelar" );
		novoButton = new JButton( "Novo" );
		okButton.addActionListener( this );
		cancelarButton.addActionListener( this );
		novoButton.addActionListener( this );
		
		nomeText = new JTextField();
		nomeText.setEnabled( false );
		iniciaisText = new JTextField();
		iniciaisText.setEnabled( false );
		JPanel corPanel = new JPanel();
		corPanel.setLayout( new BoxLayout( corPanel, BoxLayout.Y_AXIS ) );
//		corCombo = new JComboBox();
		corGroup = new BetterButtonGroup( false );
		cores = new JRadioButton[ HigieneDataProvider.CORES_TECNICOS.length ];
		for( int n = 0; n < HigieneDataProvider.CORES_TECNICOS.length; n++ )
		{
			cores[ n ] = new JRadioButton( "            " );
			cores[ n ].setBackground( HigieneDataProvider.CORES_TECNICOS[ n ] );
			corPanel.add( cores[ n ] );
			corGroup.add( cores[ n ] );
//			corCombo.addItem( new Integer( n ) );
		}
		JScrollPane corScroll = 
			new JScrollPane( corPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
								JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
//		corCombo.setRenderer( new CorTecnicoCellRenderer() );
//		corCombo.setEnabled( false );
		
		GridBagLayout gridbag = new GridBagLayout();
		getContentPane().setLayout( gridbag );
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.insets = new Insets( 1, 1, 1, 1 );
		constraints.fill = GridBagConstraints.BOTH;
		constraints.weighty = 1;
		constraints.weightx = 1;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		constraints.gridheight = 1;
		
		JPanel upperPanel = new JPanel();
		upperPanel.setLayout( new GridLayout( 1, 2 ) );
		upperPanel.add( scp );
		JPanel textPanel = new JPanel();
		upperPanel.add( textPanel );
		gridbag.setConstraints( upperPanel, constraints );
		getContentPane().add( upperPanel );
		
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.weighty = 0;
		constraints.weightx = 0.3;
		constraints.gridwidth = 1;
		constraints.gridheight = GridBagConstraints.REMAINDER;;
		
		gridbag.setConstraints( novoButton, constraints );
		getContentPane().add( novoButton );
		
		gridbag.setConstraints( okButton, constraints );
		getContentPane().add( okButton );
		
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		gridbag.setConstraints( cancelarButton, constraints );
		getContentPane().add( cancelarButton );
		
		gridbag = new GridBagLayout();
		textPanel.setLayout( gridbag );
		constraints.weighty = 0;
		constraints.weightx = 1;
		constraints.gridwidth = GridBagConstraints.REMAINDER;
		constraints.gridheight = 1;
		
		JLabel nomeLabel = new JLabel( "Nome" );
		gridbag.setConstraints( nomeLabel, constraints );
		textPanel.add( nomeLabel );
		
		gridbag.setConstraints( nomeText, constraints );
		textPanel.add( nomeText );
		
		JLabel iniciaisLabel = new JLabel( "Iniciais" );
		gridbag.setConstraints( iniciaisLabel, constraints );
		textPanel.add( iniciaisLabel );
		
		gridbag.setConstraints( iniciaisText, constraints );
		textPanel.add( iniciaisText );
		
		JLabel corLabel = new JLabel( "Cor" );
		gridbag.setConstraints( corLabel, constraints );
		textPanel.add( corLabel );
		
//		gridbag.setConstraints( corCombo, constraints );
//		textPanel.add( corCombo );
//		
		constraints.weighty = 1;
		constraints.gridheight = GridBagConstraints.REMAINDER;
		constraints.fill = GridBagConstraints.BOTH;
//		JPanel pad = new JPanel();
//		gridbag.setConstraints( pad, constraints );
//		textPanel.add( pad );
		gridbag.setConstraints( corScroll, constraints );
		textPanel.add( corScroll );
	
		new CopyPasteHandler( nomeText );
		new CopyPasteHandler( iniciaisText );
//		corCombo.addItemListener( this );
	}
	
	public void actionPerformed( ActionEvent e )
	{
		if( e.getSource().equals( okButton ) )
		{
			if( save() )
			{
				close();
			}
		}
		else if( e.getSource().equals( cancelarButton ) )
		{
			close();
		}
		if( e.getSource().equals( novoButton ) )
		{
			table.getSelectionModel().removeListSelectionListener( this );
			nomeText.setText( "" );
			iniciaisText.setText( "" );
//			corCombo.setSelectedIndex( 0 );
			corGroup.setIntSelected( 0 );
			nomeText.setEnabled( true );
			iniciaisText.setEnabled( true );
			corGroup.setEnabled( true );
			id = null;
			isNew = true;
		}
	}
	
	public void valueChanged(javax.swing.event.ListSelectionEvent listSelectionEvent)
	{
		int selected = table.getSelectedRow();
		if( selected == -1 )
		{
			return;
		}
		ColumnizedObjectArray row = (ColumnizedObjectArray)model.getRowAt( selected );
		id = row.getID();
		String nome = (String) row.getValue( 0 );
		String iniciais = (String) row.getValue( 1 );
		Integer cor = ( Integer ) row.getValue( 2 );
		nomeText.setText( nome );
		iniciaisText.setText( iniciais );
//		corCombo.setSelectedIndex( cor.intValue() );
		corGroup.setIntSelected( cor.intValue() );
		nomeText.setEnabled( true );
		iniciaisText.setEnabled( true );
//		corCombo.setEnabled( true );
		corGroup.setEnabled( true );
	}
	
	private void close()
	{
		setVisible( false );
		dispose();
	}
	
	public boolean getIsNew()
	{
		return isNew;
	}
	
	public Integer getID()
	{
		return id;
	}
	
	private boolean save()
	{
		String nome = nomeText.getText().trim();
		String nomePlain = com.evolute.utils.strings.StringPlainer.convertString( nome );
		String iniciais = iniciaisText.getText().trim();
		Integer cor = new Integer( corGroup.getIntSelected() );
		if( nome.length() == 0 || iniciais.length() == 0 )
		{
			JOptionPane.showMessageDialog( this, "O Nome e as iniciais n\u00e3o podem ser vazios.", "Erro...", 
				JOptionPane.ERROR_MESSAGE );
			return false;
		}
		try
		{
			TecnicoHSTData tecnico;
			if( id != null )
			{
				tecnico = ( TecnicoHSTData ) JDO.load( TecnicoHSTData.class, id );
			}
			else
			{
				tecnico = new TecnicoHSTData();
			}
			tecnico.set( TecnicoHSTData.NOME, nome );
			tecnico.set( TecnicoHSTData.NOME_PLAIN, nomePlain );
			tecnico.set( TecnicoHSTData.INICIAIS, iniciais );
			tecnico.set( TecnicoHSTData.IDENTIFICADOR, cor );
			tecnico.save();
			if( id == null )
			{
				id = (Integer)tecnico.get( TecnicoHSTData.ID );
			}
		}
		catch( Exception ex )
		{
			DialogException.showExceptionMessage( ex, "Erro a gravar...", false );
			return false;
		}

		return true;
	}
	
//	public void itemStateChanged( ItemEvent e )
//	{
//		corCombo.getEditor().getEditorComponent().setBackground( HigieneDataProvider.CORES_TECNICOS[ corCombo.getSelectedIndex() ] );
//	}
}
