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

package Main.panels;

import java.beans.*;
import Main.*;
import java.awt.event.*;
import Main.data.*;


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

public class DataPanel extends JPanel implements TableModelListener, PropertyChangeListener
{
	private final String ADD= "Add Row";
	private final String REMOVE= "Remove Row";
    private final String EDIT_COLUMNS= "Edit Columns";
	
	private final String FINE= "Fine:";
	private final String FINE_PROB= "Fine probability:";
	
	private JTable mTable;
	private InputDataModel mData;
	private JButton mEditRowsButton;
	private JButton mAddButton;
	private JButton mRemoveButton;
	
	JTextField mFine;
	JTextField mFineProb;
	public DataPanel()
	{
        setLayout(new BorderLayout());
		mTable= new JTable();
		mTable.setAutoCreateColumnsFromModel(false);
		mTable.setPreferredScrollableViewportSize(new Dimension(300, 200));
		mData= new InputDataModel();
		mTable.setModel(mData);
		mData.addPropertyChangeListener(this);

		mData.setColumns(InputDataSlot.DATA_NAMES);
		
          
  
	    JScrollPane ps = new JScrollPane();
		ps.setSize(230, 250);
		JTableHeader header = mTable.getTableHeader();
		
		JViewport hv = new JViewport();
		hv.setLayout(new BoxLayout(hv, BoxLayout.X_AXIS));
		hv.add(header);
		ps.setColumnHeader(hv);
		ps.getViewport().add(mTable);
		
		header.setUpdateTableInRealTime(false);
		
		JPanel finePanel= new JPanel();
		mFine= new JTextField(10);
		mFine.setText("0.0");
		mFineProb= new JTextField(10);
		mFineProb.setText("0.0");
		
		finePanel.add(new JLabel(FINE));
		finePanel.add(mFine);
		finePanel.add(new JLabel());
		finePanel.add(new JLabel(FINE_PROB));
		finePanel.add(mFineProb);
		
		JPanel tempPanel= new JPanel();
		tempPanel.setLayout(new BoxLayout(tempPanel, BoxLayout.Y_AXIS));
		tempPanel.add(ps);
		tempPanel.add(finePanel);
		
		add(tempPanel, BorderLayout.CENTER);
		mData.addTableModelListener(this);
		
		JPanel buttonPanel= new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		
		add(buttonPanel, BorderLayout.SOUTH);
		
		
		mAddButton= new JButton(ADD);
		mRemoveButton= new JButton(REMOVE);
		
		mEditRowsButton= new JButton(EDIT_COLUMNS);
		
		buttonPanel.add(mEditRowsButton);
		
		buttonPanel.add(mAddButton);
		buttonPanel.add(mRemoveButton);
		
		mAddButton.addActionListener( new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				mData.addRow(new InputDataSlot());
			}
		}
		);
		
		mRemoveButton.addActionListener( new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				int index= mTable.getSelectedRow(); 
				mData.removeRow(index);
				mTable.getSelectionModel().setSelectionInterval(index, index);
			}
		}
		);
		
		mEditRowsButton.addActionListener( new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				editRows();
			}
		}
		);
		
	
	}
	
	public void tableChanged(TableModelEvent e)
	{
		Object obj= e.getSource();
		/*if(obj instanceof TableModel)
		System.out.println("Value changed");*/	    
	}
	
	public java.util.List getInputData()
	{
		return mData.getData(); 
	}
	
	public InputDataModel getInputDataModel()
	{
		return mData; 
	}
	
	public float getFine()
	{
		String text= mFine.getText();
		float rez= 0;
		try{
			rez= Float.parseFloat(text);
		}
		catch(NumberFormatException e)
		{
			MainWindow.getInstance().getOutputStreamPanel().logn("Can't convert to float fine falue: "+text);
			
			return 0;
		}
		
		return rez;
		
	}
	
	public float getFineProbability()
	{
		String text= mFineProb.getText();
		float rez= 0;
		try{
			rez= Float.parseFloat(text);
		}
		catch(NumberFormatException e)
		{
			MainWindow.getInstance().getOutputStreamPanel().logn("Can't convert to float fine probability value: "+text);
			
			return 0;
		}
		
		return rez;
		
	}
	

	private void editRows()
	{
		EditColumns col= new EditColumns(new Frame(), "Edit Columns", true ); 
		col.setVisible(true);
		if(col.getStatus() == EditColumns.OK)
		{
			String[] array= col.getColumns();
			mData.changeColumns(array);
		}
	}
	
	public void propertyChange(PropertyChangeEvent e)
	{
		if(e.getPropertyName().equals(InputDataModel.COLUM_CHANGE))
		{
			mTable.setColumnModel(new DefaultTableColumnModel());
				//mTable.removeColumnSelectionInterval(0, mTable.getColumnCount()-1);
			
			/*for(Enumeration en= mTable.getColumnModel().getColumns();en.hasMoreElements();)
			{
				TableColumn column= (TableColumn)en.nextElement();
				mTable.removeColumn(column);
			}*/
			
			String[] col= (String[])e.getNewValue();
			for (int k = 0; k < col.length; k++)
			{
				TableColumn column;      
	
				JTextField t= new JTextField("");
				
			   
				if(k == 0)
				{
					column = new TableColumn(k, 
					20, new DefaultTableCellRenderer(), 
					null );                
					column.setMinWidth(40);
				}
				else
				{
					column = new TableColumn(k, 
					40, new FormatedRenderer(), 
					new DefaultCellEditor(t) );                
					column.setMinWidth(40);
				}
				column.setHeaderValue(col[k]);
				mTable.addColumn(column);
			}
		}
	}
	
}


