org.ajax4jsf.tag.AjaxListenerHandler Maven / Gradle / Ivy
/**
* Licensed under the Common Development and Distribution License,
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.sun.com/cddl/
*
* 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.tag;
import java.io.IOException;
import javax.el.ELException;
import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;
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;
import com.sun.facelets.FaceletContext;
import com.sun.facelets.FaceletException;
import com.sun.facelets.el.LegacyValueBinding;
import com.sun.facelets.tag.TagAttribute;
import com.sun.facelets.tag.TagAttributeException;
import com.sun.facelets.tag.TagConfig;
import com.sun.facelets.tag.TagException;
import com.sun.facelets.tag.TagHandler;
/**
* Register an ActionListener instance on the UIComponent associated with the
* closest parent UIComponent custom action. See tag
* documentation.
*
* @see javax.faces.event.ActionListener
* @see javax.faces.component.AjaxContainer
* @author Jacob Hookom
* @version $Id: AjaxListenerHandler.java,v 1.6 2006/08/01 19:15:32 alexsmirnov Exp $
*/
public final class AjaxListenerHandler extends TagHandler {
private Class listenerType;
private final TagAttribute type;
private final TagAttribute binding;
/**
* @param config
*/
public AjaxListenerHandler(TagConfig config) {
super(config);
this.binding = this.getAttribute("binding");
this.type = this.getRequiredAttribute("type");
if (type != null) {
if (!type.isLiteral()) {
throw new TagAttributeException(this.tag, this.type, Messages.getMessage(Messages.MUST_BE_LITERAL_ERROR));
}
try {
this.listenerType = Class.forName(type.getValue());
} catch (Exception e) {
throw new TagAttributeException(this.tag, this.type, e);
}
}
}
/*
* (non-Javadoc)
*
* @see com.sun.facelets.FaceletHandler#apply(com.sun.facelets.FaceletContext,
* javax.faces.component.UIComponent)
*/
public void apply(FaceletContext ctx, UIComponent parent)
throws IOException, FacesException, FaceletException, ELException {
if (parent instanceof AjaxSource) {
// only process if parent was just created
if (parent.getParent() == null) {
AjaxSource src = (AjaxSource) parent;
AjaxListener listener = null;
ValueExpression ve = null;
if (this.binding != null) {
ve = this.binding.getValueExpression(ctx,
AjaxListener.class);
// TODO - handle both JSF 1.2/1.1 cases.
listener = new AjaxListenerHelper(new LegacyValueBinding(ve));
}
if (listener == null) {
try {
listener = (AjaxListener) listenerType.newInstance();
} catch (Exception e) {
throw new TagAttributeException(this.tag, this.type, e.getCause());
}
if (ve != null) {
ve.setValue(ctx, ve);
}
}
src.addAjaxListener(listener);
}
} else {
throw new TagException(this.tag, Messages.getMessage(Messages.NOT_PARENT_AJAX_CONTAINER_ERROR, parent));
}
}
}