package com.jxml.quick.access;
import com.jxml.quick.*;
import com.jxml.quick.tf.*;
import java.util.*;
import java.lang.reflect.*;

/**
 * <p>updated by Pavel Curtis to improve error handling--Quick 1.1
 */
public class QListFieldAccess extends QFieldAccess
{
    protected void validateField(Field pd,QContext context)
	throws QPE
    {
	if (!QListPropertyAccess.listClass.isAssignableFrom(pd.getType()))
	    context.throwPE("Not a list: "+pd.getName());
    }

    public final QIterator createIterator()
    {
	return new QListIterator();
    }

    protected List getList(QTargetFactory parentFactory, Object parent, QContext context)
	throws QPE
    {
	return (List)super.get(parentFactory,parent,context);
    }

    protected List getNonNullList(QTargetFactory parentFactory, Object parent, QContext context)
	throws QPE
    {
	List result = getList(parentFactory,parent, context);

	if (result == null)
	    context.throwPE("List field \"" + field + "\" is null");
	return result;
    }

    public final void setIteratorParent(QIterator it, 
					Object parent, 
					QTargetFactory parentFactory,
					QContext context)
	throws QPE
    {
	setPD(parentFactory,context);
	((QListIterator)it).setList(getList(parentFactory,parent,context));
    }

    public final void add(Object parent, 
			  QTargetFactory parentFactory,
			  Object child, 
			  QContext context)
	throws QPE
    {
	setPD(parentFactory,context);
	getNonNullList(parentFactory,parent,context).add(child);
    }

    public final int reserve(QTargetFactory parentFactory,		//1.1.5
			Object parent,
		       QContext context)
	throws QPE
    {
	setPD(parentFactory,context);					//1.1.5
	int reserved=-1;
	List list=getNonNullList(parentFactory,parent,context);
	reserved=list.size();
	list.add(null);
	return reserved;
    }

    public final void set(int reserved,
			  Object parent, 
			  QTargetFactory parentFactory,
			  Object child, 
			  QContext context)
	throws QPE
    {
	setPD(parentFactory,context);
	getNonNullList(parentFactory,parent,context).set(reserved,child);
    }
}
