/*
Tiamat - a role-playing game framework
ParameterTable.java - A table used to edit a list of Event parameters
    
 Copyright  (C) 2006 Hugues Johnson
 
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

package com.huguesjohnson.tiamat.tools;

import java.awt.Dimension;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import com.huguesjohnson.tiamat.IAdventureCommands;
import com.huguesjohnson.tiamat.StringTable;

/**
 * ParameterTable - A table used to edit a list of Event parameters
 * @author Hugues Johnson
 */
public class ParameterTable extends JTable{
	/* Determines if a de-serialized file is compatible with this class. See <a href="http://java.sun.com/j2se/1.5.0/docs/guide/serialization/spec/class.html">Java Object Serialization Specifications</a>. */
	private static final long serialVersionUID=4133978267649975701L;

	private final static String[] columnNames={ "Key","Value" };
	private final static String[] defaultRowData={"",""};

	public ParameterTable(Dimension preferredScrollableViewportSize){
		super();
		this.init(preferredScrollableViewportSize);
	}
	
	private void init(Dimension preferredScrollableViewportSize){
		this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.initTable();
		((DefaultTableModel)this.getModel()).addRow(defaultRowData);
		//see Java bug 4709394 (http://bugs.sun.com/bugdatabase/view_bug.do;:WuuT?bug_id=4709394)
		this.putClientProperty("terminateEditOnFocusLost",Boolean.TRUE); 
		this.setPreferredScrollableViewportSize(preferredScrollableViewportSize);
	}
	
	private void setKeyColumnRenderer(){
		TableColumn keyColumn=this.getColumnModel().getColumn(0);
		JComboBox comboBox=new JComboBox();
		comboBox.setEditable(false);
		int count=IAdventureCommands.Parameters.values().length;
		for(int index=0;index<count;index++){
			comboBox.addItem(IAdventureCommands.Parameters.values()[index].value());
		}
		keyColumn.setCellEditor(new DefaultCellEditor(comboBox));
	}
	
	//checks if a new row needs to be added
	public void onTableChanged(TableModelEvent e){
		if(e.getType()!=TableModelEvent.UPDATE){ return; }
		int rowCount=this.getRowCount();
		if(this.getSelectedRow()==(rowCount-1)){
			DefaultTableModel model=(DefaultTableModel)this.getModel();
			if((model.getValueAt(rowCount-1,0)!=null)&&(model.getValueAt(rowCount-1,1)!=null)){
				String key=(String)model.getValueAt(rowCount-1,0);
				String value=(String)model.getValueAt(rowCount-1,1);
				if((key.length()>0)&&(value.length()>0)){
					model.addRow(defaultRowData);
				}
			}
		}
	}
	
	private void initTable(){
		DefaultTableModel model=new DefaultTableModel();
		model.setColumnIdentifiers(columnNames);
		model.addTableModelListener(
				new TableModelListener(){
					public void tableChanged(TableModelEvent e){
						onTableChanged(e);
					}
				}
		);
		this.setModel(model);
		this.setKeyColumnRenderer();
	}
	
	public StringTable getStringTable(){
		StringTable table=new StringTable();
		int length=this.getRowCount()-1; //last row is always empty
		DefaultTableModel model=(DefaultTableModel)this.getModel();
		for(int row=0;row<length;row++){
			String key=(String)model.getValueAt(row,0);
			String value=(String)model.getValueAt(row,1);
			table.set(key,value);
		}
		return(table);
	}
	
	public void setParameters(StringTable parameters){
		this.initTable();
		DefaultTableModel model=(DefaultTableModel)this.getModel();
		String[][] allParameters=parameters.getAll();
		if(allParameters!=null){
			if(allParameters.length==2){
				int length=allParameters[0].length;
				for(int index=0;index<length;index++){
					String[] row=new String[2];
					row[0]=allParameters[0][index];
					row[1]=allParameters[1][index];
					model.addRow(row);
				}
			}
		}
		model.addRow(defaultRowData);
	}
}