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

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


public class PropertiesTable extends JTable
{

	private PropertiesTableModel pmodel;
	
	public PropertiesTable (Properties p)
	{
		super();
		pmodel = new PropertiesTableModel();
		pmodel.load(p);
		this.setModel(pmodel);
		this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		 	
	}	

}

class PropertiesTableModel extends AbstractTableModel 
{
	private Vector totalrows;
	private String[] head = {Jxtray.getLang("properties.key"),Jxtray.getLang("properties.value")};
		 
   public PropertiesTableModel()
   {
	  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(Properties props) 
   {
	  try 
	  {
		Enumeration enum =  props.propertyNames();
		totalrows = new Vector();
		while ( enum.hasMoreElements() )
		{
			String[] record = new String[2];
			record[0] = (String) enum.nextElement();
			record[1] = props.getProperty(record[0]);
			totalrows.addElement( record );
		 }
	  }
	  catch ( Exception ex ) 
	  {
			Messages.error("PropertiesTableModel.load() : "+ ex);
		 }
   }
      
}
