package de.larmic.jsf2.component.html;

import java.util.ArrayList;

import javax.faces.component.FacesComponent;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UISelectItems;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.model.SelectItem;

@FacesComponent(HtmlComboBox.COMPONENT_TYPE)
public class HtmlComboBox extends AbstractHtmlContainer {

	public static final String COMPONENT_TYPE = "de.larmic.component.comboBox";

	@Override
	public Object encodeValue(final Object value) {
		for (final UIComponent uiComponent : this.getInputComponent().getChildren()) {
			if (uiComponent instanceof UISelectItems) {
				final ArrayList<SelectItem> items = (ArrayList<SelectItem>) ((UISelectItems) uiComponent).getValue();

				for (final SelectItem item : items) {
					final Object itemValue = item.getValue();

					if (itemValue != null && itemValue.toString().equals(value)) {
						return itemValue;
					}
				}
			}
		}
		return value;
	}

	@Override
	protected UIInput initInputComponent() {
		return new HtmlSelectOneMenu();
	}
}

@@ -187,16 +187,59 @@
 IInput;%0A
+import javax.faces.component.UISelectItem;%0A
 import j
@@ -731,24 +731,59 @@
 ectItems) %7B%0A
+%09%09%09%09@SuppressWarnings(%22unchecked%22)%0A
 %09%09%09%09final Ar
@@ -928,109 +928,216 @@
 %09%09%09%09
-final Object 
+if (this.isItemMatching(
 item
-V
+, v
 alue
- = item.getValue();%0A%0A%09%09%09%09%09if (itemValue != null && itemValue.toString().equals(
+)) %7B%0A%09%09%09%09%09%09return value;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D else if (uiComponent instanceof UISelectItem) %7B%0A%09%09%09%09if (this.isItemMatching((SelectItem) ((UISelectItem) uiComponent).getValue(), 
 valu
@@ -1151,33 +1151,21 @@
 %09%09%09%09
-%09
 return 
-itemValue;%0A%09%09%09%09%09%7D
+value;
 %0A%09%09%09
@@ -1285,11 +1285,203 @@
 nu();%0A%09%7D
+%0A%0A%09private boolean isItemMatching(final SelectItem item, final Object value) %7B%0A%09%09final Object itemValue = item.getValue();%0A%0A%09%09return itemValue != null && itemValue.toString().equals(value);%0A%09%7D
 %0A%7D%0A

Support ui:selectItem