/*
 * EditColumns.java
 *
 * Created on Ketvirtadienis, 2007, Sausio 25, 17.16
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Main.panels;

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


public class EditColumns extends JDialog
{
	private final String ADD_COLUMN= "Add Column";
	private final String REMOVE_COLUMN= "Remove Column";
	private final String OK_LABEL= "OK";
	private final String CANCEL_LABEL= "CANCEL";
	
	public final static int OK= 1;
	public final static int CANCEL= 0;
	int mStatus= CANCEL;
	
	JTextField field;
	JComboBox box;
	
	public EditColumns(Frame parent, String title, boolean modal)
	{
		super(parent, title, modal);
		GridBagLayout layout= new GridBagLayout();
		getContentPane().setLayout(layout);
		GridBagConstraints gbc= new GridBagConstraints();
	    
		box= new JComboBox(InputDataSlot.DATA_NAMES);
		gbc.gridwidth= GridBagConstraints.RELATIVE;
		gbc.fill= GridBagConstraints.HORIZONTAL;
		gbc.anchor= GridBagConstraints.WEST;
		gbc.weightx= 1;
		gbc.insets= new Insets(11, 11, 12, 12);
		layout.setConstraints(box,gbc);
		getContentPane().add(box);
		
		JButton button= new JButton(REMOVE_COLUMN);
		gbc.gridwidth= GridBagConstraints.REMAINDER;
		gbc.fill= GridBagConstraints.NONE;
		gbc.insets= new Insets(11, 0, 12, 12);
		layout.setConstraints(button,gbc);
		getContentPane().add(button);
		
		field= new JTextField();
		gbc.gridwidth= GridBagConstraints.RELATIVE;
		gbc.fill= GridBagConstraints.HORIZONTAL;
		gbc.anchor= GridBagConstraints.WEST;
		gbc.insets= new Insets(0, 11, 12, 12);
		layout.setConstraints(field,gbc);
		getContentPane().add(field);
		
		JButton buttonR= new JButton(ADD_COLUMN);
		gbc.gridwidth= GridBagConstraints.REMAINDER;
		gbc.fill= GridBagConstraints.NONE;
		gbc.insets= new Insets(0, 0, 12, 12);
		layout.setConstraints(buttonR,gbc);
		getContentPane().add(buttonR);
		
		buttonR.addActionListener( new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				addColumn();
			}
		}
		);
		
		button.addActionListener( new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				removeColumn();
				
			}
		}
		);
		
		JPanel tmpPanel= new JPanel();
		
		tmpPanel.setLayout(new BoxLayout(tmpPanel, BoxLayout.X_AXIS));
		JButton okButton= new JButton(OK_LABEL);
		tmpPanel.add(okButton);
		JButton cancelButton= new JButton(CANCEL_LABEL);
		tmpPanel.add(cancelButton);
		okButton.setMinimumSize( cancelButton.getMinimumSize() );
		okButton.setPreferredSize( cancelButton.getPreferredSize() );
		okButton.setMaximumSize( cancelButton.getMaximumSize() );
		
		gbc.gridwidth= GridBagConstraints.REMAINDER;
		gbc.fill= GridBagConstraints.NONE;
		gbc.anchor= GridBagConstraints.CENTER;
		gbc.insets= new Insets(5, 12, 12, 12);
		layout.setConstraints(tmpPanel,gbc);
		getContentPane().add(tmpPanel);
		
		okButton.addActionListener( new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				ok();
			}
		}
		);
		
		cancelButton.addActionListener( new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				cancel();
				
			}
		}
		);
		
		pack();
		
	}
	
	private void addColumn()
	{
		String name= field.getText();
		if((name.trim()).equals(""))
			return;
		for(int i=0; i< box.getItemCount(); i++)
		{
			if(box.getItemAt(i).equals(name))
			{
				return;
			}
		}
		
		box.addItem(name);
	}
	
	private void removeColumn()
	{
		int i= box.getSelectedIndex();
		if(i != -1)
			box.removeItemAt(i);
	}
	
	private void ok()
	{
		mStatus= OK;
		this.dispose();
	}
	
	private void cancel()
	{
		mStatus= CANCEL;
		this.dispose();
	}
	
	public String[] getColumns()
	{
		DefaultComboBoxModel model= ((DefaultComboBoxModel)box.getModel());
		String[] ret= new String[model.getSize()];
		for(int i= 0; i< model.getSize(); i++)
		{
			ret[i]= (String)model.getElementAt(i);
		}
		return ret; 
	}
	
	public int getStatus()
	{
		return mStatus;
	}
}
