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.html;
 
 import java.util.Map;
 
 import  javax.faces.application.ResourceDependencies;
 import  javax.faces.application.ResourceDependency;
 import  javax.faces.component.UIComponent;
 import  javax.faces.context.FacesContext;
 import  javax.faces.context.PartialViewContext;
 import  javax.faces.context.ResponseWriter;
 import  javax.faces.event.ActionEvent;
 
 import  org.ajax4jsf.javascript.JSFunction;
 import  org.ajax4jsf.javascript.JSFunctionDefinition;
 import  org.ajax4jsf.javascript.JSReference;
 import  org.richfaces.renderkit.HtmlConstants;

Author(s):
shura
 
 @ResourceDependencies({ @ResourceDependency(library = "org.richfaces", name = "ajax.reslib"),
         @ResourceDependency(library = "org.richfaces", name = "base-component.reslib"),
         @ResourceDependency(library = "org.richfaces", name = "poll.js") })
 public class AjaxPollRenderer extends RendererBase {
     public static final String COMPONENT_FAMILY = "org.richfaces.Poll";
     public static final String RENDERER_TYPE = "org.richfaces.PollRenderer";
     private static final String AJAX_POLL_FUNCTION = "new RichFaces.ui.Poll";
     private static final String ENABLED = "enabled";
 
     private void addComponentToAjaxRender(FacesContext context, UIComponent component) {
         PartialViewContext pvc = context.getPartialViewContext();
         pvc.getRenderIds().add(component.getClientId(context));
     }
 
     @Override
     protected void queueComponentEventForBehaviorEvent(FacesContext context, UIComponent componentString eventName) {
         super.queueComponentEventForBehaviorEvent(contextcomponenteventName);
 
         if (..equals(eventName)) {
             new ActionEvent(component).queue();
             addComponentToAjaxRender(contextcomponent);
         }
     }
 
     /*
      * (non-Javadoc)
      *
      * @see org.ajax4jsf.renderkit.RendererBase#doEncodeEnd(javax.faces.context.ResponseWriter,
      * javax.faces.context.FacesContext, javax.faces.component.UIComponent)
      */
 
     protected void doEncodeEnd(ResponseWriter writer, FacesContext context, UIComponent componentthrows IOException {
 
         RendererUtils utils = getUtils();
         boolean shouldRenderForm = utils.getNestingForm(contextcomponent) == null;
         String rootElementName = shouldRenderForm ? HtmlConstants.DIV_ELEM : HtmlConstants.SPAN_ELEM;
 
         AbstractPoll poll = (AbstractPollcomponent;
         writer.startElement(rootElementNamecomponent);
         writer.writeAttribute(HtmlConstants.STYLE_ATTRIBUTE, "display:none;"null);
         utils.encodeId(contextcomponent);
 
         if (shouldRenderForm) {
             String clientId = component.getClientId(context) + .;
             utils.encodeBeginForm(contextcomponentwriterclientId);
             utils.encodeEndForm(contextwriter);
         }
 
         // polling script.
         writer.startElement(HtmlConstants.SCRIPT_ELEM, component);
        writer.writeAttribute(HtmlConstants.TYPE_ATTR, "text/javascript"null);
        StringBuffer script = new StringBuffer("");
        JSFunction function = new JSFunction();
        Map<StringObjectoptions = new HashMap<StringObject>();
        RenderKitUtils.addToScriptHash(options"interval"poll.getInterval(), "1000");
        // RenderKitUtils.addToScriptHash(options, "pollId", component.getClientId(context));
        HandlersChain handlersChain = new HandlersChain(contextpoll);
        handlersChain.addBehaviors(.);
        handlersChain.addAjaxSubmitFunction();
        String handler = handlersChain.toScript();
        if (handler != null) {
            JSFunctionDefinition timerHandler = new JSFunctionDefinition(JSReference.EVENT);
            timerHandler.addToBody(handler);
            options.put(.timerHandler);
        }
        if (poll.isEnabled()) {
            options.put(true);
        }
        function.addParameter(component.getClientId(context));
        function.addParameter(options);
        // function.appendScript(script);
        writer.writeText(function.toScript(), null);
        writer.endElement(HtmlConstants.SCRIPT_ELEM);
        writer.endElement(rootElementName);
    }
    /*
     * (non-Javadoc)
     *
     * @see org.ajax4jsf.renderkit.RendererBase#getComponentClass()
     */
    protected Class<? extends UIComponent> getComponentClass() {
        // only push component is allowed.
        return AbstractPoll.class;
    }
    @Override
    protected void doDecode(FacesContext context, UIComponent component) {
        super.doDecode(contextcomponent);
        AbstractPoll poll = (AbstractPollcomponent;
        if (poll.isEnabled()) {
            Map<StringStringrequestParameterMap = context.getExternalContext().getRequestParameterMap();
            if (requestParameterMap.get(poll.getClientId(context)) != null) {
                new ActionEvent(poll).queue();
                addComponentToAjaxRender(contextcomponent);
            }
        }
    }
New to GrepCode? Check out our FAQ X