

/*****> Class : CustomTableModel1 && CustomEditableCellTableModel
	*
	**> Auteur : Johann Heymes
	*
	**> Creation : 10 mai 2002
	*
	**> Licence :
	*		Ce programme est un logiciel gratuit ,  il peut être modifié et / ou
	*	distribué en  accord avec  les termes de la licence  'GNU General Public
	*	Licence'  tel qu'elle  est publiée  par la  'Free Software Foundation  .
	*	(version 2 ou précédante).
	*		Ce programme est distribué  dans l'espoir  qu'il pourra être utile ,
	*	mais  SANS AUCUNE GARANTIE . Voir la  'GNU General Public License'  pour 
	*	plus de détail.
	*		Vous devriez avoir reçu une copie de la 'GNU General Public License'
	*	avec la distribution  de ce programme,  si non,  vous pouvez écrire à la
	*	'Free Software Foudation,  Inc.,  59 Temple Place -  Suite 330,  Boston,
	*	MA 02111-1307, USA'.
	*
	**> Description :
	*		Explique comment rendre non editable certaines cellules d'une JTable.
	*
	***/

import javax.swing.table.DefaultTableModel;
import javax.swing.JFrame;
import javax.swing.JTable;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Vector;


public class CustomTableModel1 
{
	
	public CustomTableModel1 ()
	{	JFrame frame = new JFrame ("CustomEditableCellTableModel");
		frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		frame.setBounds (300, 300, 200, 300);
		
		
		String [][] data = {
			{ "not editable", "editable", "editable", "editable", "editable"},
			{ "editable", "editable", "editable", "editable", "editable"},
			{ "editable", "editable", "not editable", "editable", "editable"},
			{ "editable", "editable", "editable", "editable", "editable"},
			{ "editable", "editable", "editable", "editable", "not editable"}
		};
		String [] columns = { "0", "1", "2", "3", "4"};
		CustomEditableCellTableModel model = new CustomEditableCellTableModel (data, columns);
		
		// defini ici les cellules qui ne pourront pas etre editable :
		model.addNotEditableCell (0, 0);
		model.addNotEditableCell (2, 2);
		model.addNotEditableCell (4, 4);
		
		JTable table = new JTable (model);
		frame.getContentPane ().add (table);
		
		frame.pack ();
		frame.setVisible (true);
	}
	
	public static void main (String argv [])
	{	new CustomTableModel1 ();
	}
}


class CustomEditableCellTableModel extends DefaultTableModel 
{
	private ArrayList listCellNotEditable;

	public CustomEditableCellTableModel () 
	{   super ();
		this.listCellNotEditable = new ArrayList ();
	}

	public CustomEditableCellTableModel (int numRows, int numColumns) 
	{   super (numRows, numColumns);
		this.listCellNotEditable = new ArrayList ();
	}

	public CustomEditableCellTableModel (Vector columnNames, int numRows) 
	{   super (columnNames, numRows);
		this.listCellNotEditable = new ArrayList ();
	}

	public CustomEditableCellTableModel (Object[] columnNames, int numRows) 
	{   super (columnNames, numRows);
		this.listCellNotEditable = new ArrayList ();
	}

	public CustomEditableCellTableModel (Vector data, Vector columnNames) 
	{   super (data, columnNames);
		this.listCellNotEditable = new ArrayList ();
	}

	public CustomEditableCellTableModel (Object[][] data, Object[] columnNames) 
	{   super (data, columnNames);
		this.listCellNotEditable = new ArrayList ();
	}
	
	public void addNotEditableCell (int row, int column)
	{	if ( ! (column >= 0 && column < this.getColumnCount ()) )
		{	return;
		}
		if ( ! ((row >= 0) && (row < this.getRowCount ())) )
		{	return;
		}
		this.listCellNotEditable.add (new Point (row, column));
	}
	
	public void removeNotEditableCell (int row, int column)
	{	int index = this.listCellNotEditable.indexOf (new Point (row, column));
		if ( index != -1 )
		{	this.listCellNotEditable.remove (index);
		}
	}
	
	// surcharge de la methode afin de revoyer false a toutes les cellules 
	// marque comme non editable 
	public boolean isCellEditable (int row, int column)
	{   int index = this.listCellNotEditable.indexOf (new Point (row, column));
		if ( index == -1 )
		{	return ( true );
		}
		else
		{	return ( false );
		}
	} 
}