Author: abelevich
Date: 2010-05-17 08:26:02 -0400 (Mon, 17 May 2010)
New Revision: 17073
Added:
root/ui/core/trunk/api/src/main/java/org/ajax4jsf/component/behavior/BehaviorStateHelper.java
root/ui/core/trunk/api/src/main/java/org/ajax4jsf/component/behavior/ClientBehavior.java
root/ui/core/trunk/api/src/main/java/org/richfaces/view/facelets/html/BehaviorTagHandlerDelegate.java
root/ui/core/trunk/api/src/main/java/org/richfaces/view/facelets/html/CustomBehaviorHandler.java
root/ui/core/trunk/api/src/main/java/org/richfaces/view/facelets/tag/
root/ui/core/trunk/api/src/main/java/org/richfaces/view/facelets/tag/BehaviorRule.java
Modified:
root/ui/core/trunk/api/src/main/java/org/ajax4jsf/component/behavior/AjaxBehavior.java
root/ui/core/trunk/api/src/main/java/org/ajax4jsf/renderkit/AjaxBehaviorRenderer.java
Log:
RF-8635, add base classes for the behaviors
Modified:
root/ui/core/trunk/api/src/main/java/org/ajax4jsf/component/behavior/AjaxBehavior.java
===================================================================
---
root/ui/core/trunk/api/src/main/java/org/ajax4jsf/component/behavior/AjaxBehavior.java 2010-05-17
11:49:37 UTC (rev 17072)
+++
root/ui/core/trunk/api/src/main/java/org/ajax4jsf/component/behavior/AjaxBehavior.java 2010-05-17
12:26:02 UTC (rev 17073)
@@ -1,3 +1,25 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
package org.ajax4jsf.component.behavior;
import java.util.Collection;
@@ -2,8 +24,7 @@
import java.util.Collections;
+import java.util.EnumSet;
import java.util.Set;
-import javax.el.ELContext;
-import javax.el.ValueExpression;
-import javax.faces.component.UIComponentBase;
-import javax.faces.context.FacesContext;
+import javax.faces.FacesException;
+import javax.faces.component.behavior.ClientBehaviorHint;
@@ -15,374 +36,248 @@
import org.richfaces.cdk.annotations.Tag;
import org.richfaces.cdk.annotations.TagType;
+
/**
* @author Anton Belevich
+ *
*/
+
@JsfBehavior(
- id = "org.ajax4jsf.behavior.Ajax",
- tag = @Tag(name = "ajax", handler =
"org.richfaces.view.facelets.html.AjaxHandler", type = TagType.Facelets)
+ id = "org.ajax4jsf.behavior.Ajax",
+ tag = @Tag(name = "ajax", handler =
"org.richfaces.view.facelets.html.AjaxHandler", type = TagType.Facelets)
)
-public class AjaxBehavior extends javax.faces.component.behavior.AjaxBehavior implements
AjaxClientBehavior {
+public class AjaxBehavior extends ClientBehavior implements AjaxClientBehavior {
+
public static final String BEHAVIOR_ID = "org.ajax4jsf.behavior.Ajax";
- private Set<String> execute;
- private Boolean limitRender;
- private String onbeforedomupdate;
- private String onbegin;
- private String oncomplete;
- private Object data;
- private String queueId;
- private Set<String> render;
- private String similarityGroupingId;
- private String statusId;
+
+ private static final Set<ClientBehaviorHint> HINTS =
+ Collections.unmodifiableSet(EnumSet.of(ClientBehaviorHint.SUBMITTING));
+
+
+ enum PropertyKeys {
+ data, execute, onbeforedomupdate, onbegin, oncomplete, onerror, onevent,
+ queueId, render, similarityGroupingId, status, disabled, limitRender, immediate
+ }
+
+ private static final Set<String> ALL_LIST =
Collections.singleton("@all");
+ private static final Set<String> FORM_LIST =
Collections.singleton("@form");
+ private static final Set<String> THIS_LIST =
Collections.singleton("@this");
+ private static final Set<String> REGION_LIST =
Collections.singleton("@region");
+ private static final Set<String> NONE_LIST =
Collections.singleton("@none");
- private static enum Attributes {
- limitRender, queueId, status, render, execute, similarityGroupingId, oncomplete,
onbegin, onbeforedomupdate,
- other, data;
-
- public static Attributes toAttribute(String name) {
- try {
- return valueOf(name);
- } catch (Exception ex) {
- return other;
+ @Override
+ public void setLiteralAttribute(String name, Object value) {
+ if (compare(PropertyKeys.data, name)) {
+ setData(value);
+ } else if (compare(PropertyKeys.execute, name)) {
+ Set<String> set = toSet(name, value);
+ if(set != null) {
+ setExecute(set);
+ } else {
+ throw new FacesException(value
+ + " : '"
+ + name
+ + "' attribute value must be either a String or a
Collection");
}
+
+ } else if (compare(PropertyKeys.render, name)) {
+ Set<String> set = toSet(name, value);
+ if(set != null) {
+ setRender(set);
+ } else {
+ throw new FacesException(value
+ + " : '"
+ + name
+ + "' attribute value must be either a String or a
Collection");
+ }
+
+ } else if (compare(PropertyKeys.onbeforedomupdate, name)) {
+ setOnbeforedomupdate((String)value);
+ } else if (compare(PropertyKeys.onbegin, name)) {
+ setOnbegin((String)value);
+ } else if (compare(PropertyKeys.oncomplete, name)) {
+ setOncomplete((String)value);
+ } else if (compare(PropertyKeys.onerror, name)) {
+ setOnerror((String)value);
+ } else if (compare(PropertyKeys.onevent, name)) {
+ setOnevent((String)value);
+ } else if (compare(PropertyKeys.queueId, name)) {
+ setQueueId((String)value);
+ } else if (compare(PropertyKeys.similarityGroupingId, name)) {
+ setSimilarityGroupingId((String)value);
+ } else if (compare(PropertyKeys.status, name)) {
+ setStatus((String)value);
+ } else if (compare(PropertyKeys.disabled, name)) {
+ setDisabled((Boolean)value);
+ } else if (compare(PropertyKeys.limitRender, name)) {
+ setLimitRender((Boolean)value);
+ } else if(compare(PropertyKeys.immediate, name)) {
+ setImmediate((Boolean)value);
}
}
- protected Set<String> asSet(Object render) {
- return AjaxRendererUtils.asSet(render);
+ private Set<String> toSet(String propertyName, Object value) {
+ if (value instanceof String) {
+ String strValue = (String)value;
+ if (strValue.indexOf(' ') == -1) {
+ return toSingleton(propertyName, strValue);
+ }
+ return AjaxRendererUtils.asSet(strValue);
+ }
+ return null;
}
-
- public void setOnbegin(String onbegin) {
- this.onbegin = onbegin;
- clearInitialState();
- }
-
- public String getOnbegin() {
- if (this.onbegin != null) {
- return this.onbegin;
+
+ private static Set <String> toSingleton(String propertyName, String value) {
+ if ((null == value) || (value.length() == 0)) {
+ return null;
}
- ValueExpression ve = getValueExpression(Attributes.onbegin.toString());
+ if (value.charAt(0) == '@') {
+ Set<String> list;
- if (ve != null) {
- ELContext elContext = FacesContext.getCurrentInstance().getELContext();
-
- return (String) ve.getValue(elContext);
+ if (AjaxRendererUtils.ALL.equals(value)) {
+ list = ALL_LIST;
+ } else if (AjaxRendererUtils.FORM.equals(value)){
+ list = FORM_LIST;
+ } else if (AjaxRendererUtils.THIS.equals(value)) {
+ list = THIS_LIST;
+ } else if (AjaxRendererUtils.REGION.equals(value)) {
+ list = REGION_LIST;
+ } else if (AjaxRendererUtils.NONE.equals(value)) {
+ list = NONE_LIST;
+ } else {
+ throw new FacesException(value
+ + " : Invalid id keyword specified for
'"
+ + propertyName
+ + "' attribute");
+ }
+
+ return list;
}
+ return Collections.singleton(value);
+ }
+
+ public Object getData() {
+ return getStateHelper().eval(PropertyKeys.data);
+ }
+
+ public void setData(Object data) {
+ getStateHelper().put(PropertyKeys.data, data);
+ }
- return this.onbegin;
+ public Collection<String> getExecute() {
+ return (Collection<String>)getStateHelper().eval(PropertyKeys.execute);
}
- public void setOnbeforedomupdate(String onbeforedomupdate) {
- this.onbeforedomupdate = onbeforedomupdate;
- clearInitialState();
+ public void setExecute(Collection<String> execute) {
+ getStateHelper().put(PropertyKeys.execute,execute);
}
public String getOnbeforedomupdate() {
- if (this.onbeforedomupdate != null) {
- return this.onbeforedomupdate;
- }
+ return (String)getStateHelper().eval(PropertyKeys.onbeforedomupdate);
+ }
- ValueExpression ve =
getValueExpression(Attributes.onbeforedomupdate.toString());
-
- if (ve != null) {
- ELContext elContext = FacesContext.getCurrentInstance().getELContext();
-
- return (String) ve.getValue(elContext);
- }
-
- return this.onbeforedomupdate;
+ public void setOnbeforedomupdate(String onbeforedomupdate) {
+ getStateHelper().put(PropertyKeys.onbeforedomupdate,onbeforedomupdate);
}
- public void setOncomplete(String oncomplete) {
- this.oncomplete = oncomplete;
- clearInitialState();
+ public String getOnbegin() {
+ return (String)getStateHelper().eval(PropertyKeys.onbegin);
}
+
+ public void setOnbegin(String onbegin) {
+ getStateHelper().put(PropertyKeys.onbegin, onbegin);
+ }
public String getOncomplete() {
- if (this.oncomplete != null) {
- return this.oncomplete;
- }
-
- ValueExpression ve = getValueExpression(Attributes.oncomplete.toString());
-
- if (ve != null) {
- ELContext elContext = FacesContext.getCurrentInstance().getELContext();
-
- return (String) ve.getValue(elContext);
- }
-
- return this.oncomplete;
+ return (String)getStateHelper().eval(PropertyKeys.oncomplete);
}
-
- public void setData(Object data) {
- this.data = data;
- clearInitialState();
+
+ public void setOncomplete(String oncomplete) {
+ getStateHelper().put(PropertyKeys.oncomplete,oncomplete);
}
- public Object getData() {
- if (this.data != null) {
- return this.data;
- }
-
- ValueExpression ve = getValueExpression(Attributes.data.toString());
-
- if (ve != null) {
- ELContext elContext = FacesContext.getCurrentInstance().getELContext();
-
- return ve.getValue(elContext);
- }
-
- return this.data;
+ public String getOnerror() {
+ return (String)getStateHelper().eval(PropertyKeys.onerror);
}
-
- @Override
- public void setRender(Collection<String> render) {
- this.render = asSet(render);
- clearInitialState();
+
+ public void setOnerror(String onerror) {
+ getStateHelper().put(PropertyKeys.onerror,onerror);
}
- @Override
- public Collection<String> getRender() {
- return getCollectionValue(render, Attributes.render.toString());
+ public String getOnevent() {
+ return (String)getStateHelper().eval(PropertyKeys.onevent);
}
-
- @Override
- public void setExecute(Collection<String> execute) {
- this.execute = asSet(execute);
- clearInitialState();
+
+ public void setOnevent(String onevent) {
+ getStateHelper().put(PropertyKeys.onevent,onevent);
}
- @Override
- public Collection<String> getExecute() {
- return getCollectionValue(execute, Attributes.execute.toString());
+ public String getQueueId() {
+ return (String)getStateHelper().eval(PropertyKeys.queueId);
}
+
+ public void setQueueId(String queueId) {
+ getStateHelper().put(PropertyKeys.queueId,queueId);
+ }
- protected Collection<String> getCollectionValue(Collection<String>
collection, String attribute) {
- if (collection != null) {
- return collection;
- }
-
- ValueExpression ve = getValueExpression(attribute);
-
- if (ve != null) {
- FacesContext context = FacesContext.getCurrentInstance();
- Object value = ve.getValue(context.getELContext());
-
- return asSet(value);
- }
-
- return Collections.emptyList();
+ public Collection<String> getRender() {
+ return (Collection<String>)getStateHelper().eval(PropertyKeys.render);
}
+
+ public void setRender(Collection<String> render) {
+ getStateHelper().put(PropertyKeys.render, render);
+ }
public String getSimilarityGroupingId() {
- if (this.similarityGroupingId != null) {
- return this.similarityGroupingId;
- }
-
- ValueExpression ve =
getValueExpression(Attributes.similarityGroupingId.toString());
-
- if (ve != null) {
- ELContext elContext = FacesContext.getCurrentInstance().getELContext();
-
- return (String) ve.getValue(elContext);
- }
-
- return this.similarityGroupingId;
+ return (String)getStateHelper().eval(PropertyKeys.similarityGroupingId);
}
public void setSimilarityGroupingId(String similarityGroupingId) {
- this.similarityGroupingId = similarityGroupingId;
- clearInitialState();
+ getStateHelper().put(PropertyKeys.similarityGroupingId, similarityGroupingId);
}
public String getStatus() {
- if (this.statusId != null) {
- return this.statusId;
- }
-
- ValueExpression ve = getValueExpression(Attributes.status.toString());
-
- if (ve != null) {
- ELContext elContext = FacesContext.getCurrentInstance().getELContext();
-
- return (String) ve.getValue(elContext);
- }
-
- return this.statusId;
+ return (String)getStateHelper().eval(PropertyKeys.status);
}
-
- public void setStatus(String statusId) {
- this.statusId = statusId;
- clearInitialState();
+
+ public void setStatus(String status) {
+ getStateHelper().put(PropertyKeys.status, status);
}
-
- public String getQueueId() {
- if (this.queueId != null) {
- return this.queueId;
- }
-
- ValueExpression ve = getValueExpression(Attributes.queueId.toString());
-
- if (ve != null) {
- ELContext elContext = FacesContext.getCurrentInstance().getELContext();
-
- return (String) ve.getValue(elContext);
- }
-
- return this.queueId;
+
+ public boolean isDisabled() {
+ return (Boolean)getStateHelper().eval(PropertyKeys.disabled, false);
}
-
- public void setQueueId(String queueId) {
- this.queueId = queueId;
- clearInitialState();
- }
- public boolean isLimitRender() {
- if (this.limitRender != null) {
- return this.limitRender.booleanValue();
- }
+ public void setDisabled(boolean disabled) {
+ getStateHelper().put(PropertyKeys.disabled, disabled);
+ }
- ValueExpression ve = getValueExpression(Attributes.limitRender.toString());
-
- if (ve != null) {
- ELContext elContext = FacesContext.getCurrentInstance().getELContext();
- Boolean value = (Boolean) ve.getValue(elContext);
-
- return value != null ? value.booleanValue() : false;
- }
-
- return false;
+ public boolean isLimitRender() {
+ return (Boolean)getStateHelper().eval(PropertyKeys.limitRender, false);
}
public void setLimitRender(boolean limitRender) {
- this.limitRender = limitRender;
- clearInitialState();
+ getStateHelper().put(PropertyKeys.limitRender, limitRender);
}
+
+ public boolean isImmediate(){
+ return (Boolean)getStateHelper().eval(PropertyKeys.immediate, false);
+ }
+ public void setImmediate(boolean immediate) {
+ getStateHelper().put(PropertyKeys.limitRender, immediate);
+ }
+
@Override
public String getRendererType() {
return BEHAVIOR_ID;
}
-
+
@Override
- public void setValueExpression(String name, ValueExpression expression) {
- if (expression != null && expression.isLiteralText()) {
- Object value = getExpressionLiteralValue(expression);
-
- switch (Attributes.toAttribute(name)) {
- case limitRender:
- this.limitRender = value != null ? (Boolean) value : Boolean.FALSE;
-
- break;
-
- case execute:
- this.execute = value != null ? asSet(value) : null;
-
- break;
-
- case render:
- this.render = value != null ? asSet(value) : null;
-
- break;
-
- case queueId:
- this.queueId = value != null ? (String) value : null;
-
- break;
-
- case status:
- this.statusId = value != null ? (String) value : null;
-
- break;
-
- case similarityGroupingId:
- this.similarityGroupingId = value != null ? (String) value : null;
-
- break;
-
- case onbeforedomupdate:
- this.onbeforedomupdate = value != null ? (String) value : null;
-
- break;
-
- case onbegin:
- this.onbegin = value != null ? (String) value : null;
-
- break;
-
- case oncomplete:
- this.oncomplete = value != null ? (String) value : null;
-
- break;
-
- case data:
- this.data = value != null ? value : null;
-
- break;
-
- default:
- break;
- }
- }
-
- super.setValueExpression(name, expression);
+ public Set<ClientBehaviorHint> getHints() {
+ return HINTS;
}
+}
- protected Object getExpressionLiteralValue(ValueExpression expression) {
- return expression != null ?
expression.getValue(FacesContext.getCurrentInstance().getELContext()) : null;
- }
- @Override
- public Object saveState(FacesContext context) {
- Object superState = super.saveState(context);
- Object[] values;
-
- if (initialStateMarked()) {
- if (superState == null) {
- values = null;
- } else {
- values = new Object[]{superState};
- }
- } else {
- values = new Object[11];
- values[0] = superState;
- values[1] = limitRender;
- values[2] = execute;
- values[3] = render;
- values[4] = queueId;
- values[5] = statusId;
- values[6] = similarityGroupingId;
- values[7] = onbeforedomupdate;
- values[8] = onbegin;
- values[9] = oncomplete;
- values[10] = UIComponentBase.saveAttachedState(context, data);
- }
-
- 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.limitRender = (Boolean) values[1];
- this.execute = asSet(values[2]);
- this.render = asSet(values[3]);
- this.queueId = (String) values[4];
- this.statusId = (String) values[5];
- this.similarityGroupingId = (String) values[6];
- this.onbeforedomupdate = (String) values[7];
- this.onbegin = (String) values[8];
- this.oncomplete = (String) values[9];
- this.data = UIComponentBase.restoreAttachedState(context, values[10]);
- clearInitialState();
- }
- }
- }
-
-}
Added:
root/ui/core/trunk/api/src/main/java/org/ajax4jsf/component/behavior/BehaviorStateHelper.java
===================================================================
---
root/ui/core/trunk/api/src/main/java/org/ajax4jsf/component/behavior/BehaviorStateHelper.java
(rev 0)
+++
root/ui/core/trunk/api/src/main/java/org/ajax4jsf/component/behavior/BehaviorStateHelper.java 2010-05-17
12:26:02 UTC (rev 17073)
@@ -0,0 +1,52 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * 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.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.ajax4jsf.component.behavior;
+
+import javax.el.ValueExpression;
+import javax.faces.context.FacesContext;
+
+import org.richfaces.PartialStateHolderHelper;
+
+/**
+ * @author Anton Belevich
+ *
+ */
+public class BehaviorStateHelper extends PartialStateHolderHelper {
+
+ private ClientBehavior behavior;
+
+ public BehaviorStateHelper(ClientBehavior behavior) {
+ super(behavior);
+ this.behavior = behavior;
+ }
+
+ @Override
+ protected Object getValueExpressionValue(String name) {
+ Object retVal = null;
+ ValueExpression ve = behavior.getValueExpression(name);
+ if (ve != null) {
+ retVal = ve.getValue(FacesContext.getCurrentInstance().getELContext());
+ }
+ return retVal;
+ }
+}
Added:
root/ui/core/trunk/api/src/main/java/org/ajax4jsf/component/behavior/ClientBehavior.java
===================================================================
---
root/ui/core/trunk/api/src/main/java/org/ajax4jsf/component/behavior/ClientBehavior.java
(rev 0)
+++
root/ui/core/trunk/api/src/main/java/org/ajax4jsf/component/behavior/ClientBehavior.java 2010-05-17
12:26:02 UTC (rev 17073)
@@ -0,0 +1,192 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * 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.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.ajax4jsf.component.behavior;
+
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.el.ELContext;
+import javax.el.ELException;
+import javax.el.ValueExpression;
+import javax.faces.FacesException;
+import javax.faces.component.StateHelper;
+import javax.faces.component.UIComponentBase;
+import javax.faces.component.behavior.ClientBehaviorBase;
+import javax.faces.context.FacesContext;
+
+/**
+ * @author Anton Belevich
+ *
+ * base class for the client behaviors
+ */
+public abstract class ClientBehavior extends ClientBehaviorBase {
+
+ private StateHelper behaviorStateHelper = null;
+
+ private Map<String, ValueExpression> bindings;
+
+ public StateHelper getStateHelper() {
+ if (behaviorStateHelper == null) {
+ behaviorStateHelper = new BehaviorStateHelper(this);
+ }
+ return behaviorStateHelper;
+ }
+
+ public ValueExpression getValueExpression(String name) {
+ if (name == null) {
+ throw new NullPointerException();
+ }
+
+ return ((bindings == null) ? null : bindings.get(name));
+ }
+
+ public void setValueExpression(String name, ValueExpression valueExpression) {
+ if (name == null) {
+ throw new NullPointerException();
+ }
+
+ if (valueExpression != null) {
+
+ if (valueExpression.isLiteralText()) {
+ setLiteralValue(name, valueExpression);
+ } else {
+ if (bindings == null) {
+ bindings = new HashMap<String, ValueExpression>(6, 1.0f);
+ }
+ bindings.put(name, valueExpression);
+ }
+
+ } else {
+ if (bindings != null) {
+ bindings.remove(name);
+ if (bindings.isEmpty()) {
+ bindings = null;
+ }
+ }
+ }
+ clearInitialState();
+ }
+
+ protected FacesContext getFacesContext() {
+ return FacesContext.getCurrentInstance();
+ }
+
+ public void setLiteralValue(String name, ValueExpression expression) {
+ assert (expression.isLiteralText());
+ ELContext context = getFacesContext().getELContext();
+
+ try {
+ setLiteralAttribute(name, expression.getValue(context));
+ } catch (ELException elem) {
+ throw new FacesException(elem);
+ }
+ }
+
+ public abstract void setLiteralAttribute(String name, Object value);
+
+ @Override
+ public Object saveState(FacesContext context) {
+ Object[] state = null;
+
+ Object parentState = super.saveState(context);
+ if (initialStateMarked()) {
+ if (parentState != null) {
+ state = new Object[] { parentState };
+ }
+ } else {
+ state = new Object[3];
+ state[0] = parentState;
+
+ Object attrs = null;
+ if (behaviorStateHelper != null) {
+ attrs = behaviorStateHelper.saveState(context);
+ }
+ state[1] = attrs;
+ state[2] = saveBindings(context, bindings);
+
+ }
+ return state;
+ }
+
+ @Override
+ public void restoreState(FacesContext context, Object state) {
+ if (state != null) {
+ Object[] stateObject = (Object[]) state;
+ super.restoreState(context, stateObject[0]);
+ if (stateObject.length > 1) {
+ if (behaviorStateHelper != null) {
+ behaviorStateHelper.restoreState(context, stateObject[1]);
+ }
+ }
+ restoreBindings(context, stateObject[2]);
+ }
+ }
+
+ // Utility for saving bindings state
+ private static Object saveBindings(FacesContext context, Map<String,
ValueExpression> bindings) {
+
+ if (bindings == null) {
+ return (null);
+ }
+
+ Object[] values = new Object[2];
+ values[0] = bindings.keySet().toArray(new String[bindings.size()]);
+
+ Object[] bindingValues = bindings.values().toArray();
+ for (int i = 0; i < bindingValues.length; i++) {
+ bindingValues[i] = UIComponentBase.saveAttachedState(context,
bindingValues[i]);
+ }
+
+ values[1] = bindingValues;
+
+ return (values);
+ }
+
+ // Utility for restoring bindings from state
+ private static Map<String, ValueExpression> restoreBindings(FacesContext
context, Object state) {
+
+ if (state == null) {
+ return (null);
+ }
+
+ Object[] values = (Object[]) state;
+ String[] names = (String[]) values[0];
+ Object[] states = (Object[]) values[1];
+ Map<String, ValueExpression> bindings = new HashMap<String,
ValueExpression>(names.length);
+ for (int i = 0; i < names.length; i++) {
+ bindings.put(names[i], (ValueExpression)
UIComponentBase.restoreAttachedState(context, states[i]));
+ }
+ return (bindings);
+ }
+
+ protected boolean compare(Serializable key, String name) {
+ boolean retValue = false;
+ if (key != null) {
+ String keyName = key.toString();
+ retValue = keyName.equals(name);
+ }
+ return retValue;
+ }
+
+}
Modified:
root/ui/core/trunk/api/src/main/java/org/ajax4jsf/renderkit/AjaxBehaviorRenderer.java
===================================================================
---
root/ui/core/trunk/api/src/main/java/org/ajax4jsf/renderkit/AjaxBehaviorRenderer.java 2010-05-17
11:49:37 UTC (rev 17072)
+++
root/ui/core/trunk/api/src/main/java/org/ajax4jsf/renderkit/AjaxBehaviorRenderer.java 2010-05-17
12:26:02 UTC (rev 17073)
@@ -1,3 +1,25 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
package org.ajax4jsf.renderkit;
import javax.faces.application.ResourceDependencies;
@@ -24,9 +46,13 @@
*/
@FacesBehaviorRenderer(rendererType = "org.ajax4jsf.behavior.Ajax",
renderKitId = RenderKitFactory.HTML_BASIC_RENDER_KIT)
-@ResourceDependencies({@ResourceDependency(library = "javax.faces", name =
"jsf.js") ,
- @ResourceDependency(name = "jquery.js", target =
"head") ,
- @ResourceDependency(name = "richfaces.js", target =
"head") })
+
+@ResourceDependencies(
+ {@ResourceDependency(library = "javax.faces", name = "jsf.js") ,
+ @ResourceDependency(name = "jquery.js", target = "head") ,
@ResourceDependency(name = "richfaces.js", target = "head"),
@ResourceDependency(name="richfaces-queue.js")
+ }
+)
+
public class AjaxBehaviorRenderer extends ClientBehaviorRenderer {
@Override
public void decode(FacesContext context, UIComponent component, ClientBehavior
behavior) {
@@ -58,23 +84,21 @@
}
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();
+ boolean immediate = ajaxBehavior.isImmediate();
+ if(!immediate) {
+ 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);
}
@@ -88,9 +112,7 @@
JSFunction ajaxFunction = AjaxRendererUtils.buildAjaxFunction(context, parent,
AjaxRendererUtils.AJAX_FUNCTION_NAME);
AjaxEventOptions options = buildOptions(context, bContext, behavior);
-
ajaxFunction.addParameter(options);
-
return ajaxFunction.toString();
}
@@ -99,10 +121,7 @@
UIComponent parent = bContext.getComponent();
String eventName = bContext.getEventName();
AjaxEventOptions options = AjaxRendererUtils.buildEventOptions(context, parent,
behavior);
-
options.setParameter(AjaxRendererUtils.BEHAVIOR_EVENT_PARAMETER, eventName);
- options.setParameter(AjaxRendererUtils.AJAX_COMPONENT_ID_PARAMETER,
parent.getClientId());
-
return options;
}
}
Added:
root/ui/core/trunk/api/src/main/java/org/richfaces/view/facelets/html/BehaviorTagHandlerDelegate.java
===================================================================
---
root/ui/core/trunk/api/src/main/java/org/richfaces/view/facelets/html/BehaviorTagHandlerDelegate.java
(rev 0)
+++
root/ui/core/trunk/api/src/main/java/org/richfaces/view/facelets/html/BehaviorTagHandlerDelegate.java 2010-05-17
12:26:02 UTC (rev 17073)
@@ -0,0 +1,103 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * 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.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.richfaces.view.facelets.html;
+
+import java.io.IOException;
+
+import javax.faces.application.Application;
+import javax.faces.component.UIComponent;
+import javax.faces.component.behavior.Behavior;
+import javax.faces.context.FacesContext;
+import javax.faces.view.AttachedObjectHandler;
+import javax.faces.view.facelets.FaceletContext;
+import javax.faces.view.facelets.MetaRuleset;
+import javax.faces.view.facelets.TagHandlerDelegate;
+
+import org.ajax4jsf.component.behavior.ClientBehavior;
+import org.richfaces.view.facelets.tag.BehaviorRule;
+
+/**
+ * @author Anton Belevich
+ *
+ */
+public class BehaviorTagHandlerDelegate extends TagHandlerDelegate implements
AttachedObjectHandler {
+
+ TagHandlerDelegate wrappedHandlerDelegate;
+
+ CustomBehaviorHandler owner;
+
+ private String behaviorId;
+
+ private String eventName;
+
+ public BehaviorTagHandlerDelegate(CustomBehaviorHandler owner, TagHandlerDelegate
wrappedHandlerDelegate) {
+ this.owner = owner;
+ this.wrappedHandlerDelegate = wrappedHandlerDelegate;
+ this.behaviorId = owner.getBehaviorId();
+ this.eventName = owner.getEventName();
+ }
+
+ @Override
+ public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
+
+ if (owner.isWrapping()) {
+
+ Application application = ctx.getFacesContext().getApplication();
+ Behavior behavior = application.createBehavior(this.behaviorId);
+
+ if(behavior instanceof ClientBehavior) {
+ ClientBehavior clientBehavior = (ClientBehavior)behavior;
+ owner.setAttributes(ctx, clientBehavior);
+
+ FacesContext context = ctx.getFacesContext();
+ BehaviorStack behaviorStack = BehaviorStack.getBehaviorStack(context,
true);
+ behaviorStack.pushBehavior(context, clientBehavior, this.behaviorId,
this.eventName);
+ owner.applyNextHandler(ctx, parent);
+ behaviorStack.popBehavior();
+ }
+
+ } else {
+ wrappedHandlerDelegate.apply(ctx, parent);
+ }
+
+ }
+
+ public MetaRuleset createMetaRuleset(Class type) {
+ MetaRuleset metaRuleset = wrappedHandlerDelegate.createMetaRuleset(type);
+ metaRuleset.addRule(new BehaviorRule());
+ return metaRuleset;
+ }
+
+ public void applyAttachedObject(FacesContext context, UIComponent parent) {
+ if (wrappedHandlerDelegate instanceof AttachedObjectHandler) {
+ ((AttachedObjectHandler) wrappedHandlerDelegate).applyAttachedObject(context,
parent);
+ }
+ }
+
+ public String getFor() {
+ if (wrappedHandlerDelegate instanceof AttachedObjectHandler) {
+ return ((AttachedObjectHandler) wrappedHandlerDelegate).getFor();
+ }
+ return null;
+ }
+}
Added:
root/ui/core/trunk/api/src/main/java/org/richfaces/view/facelets/html/CustomBehaviorHandler.java
===================================================================
---
root/ui/core/trunk/api/src/main/java/org/richfaces/view/facelets/html/CustomBehaviorHandler.java
(rev 0)
+++
root/ui/core/trunk/api/src/main/java/org/richfaces/view/facelets/html/CustomBehaviorHandler.java 2010-05-17
12:26:02 UTC (rev 17073)
@@ -0,0 +1,58 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * 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.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.richfaces.view.facelets.html;
+
+import javax.faces.view.facelets.BehaviorConfig;
+import javax.faces.view.facelets.BehaviorHandler;
+import javax.faces.view.facelets.CompositeFaceletHandler;
+import javax.faces.view.facelets.TagHandler;
+import javax.faces.view.facelets.TagHandlerDelegate;
+
+/**
+ * @author Anton Belevich
+ *
+ */
+public abstract class CustomBehaviorHandler extends BehaviorHandler {
+
+ TagHandlerDelegate helper;
+
+ public CustomBehaviorHandler(BehaviorConfig config) {
+ super(config);
+ }
+
+ public boolean isWrapping() {
+ return ((this.nextHandler instanceof TagHandler) || (this.nextHandler instanceof
CompositeFaceletHandler));
+ }
+
+ public boolean isWrappingAttachQueue() {
+ return (this.nextHandler instanceof AttachQueueHandler);
+ }
+
+ @Override
+ protected TagHandlerDelegate getTagHandlerDelegate() {
+ if (helper == null) {
+ helper = new BehaviorTagHandlerDelegate(this,
delegateFactory.createBehaviorHandlerDelegate(this));
+ }
+ return helper;
+ }
+}
Added:
root/ui/core/trunk/api/src/main/java/org/richfaces/view/facelets/tag/BehaviorRule.java
===================================================================
---
root/ui/core/trunk/api/src/main/java/org/richfaces/view/facelets/tag/BehaviorRule.java
(rev 0)
+++
root/ui/core/trunk/api/src/main/java/org/richfaces/view/facelets/tag/BehaviorRule.java 2010-05-17
12:26:02 UTC (rev 17073)
@@ -0,0 +1,113 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * 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.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.richfaces.view.facelets.tag;
+
+import javax.faces.view.facelets.FaceletContext;
+import javax.faces.view.facelets.MetaRule;
+import javax.faces.view.facelets.Metadata;
+import javax.faces.view.facelets.MetadataTarget;
+import javax.faces.view.facelets.TagAttribute;
+
+import org.ajax4jsf.component.behavior.ClientBehavior;
+import org.richfaces.log.RichfacesLogger;
+import org.slf4j.Logger;
+
+/**
+ * @author Anton Belevich
+ *
+ */
+public class BehaviorRule extends MetaRule {
+
+ private static Logger log = RichfacesLogger.CONNECTION.getLogger();
+
+ static final class LiteralAttributeMetadata extends Metadata {
+
+ private final String name;
+ private final String value;
+
+
+
+ public LiteralAttributeMetadata(String name, String value) {
+ this.value = value;
+ this.name = name;
+ }
+
+ public void applyMetadata(FaceletContext ctx, Object instance) {
+ ((ClientBehavior) instance).setLiteralAttribute(this.name, this.value);
+ }
+ }
+
+ static final class ValueExpressionMetadata extends Metadata {
+
+ private final String name;
+
+ private final TagAttribute attr;
+
+ private final Class type;
+
+ public ValueExpressionMetadata(String name, Class type, TagAttribute attr) {
+ this.name = name;
+ this.attr = attr;
+ this.type = type;
+ }
+
+ public void applyMetadata(FaceletContext ctx, Object instance) {
+ ((ClientBehavior) instance).setValueExpression(this.name,
this.attr.getValueExpression(ctx, this.type));
+ }
+
+ }
+
+ @Override
+ public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta)
{
+ if (meta.isTargetInstanceOf(ClientBehavior.class)) {
+ if (!attribute.isLiteral()) {
+
+ Class type = meta.getPropertyType(name);
+ if (type == null) {
+ type = Object.class;
+ }
+ return new ValueExpressionMetadata(name, type, attribute);
+ } else
+
+ if(meta != null) {
+
+ if (meta.getWriteMethod(name) != null) {
+ if("execute".equals(name) ||
"render".equals(name)) {
+ return new LiteralAttributeMetadata(name,
attribute.getValue());
+ }
+
+ } else {
+ if (log.isDebugEnabled()) {
+ log
+ .debug(attribute + " Property '" + name +
"' is not on type: "
+ + meta.getTargetClass().getName());
+ }
+ return new LiteralAttributeMetadata(name, attribute.getValue());
+ }
+
+ }
+ }
+
+ return null;
+ }
+}