package org.vaadin.spring.samples.mvp.ui.component.selector;

import java.util.EnumSet;

import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.MethodUtils;

import com.vaadin.ui.ComboBox;

public class DefaultSelector extends ComboBox {

    private static final long serialVersionUID = -3103214555075605482L;

    public DefaultSelector(@NotNull String items[]) {
        this(null, items);
    }

    public DefaultSelector(String caption, @NotNull String[] items) {
        if (StringUtils.isNotBlank(caption)) {
            setCaption(caption);
        }
        setItemCaptionMode(ItemCaptionMode.ID);
        setNullSelectionAllowed(false);
        setImmediate(true);
        int i = 0;
        for (String item: items) {
            addItem(item);
            if (i == 0) {
                setValue(item);
            }
            i++;
        }
    }

    @SuppressWarnings("unchecked")
    public <E extends Enum<E>> DefaultSelector(E e, String accessor) throws Exception {
        Class<E> enumClass = (Class<E>) e.getClass();
        EnumSet<E> set = EnumSet.allOf(enumClass);
        String value;
        for (E item: set) {
            value = (String) MethodUtils.invokeExactMethod(item, accessor, null);
            addItem(value);
            setItemCaption(value, value);
        }
        String selectedValue = (String) MethodUtils.invokeExactMethod(e, accessor, null);
        setValue(selectedValue);

        setNullSelectionAllowed(false);
        setImmediate(true);
    }
}

@@ -1278,35 +1278,45 @@
 tem, accessor, n
-ull
+ew Object%5B%5D%7B%7D
 );%0A            a
@@ -1466,19 +1466,29 @@
 essor, n
-ull
+ew Object%5B%5D%7B%7D
 );%0A     

Correct <NULL> warning, used empty objects instead of null