package org.minimalj.frontend.form.element;

import java.util.List;
import java.util.Objects;

import org.minimalj.frontend.Frontend;
import org.minimalj.frontend.Frontend.IComponent;
import org.minimalj.frontend.Frontend.Input;
import org.minimalj.model.properties.PropertyInterface;
import org.minimalj.util.IdUtils;
import org.minimalj.util.mock.Mocking;

public class ComboBoxFormElement<T> extends AbstractFormElement<T> implements Enable, Mocking {

	private final List<T> values;
	private final Input<T> comboBox;

	public ComboBoxFormElement(PropertyInterface property, List<T> values) {
		super(property);
		this.values = values;
		comboBox = Frontend.getInstance().createComboBox(values, listener());
	}
	
	@Override
	public IComponent getComponent() {
		return comboBox;
	}

	@Override
	public void setEnabled(boolean enabled) {
		comboBox.setEditable(enabled);
	}

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

	@Override
	public void setValue(T value) {
		// 'contains' uses the default equals method 
		if (value != null && !values.contains(value)) {
			// there could be a different instance with same id.
			// if yes take it.
			Object id = IdUtils.getId(value);
			value = values.stream().filter(c -> Objects.equals(id, IdUtils.getId(c))).findFirst().orElse(null);
		}
		comboBox.setValue(value);
	}

	@Override
	public void mock() {
		int index = (int)(Math.random() * values.size());
		setValue(values.get(index));
	}
	
}

@@ -36,24 +36,53 @@
 element;%0D%0A%0D%0A
+import java.util.ArrayList;%0D%0A
 import java.
@@ -672,22 +672,82 @@
 alues = 
-values
+this instanceof CodeFormElement ? values : new ArrayList%3C%3E(values)
 ;%0D%0A%09%09com
@@ -792,16 +792,21 @@
 omboBox(
+this.
 values, 

Fix for QueryResultList as values