Created
April 5, 2010 05:59
-
-
Save mojavelinux/356085 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ""; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExampleController | |
{ | |
@Inject @ClientId("registerButton") UICommand registerButton; | |
public void action() | |
{ | |
System.out.println(registerButton); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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