📄 idforcingcomponentfactory.java
字号:
/** * */package de.mindmatters.faces.application;import java.lang.reflect.Method;import java.util.Iterator;import javax.faces.FactoryFinder;import javax.faces.application.Application;import javax.faces.component.EditableValueHolder;import javax.faces.component.NamingContainer;import javax.faces.component.UICommand;import javax.faces.component.UIComponent;import javax.faces.component.UIData;import javax.faces.component.UIForm;import javax.faces.context.FacesContext;import javax.faces.el.ValueBinding;import javax.faces.render.RenderKit;import javax.faces.render.RenderKitFactory;import javax.faces.render.Renderer;import net.sf.cglib.proxy.Callback;import net.sf.cglib.proxy.CallbackFilter;import net.sf.cglib.proxy.Enhancer;import net.sf.cglib.proxy.MethodInterceptor;import net.sf.cglib.proxy.MethodProxy;import net.sf.cglib.proxy.NoOp;import org.springframework.util.ClassUtils;/** * <strong>ComponentFactory</strong> that generates "clean" client ids (in fact * they are equal to their component id) for input components within a * {@link de.mindmatters.faces.component.html.HtmlForm}. * * @author Andreas Kuhrwahl * */public final class IdForcingComponentFactory implements ComponentFactory { /** * CGLIB MethodInterceptor to reimplement * {@link UIComponent#getClientId(FacesContext)}. */ private static final class UIComponentInterceptor implements MethodInterceptor { /** Singleton instance of UIComponentInterceptor. */ public static final UIComponentInterceptor INSTANCE = new UIComponentInterceptor(); /** * It's a Singleton! */ private UIComponentInterceptor() { super(); } /** * {@inheritDoc} */ public Object intercept(final Object obj, final Method method, final Object[] args, final MethodProxy mp) throws Throwable { FacesContext context = FacesContext.getCurrentInstance(); String clientId = null; UIComponent component = (UIComponent) obj; if (isGetFormSubmit(component)) { clientId = component.getId(); UIComponent parentContainer = findParentNamingContainer(component); if (parentContainer != null) { if (parentContainer instanceof UIData) { int rowIndex = ((UIData) parentContainer).getRowIndex(); if (rowIndex != -1) { clientId = clientId + "[" + rowIndex + "]"; } } } Renderer renderer = getRenderer(context, component); if (renderer != null) { clientId = renderer.convertClientId(context, clientId); } } else { clientId = (String) mp.invokeSuper(obj, args); } return clientId; } /** * Checks whether the method of the parent form is 'GET' or not. * * @param component * the UIInput component * @return <code>true</code> if the parent form exists and it's method * is 'GET' <code>false</code> otherwise */ private boolean isGetFormSubmit(final UIComponent component) { UIComponent parent = component.getParent(); while (parent != null) { if (parent instanceof UIForm) { break; } else { parent = parent.getParent(); } } String method = null; if (parent != null) { method = (String) parent.getAttributes().get("method"); } boolean isGetRequest = false; if (method != null) { isGetRequest = method.equalsIgnoreCase("get"); } return isGetRequest; } /** * Returns a parent naming container if any. * * @param component * the UIInput component * @return the parent naming container if any or <code>null</code> */ private UIComponent findParentNamingContainer( final UIComponent component) { UIComponent parent = component.getParent(); while (parent != null) { if (parent instanceof NamingContainer) { break; } else { parent = parent.getParent(); } } return parent; } /** * Returns the renderer of the given component <code>component</code>. * * @param context * {@link FacesContext} for the current request * @param component * the UIInput component * @return The renderer of the given component <code>component</code> * or <code>null</code> if none exists */ private Renderer getRenderer(final FacesContext context, final UIComponent component) { Renderer renderer = null; String rendererType = component.getRendererType(); if (rendererType != null) { String renderKitId = context.getViewRoot().getRenderKitId(); RenderKit renderKit = ((RenderKitFactory) FactoryFinder .getFactory(FactoryFinder.RENDER_KIT_FACTORY)) .getRenderKit(context, renderKitId); renderer = renderKit.getRenderer(component.getFamily(), rendererType); } return renderer; } } /** * Creates a CGLib-proxy class for all UIInput classes. * * @param componentClass * The UIInput class * @return The proxy */ private static Class createModifiedUIComponentClass( final Class componentClass) { Enhancer enhancer = new Enhancer(); enhancer.setCallbackTypes(new Class[] { NoOp.class, UIComponentInterceptor.class }); enhancer.setCallbackFilter(new CallbackFilter() { public int accept(final Method method) { int returnCode; if (method.getName().equals("getClientId")) { returnCode = 1; } else { returnCode = 0; } return returnCode; } }); enhancer.setSuperclass(componentClass); Class proxyClass = enhancer.createClass(); Enhancer.registerStaticCallbacks(proxyClass, new Callback[] { NoOp.INSTANCE, UIComponentInterceptor.INSTANCE }); return proxyClass; } /** * {@inheritDoc} */ public void addComponent(final Application application, final String componentType, final String componentClassName) { try { Class componentClass = ClassUtils.forName(componentClassName); if (EditableValueHolder.class.isAssignableFrom(componentClass) || UICommand.class.isAssignableFrom(componentClass)) { componentClass = createModifiedUIComponentClass(componentClass); } application.addComponent(componentType, componentClass.getName()); } catch (Exception ex) { application.addComponent(componentType, componentClassName); } } /** * {@inheritDoc} */ public UIComponent createComponent(final Application application, final String componentType) { return application.createComponent(componentType); } /** * {@inheritDoc} */ public UIComponent createComponent(final Application application, final ValueBinding componentBinding, final FacesContext context, final String componentType) { return application.createComponent(componentBinding, context, componentType); } /** * {@inheritDoc} */ public Iterator getComponentTypes(final Application application) { return application.getComponentTypes(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -