Author: abelevich
Date: 2009-08-20 12:38:57 -0400 (Thu, 20 Aug 2009)
New Revision: 15240
Added:
root/ui/trunk/components/core/src/main/java/org/ajax4jsf/renderkit/AjaxBeahaviorRenderer.java
Log:
a4j:ajax Behavior Renderer
Added:
root/ui/trunk/components/core/src/main/java/org/ajax4jsf/renderkit/AjaxBeahaviorRenderer.java
===================================================================
---
root/ui/trunk/components/core/src/main/java/org/ajax4jsf/renderkit/AjaxBeahaviorRenderer.java
(rev 0)
+++
root/ui/trunk/components/core/src/main/java/org/ajax4jsf/renderkit/AjaxBeahaviorRenderer.java 2009-08-20
16:38:57 UTC (rev 15240)
@@ -0,0 +1,105 @@
+package org.ajax4jsf.renderkit;
+
+import java.util.Map;
+
+import javax.faces.application.ResourceDependencies;
+import javax.faces.application.ResourceDependency;
+import javax.faces.component.ActionSource;
+import javax.faces.component.EditableValueHolder;
+import javax.faces.component.UIComponent;
+import javax.faces.component.behavior.ClientBehavior;
+import javax.faces.component.behavior.ClientBehaviorContext;
+import javax.faces.context.FacesContext;
+import javax.faces.event.AjaxBehaviorEvent;
+import javax.faces.event.PhaseId;
+import javax.faces.render.ClientBehaviorRenderer;
+import javax.faces.render.FacesBehaviorRenderer;
+import javax.faces.render.RenderKitFactory;
+
+import org.ajax4jsf.component.behavior.AjaxBehavior;
+import org.ajax4jsf.javascript.JSFunction;
+import org.richfaces.context.PartialViewContextFactoryImpl;
+import org.richfaces.context.PartialViewContextImpl;
+
+/**
+ * @author Anton Belevich
+ *
+ */
+(a)FacesBehaviorRenderer(rendererType="org.ajax4jsf.behavior.Ajax",renderKitId=RenderKitFactory.HTML_BASIC_RENDER_KIT)
+@ResourceDependencies({
+ @ResourceDependency(name="jquery.js", target="head"),
+ @ResourceDependency(name="richfaces.js", target="head")
+})
+public class AjaxBeahaviorRenderer extends ClientBehaviorRenderer {
+ @Override
+ public void decode(FacesContext context, UIComponent component, ClientBehavior behavior)
{
+
+ if (null == context || null == component || null == behavior) {
+ throw new NullPointerException();
+ }
+
+ if (!(behavior instanceof AjaxBehavior)) {
+ throw new
IllegalArgumentException("org.ajax4jsf.component.behavior.AjaxBehavior required:
" + behavior);
+ }
+
+ AjaxBehavior ajaxBehavior = (AjaxBehavior)behavior;
+
+ if (ajaxBehavior.isDisabled()) {
+ return;
+ }
+
+ component.queueEvent(createEvent(component, ajaxBehavior));
+ }
+
+ private static AjaxBehaviorEvent createEvent(UIComponent component, AjaxBehavior
ajaxBehavior) {
+ AjaxBehaviorEvent event = new AjaxBehaviorEvent(component, ajaxBehavior);
+ PhaseId phaseId = isImmediate(component, ajaxBehavior) ?
PhaseId.APPLY_REQUEST_VALUES : PhaseId.INVOKE_APPLICATION;
+ event.setPhaseId(phaseId);
+
+ return event;
+ }
+
+ private static boolean isImmediate(UIComponent component, AjaxBehavior ajaxBehavior) {
+ boolean immediate = false;
+
+ if (ajaxBehavior.isImmediateSet()) {
+ immediate = ajaxBehavior.isImmediate();
+ } else if (component instanceof EditableValueHolder) {
+ immediate = ((EditableValueHolder)component).isImmediate();
+ } else if (component instanceof ActionSource) {
+ immediate = ((ActionSource)component).isImmediate();
+ }
+ return immediate;
+ }
+
+ @Override
+ public String getScript(ClientBehaviorContext behaviorContext, ClientBehavior behavior)
{
+ String script = null;
+ if(behavior instanceof AjaxBehavior) {
+ script = buildAjaxCommand(behaviorContext, (AjaxBehavior)behavior);
+ }
+ return script;
+ }
+
+ public String buildAjaxCommand(ClientBehaviorContext bContext, AjaxBehavior behavior) {
+ UIComponent parent = bContext.getComponent();
+ FacesContext context = bContext.getFacesContext();
+
+ JSFunction ajaxFunction = AjaxRendererUtils.buildAjaxFunction(parent, context,
"RichFaces.ajax");
+
+ Map<String, Object> options = buildOptions(context, bContext);
+ ajaxFunction.addParameter(options);
+
+ return ajaxFunction.toString();
+ }
+
+ public Map<String, Object> buildOptions(FacesContext context,
ClientBehaviorContext bContext) {
+ UIComponent parent = bContext.getComponent();
+ String eventName = bContext.getEventName();
+
+ Map<String,Object> options = AjaxRendererUtils.buildEventOptions(context,
parent);
+ options.put(PartialViewContextImpl.BEHAVIOR_EVENT_PARAMETER, eventName);
+ options.put(PartialViewContextFactoryImpl.RICHFACES_AJAX, parent.getClientId());
+ return options;
+ }
+}
Show replies by date