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

org.ajax4jsf.taglib.ajax.AjaxListenerTag Maven / Gradle / Ivy

Go to download

Ajax4jsf is an open source extension to the JavaServer Faces standard that adds AJAX capability to JSF applications without requiring the writing of any JavaScript.

The newest version!
/**
 * Copyright 2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ajax4jsf.taglib.ajax;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.el.ReferenceSyntaxException;
import javax.faces.el.ValueBinding;
import javax.faces.webapp.UIComponentTag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

import org.ajax4jsf.framework.ajax.AjaxListener;
import org.ajax4jsf.framework.ajax.AjaxListenerHelper;
import org.ajax4jsf.framework.ajax.AjaxSource;
import org.ajax4jsf.framework.util.message.Messages;


/**
 * @author shura
 * 
 * Custom tag for append AJAX request listeners to AjaxContainer.
 *  
 */
public class AjaxListenerTag extends TagSupport {

    /**
     * 
     */
    private static final long serialVersionUID = 2576519366310474212L;
    /**
     * class name for ajax events listener.   
     */
    private String type = null;
    
    private String binding = null;

    /**
     *  
     */
    public AjaxListenerTag() {
    }

    public void setType(String type) {
        this.type = type;
    }

    /**
	 * @param binding the binding to set
	 */
	public void setBinding(String binding) {
		this.binding = binding;
	}

	public int doStartTag() throws JspException {
        if (type == null) {
            throw new JspException(Messages.getMessage(Messages.NULL_TYPE_ATTRIBUTE_ERROR));
        }

        //Find parent UIComponentTag
        UIComponentTag componentTag = UIComponentTag
                .getParentUIComponentTag(pageContext);
        if (componentTag == null) {
            throw new JspException(
            		Messages.getMessage(Messages.NO_UI_COMPONENT_TAG_ANCESTOR_ERROR, "AjaxListenerTag"));
        }

        if (componentTag.getCreated()) {
            //Component was just created, so we add the Listener
            UIComponent component = componentTag.getComponentInstance();
            if (component instanceof AjaxSource) {
                AjaxListener listener;
                if(null != binding){
                	ValueBinding valueBinding;
                	try {
					valueBinding = FacesContext.getCurrentInstance().getApplication().createValueBinding(binding);
                	} catch (ReferenceSyntaxException e) {
						throw new JspException(e);
					}
                	listener = new AjaxListenerHelper(valueBinding);
                } else {
                String className;
                if (UIComponentTag.isValueReference(type)) {
                    FacesContext facesContext = FacesContext
                            .getCurrentInstance();
                    ValueBinding valueBinding = facesContext.getApplication()
                            .createValueBinding(type);
                    className = (String) valueBinding.getValue(facesContext);
                } else {
                    className = type;
                }
				try {
					listener = (AjaxListener) Class.forName(className).newInstance();
				} catch (Exception e) {
	                throw new JspException(Messages.getMessage(Messages.INSTANTIATE_LISTENER_ERROR, className, component.getId()), e);
				} 
                }
                ((AjaxSource) component).addAjaxListener(listener);
            } else {
                throw new JspException(Messages.getMessage(Messages.NOT_AJAX_CONTAINER_ERROR, component.getId()));
            }
        }

        return Tag.SKIP_BODY;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy