/*
 *
 * 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.table.AbstractTableModel;

import javax.swing.ListSelectionModel;

import jxtray.Jxtray;
import jxtray.JxtrayConstants;
import jxtray.Messages;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;



import java.util.Vector;
public class QueryTable extends JTable
{
	private QueryTableModel pmodel;
	
	public QueryTable (Statement stmt, String query) throws Exception
	{
		super();
		pmodel = new QueryTableModel(stmt);
		pmodel.query(query);
		this.setModel(pmodel);
		this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	}
	
	public QueryTable (ResultSet res) throws Exception
	{
		super();
		pmodel = new QueryTableModel(null);
		pmodel.query(res);
		this.setModel(pmodel);
		this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	}	

}

class QueryTableModel extends AbstractTableModel 
{
	private Vector totalrows;
	private String[] head = {Jxtray.getLang("properties.key"),Jxtray.getLang("properties.value")};
	private Statement stmt;

		 
   public QueryTableModel(Statement stmt)
   {
	  this.stmt = stmt;
	  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);
		fireTableCellUpdated(row, col);
   }
   public boolean isCellEditable(int row, int col) 
   {
		return false;
	  
   }
   public void fire() 
   {
	  fireTableChanged(null);
   }
   
   public void query(String query) throws Exception 
   {
			ResultSet res = stmt.executeQuery( query );
			query(res);
   }
	  
   public void query(ResultSet res) throws Exception 
   {
		ResultSetMetaData rmtd = res.getMetaData();
		head = new String[rmtd.getColumnCount()];
		for ( int i = 0; i < rmtd.getColumnCount(); i++)
		{
				head[i] = rmtd.getColumnName(i+1);
		}
				totalrows = new Vector();
				int cpt=0;
				while ( res.next() ) 
				{
					  String[] record = new String[rmtd.getColumnCount()];
					  for( int i = 0; i < rmtd.getColumnCount(); i++ ) 
					  {
						 record[i] = res.getString(i+1);
					  }
					  totalrows.addElement( record );
					  cpt++;
					  if ( cpt >= JxtrayConstants.MAX_ROW_SELECT )
					  {
						   Messages.warning(Jxtray.getLang("message.maxrowselected"));
						   break;
					  }
				   }

		  }	  
}