Author: abelevich
Date: 2010-07-01 12:01:54 -0400 (Thu, 01 Jul 2010)
New Revision: 17696
Modified:
root/ui/core/trunk/ui/src/main/java/org/ajax4jsf/component/behavior/AjaxBehavior.java
root/ui/core/trunk/ui/src/main/java/org/ajax4jsf/component/behavior/ClientBehavior.java
Log:
https://jira.jboss.org/browse/RF-8733
Modified:
root/ui/core/trunk/ui/src/main/java/org/ajax4jsf/component/behavior/AjaxBehavior.java
===================================================================
---
root/ui/core/trunk/ui/src/main/java/org/ajax4jsf/component/behavior/AjaxBehavior.java 2010-07-01
13:15:39 UTC (rev 17695)
+++
root/ui/core/trunk/ui/src/main/java/org/ajax4jsf/component/behavior/AjaxBehavior.java 2010-07-01
16:01:54 UTC (rev 17696)
@@ -22,11 +22,14 @@
package org.ajax4jsf.component.behavior;
+import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
+import java.util.HashSet;
import java.util.Set;
+import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.component.behavior.ClientBehaviorHint;
import javax.faces.context.FacesContext;
@@ -52,40 +55,30 @@
private static final Set<ClientBehaviorHint> HINTS =
Collections.unmodifiableSet(EnumSet
.of(ClientBehaviorHint.SUBMITTING));
-
+
enum PropertyKeys {
data, execute, onbeforedomupdate, onbegin, oncomplete, onerror, onevent, queueId,
render,
status, disabled, limitRender, immediate, bypassUpdates
}
-
+
+ private Set<String> execute;
+
+ private Set<String> render;
+
private static final Set<String> ALL_SINGLETON_SET =
Collections.singleton(AjaxRendererUtils.ALL);
private static final Set<String> FORM_SINGLETON_SET =
Collections.singleton(AjaxRendererUtils.FORM);
private static final Set<String> THIS_SINGLETON_SET =
Collections.singleton(AjaxRendererUtils.THIS);
private static final Set<String> REGION_SINGLETON_SET =
Collections.singleton(AjaxRendererUtils.REGION);
private static final Set<String> NONE_SINGLETON_SET =
Collections.singleton(AjaxRendererUtils.NONE);
-
+
@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");
- }
-
+ setExecute(toSet(PropertyKeys.execute, value));
} 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");
- }
-
+ setRender(toSet(PropertyKeys.render, value));
} else if (compare(PropertyKeys.onbeforedomupdate, name)) {
setOnbeforedomupdate((String) value);
} else if (compare(PropertyKeys.onbegin, name)) {
@@ -111,23 +104,38 @@
}
}
- private Set<String> toSet(String propertyName, Object value) {
+ private Set<String> toSet(Serializable propertyName, Object value) {
+
+ Set<String> result = null;
+
if (value instanceof String) {
- String strValue = (String) value;
- if (strValue.indexOf(' ') == -1) {
- return toSingleton(propertyName, strValue);
- }
- return AjaxRendererUtils.asSet(strValue);
+ result = toSingletonSet(propertyName.toString(), (String) value);
}
- return null;
+
+ if (result == null) {
+ result = AjaxRendererUtils.asSet(value);
+ }
+
+ if (result == null) {
+ throw new FacesException(
+ propertyName.toString()
+ + "' attribute value must be Collection, List, array,
String, comma-separated String, whitespace-separate String'");
+ }
+
+ return result;
}
-
- private static Set<String> toSingleton(String propertyName, String value) {
- if ((null == value) || (value.length() == 0)) {
+
+ private Set<String> toSingletonSet(String propertyName, String value) {
+
+ if(value == null) {
return null;
}
-
- if (value.charAt(0) == '@') {
+
+ if(value.trim().length() == 0) {
+ return null;
+ }
+
+ if(value.charAt(0) == '@') {
if (AjaxRendererUtils.ALL.equals(value)) {
return ALL_SINGLETON_SET;
} else if (AjaxRendererUtils.FORM.equals(value)) {
@@ -138,9 +146,13 @@
return REGION_SINGLETON_SET;
} else if (AjaxRendererUtils.NONE.equals(value)) {
return NONE_SINGLETON_SET;
+ } else {
+ throw new FacesException(value + " : Invalid id keyword specified
for '"
+ + propertyName + "' attribute");
}
}
- return Collections.singleton(value);
+
+ return null;
}
public Object getData() {
@@ -151,21 +163,15 @@
getStateHelper().put(PropertyKeys.data, data);
}
- @SuppressWarnings("unchecked")
public Collection<String> getExecute() {
- Object executeObject = getStateHelper().eval(PropertyKeys.execute);
-
- if (executeObject instanceof Collection<?>) {
- return (Collection<String>) executeObject;
- } else {
- return AjaxRendererUtils.asSet(executeObject);
- }
+ return getCollectionValue(PropertyKeys.execute, execute);
}
public void setExecute(Collection<String> execute) {
- getStateHelper().put(PropertyKeys.execute, execute);
+ this.execute = copyToSet(execute);
+ clearInitialState();
}
-
+
public String getOnbeforedomupdate() {
return (String) getStateHelper().eval(PropertyKeys.onbeforedomupdate);
}
@@ -214,21 +220,15 @@
getStateHelper().put(PropertyKeys.queueId, queueId);
}
- @SuppressWarnings("unchecked")
public Collection<String> getRender() {
- Object renderObject = getStateHelper().eval(PropertyKeys.render);
-
- if (renderObject instanceof Collection<?>) {
- return (Collection<String>) renderObject;
- } else {
- return AjaxRendererUtils.asSet(renderObject);
- }
+ return getCollectionValue(PropertyKeys.render, render);
}
public void setRender(Collection<String> render) {
- getStateHelper().put(PropertyKeys.render, render);
+ this.render = copyToSet(render);
+ clearInitialState();
}
-
+
public String getStatus() {
return (String) getStateHelper().eval(PropertyKeys.status);
}
@@ -295,4 +295,93 @@
super.broadcast(event);
}
+
+ private Object saveSet(Serializable propertyName, Set<String> set) {
+ if ((set == null) || set.isEmpty()) {
+ return null;
+ }
+
+ int size = set.size();
+
+ if (size == 1) {
+ return set.toArray(new String[1])[0];
+ }
+
+ return set.toArray(new String[size]);
+ }
+
+ private Set<String> restoreSet(Serializable propertyName, Object state) {
+ if (state == null) {
+ return null;
+ }
+
+ Set<String> set = toSet(propertyName, state);
+ return set;
+ }
+
+ private Set<String> copyToSet(Collection<String> collection) {
+ return Collections.unmodifiableSet(new HashSet<String>(collection));
+ }
+
+ private Collection<String> getCollectionValue(Serializable propertyName,
Collection<String> collection) {
+ if (collection!= null) {
+ return collection;
+ }
+
+ Collection<String> result = null;
+
+ ValueExpression expression = getValueExpression(propertyName.toString());
+ if (expression != null) {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ Object value = expression.getValue(facesContext.getELContext());
+
+ if (value != null) {
+
+ if (value instanceof Collection) {
+ return (Collection<String>)value;
+ }
+
+ result = toSet(propertyName, value);
+ }
+ }
+ return result == null ? Collections.<String>emptyList() : result;
+ }
+
+ @Override
+ public void restoreState(FacesContext context, Object state) {
+ if (state != null) {
+ Object[] values = (Object[]) state;
+ super.restoreState(context, values[0]);
+
+ if (values.length != 1) {
+ execute = restoreSet(PropertyKeys.execute, values[1]);
+ render = restoreSet(PropertyKeys.render, values[2]);
+
+ clearInitialState();
+ }
+ }
+ }
+
+ @Override
+ public Object saveState(FacesContext context) {
+ Object parentState = super.saveState(context);
+ Object state = null;
+
+ if (initialStateMarked()) {
+
+ if (parentState != null) {
+ state = new Object[] { parentState };
+ }
+
+ } else {
+ Object [] values = new Object[3];
+ values[0] = parentState;
+ values[1] = saveSet(PropertyKeys.execute, execute);
+ values[2] = saveSet(PropertyKeys.render, render);
+
+ state = values;
+ }
+
+ return state;
+ }
}
Modified:
root/ui/core/trunk/ui/src/main/java/org/ajax4jsf/component/behavior/ClientBehavior.java
===================================================================
---
root/ui/core/trunk/ui/src/main/java/org/ajax4jsf/component/behavior/ClientBehavior.java 2010-07-01
13:15:39 UTC (rev 17695)
+++
root/ui/core/trunk/ui/src/main/java/org/ajax4jsf/component/behavior/ClientBehavior.java 2010-07-01
16:01:54 UTC (rev 17696)
@@ -45,7 +45,7 @@
private StateHelper behaviorStateHelper = null;
private Map<String, ValueExpression> bindings;
-
+
public StateHelper getStateHelper() {
if (behaviorStateHelper == null) {
behaviorStateHelper = new BehaviorStateHelper(this);
@@ -115,14 +115,15 @@
state = new Object[] { parentState };
}
} else {
+
+ Object savedHelper = null;
+ if(behaviorStateHelper != null) {
+ savedHelper = behaviorStateHelper.saveState(context);
+ }
+
state = new Object[3];
state[0] = parentState;
-
- Object attrs = null;
- if (behaviorStateHelper != null) {
- attrs = behaviorStateHelper.saveState(context);
- }
- state[1] = attrs;
+ state[1] = savedHelper;
state[2] = saveBindings(context, bindings);
}
@@ -134,12 +135,13 @@
if (state != null) {
Object[] stateObject = (Object[]) state;
super.restoreState(context, stateObject[0]);
- if (stateObject.length > 1) {
- if (behaviorStateHelper != null) {
- behaviorStateHelper.restoreState(context, stateObject[1]);
- }
+
+ if (stateObject.length == 3) {
+ getStateHelper().restoreState(context, stateObject[1]);
+ restoreBindings(context, stateObject[2]);
+
+ clearInitialState();
}
- restoreBindings(context, stateObject[2]);
}
}
@@ -188,5 +190,4 @@
}
return retValue;
}
-
}