/*
 * DataChooserWindow.java
 *
 * Created on 29 de Abril de 2004, 16:53
 */

package siprp.importer;

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

import com.evolute.utils.ui.*;
/**
 *
 * @author  lflores
 */
public class DataChooserWindow extends CustomJDialog
{
	private final String labels[];
	private final Object items[];
	private final Object empty[];
	private final Object allItems[][];
	private final JComboBox combos[];
	private final int preselected[];
	
	private boolean canceled = false;
	
	/** Creates a new instance of DataChooserWindow */
	public DataChooserWindow( JFrame modalFrame, String names[], Object data[][] )
	{
		this( modalFrame, names, data, null );
	}
	
	public DataChooserWindow( JFrame modalFrame, String names[], Object data[][], int selected[] )
	{
		super( modalFrame, true );
		labels = names;
		items = data[ 0 ];
		empty = new String[ items.length ];
		for( int i = 0; i < empty.length; ++i )
		{
			empty[ i ] = "";
		}
		allItems = data;
		combos = new JComboBox[ labels.length ];
		preselected = selected;
		setupComponents();
		if( modalFrame == null )
		{
			center();
		}
		else
		{
			centerSuper();
		}
		setVisible( true );
	}
	
	private void setupComponents()
	{
		setTitle( "Escolha os campos correspondentes:" );
		setSize(450, 120 + 25 * labels.length );
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets( 2, 2, 2, 2 );
		gbc.fill = GridBagConstraints.HORIZONTAL;
		Container cont = getContentPane();
		cont.setLayout( gbl );
		for( int i = 0; i < labels.length; ++i )
		{
			gbc.weightx = 0;
			gbc.gridwidth = 1;
			JLabel label = new JLabel( labels[ i ] );
			gbl.setConstraints( label, gbc );
			cont.add( label );
/*			gbc.weightx = 1;
			JPanel pad = new JPanel();
			gbl.setConstraints( pad, gbc );
			cont.add( pad );
*/			gbc.weightx = 1;
			gbc.gridwidth = GridBagConstraints.REMAINDER;
			JComboBox combo = new JComboBox( items );
			int index;
			if( preselected == null )
			{
				index = Math.min( i, items.length - 1 );
			}
			else
			{
				if( i < preselected.length )
				{
					index = Math.min( preselected[ i ], items.length - 1 );
					if( index == -1 )
					{
						index = items.length - 1;
					}
				}
				else
				{
					index = items.length - 1;
				}
				
			}
			combo.setSelectedIndex( index );
			combos[ i ] = combo;
			gbl.setConstraints( combo, gbc );
			cont.add( combo );
		}
		gbc.insets = new Insets( 10, 2, 2, 2 );
		gbc.weightx = 1;
		gbc.gridwidth = 2;
		JPanel pad = new JPanel();
		gbl.setConstraints( pad, gbc );
		cont.add( pad );
		gbc.weightx = 0;
		gbc.gridwidth = 1;
		JButton butOK = new JButton( "OK" );
		gbl.setConstraints( butOK, gbc );
		cont.add( butOK );
		gbc.weightx = 0;
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		JButton butCancel = new JButton( "Cancelar" );
		gbl.setConstraints( butCancel, gbc );
		cont.add( butCancel );
		butOK.addActionListener( new ActionListener() {
				public void actionPerformed( ActionEvent e )
				{
					setVisible( false );
					dispose();
				}
			});
		butCancel.addActionListener( new ActionListener() {
				public void actionPerformed( ActionEvent e )
				{
					setVisible( false );
					dispose();
					canceled = true;
				}
			});
	}
	
	public Hashtable getData()
	{
		if( canceled )
		{
			return null;
		}
		Hashtable hash = new Hashtable();
		for( int i = 0; i < labels.length; ++i )
		{
			hash.put( labels[ i ], combos[ i ].getSelectedItem() );
		}
		return hash;
	}
	
	public Hashtable[] getMultipleData()
	{
		if( canceled )
		{
			return null;
		}
		Hashtable hash[] = new Hashtable[ allItems.length ];
//System.out.println( "Hash[] size: " + hash.length );
		for( int j = 0; j < hash.length; ++j )
		{
			hash[ j ] = new Hashtable();
		}
		for( int i = 0; i < labels.length; ++i )
		{
			String sel = combos[ i ].getSelectedItem().toString();
			if( sel.length() > 0 )
			{
				for( int j = 0; j < allItems.length; ++j )
				{
					hash[ j ].put( labels[ i ], allItems[ j ][ combos[ i ].getSelectedIndex() ] );
				}
			}
		}
		return hash;
	}
	
/*	public static void main( String arg[] )
	{
		String names[] = new String[] { "Nome:", "Morada:", "Telefone:" };
		String data[] = new String[] { "Joao Catita", "Damaiasdg sdfg sdf gsdfs hgf h", "nao tem", "so a noite", "" };
		DataChooserWindow dcw = new DataChooserWindow( null, names, data, new int[] { 3, 2 ,1 } );
	}*/
}
