Author: abelevich
Date: 2009-08-18 11:30:03 -0400 (Tue, 18 Aug 2009)
New Revision: 15204
Added:
root/examples/trunk/components/core-demo/src/main/java/org/ajax4jsf/
root/examples/trunk/components/core-demo/src/main/java/org/ajax4jsf/component/
root/examples/trunk/components/core-demo/src/main/java/org/ajax4jsf/component/behavior/
root/examples/trunk/components/core-demo/src/main/java/org/ajax4jsf/component/behavior/AjaxBehavior.java
root/examples/trunk/components/core-demo/src/main/java/org/ajax4jsf/renderkit/
root/examples/trunk/components/core-demo/src/main/java/org/ajax4jsf/renderkit/AjaxBeahaviorRenderer.java
root/examples/trunk/components/core-demo/src/main/java/org/ajax4jsf/tag/
root/examples/trunk/components/core-demo/src/main/java/org/ajax4jsf/tag/AjaxHandler.java
root/examples/trunk/components/core-demo/src/main/webapp/WEB-INF/ajax-taglib.xml
root/examples/trunk/components/core-demo/src/main/webapp/pages/
root/examples/trunk/components/core-demo/src/main/webapp/pages/support.xhtml
Modified:
root/examples/trunk/components/core-demo/src/main/webapp/WEB-INF/web.xml
Log:
a4j:support behavior basic functionality
Added:
root/examples/trunk/components/core-demo/src/main/java/org/ajax4jsf/component/behavior/AjaxBehavior.java
===================================================================
---
root/examples/trunk/components/core-demo/src/main/java/org/ajax4jsf/component/behavior/AjaxBehavior.java
(rev 0)
+++
root/examples/trunk/components/core-demo/src/main/java/org/ajax4jsf/component/behavior/AjaxBehavior.java 2009-08-18
15:30:03 UTC (rev 15204)
@@ -0,0 +1,92 @@
+package org.ajax4jsf.component.behavior;
+
+import java.util.HashMap;
+
+import javax.el.ValueExpression;
+import javax.faces.component.behavior.FacesBehavior;
+import javax.faces.context.FacesContext;
+
+/**
+ * @author Anton Belevich
+ *
+ */
+(a)FacesBehavior(value="org.ajax4jsf.behavior.Ajax")
+public class AjaxBehavior extends javax.faces.component.behavior.AjaxBehavior {
+
+ public static final String BEHAVIOR_ID = "org.ajax4jsf.behavior.Ajax";
+
+ public static final String EVENT = "event";
+
+ private String event;
+
+ public String getEvent() {
+ if (this.event != null) {
+ return this.event;
+ }
+
+ ValueExpression expression = getValueExpression("event");
+
+ if (expression != null) {
+ FacesContext ctx = FacesContext.getCurrentInstance();
+ return (String)expression.getValue(ctx.getELContext());
+ }
+
+ return null;
+ }
+
+ public void setEvent(String event) {
+ this.event = event;
+ clearInitialState();
+ }
+
+ @Override
+ public Object saveState(FacesContext context) {
+
+ Object[] values;
+ Object superState = super.saveState(context);
+
+ if (initialStateMarked()) {
+ if (superState == null) {
+ values = null;
+ } else {
+ values = new Object[] { superState };
+ }
+ } else {
+ values = new Object[2];
+ values[0] = superState;
+ values[1] = this.event;
+ }
+ return values;
+ }
+
+ @Override
+ public void restoreState(FacesContext context, Object state) {
+
+ if (state != null) {
+
+ Object[] values = (Object[]) state;
+ super.restoreState(context, values[0]);
+
+ if (values.length != 1) {
+ this.event = (String)values[1];
+ clearInitialState();
+ }
+ }
+ }
+
+
+
+ public void setValueExpression(String name, ValueExpression binding) {
+ if(EVENT.equals(name) && binding.isLiteralText()) {
+ this.event =
(String)binding.getValue(FacesContext.getCurrentInstance().getELContext());
+ clearInitialState();
+ } else {
+ super.setValueExpression(name, binding);
+ }
+ }
+
+ @Override
+ public String getRendererType() {
+ return BEHAVIOR_ID;
+ }
+}
Added:
root/examples/trunk/components/core-demo/src/main/java/org/ajax4jsf/renderkit/AjaxBeahaviorRenderer.java
===================================================================
---
root/examples/trunk/components/core-demo/src/main/java/org/ajax4jsf/renderkit/AjaxBeahaviorRenderer.java
(rev 0)
+++
root/examples/trunk/components/core-demo/src/main/java/org/ajax4jsf/renderkit/AjaxBeahaviorRenderer.java 2009-08-18
15:30:03 UTC (rev 15204)
@@ -0,0 +1,69 @@
+package org.ajax4jsf.renderkit;
+
+import java.util.Map;
+
+import javax.faces.application.ResourceDependencies;
+import javax.faces.application.ResourceDependency;
+import javax.faces.component.UIComponent;
+import javax.faces.component.behavior.ClientBehavior;
+import javax.faces.component.behavior.ClientBehaviorContext;
+import javax.faces.component.behavior.ClientBehaviorHolder;
+import javax.faces.context.FacesContext;
+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.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)
{
+ System.out.println("AjaxBeahaviorRenderer.decode()");
+ }
+
+ @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 behaviorContext, AjaxBehavior
behavior) {
+ UIComponent parent = behaviorContext.getComponent();
+ FacesContext context = behaviorContext.getFacesContext();
+
+ JSFunction ajaxFunction = AjaxRendererUtils.buildAjaxFunction(parent, context,
"RichFaces.ajax");
+
+ Map<String, Object> options = buildOptions(context, behavior, parent);
+ ajaxFunction.addParameter(options);
+
+ return ajaxFunction.toString();
+ }
+
+ public Map<String, Object> buildOptions(FacesContext context, AjaxBehavior
behavior, UIComponent parent) {
+ Map<String,Object> options = AjaxRendererUtils.buildEventOptions(context,
parent);
+ String event = behavior.getEvent();
+ if(event == null) {
+ ClientBehaviorHolder bHolder = (ClientBehaviorHolder)parent;
+ event = bHolder.getDefaultEventName();
+ //TODO: if default event name == null (log or throw exception ???)
+ }
+ options.put(PartialViewContextImpl.BEHAVIOR_EVENT_PARAMETER, event);
+ options.put("execute", behavior.getExecute());
+ options.put("render", behavior.getExecute());
+ return options;
+ }
+}
Added:
root/examples/trunk/components/core-demo/src/main/java/org/ajax4jsf/tag/AjaxHandler.java
===================================================================
---
root/examples/trunk/components/core-demo/src/main/java/org/ajax4jsf/tag/AjaxHandler.java
(rev 0)
+++
root/examples/trunk/components/core-demo/src/main/java/org/ajax4jsf/tag/AjaxHandler.java 2009-08-18
15:30:03 UTC (rev 15204)
@@ -0,0 +1,120 @@
+package org.ajax4jsf.tag;
+
+import java.io.IOException;
+import java.util.Collection;
+
+import javax.faces.application.Application;
+import javax.faces.component.UIComponent;
+import javax.faces.component.behavior.ClientBehaviorHolder;
+import javax.faces.context.FacesContext;
+import javax.faces.view.BehaviorHolderAttachedObjectHandler;
+import javax.faces.view.facelets.BehaviorConfig;
+import javax.faces.view.facelets.CompositeFaceletHandler;
+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;
+
+/**
+ * @author Anton Belevich
+ *
+ */
+
+public class AjaxHandler extends TagHandler implements
BehaviorHolderAttachedObjectHandler {
+
+ private final TagAttribute event;
+ private final TagAttribute execute;
+ private final TagAttribute render;
+ private final TagAttribute onevent;
+ private final TagAttribute onerror;
+ private final TagAttribute disabled;
+ private final TagAttribute immediate;
+ private final TagAttribute listener;
+
+ public AjaxHandler(BehaviorConfig config) {
+ super(config);
+ this.event = this.getAttribute("event");
+ this.execute = this.getAttribute("execute");
+ this.render = this.getAttribute("render");
+ this.onevent = this.getAttribute("onevent");
+ this.onerror = this.getAttribute("onerror");
+ this.disabled = this.getAttribute("disabled");
+ this.immediate = this.getAttribute("immediate");
+ this.listener = this.getAttribute("listener");
+ }
+
+ public void apply(FaceletContext context, UIComponent parent) throws IOException {
+ String eventName = getEventName();
+ if(!isWrapping()) {
+ applyAttachedObject(context, parent, eventName);
+ } else {
+ this.nextHandler.apply(context, parent);
+ }
+ }
+
+ public void applyAttachedObject(FaceletContext context, UIComponent parent, String
eventName) {
+ ClientBehaviorHolder bHolder = (ClientBehaviorHolder) parent;
+ if (null == eventName) {
+ eventName = bHolder.getDefaultEventName();
+ if (null == eventName) {
+ throw new TagException(this.tag, "Event attribute could not be determined:
" + eventName);
+ }
+ } else {
+ Collection<String> eventNames = bHolder.getEventNames();
+ if (!eventNames.contains(eventName)) {
+ throw new TagException(this.tag, eventName + "event is not supported for
the " + parent.getClass().getSimpleName());
+ }
+ }
+ AjaxBehavior ajaxBehavior = createBehavior(context, parent, eventName);
+ bHolder.addClientBehavior(eventName, ajaxBehavior);
+ }
+
+
+ public AjaxBehavior createBehavior(FaceletContext context, UIComponent parent, String
eventName) {
+ Application application = context.getFacesContext().getApplication();
+ AjaxBehavior ajaxBehavior =
(AjaxBehavior)application.createBehavior(AjaxBehavior.BEHAVIOR_ID);
+
+ setBehaviorAttribute(context, ajaxBehavior, this.event, String.class);
+ setBehaviorAttribute(context, ajaxBehavior, this.onevent, String.class);
+ setBehaviorAttribute(context, ajaxBehavior, this.onerror, String.class);
+ setBehaviorAttribute(context, ajaxBehavior, this.disabled, Boolean.class);
+ setBehaviorAttribute(context, ajaxBehavior, this.immediate, Boolean.class);
+ setBehaviorAttribute(context, ajaxBehavior, this.execute, Object.class);
+ setBehaviorAttribute(context, ajaxBehavior, this.render, Object.class);
+
+ if (null != listener) {
+// behavior.addAjaxBehaviorListener(new AjaxBehaviorListenerImpl(
+// this.listener.getMethodExpression(ctx, Object.class, new Class[] {
AjaxBehaviorEvent.class }),
+// this.listener.getMethodExpression(ctx, Object.class, new Class[] { })));
+ }
+
+ return ajaxBehavior;
+ }
+
+ public void applyAttachedObject(FacesContext context, UIComponent parent) {
+ FaceletContext ctx = (FaceletContext)
context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
+ applyAttachedObject(ctx, parent, getEventName());
+ }
+
+ 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;
+ }
+
+ private boolean isWrapping() {
+ return ((this.nextHandler instanceof TagHandler) || (this.nextHandler instanceof
CompositeFaceletHandler));
+ }
+
+}
Added: root/examples/trunk/components/core-demo/src/main/webapp/WEB-INF/ajax-taglib.xml
===================================================================
--- root/examples/trunk/components/core-demo/src/main/webapp/WEB-INF/ajax-taglib.xml
(rev 0)
+++
root/examples/trunk/components/core-demo/src/main/webapp/WEB-INF/ajax-taglib.xml 2009-08-18
15:30:03 UTC (rev 15204)
@@ -0,0 +1,14 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<facelet-taglib
xmlns="http://java.sun.com/xml/ns/javaee"
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
+ version="2.0">
+ <
namespace>http://richfaces.org/a4j/behavior</namespace>
+ <tag>
+ <tag-name>ajax</tag-name>
+ <behavior>
+ <behavior-id>org.ajax4jsf.behavior.Ajax</behavior-id>
+ <handler-class>org.ajax4jsf.tag.AjaxHandler</handler-class>
+ </behavior>
+ </tag>
+</facelet-taglib>
Modified: root/examples/trunk/components/core-demo/src/main/webapp/WEB-INF/web.xml
===================================================================
--- root/examples/trunk/components/core-demo/src/main/webapp/WEB-INF/web.xml 2009-08-18
14:04:15 UTC (rev 15203)
+++ root/examples/trunk/components/core-demo/src/main/webapp/WEB-INF/web.xml 2009-08-18
15:30:03 UTC (rev 15204)
@@ -19,6 +19,11 @@
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
+
+ <context-param>
+ <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
+ <param-value>/WEB-INF/ajax-taglib.xml</param-value>
+ </context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
Added: root/examples/trunk/components/core-demo/src/main/webapp/pages/support.xhtml
===================================================================
--- root/examples/trunk/components/core-demo/src/main/webapp/pages/support.xhtml
(rev 0)
+++
root/examples/trunk/components/core-demo/src/main/webapp/pages/support.xhtml 2009-08-18
15:30:03 UTC (rev 15204)
@@ -0,0 +1,38 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html
xmlns="http://www.w3.org/1999/xhtml"
+
xmlns:f="http://java.sun.com/jsf/core"
+
xmlns:h="http://java.sun.com/jsf/html"
+
xmlns:ui="http://java.sun.com/jsf/facelets"
+
xmlns:a="http://richfaces.org/a4j/behavior">
+ <h:head>
+ Richfaces Ajax Sample
+ </h:head>
+ <h:body>
+ <h:form>
+ <h:panelGrid columns="2">
+ <h:inputText value="#{sampleBean.firstText}">
+ <f:ajax event="keyup" render="firstOut"/>
+ </h:inputText>
+ <h:outputText id="firstOut" value="#{sampleBean.firstText}"
/>
+
+ <h:inputText value="#{sampleBean.secondText}">
+ <f:ajax event="keyup" render="secondOut"/>
+ </h:inputText>
+ <h:outputText id="secondOut" value="#{sampleBean.secondText}"
/>
+ </h:panelGrid>
+
+ <h:panelGrid columns="2">
+ <h:inputText value="#{sampleBean.thirdText}">
+ <a:ajax/>
+ </h:inputText>
+ <h:outputText id="thirdOut" value="#{sampleBean.thirdText}"
/>
+
+ <h:inputText value="#{sampleBean.fourthText}" >
+ <a:ajax event="keyup" execute="fourthOut"
render="fourthOut"/>
+ </h:inputText>
+ <h:outputText id="fourthOut" value="#{sampleBean.fourthText}"
/>
+ </h:panelGrid>
+
+ </h:form>
+ </h:body>
+</html>
\ No newline at end of file