Author: abelevich
Date: 2010-05-17 11:47:21 -0400 (Mon, 17 May 2010)
New Revision: 17084
Modified:
root/ui/core/trunk/api/src/main/java/org/richfaces/view/facelets/html/AjaxHandler.java
Log:
use CustomBehaviorHandler as base
Modified:
root/ui/core/trunk/api/src/main/java/org/richfaces/view/facelets/html/AjaxHandler.java
===================================================================
---
root/ui/core/trunk/api/src/main/java/org/richfaces/view/facelets/html/AjaxHandler.java 2010-05-17
14:41:26 UTC (rev 17083)
+++
root/ui/core/trunk/api/src/main/java/org/richfaces/view/facelets/html/AjaxHandler.java 2010-05-17
15:47:21 UTC (rev 17084)
@@ -1,6 +1,6 @@
/*
* JBoss, Home of Professional Open Source
- * Copyright 2009, Red Hat, Inc. and individual contributors
+ * Copyright ${year}, Red Hat, Inc. and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
@@ -25,113 +25,96 @@
import java.beans.BeanDescriptor;
import java.beans.BeanInfo;
import java.io.IOException;
-import java.io.Serializable;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
+import java.util.Map;
-import javax.el.ELContext;
-import javax.el.MethodExpression;
import javax.faces.application.Application;
import javax.faces.component.UIComponent;
+import javax.faces.component.behavior.Behavior;
import javax.faces.component.behavior.ClientBehavior;
import javax.faces.component.behavior.ClientBehaviorHolder;
import javax.faces.context.FacesContext;
-import javax.faces.event.AbortProcessingException;
-import javax.faces.event.AjaxBehaviorEvent;
-import javax.faces.event.AjaxBehaviorListener;
+import javax.faces.view.AttachedObjectHandler;
import javax.faces.view.AttachedObjectTarget;
-import javax.faces.view.BehaviorHolderAttachedObjectHandler;
import javax.faces.view.BehaviorHolderAttachedObjectTarget;
import javax.faces.view.facelets.BehaviorConfig;
import javax.faces.view.facelets.ComponentHandler;
import javax.faces.view.facelets.FaceletContext;
-import javax.faces.view.facelets.TagAttribute;
import javax.faces.view.facelets.TagException;
-import javax.faces.view.facelets.TagHandler;
import org.ajax4jsf.component.behavior.AjaxBehavior;
import org.richfaces.component.AbstractAttachQueue;
import org.richfaces.view.facelets.html.BehaviorStack.BehaviorInfo;
-//TODO nick - use TagHandlerDelegate
-public class AjaxHandler extends TagHandler implements
BehaviorHolderAttachedObjectHandler {
+/**
+ * @author Anton Belevich
+ *
+ */
+public class AjaxHandler extends CustomBehaviorHandler {
- private final TagAttribute disabled;
- private final TagAttribute event;
- private final TagAttribute execute;
- private final TagAttribute immediate;
- private final TagAttribute limitRender;
- private final TagAttribute listener;
- private final TagAttribute onbeforedomupdate;
- private final TagAttribute onbegin;
- private final TagAttribute oncomplete;
- private final TagAttribute onerror;
- private final TagAttribute onevent;
- private final TagAttribute render;
- private final TagAttribute similarityGroupingId;
- private final TagAttribute statusId;
-
+ private static final String JAVAX_FACES_RETARGETABLE_HANDLERS =
"javax.faces.RetargetableHandlers";
+
+
+
public AjaxHandler(BehaviorConfig config) {
- super(config);
- this.event = this.getAttribute("event");
- this.execute = this.getAttribute("execute");
- this.render = this.getAttribute("render");
- this.disabled = this.getAttribute("disabled");
- this.immediate = this.getAttribute("immediate");
- this.listener = this.getAttribute("listener");
- this.limitRender = this.getAttribute("limitRender");
- this.statusId = this.getAttribute("status");
- this.similarityGroupingId = this.getAttribute("similarityGroupingId");
- this.onevent = this.getAttribute("onevent");
- this.onerror = this.getAttribute("onerror");
- this.onbegin = this.getAttribute("onbegin");
- this.oncomplete = this.getAttribute("oncomplete");
- this.onbeforedomupdate = this.getAttribute("onbeforedomupdate");
+ super(config);
+
}
-
+
public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
String eventName = getEventName();
- AjaxBehavior ajaxBehavior = createAjaxBehavior(ctx, eventName);
- FacesContext context = ctx.getFacesContext();
- BehaviorStack ajaxBehaviors = BehaviorStack.getBehaviorStack(context, true);
- ajaxBehaviors.pushBehavior(context, ajaxBehavior, AjaxBehavior.BEHAVIOR_ID,
eventName);
-
- AttachQueueStack attachQueueStack = AttachQueueStack.getStack(context, true);
- AttachQueueInfo attachQueueInfo = new AttachQueueInfo(parent);
- attachQueueStack.push(attachQueueInfo);
-
- nextHandler.apply(ctx, parent);
-
- attachQueueStack.pop();
-
- BehaviorInfo behaviorInfo = ajaxBehaviors.popBehavior();
- if (behaviorInfo != null) {
- if (behaviorInfo.isWrapping()) {
- AbstractAttachQueue attachQueue = attachQueueInfo.getAttachQueue();
- if (attachQueue != null) {
- List<ClientBehavior> behaviors = behaviorInfo.getBehaviors();
- if (behaviors != null) {
- for (ClientBehavior behavior : behaviors) {
- if (behavior instanceof AjaxBehavior) {
- AjaxBehavior createdAjaxBehavior = (AjaxBehavior)
behavior;
- attachQueue.associateWith(createdAjaxBehavior);
+
+ Application application = ctx.getFacesContext().getApplication();
+ Behavior behavior = application.createBehavior(getBehaviorId());
+ if(behavior instanceof AjaxBehavior) {
+ setAttributes(ctx, behavior);
+ AjaxBehavior ajaxBehavior = (AjaxBehavior)behavior;
+
+ FacesContext context = ctx.getFacesContext();
+ BehaviorStack ajaxBehaviors = BehaviorStack.getBehaviorStack(context, true);
+ ajaxBehaviors.pushBehavior(context, ajaxBehavior, AjaxBehavior.BEHAVIOR_ID,
eventName);
+
+ AttachQueueStack attachQueueStack = AttachQueueStack.getStack(context,
true);
+ AttachQueueInfo attachQueueInfo = new AttachQueueInfo(parent);
+ attachQueueStack.push(attachQueueInfo);
+
+ nextHandler.apply(ctx, parent);
+
+ attachQueueStack.pop();
+
+ BehaviorInfo behaviorInfo = ajaxBehaviors.popBehavior();
+ if (behaviorInfo != null) {
+ if (behaviorInfo.isWrapping()) {
+ AbstractAttachQueue attachQueue = attachQueueInfo.getAttachQueue();
+ if (attachQueue != null) {
+ List<ClientBehavior> behaviors =
behaviorInfo.getBehaviors();
+ if (behaviors != null) {
+ for (ClientBehavior processBehavior : behaviors) {
+ if (processBehavior instanceof AjaxBehavior) {
+ AjaxBehavior createdAjaxBehavior = (AjaxBehavior)
behavior;
+ attachQueue.associateWith(createdAjaxBehavior);
+ }
}
}
}
+ } else {
+ AbstractAttachQueue attachQueue = attachQueueInfo.getAttachQueue();
+ if (attachQueue != null) {
+ attachQueue.associateWith(ajaxBehavior);
+ }
+
+ applyNested(ctx, parent, ajaxBehavior);
}
} else {
- AbstractAttachQueue attachQueue = attachQueueInfo.getAttachQueue();
- if (attachQueue != null) {
- attachQueue.associateWith(ajaxBehavior);
- }
-
- applyNested(ctx, parent, ajaxBehavior);
+ //TODO nick - is this possible?
}
- } else {
- //TODO nick - is this possible?
+
}
}
-
+
+
private void applyNested(FaceletContext ctx, UIComponent parent, AjaxBehavior
behavior) {
if (!ComponentHandler.isNew(parent)) {
return;
@@ -177,7 +160,7 @@
}
if (supportedEvent) {
- TagHandlerUtils.getOrCreateRetargetableHandlersList(parent).add(this);
+ getOrCreateRetargetableHandlersList(parent).add(this);
} else {
throw new TagException(tag, "Error: enclosing composite component
does not support event " + eventName);
}
@@ -189,7 +172,21 @@
throw new TagException(this.tag, "Unable to attach <a4j:ajax> to
non-ClientBehaviorHolder parent");
}
}
+
+ private static List<AttachedObjectHandler>
getOrCreateRetargetableHandlersList(UIComponent component) {
+ Map<String, Object> attrs = component.getAttributes();
+ @SuppressWarnings({
+ "unchecked"}) List<AttachedObjectHandler> list =
+ (List<AttachedObjectHandler>)
attrs.get(JAVAX_FACES_RETARGETABLE_HANDLERS);
+ if (list == null) {
+ list = new ArrayList<AttachedObjectHandler>();
+ attrs.put(JAVAX_FACES_RETARGETABLE_HANDLERS, list);
+ }
+
+ return list;
+ }
+
private String resolveEventName(ClientBehaviorHolder bHolder) {
String eventName = getEventName();
if (null == eventName) {
@@ -210,87 +207,4 @@
return eventName;
}
-
- public AjaxBehavior createAjaxBehavior(FaceletContext fContext, String eventName) {
- Application application = fContext.getFacesContext().getApplication();
- AjaxBehavior ajaxBehavior = (AjaxBehavior)
application.createBehavior(AjaxBehavior.BEHAVIOR_ID);
-
- setBehaviorAttribute(fContext, ajaxBehavior, this.disabled, Boolean.class);
- setBehaviorAttribute(fContext, ajaxBehavior, this.immediate, Boolean.class);
- setBehaviorAttribute(fContext, ajaxBehavior, this.execute, Object.class);
- setBehaviorAttribute(fContext, ajaxBehavior, this.render, Object.class);
- setBehaviorAttribute(fContext, ajaxBehavior, this.limitRender, Boolean.class);
- setBehaviorAttribute(fContext, ajaxBehavior, this.statusId, String.class);
- setBehaviorAttribute(fContext, ajaxBehavior, this.similarityGroupingId,
String.class);
- setBehaviorAttribute(fContext, ajaxBehavior, this.onevent, String.class);
- setBehaviorAttribute(fContext, ajaxBehavior, this.onerror, String.class);
- setBehaviorAttribute(fContext, ajaxBehavior, this.onbegin, String.class);
- setBehaviorAttribute(fContext, ajaxBehavior, this.oncomplete, String.class);
- setBehaviorAttribute(fContext, ajaxBehavior, this.onbeforedomupdate,
String.class);
- registerBehaviorListener(fContext, ajaxBehavior, listener);
-
- return ajaxBehavior;
- }
-
- public void applyAttachedObject(FacesContext context, UIComponent parent) {
- FaceletContext ctx = (FaceletContext)
context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
-
- ClientBehaviorHolder bHolder = (ClientBehaviorHolder) parent;
- String eventName = resolveEventName(bHolder);
-
- AjaxBehavior ajaxBehavior = createAjaxBehavior(ctx, eventName);
- bHolder.addClientBehavior(eventName, ajaxBehavior);
- }
-
- private void setBehaviorAttribute(FaceletContext ctx, AjaxBehavior behavior,
TagAttribute attr, Class<?> type) {
- if (attr != null) {
- behavior.setValueExpression(attr.getLocalName(), attr.getValueExpression(ctx,
type));
- }
- }
-
- public String getEventName() {
- return (this.event != null) ? this.event.getValue() : null;
- }
-
- public String getFor() {
- return null;
- }
-
- public void registerBehaviorListener(FaceletContext fContext, AjaxBehavior behavior,
TagAttribute listenerAttr) {
- if (listenerAttr != null) {
- MethodExpression mExpression = listenerAttr.getMethodExpression(fContext,
Object.class,
- new Class[] {AjaxBehaviorEvent.class});
-
- behavior.addAjaxBehaviorListener(new AjaxBehaviorListenerImpl(mExpression));
- }
- }
-
- static class AjaxBehaviorListenerImpl implements AjaxBehaviorListener, Serializable
{
- /**
- *
- */
- private static final long serialVersionUID = 1812543470662919310L;
-
- private MethodExpression methodExpression;
-
- public AjaxBehaviorListenerImpl() {}
-
- public AjaxBehaviorListenerImpl(MethodExpression expression) {
- this.methodExpression = expression;
- }
-
- public void processAjaxBehavior(AjaxBehaviorEvent event) throws
AbortProcessingException {
- if (methodExpression != null) {
- ELContext elContext = FacesContext.getCurrentInstance().getELContext();
-
- try {
- methodExpression.invoke(elContext, new Object[] {event});
- } catch (Exception e) {
-
- // TODO: log or message ??
- throw new AbortProcessingException(e);
- }
- }
- }
- }
}