/**
 * Class AttributeJTable
 * extends JTable
 * This class provides 3 public methods that allow to:
 * - create a 2 columns JTable with a given list of attributes on the left
 * column, the right column being ready to display their values.
 * - copy these attribute values to a given Element node.
 * - copy the attribute values of a given Element node to the table.
 **/

package axsvg.edition;

import axsvg.*;
import javax.swing.*;
import java.util.*;
import org.w3c.dom.*;

public class AttributeJTable extends JTable {
    
    //The list of attributes names, which will be dipslayed on the left
    //column of the table.
    private final ListOfAttributeNames list;
 

    /**
     * Method AttributeJtable
     * The constructor of the class
     * @param a ListOfAttributeNames which contains (surprise) the list of
     * attribute to be handled by the AttributeJTable being created.
     **/
    public AttributeJTable(ListOfAttributeNames atrList) {
	super(atrList.size(), 2);
	list = atrList;
	int index = 0;
	String name = list.getNextAttributeName();
	
	//fills the left column with the names...
	while(name != null) {
	    setValueAt(name, index, 0);
	    index++;
	    name = list.getNextAttributeName();
	}
	
    }


    /**
     * Method copyAttributesToTable
     * @param an Element Node. The attribute values of this Element node will
     * be stored into the table right column, next to their name (in the left
     * column). Attributes with other names will be ignored.
     * Resets the table if the Element is null.
     **/
    public void copyAttributesToTable(Element elt) {
	clearSelection();
	loseFocus();
	if(elt == null) {
	    resetTable();
	    return;
	}
	int index = 0;
	String name = list.getNextAttributeName();
	while(name != null) {
	    String value = elt.getAttribute(name);
	    setValueAt(value, index, 1);
	    index++;
	    name = list.getNextAttributeName();
	}
    }


    /**
     * Method resetTable
     * Sets all the values to "".
     **/
    public void resetTable() {
	clearSelection();
	loseFocus();
	for(int i = 0; i < list.size(); i++) {
	    setValueAt("", i, 1);
	}
    }
   
    /**
     * Method copyTableToAttributes
     * @param an Element Node. The attributes and their values, both held by
     * the table, will be added to this Element node.
     **/
    public void copyTableToAttributes(Element elt) {
	clearSelection();
	loseFocus();
	int index = 0;
	String name = list.getNextAttributeName();
	while(name != null) {
	    String value = (String)getValueAt(index, 1);
	    elt.setAttribute(name, value);
	    index++;
	    name = list.getNextAttributeName();
	}
    }


    /**
     * Method loseFocus
     * I noticed that when a cell of the table was focused, one cannot
     * set its value (with setValueAt method). This very dirty method
     * make any focused cell to lose the focus. Fell free to use a cleaner
     * solution...
     **/
    public void loseFocus() {
	try {
	    editCellAt(-1,-1);
	} catch(Exception e) {
	    return;
	}
    }
}
