All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.richfaces.renderkit.html.QueueResourceComponentRenderer Maven / Gradle / Ivy

There is a newer version: 4.3.7.Final
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2010, Red Hat, Inc. and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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 software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.richfaces.renderkit.html;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.faces.application.ResourceDependencies;
import javax.faces.application.ResourceDependency;
import javax.faces.component.UIComponent;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;

import org.ajax4jsf.javascript.ScriptUtils;
import org.ajax4jsf.renderkit.RendererUtils;
import org.ajax4jsf.renderkit.RendererUtils.HTML;
import org.richfaces.cdk.annotations.JsfRenderer;
import org.richfaces.component.QueueRegistry;

/**
 * @author Nick Belaevski
 * 
 */
@JsfRenderer(type = "org.richfaces.QueueResourceComponentRenderer", family = UIOutput.COMPONENT_FAMILY)
@ResourceDependencies(value = {
        @ResourceDependency(library = "javax.faces", name = "jsf.js"),
        @ResourceDependency(name = "jquery.js"), 
        @ResourceDependency(name = "richfaces.js"),
        @ResourceDependency(name = "richfaces-queue.js")})
public class QueueResourceComponentRenderer extends Renderer {

    private static final String FUNCTION_NAME = "RichFaces.queue.setQueueOptions";

    private final RendererUtils utils = RendererUtils.getInstance();
    
    private enum QueueOptions {
        onbeforedomupdate, oncomplete, onerror, onevent, onrequestdequeue, onrequestqueue, onsubmit, 
        requestDelay, timeout, status, queueId, ignoreDupResponses, requestGroupingId
    }

    private void appendOptions(UIComponent queue, Map optionsHash) {
        Map attributes = queue.getAttributes();
        
        for (QueueOptions option : QueueOptions.values()) {
            String optionName = option.name();
            Object value = attributes.get(optionName);
            utils.addToScriptHash(optionsHash, optionName, value);
        }
    }
    
    @Override
    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
        super.encodeEnd(context, component);
        
        QueueRegistry registry = QueueRegistry.getInstance(context);
        if (registry != null && registry.hasQueuesToEncode()) {
            ResponseWriter writer = context.getResponseWriter();
            
            writer.startElement(HTML.SCRIPT_ELEM, component);
            writer.writeAttribute(HTML.ID_ATTRIBUTE, component.getClientId(context), null);
            writer.writeAttribute(HTML.TYPE_ATTR, HTML.JAVASCRIPT_TYPE, null);
            
            writer.writeText(FUNCTION_NAME, null);
            writer.writeText("({", null);
            
            Map queueOptionsMap = new LinkedHashMap();

            boolean isFirst = true;
            Map registeredQueues = registry.getRegisteredQueues();
            for (Entry queueEntry : registeredQueues.entrySet()) {
                if (isFirst) {
                    isFirst = false;
                } else {
                    writer.writeText(",", null);
                }

                queueOptionsMap.clear();

                String queueName = queueEntry.getKey();
                UIComponent queue = queueEntry.getValue();

                appendOptions(queue, queueOptionsMap);

                ScriptUtils.writeToStream(writer, queueName);
                writer.writeText(":", null);
                ScriptUtils.writeToStream(writer, queueOptionsMap);
            }

            writer.writeText("});", null);
            
            writer.endElement(HTML.SCRIPT_ELEM);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy