Monday, May 11, 2009

Optional valueChangeListener for h:selectonemenu

Here is an implementation of the needed tag handler:

public final class ValueChangeListenerHandler extends TagHandler {

private final static Class[] VALUECHANGE_SIG = new Class[] {ValueChangeEvent.class};

private final TagAttribute value;

public ValueChangeListenerHandler(TagConfig config) {
super(config);
value = this.getRequiredAttribute("value");
}

/*
* (non-Javadoc)
*
* @see com.sun.facelets.FaceletHandler#apply(com.sun.facelets.FaceletContext,
* javax.faces.component.UIComponent)
*/
public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, ELException {

if (parent instanceof EditableValueHolder) {
if (ComponentSupport.isNew(parent)) {
((EditableValueHolder) parent).addValueChangeListener(new
MethodExpressionValueChangeListener(
value.getMethodExpression(ctx, null, VALUECHANGE_SIG)));
}
} else {
throw new TagException(this.tag, "Parent is not of type EditableValueHolder, type is: " + parent);
}
}
}

Usage:

h:selectonemenu value="#{value}"
c:if test="#{not empty changeListener and changeListener}"
custom:valuechangelistener value="#{backing.selectOneMenuChanged}"
/c:if
/h:selectonemenu

Too bad Blogger doesn't have support for displaying code.

No comments:

Post a Comment