package org.minimalj.frontend.form.element;

import java.util.List;

import org.minimalj.backend.Backend;
import org.minimalj.frontend.Frontend;
import org.minimalj.frontend.Frontend.IComponent;
import org.minimalj.frontend.Frontend.Input;
import org.minimalj.frontend.Frontend.InputComponentListener;
import org.minimalj.frontend.Frontend.Search;
import org.minimalj.model.Keys;
import org.minimalj.model.ViewUtil;
import org.minimalj.persistence.criteria.SearchCriteria;
import org.minimalj.util.CloneHelper;

public class ReferenceFormElement<T> extends AbstractFormElement<T> {
	// private static final Logger logger = Logger.getLogger(ReferenceField.class.getName());
	
	private final Class<?> fieldClazz;
	private final Object[] searchColumns;
	protected final Input<T> lookup;
	
	public ReferenceFormElement(Object key, Object... searchColumns) {
		super(Keys.getProperty(key));
		fieldClazz = getProperty().getClazz();
		this.searchColumns = searchColumns;
		lookup = Frontend.getInstance().createLookup(new ReferenceFieldChangeListener(), new ReferenceFieldSearch(), searchColumns);
	}

	private class ReferenceFieldSearch implements Search<T> {

		@Override
		public List<T> search(String searchText) {
			return (List<T>) Backend.read(fieldClazz, new SearchCriteria(searchText, searchColumns), 100);
		}
	}
	
	@Override
	public IComponent getComponent() {
		return lookup;
	}

	@Override
	public T getValue() {
		return lookup.getValue();
	}

	@Override
	public void setValue(T object) {
		lookup.setValue(object);
	}

	private class ReferenceFieldChangeListener implements InputComponentListener {

		@Override
		public void changed(IComponent source) {
			Object selectedObject = lookup.getValue();
			@SuppressWarnings("unchecked")
			T objectAsView = (T) ViewUtil.view(selectedObject, CloneHelper.newInstance(getProperty().getClazz()));
			setValue(objectAsView);
			fireChange();
		}
		
	}

}

@@ -237,70 +237,8 @@
 ut;%0A
-import org.minimalj.frontend.Frontend.InputComponentListener;%0A
 impo
@@ -315,44 +315,8 @@
 ys;%0A
-import org.minimalj.model.ViewUtil;%0A
 impo
@@ -372,209 +372,167 @@
 ia;%0A
-import org.minimalj.util.CloneHelper;%0A%0Apublic class ReferenceFormElement%3CT%3E extends AbstractFormElement%3CT%3E %7B%0A%09// private static final Logger logger = Logger.getLogger(ReferenceField.class.getName());%0A%09
+%0A// To make this class generic is a little bit senseless as%0A// there are no checks at all%0Apublic class ReferenceFormElement%3CT%3E extends AbstractFormElement%3CT%3E %7B
 %0A%09pr
@@ -553,9 +553,9 @@
 ass%3C
-?
+T
 %3E fi
@@ -639,16 +639,48 @@
 okup;%0A%09%0A
+%09@SuppressWarnings(%22unchecked%22)%0A
 %09public 
@@ -785,16 +785,27 @@
 dClazz =
+ (Class%3CT%3E)
  getProp
@@ -912,33 +912,9 @@
 kup(
-new ReferenceFieldChangeL
+l
 iste
@@ -1405,384 +1405,6 @@
 %0A%09%7D%0A
-%0A%09private class ReferenceFieldChangeListener implements InputComponentListener %7B%0A%0A%09%09@Override%0A%09%09public void changed(IComponent source) %7B%0A%09%09%09Object selectedObject = lookup.getValue();%0A%09%09%09@SuppressWarnings(%22unchecked%22)%0A%09%09%09T objectAsView = (T) ViewUtil.view(selectedObject, CloneHelper.newInstance(getProperty().getClazz()));%0A%09%09%09setValue(objectAsView);%0A%09%09%09fireChange();%0A%09%09%7D%0A%09%09%0A%09%7D%0A%0A
 %7D%0A

fix stackoverflow exception (self call)