I'm trying to create a composite component that will allow the user to toggle between a h:singleSelectMenu and h:selectManyListbox. I have it working sort of. It works as long as the value field points to a Collection...it does NOT work if the value field is null.
singleMultiSelect.xhtml
<?xml version="1.0" encoding="US-ASCII"?>
<!DOCTYPE html>
<ui:composition
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:ace="http://www.icefaces.org/icefaces/components"
>
<cc:interface componentType="singleMultiSelect">
<!-- The initial list of objects -->
<cc:attribute name="list" type="java.util.List" required="true"/>
<!-- The selected objects -->
<cc:attribute name="selected" type="java.util.Collection" required="true"/>
<!-- whether to display the selectOneMenu (true) or selectManyBox (false) -->
<cc:attribute name="singleSelect" type="java.lang.Boolean"
required="false" default="true"/>
</cc:interface>
<cc:implementation>
<span id="#{cc.clientId}">
<ace:checkboxButton id="singleSelectChkBx"
value="#{cc.attrs.singleSelect}">
<ace:ajax render="#{cc.clientId}"/>
</ace:checkboxButton>
<h:selectOneMenu id="selectOneMenu"
rendered="#{cc.attrs.singleSelect}"
value="#{cc.singleSelected}">
<f:selectItems value="#{cc.attrs.list}"/>
</h:selectOneMenu>
<h:selectManyListbox id="selectManyListbox"
rendered="#{! cc.attrs.singleSelect}"
value="#{cc.attrs.selected}">
<f:selectItems value="#{cc.attrs.list}"/>
</h:selectManyListbox>
</span>
</cc:implementation>
</ui:composition>
SingleMultiSelect.java
public class SingleMultiSelect extends UINamingContainer {
public SingleMultiSelect() {
super();
}
/**
* Converts the Object selected within the selectOneMenu to the list
* used by the component.
*
* @param singleSelected
*/
public void setSingleSelected(Object singleSelected) {
getSelected().clear();
if(singleSelected != null) {
getSelected().add(singleSelected);
}
}
/**
* Converts the collection used by the component to a single
* Object selected within the selectOneMenu.
*
* @return
*/
public Object getSingleSelected() {
return getSelected().size() > 0 ? getSelected().iterator().next() : null;
}
private Collection getSelected() {
return (Collection) getAttributes().get("selected");
}
}
I tried writing to the attributes map but that didn't work
public void setSingleSelected(Object singleSelected) {
HashSet selected = new HashSet();
selected.add(singleSelected);
((Collection) getAttributes()).put("selected", selected);
}