Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
License Agreement. Rich Faces - Natural Ajax for Java Server Faces (JSF) Copyright (C) 2007 Exadel, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 package org.richfaces.renderkit.util;
 
 import static org.richfaces.renderkit.util.AjaxRendererUtils.buildAjaxFunction;
 
 import java.util.List;
 import java.util.Map;
 
 import  javax.faces.component.UIComponent;
 import  javax.faces.component.behavior.ClientBehavior;
 import  javax.faces.component.behavior.ClientBehaviorContext;
 import  javax.faces.component.behavior.ClientBehaviorContext.Parameter;
 import  javax.faces.component.behavior.ClientBehaviorHint;
 import  javax.faces.component.behavior.ClientBehaviorHolder;
 import  javax.faces.context.FacesContext;
 
 import  org.ajax4jsf.javascript.JSFunction;
 import  org.ajax4jsf.javascript.JSReference;
 import  org.ajax4jsf.javascript.ScriptString;

Author(s):
Nick Belaevski
Since:
4.0
 
 public final class HandlersChain {
    
 
     private static final RendererUtils RENDERER_UTILS = RendererUtils.getInstance();
     // private static final Logger LOG = RichfacesLogger.RENDERKIT.getLogger();
     private boolean hasSubmittingBehavior = false;
     private boolean includeClientId = false;
     // TODO: review for optimization
     private List<Stringhandlers = new ArrayList<String>(2);
     private FacesContext facesContext;
     private UIComponent component;
     private Collection<Parameter> parameters;
 
     public HandlersChain(FacesContext facesContext, UIComponent component) {
         this. = facesContext;
         this. = component;
     }
 
     public HandlersChain(FacesContext facesContext, UIComponent componentboolean includeClientId) {
         this. = facesContext;
         this. = component;
         this. = includeClientId;
     }
 
     public HandlersChain(FacesContext facesContext, UIComponent componentCollection<Parameter> parameters) {
         this. = facesContext;
         this. = component;
         this. = parameters;
     }
 
     public HandlersChain(FacesContext facesContext, UIComponent componentCollection<Parameter> parameters,
         boolean includeClientId) {
         this. = facesContext;
         this. = component;
         this. = parameters;
         this. = includeClientId;
     }
 
     private static boolean isNotEmpty(String s) {
         return (s != null) && (s.length() != 0);
     }
 
     private List<ClientBehavior> getBehaviorsList(String behaviorName) {
         List<ClientBehavior> behaviors = null;
 
         if ( instanceof ClientBehaviorHolder) {
             ClientBehaviorHolder clientBehaviorHolder = (ClientBehaviorHolder) ;
             Map<StringList<ClientBehavior>> clientBehaviorsMap = clientBehaviorHolder.getClientBehaviors();
 
             if (clientBehaviorsMap != null) {
                 behaviors = clientBehaviorsMap.get(behaviorName);
             }
        }
        return behaviors;
    }
    private Collection<Parameter> getParameters() {
        if ( == null) {
            Map<StringObjectparametersMap = .createParametersMap();
             = createParametersList(parametersMap);
        }
        return ;
    }
    public boolean hasSubmittingBehavior() {
        return ;
    }
    public void addInlineHandlerAsValue(String handlerValue) {
        if (isNotEmpty(handlerValue)) {
            .add(handlerValue);
        }
    }
    public void addInlineHandlerFromAttribute(String attributeName) {
        addInlineHandlerAsValue((String.getAttributes().get(attributeName));
    }
    public void addBehaviors(String domEventName) {
        addBehaviors(domEventNamenull);
    }
    public void addBehaviors(String domEventNameString logicalEventName) {
        String name = domEventName;
        List<ClientBehavior> behaviorsList = getBehaviorsList(domEventName);
        if ((behaviorsList == null) && (logicalEventName != null)) {
            behaviorsList = getBehaviorsList(logicalEventName);
            name = logicalEventName;
        }
        if (behaviorsList == null) {
            return;
        }
        ClientBehaviorContext behaviorContext = ClientBehaviorContext.createClientBehaviorContext(,
            name ? .getClientId() : nullgetParameters());
        for (ClientBehavior clientBehavior : behaviorsList) {
            String behaviorScript = clientBehavior.getScript(behaviorContext);
            if (isNotEmpty(behaviorScript)) {
                if (clientBehavior.getHints().contains(ClientBehaviorHint.SUBMITTING)) {
                     = true;
                }
                .add(behaviorScript);
            }
        }
    }
    public void addAjaxSubmitFunction() {
        if (!this.hasSubmittingBehavior()) {
             = true;
            ScriptString ajaxFunction = buildAjaxFunction();
            this.addInlineHandlerAsValue(ajaxFunction.toScript());
        }
    }
    public String toScript() {
        String result = null;
        if (!.isEmpty()) {
            if (.size() == 1) {
                result = .get(0);
            } else {
                JSFunction jsFunction = new JSFunction("jsf.util.chain", JSReference.THIS, JSReference.EVENT);
                for (String handler : ) {
                    jsFunction.addParameter(handler);
                }
                result = jsFunction.toScript();
            }
        }
        return result;
    }
    public static List<Parameter> createParametersList(Map<StringObjectparametersMap) {
        List<Parameter> parameters = new ArrayList<Parameter>(parametersMap.size());
        for (Entry<StringObjectentry : parametersMap.entrySet()) {
            parameters.add(new Parameter(entry.getKey(), entry.getValue()));
        }
        return parameters;
    }
New to GrepCode? Check out our FAQ X