/*
 *
 * Copyright (C) 13 janv. 2003  Jean-Yves Beaujean 
 * 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 jxtray.table;


import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.ListSelectionModel;

import jxtray.Jxtray;
import jxtray.Messages;

import jxtray.SQLHistoryEntry;
import jxtray.SQLHistoryCache;

import java.util.Enumeration;
import java.util.Vector;

import java.awt.Component;




public class SQLHistoryTable extends JTable
{

	private SQLHistoryTableModel pmodel;
	
	public SQLHistoryTable ()
	{
		super();
		pmodel = new SQLHistoryTableModel();
		pmodel.load();
		this.setModel(pmodel);
		this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.getColumnModel().getColumn(0).setPreferredWidth(50);
		this.getColumnModel().getColumn(0).setMaxWidth(50);
		this.getColumnModel().getColumn(2).setPreferredWidth(130);
		this.getColumnModel().getColumn(2).setMaxWidth(130);
		this.getColumnModel().getColumn(0).setCellRenderer(new ColorRenderer());
		 	
	}	

}

class SQLHistoryTableModel extends AbstractTableModel 
{
	private Vector totalrows;
	private String[] head = {"status","query","date"};
		 
   public SQLHistoryTableModel()
   {
	  totalrows = new Vector();
   }

   public String getColumnName(int i) { return head[i]; }
   public int getColumnCount() { return head.length; }
   public int getRowCount() { return totalrows.size(); }
   public Object getValueAt(int row, int col) 
   {
  		Object record[] = (Object[]) totalrows.elementAt(row);
   		return record[col];
   }
   
   public void setValueAt(Object value, int row, int col)
   {
		String record[] = (String[]) totalrows.elementAt(row);
		record[col] = (String) value;
		totalrows.setElementAt(record,row);
		Jxtray.setProperty(record[0],record[1]);
		fireTableCellUpdated(row, col);
   }
   public boolean isCellEditable(int row, int col) 
   {
		if ( col == 1)
			return true;
		else
			return false;
   }
   public void fire() 
   {
	  fireTableChanged(null);
   }
   
   public void load() 
   {
	  try 
	  {
		
	  	SQLHistoryCache cache = Jxtray.getSQLHistoryCache();
	  	Enumeration enum =  cache.elements();
		totalrows = new Vector();
		while ( enum.hasMoreElements() )
		{
			String[] record = new String[3];
			SQLHistoryEntry entry = (SQLHistoryEntry) enum.nextElement();
			record[0] = ""+entry.getLastState();
			record[1] = entry.getQuery();
			record[2] = entry.getDate();
			totalrows.addElement( record );
		 }
	  }
	  catch ( Exception ex ) 
	  {
			Messages.error("SQLHistoryTableModel.load() : "+ ex);
		 }
   }
      
}


class ColorRenderer extends JLabel implements TableCellRenderer 
{

	private String value;
	public ColorRenderer() 
	{
	}

	public Component getTableCellRendererComponent( JTable table, Object color, boolean isSelected, boolean hasFocus, int row, int column) 
	{
			String value = (String) table.getValueAt(row,column);
			this.setIcon(new ImageIcon(getClass().getResource("/images/"+value+".png")));
			this.setHorizontalAlignment(JLabel.CENTER);
			return this;
	}
	

}


