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

public class QPropertyEditor implements QTargetFactory
{
    protected Class targetClass=null; // required
    protected PropertyEditor pe=null; // optional
    public ArrayList values=new ArrayList();

    public QPropertyEditor(){}

    public QPropertyEditor(String targetClass, String pe)
	throws ClassNotFoundException, InstantiationException, IllegalAccessException
    {
	this.targetClass=Class.forName(targetClass);
	if (!"".equals(pe))
		this.pe=(PropertyEditor)Class.forName(pe).newInstance();
    }

    protected synchronized void PE(QContext context)
	throws QPE
    {
	if (pe!=null)
	    return;
	pe=PropertyEditorManager.findEditor(targetClass);
	if (pe==null)
	    context.throwPE
		("Unable to find editor for "+targetClass.getName());
    }

    public void setTargetClassName(String targetClassName, 
				   QContext context)
	throws QPE
    {
	targetClass=context.forName(targetClassName);
    }

    public String getTargetClassName()
    {
	return targetClass.getName();
    }

    public void setPropertyEditorClassName(String peClassName,
					   QContext context)
	throws QPE
    {
	Class peClass=context.forName(peClassName);
	try
	    {
		pe=(PropertyEditor)peClass.newInstance();
	    }
	catch (InstantiationException e)
	    {
		context.throwPE(e);
	    }
	catch (IllegalAccessException e)
	    {
		context.throwPE(e);
	    }
    }

    public String getPropertyEditorClassName()
    {
	return pe.getClass().getName();
    }

    public Object create(QContext context)
	throws QPE
    {
	return null;
    }

    public boolean isInstance(Object object, QContext context)
    {
	return targetClass.isInstance(object);
    }

    public String getName(Object object)
    {
	return "";
    }

    public void validate(String text, QContext context)
	throws QPE
    {
	int s=values.size();
	if (s==0)
	    return;
	int i;
	for (i=0;i<s;++i)
	    {
		if (text.equals(values.get(i)))
		    return;
	    }
	System.err.println("targetClass="+targetClass.getName());
	System.err.println("values="+values);
	context.throwPE("Invalid value: >>"+text+"<<");
    }

    public String getText(Object object, 
			  QContext context)
	throws QPE
    {
	PE(context);
	String text;
	synchronized(this)
	{
	    pe.setValue(object);
	    text=pe.getAsText();
	}
	if (!"".equals(text))
		validate(text,context);
	return text;
    }

    public void add(Object object, 
		    String name, 
		    String text,
		    QAccess access,
		    Object parent,
		    QTargetFactory parentFactory,
		    QContext context)
	throws QPE
    {
	PE(context);
	validate(text,context);
	synchronized(this)
	{
	    pe.setAsText(text);
	    object=pe.getValue();
	}
	if (parent==null)
	    context.doc.setRoot(object);
	else
	    {
		access.add(parent,parentFactory,object,context);
	    }
    }

    public PropertyDescriptor[] getPropertyDescriptors(QContext context)
	throws QPE
    {
	return null;
    }

    public Field[] getFields(QContext context)
	throws QPE
    {
	return null;
    }
}
