/*
Tiamat - a role-playing game framework
ScriptTableValueTable.java - A table used to edit a ScriptTableValue
    
 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.scriptTable;

import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import com.huguesjohnson.tiamat.Event;
import com.huguesjohnson.tiamat.ScriptTableValue;

/**
 * ScriptTableValueTable.java - A table used to edit a ScriptTableValue
 * @author Hugues Johnson
 */
public class ScriptTableValueTable 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=-670060907900055401L;

	private final static String[] columnNames={ "Action","Parameters" };
	private ArrayList<Event> rowData;
	
	public ScriptTableValueTable(Dimension preferredScrollableViewportSize){
		super();
		this.init(preferredScrollableViewportSize);
	}
	
	public ScriptTableValueTable(Dimension preferredScrollableViewportSize,ScriptTableValue value){
		super();
		this.init(preferredScrollableViewportSize);
		this.setScriptTableValue(value);
	}

	private void init(Dimension preferredScrollableViewportSize){
		this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.initTable();
		//set cell renderers
		this.getColumnModel().getColumn(0).setCellRenderer(new ScriptTableCellRenderer());
		this.getColumnModel().getColumn(1).setCellRenderer(new ScriptTableCellRenderer());
		//set preferred size
		this.setPreferredScrollableViewportSize(preferredScrollableViewportSize);
	}	
	
	@SuppressWarnings("serial")
	private void initTable(){
		this.rowData=new ArrayList<Event>();
		//set model
		DefaultTableModel model=new DefaultTableModel(){
			  public boolean isCellEditable(int rowIndex, int columnIndex){
			        return(false);
			    }
		};
		model.setColumnIdentifiers(columnNames);
		this.setModel(model);
	}
	
	public void setScriptTableValue(ScriptTableValue value){
		this.initTable();
		ArrayList<Event> events=value.events;
		int length=events.size();
		for(int index=0;index<length;index++){
			this.addItem(events.get(index));
		}
	}
	
	private void resizeRow(int rowIndex,int numRows){
		int preferredHeight=this.rowHeight*numRows;
		this.setRowHeight(rowIndex,preferredHeight);
	}
	
	public void addItem(Event item){
		this.rowData.add(item);
		DefaultTableModel model=(DefaultTableModel)this.getModel();
		model.addRow(new String[]{item.getAction(),ScriptTableUtilities.getParametersString(item.getParameters())});
		int rowCount=model.getRowCount();
		int numRows=1;
		String[][] allEntries=item.getParameters().getAll();
		if(allEntries[0]!=null){
			int length=allEntries[0].length;
			if(length>0){ numRows=length; }
		}
		this.resizeRow(rowCount-1,numRows);
	}
	
	public void deleteItem(int index){
		this.rowData.remove(index);
		DefaultTableModel model=(DefaultTableModel)this.getModel();
		model.removeRow(index);
	}

	public void deleteSelectedItem(){
		this.deleteItem(this.getSelectedRow());
	}
	
	public void setItem(int index,Event item){
		this.rowData.set(index,item);
		DefaultTableModel model=(DefaultTableModel)this.getModel();
		model.setValueAt(item.getAction(),index,0);
		model.setValueAt(ScriptTableUtilities.getParametersString(item.getParameters()),index,1);
		int numRows=1;
		String[][] allEntries=item.getParameters().getAll();
		if(allEntries[0]!=null){
			int length=allEntries[0].length;
			if(length>0){ numRows=length; }
		}
		this.resizeRow(index,numRows);
	}
	
	public Event getSelectedItem(){
		return(this.getItem(this.getSelectedRow()));
	}

	public Event getItem(int index){
		return(this.rowData.get(index));
	}
	
	public ScriptTableValue getScriptTableValue(){
		return(new ScriptTableValue(this.rowData.toArray(new Event[0])));
	}
}