// $Header$
/*
 * Copyright 2003-2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.apache.jorphan.gui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;

import org.apache.jorphan.logging.LoggingManager;
import org.apache.jorphan.reflect.Functor;
import org.apache.log.Logger;

/**
 * @version $Revision$
 */
public class ObjectTableModel extends DefaultTableModel {
	private static Logger log = LoggingManager.getLoggerForClass();

	private transient ArrayList objects = new ArrayList();

	private transient List headers = new ArrayList();

	private transient ArrayList classes = new ArrayList();

	private transient ArrayList readFunctors = new ArrayList();

	private transient ArrayList writeFunctors = new ArrayList();

	public ObjectTableModel(String[] headers, Functor[] readFunctors, Functor[] writeFunctors, Class[] editorClasses) {
		this.headers.addAll(Arrays.asList(headers));
		this.classes.addAll(Arrays.asList(editorClasses));
		this.readFunctors = new ArrayList(Arrays.asList(readFunctors));
		this.writeFunctors = new ArrayList(Arrays.asList(writeFunctors));

	}

	public Iterator iterator() {
		return objects.iterator();
	}

	public void clearData() {
		int size = getRowCount();
		objects.clear();
		super.fireTableRowsDeleted(0, size);
	}

	public void addRow(Object value) {
		log.debug("Adding row value: " + value);
		objects.add(value);
		super.fireTableRowsInserted(objects.size() - 1, objects.size());
	}

	public void insertRow(Object value, int index) {
		objects.add(index, value);
		super.fireTableRowsInserted(index, index + 1);
	}

	/**
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	public int getColumnCount() {
		return headers.size();
	}

	/**
	 * @see javax.swing.table.TableModel#getColumnName(int)
	 */
	public String getColumnName(int col) {
		return (String) headers.get(col);
	}

	/**
	 * @see javax.swing.table.TableModel#getRowCount()
	 */
	public int getRowCount() {
		if (objects == null) {
			return 0;
		}
		return objects.size();
	}

	/**
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	public Object getValueAt(int row, int col) {
		log.debug("Getting row value");
		Object value = objects.get(row);
		if(headers.size() == 1 && col >= readFunctors.size())
			return value;
		Functor getMethod = (Functor) readFunctors.get(col);
		if (getMethod != null && value != null) {
			return getMethod.invoke(value);
		}
		return null;
	}

	/**
	 * @see javax.swing.table.TableModel#isCellEditable(int, int)
	 */
	public boolean isCellEditable(int arg0, int arg1) {
		return true;
	}

	/**
	 * @see javax.swing.table.DefaultTableModel#moveRow(int, int, int)
	 */
	public void moveRow(int start, int end, int to) {
		List subList = objects.subList(start, end);
		for (int x = end - 1; x >= start; x--) {
			objects.remove(x);
		}
		objects.addAll(to, subList);
		super.fireTableChanged(new TableModelEvent(this));
	}

	/**
	 * @see javax.swing.table.DefaultTableModel#removeRow(int)
	 */
	public void removeRow(int row) {
		objects.remove(row);
		super.fireTableRowsDeleted(row, row);
	}

	/**
	 * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
	 */
	public void setValueAt(Object cellValue, int row, int col) {
		if (row < objects.size()) {
			Object value = objects.get(row);
			if (col < writeFunctors.size()) {
				Functor setMethod = (Functor) writeFunctors.get(col);
				if (setMethod != null) {
					setMethod.invoke(value, new Object[] { cellValue });
					super.fireTableDataChanged();
				}
			}
			else if(headers.size() == 1)
			{
				objects.set(row,cellValue);
			}
		}
	}

	/**
	 * @see javax.swing.table.TableModel#getColumnClass(int)
	 */
	public Class getColumnClass(int arg0) {
		return (Class) classes.get(arg0);
	}

}

@@ -1,16 +1,4 @@
-// $Header$%0A
 /*%0A 
@@ -18,16 +18,21 @@
 003-2004
+,2006
  The Apa
@@ -1767,16 +1767,682 @@
 ors));%0A%0A
+        int numHeaders = headers.length;%0A%0A        int numClasses = classes.size();%0A        if (numClasses != numHeaders)%7B%0A            log.warn(%22Header count=%22+numHeaders+%22 but classes count=%22+numClasses);%0A        %7D%0A        %0A        // Functor count = 0 is handled specially %0A        int numWrite = writeFunctors.length;%0A        if (numWrite %3E 0 && numWrite != numHeaders)%7B%0A            log.warn(%22Header count=%22+numHeaders+%22 but writeFunctor count=%22+numWrite);%0A        %7D%0A        %0A        int numRead = readFunctors.length;%0A        if (numRead %3E 0 && numRead != numHeaders)%7B%0A            log.warn(%22Header count=%22+numHeaders+%22 but readFunctor count=%22+numRead);%0A        %7D%0A
 %09%7D%0A%0A%09pub

Add some checks for incorrect parameters