Skip to content

Instantly share code, notes, and snippets.

@mojavelinux
Created April 5, 2010 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mojavelinux/356085 to your computer and use it in GitHub Desktop.
Save mojavelinux/356085 to your computer and use it in GitHub Desktop.
package org.jboss.seam.faces.component;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.enterprise.util.Nonbinding;
import javax.inject.Qualifier;
/**
* @author Dan Allen
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Qualifier
public @interface ClientId {
/**
* The search expression that matches one or more UIComponent instances by its clientId.
*/
@Nonbinding String value() default "";
}
public class ExampleController
{
@Inject @ClientId("registerButton") UICommand registerButton;
public void action()
{
System.out.println(registerButton);
}
}
package org.jboss.seam.faces.component;
import java.lang.annotation.Annotation;
import java.util.Set;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.Typed;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.faces.component.EditableValueHolder;
import javax.faces.component.UICommand;
import javax.faces.component.UIComponent;
import javax.faces.component.UIData;
import javax.faces.component.UIForm;
import javax.faces.component.UIInput;
import javax.faces.component.UIPanel;
import javax.faces.context.FacesContext;
/**
* @author Dan Allen
*/
public class UIComponentProducer
{
public @Produces @ClientId UIComponent findComponent(InjectionPoint ip, FacesContext ctx)
{
return findComponentWithType(ip, ctx, UIComponent.class);
}
public @Produces @ClientId @Typed(UIForm.class) UIForm findFormComponent(InjectionPoint ip, FacesContext ctx)
{
return findComponentWithType(ip, ctx, UIForm.class);
}
public @Produces @ClientId @Typed(UICommand.class) UICommand findCommandComponent(InjectionPoint ip, FacesContext ctx)
{
return findComponentWithType(ip, ctx, UICommand.class);
}
public @Produces @ClientId @Typed(UIInput.class) UIInput findInputComponent(InjectionPoint ip, FacesContext ctx)
{
return findComponentWithType(ip, ctx, UIInput.class);
}
public @Produces @ClientId @Typed(UIPanel.class) UIPanel findPanelComponent(InjectionPoint ip, FacesContext ctx)
{
return findComponentWithType(ip, ctx, UIPanel.class);
}
public @Produces @ClientId @Typed(UIData.class) UIData findDataComponent(InjectionPoint ip, FacesContext ctx)
{
return findComponentWithType(ip, ctx, UIData.class);
}
private <T extends UIComponent> T findComponentWithType(InjectionPoint ip, FacesContext ctx, Class<T> expectedType)
{
UIComponent candidate = ctx.getViewRoot().findComponent(getSearchExpression(ip));
if (candidate != null && expectedType.isAssignableFrom(candidate.getClass()))
{
return expectedType.cast(candidate);
}
return null;
}
private String getSearchExpression(InjectionPoint ip)
{
for (Annotation candidate : ip.getQualifiers())
{
if (candidate.annotationType().equals(ClientId.class))
{
String expr = ((ClientId) candidate).value().trim();
if (expr.length() == 0)
{
throw new IllegalArgumentException("Client id value cannot be empty at " + ip);
}
return expr;
}
}
throw new IllegalArgumentException("Injection point " + ip + " does not have " + ClientId.class + " qualifier");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment