Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.richfaces.renderkit.html;
  
  //
  // Imports
  //
  
  import java.util.List;
  
 import  javax.faces.FacesException;
 import  javax.faces.component.ActionSource;
 import  javax.faces.component.EditableValueHolder;
 import  javax.faces.component.UIComponent;
 import  javax.faces.component.behavior.ClientBehavior;
 import  javax.faces.component.behavior.ClientBehaviorContext;
 import  javax.faces.context.FacesContext;
 import  javax.faces.event.AjaxBehaviorEvent;
 import  javax.faces.event.PhaseId;
 import  javax.faces.render.ClientBehaviorRenderer;
 
 import  org.ajax4jsf.javascript.JSReference;
 import  org.ajax4jsf.javascript.ScriptUtils;
 import  org.richfaces.application.ServiceTracker;
 import  org.richfaces.javascript.JavaScriptService;
 
 import  com.google.common.collect.Lists;

Renderer for component class org.richfaces.renderkit.html.AjaxValidatorRenderer
 
 public class ClientValidatorRenderer extends ClientBehaviorRenderer {
     public static final String RENDERER_TYPE = "org.richfaces.ClientValidatorRenderer";
     public static final String VALUE_VAR = "value";
     public static final String CONVERTED_VALUE_VAR = "convertedValue";
     public static final JSReference VALUE_LITERAL = new JSReference("value");
     public static final JSReference CONVERTED_VALUE_LITERAL = new JSReference("convertedValue");
 
     public ClientValidatorRenderer() {
         super();
     }
 
     @Override
     public String getScript(ClientBehaviorContext behaviorContext, ClientBehavior behavior) {
         if (null == behaviorContext) {
             throw new NullPointerException();
         }
         if (null == behavior) {
             throw new NullPointerException();
         }
         if (behavior instanceof ClientValidatorBehavior) {
             ClientValidatorBehavior clientValidator = (ClientValidatorBehaviorbehavior;
             if (clientValidator.isDisabled()) {
                 return null;
             } else {
                 return buildAndStoreValidatorScript(behaviorContextclientValidator);
             }
         } else {
             throw new FacesException(
                     "ClientBehavior for ClientValidatorRenderer does not implement ClientValidatorBehavior interface");
         }
     }
 
     @Override
     public void decode(FacesContext context, UIComponent component, ClientBehavior behavior) {
         if (null == context || null == component || null == behavior) {
             throw new NullPointerException();
         }
 
         if (!(behavior instanceof ClientValidatorBehavior)) {
             throw new IllegalArgumentException(
                     "Instance of org.ruchvaces.component.behaviot.ClientValidatorBehavior required: " + behavior);
         }
         ClientValidatorBehavior ajaxBehavior = (ClientValidatorBehaviorbehavior;
 
         // First things first - if AjaxBehavior is disabled, we are done.
         if (!ajaxBehavior.isDisabled()) {
             component.queueEvent(createEvent(componentajaxBehavior));
         }
     }
 
     // Creates an AjaxBehaviorEvent for the specified component/behavior
     private static AjaxBehaviorEvent createEvent(UIComponent componentClientValidatorBehavior ajaxBehavior) {
 
         AjaxBehaviorEvent event = new AjaxBehaviorEvent(componentajaxBehavior);
 
         PhaseId phaseId = isImmediate(componentajaxBehavior) ? PhaseId.APPLY_REQUEST_VALUES : PhaseId.PROCESS_VALIDATIONS;
 
         event.setPhaseId(phaseId);
 
         return event;
     }
    // Tests whether we should perform immediate processing. Note
    // that we "inherit" immediate from the parent if not specified
    // on the behavior.
    private static boolean isImmediate(UIComponent componentClientValidatorBehavior ajaxBehavior) {
        boolean immediate = false;
        if (ajaxBehavior.isImmediateSet()) {
            immediate = ajaxBehavior.isImmediate();
        } else if (component instanceof EditableValueHolder) {
            immediate = ((EditableValueHolder) component).isImmediate();
        } else if (component instanceof ActionSource) {
            immediate = ((ActionSource) component).isImmediate();
        }
        return immediate;
    }

    

This method builds client-side validation script and stores it in View resource component

Parameters:
behaviorContext
behavior
Returns:
name of the JavaScript function to call
    String buildAndStoreValidatorScript(ClientBehaviorContext behaviorContextClientValidatorBehavior behavior) {
        ComponentValidatorScript validatorScript = createValidatorScript(behaviorContextbehavior);
        if (null != validatorScript) {
            FacesContext facesContext = behaviorContext.getFacesContext();
            JavaScriptService javaScriptService = ServiceTracker.getService(JavaScriptService.class);
            validatorScript = javaScriptService.addScript(facesContextvalidatorScript);
            return validatorScript.createCallScript(behaviorContext.getComponent().getClientId(facesContext),
                    behaviorContext.getSourceId());
        } else {
            return null;
        }
    }
    ComponentValidatorScript createValidatorScript(ClientBehaviorContext behaviorContextClientValidatorBehavior behavior) {
        ValidatorScriptBase validatorScript;
        Collection<ValidatorDescriptorvalidators = behavior.getValidators(behaviorContext);
        if (!validators.isEmpty()) {
            try {
                ConverterDescriptor converter = behavior.getConverter(behaviorContext);
                if (null != converter) {
                    try {
                        LibraryScriptFunction clientSideConverterScript = getClientSideConverterScript(
                                behaviorContext.getFacesContext(), converter);
                        validatorScript = createValidatorScript(behaviorContextbehaviorvalidators,
                                clientSideConverterScript);
                    } catch (ScriptNotFoundException e) {
                        // ajax-only validation
                        validatorScript = new AjaxOnlyScript(createAjaxScript(behaviorContextbehavior));
                    }
                } else {
                    validatorScript = createValidatorScript(behaviorContextbehaviorvalidatorsnull);
                }
            } catch (ConverterNotFoundException e) {
                // fallback to ajax-only validation
                validatorScript = new AjaxOnlyScript(createAjaxScript(behaviorContextbehavior));
            }
            String clientId = getComponentClientId(behaviorContext);
            String name = ScriptUtils.getValidJavascriptName(validatorScript.toScript());
            validatorScript.setName(name);
            return validatorScript;
        } else {
            // No validation required.
            return null;
        }
    }
    private String getComponentClientId(ClientBehaviorContext behaviorContext) {
        return behaviorContext.getComponent().getClientId(behaviorContext.getFacesContext());
    }
    private ValidatorScriptBase createValidatorScript(ClientBehaviorContext behaviorContextClientValidatorBehavior behavior,
            Collection<ValidatorDescriptorvalidatorsLibraryScriptFunction clientSideConverterScript) {
        Collection<? extends LibraryScriptFunctionvalidatorScripts = getClientSideValidatorScript(
                behaviorContext.getFacesContext(), validators);
        if (validatorScripts.isEmpty()) {
            return new AjaxOnlyScript(createAjaxScript(behaviorContextbehavior));
        } else if (validatorScripts.size() < validators.size()) {
            return new ClientAndAjaxScript(clientSideConverterScriptvalidatorScriptscreateAjaxScript(behaviorContext,
                    behavior), behavior.getOnvalid(), behavior.getOninvalid());
        } else {
            return new ClientOnlyScript(clientSideConverterScriptvalidatorScriptsbehavior.getOnvalid(),
                    behavior.getOninvalid());
        }
    }
    private String createAjaxScript(ClientBehaviorContext behaviorContextClientValidatorBehavior behavior) {
        String ajaxScript = behavior.getAjaxScript(behaviorContext);
        String clientId = getComponentClientId(behaviorContext);
        ajaxScript = replaceTextToVariable(ajaxScriptclientId.);
        String sourceId = behaviorContext.getSourceId();
        if (null != sourceId) {
            // TODO - send sourceId as separate parameter.
            ajaxScript = replaceTextToVariable(ajaxScriptsourceId.);
        }
        return ajaxScript;
    }
    private String replaceTextToVariable(String ajaxScriptString clientIdString variableName) {
        ajaxScript = ajaxScript.replace("'" + clientId + "'"variableName);
        ajaxScript = ajaxScript.replace("\"" + clientId + "\""variableName);
        return ajaxScript;
    }

    

Build client-side function call for Server-side component descriptor.

Parameters:
behaviorContext
validator
Returns:
Throws:
ScriptNotFoundException

    

Parameters:
behaviorContext
converter
Returns:
Throws:
ScriptNotFoundException
    LibraryScriptFunction getClientSideConverterScript(FacesContext facesContextConverterDescriptor converter)
            throws ScriptNotFoundException {
        ClientScriptService clientScriptService = ServiceTracker.getService(facesContextClientScriptService.class);
        return createClientFunction(facesContextconverterclientScriptService);
    }
    private LibraryScriptFunction createClientFunction(FacesContext facesContextFacesObjectDescriptor descriptor,
            ClientScriptService clientScriptServicethrows ScriptNotFoundException {
        LibraryFunction script = clientScriptService.getScript(facesContextdescriptor.getImplementationClass());
        return new LibraryScriptFunction(scriptdescriptor.getMessage(), descriptor.getAdditionalParameters());
    }

    

Build client-side function call for Server-side component descriptor.

Parameters:
facesContext
validators
Returns:
Throws:
ScriptNotFoundException
    Collection<? extends LibraryScriptFunctiongetClientSideValidatorScript(FacesContext facesContext,
            Collection<ValidatorDescriptorvalidators) {
        ClientScriptService clientScriptService = ServiceTracker.getService(facesContextClientScriptService.class);
        List<LibraryScriptFunctionscripts = Lists.newArrayList();
        for (FacesObjectDescriptor validator : validators) {
            try {
                scripts.add(createClientFunction(facesContextvalidatorclientScriptService));
            } catch (ScriptNotFoundException e) {
                // Skip this validator for AJAX call.
            }
        }
        return scripts;
    }
New to GrepCode? Check out our FAQ X