[richfaces-svn-commits] JBoss Rich Faces SVN: r11978 - in trunk: docs/xslt and 11 other directories.
richfaces-svn-commits at lists.jboss.org
richfaces-svn-commits at lists.jboss.org
Mon Dec 22 20:20:32 EST 2008
Author: alexsmirnov
Date: 2008-12-22 20:20:32 -0500 (Mon, 22 Dec 2008)
New Revision: 11978
Added:
trunk/docs/xslt/
trunk/docs/xslt/en/
trunk/ui/beanValidator/src/main/java/org/richfaces/validator/GraphValidator.java
trunk/ui/message/src/test/java/org/richfaces/component/
trunk/ui/message/src/test/java/org/richfaces/component/UIMessagesTest.java
Modified:
trunk/framework/impl/src/main/java/org/ajax4jsf/application/AjaxViewHandler.java
trunk/samples/beanValidatorSample/src/main/webapp/pages/index.xhtml
trunk/ui/beanValidator/pom.xml
trunk/ui/beanValidator/src/main/config/component/beanValidator.xml
trunk/ui/beanValidator/src/main/java/org/richfaces/component/UIBeanValidator.java
trunk/ui/beanValidator/src/main/java/org/richfaces/component/UIGraphValidator.java
trunk/ui/beanValidator/src/main/java/org/richfaces/validator/FacesBeanValidator.java
trunk/ui/beanValidator/src/test/java/org/richfaces/component/BeanValidatorComponentTest.java
trunk/ui/message/src/main/java/org/richfaces/component/UIRichMessages.java
trunk/ui/message/src/main/java/org/richfaces/renderkit/html/HtmlRichMessagesRenderer.java
Log:
https://jira.jboss.org/jira/browse/RF-4433
Modified: trunk/framework/impl/src/main/java/org/ajax4jsf/application/AjaxViewHandler.java
===================================================================
--- trunk/framework/impl/src/main/java/org/ajax4jsf/application/AjaxViewHandler.java 2008-12-22 18:48:42 UTC (rev 11977)
+++ trunk/framework/impl/src/main/java/org/ajax4jsf/application/AjaxViewHandler.java 2008-12-23 01:20:32 UTC (rev 11978)
@@ -129,27 +129,6 @@
}
}
- /*
- * (non-Javadoc)
- *
- * @see org.ajax4jsf.framework.ViewHandlerWrapper#restoreView(javax.faces.context.FacesContext,
- * java.lang.String)
- */
-// public UIViewRoot restoreView(FacesContext context, String viewId) {
-// UIViewRoot viewRoot = super.restoreView(context, viewId);
-// ExternalContext externalContext = context.getExternalContext();
-// Map<String, Object> requestMap = externalContext.getRequestMap();
-// if (null == viewRoot) {
-// requestMap.put(VIEW_EXPIRED, Messages
-// .getMessage(Messages.AJAX_VIEW_EXPIRED));
-// if (_log.isDebugEnabled()) {
-// _log
-// .debug("Detect session expiration in AJAX request - view don't restored for a viewId "
-// + viewId);
-// }
-// }
-// return viewRoot;
-// }
/*
* (non-Javadoc)
Modified: trunk/samples/beanValidatorSample/src/main/webapp/pages/index.xhtml
===================================================================
--- trunk/samples/beanValidatorSample/src/main/webapp/pages/index.xhtml 2008-12-22 18:48:42 UTC (rev 11977)
+++ trunk/samples/beanValidatorSample/src/main/webapp/pages/index.xhtml 2008-12-23 01:20:32 UTC (rev 11978)
@@ -32,7 +32,7 @@
</h:panelGrid>
<h:commandButton value="Submit"></h:commandButton>
</rich:panel>
- <rich:messages />
+ <rich:messages id="fieldsMessages"/>
</h:form>
<h:form id="form1">
<h2>Input fields with label and message in the JSF dataTable.
Modified: trunk/ui/beanValidator/pom.xml
===================================================================
--- trunk/ui/beanValidator/pom.xml 2008-12-22 18:48:42 UTC (rev 11977)
+++ trunk/ui/beanValidator/pom.xml 2008-12-23 01:20:32 UTC (rev 11978)
@@ -48,6 +48,11 @@
<version>3.3.0-SNAPSHOT</version>
</dependency>
<dependency>
+ <groupId>org.richfaces.ui</groupId>
+ <artifactId>message</artifactId>
+ <version>3.3.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>3.1.0.GA</version>
Modified: trunk/ui/beanValidator/src/main/config/component/beanValidator.xml
===================================================================
--- trunk/ui/beanValidator/src/main/config/component/beanValidator.xml 2008-12-22 18:48:42 UTC (rev 11977)
+++ trunk/ui/beanValidator/src/main/config/component/beanValidator.xml 2008-12-23 01:20:32 UTC (rev 11978)
@@ -149,6 +149,15 @@
</property>
<property exist="true">
+ <name>type</name>
+ <classname>java.lang.String</classname>
+ <description>
+ JSF Validator type, that implements GraphValidator interface.This validator is used for the Graph and input fields validation.
+ </description>
+ <defaultvalue>"org.richfaces.BeanValidator"</defaultvalue>
+ </property>
+
+ <property exist="true">
<name>value</name>
<classname>java.lang.Object</classname>
<description>
Modified: trunk/ui/beanValidator/src/main/java/org/richfaces/component/UIBeanValidator.java
===================================================================
--- trunk/ui/beanValidator/src/main/java/org/richfaces/component/UIBeanValidator.java 2008-12-22 18:48:42 UTC (rev 11977)
+++ trunk/ui/beanValidator/src/main/java/org/richfaces/component/UIBeanValidator.java 2008-12-23 01:20:32 UTC (rev 11978)
@@ -22,16 +22,15 @@
package org.richfaces.component;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
+import java.util.HashSet;
import java.util.Iterator;
-import java.util.List;
import java.util.Set;
import javax.el.MethodExpression;
import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.component.EditableValueHolder;
+import javax.faces.component.NamingContainer;
import javax.faces.component.UIComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.component.UIInput;
@@ -57,169 +56,206 @@
* JSF component class
*
*/
-public abstract class UIBeanValidator extends UIComponentBase implements AjaxComponent, AjaxSupport, AjaxContainer {
+public abstract class UIBeanValidator extends UIComponentBase implements
+ AjaxComponent, AjaxSupport, AjaxContainer {
- public static final String COMPONENT_TYPE = "org.richfaces.BeanValidator";
+ public static final String COMPONENT_TYPE = "org.richfaces.BeanValidator";
- public static final String COMPONENT_FAMILY = "org.richfaces.BeanValidator";
+ public static final String COMPONENT_FAMILY = "org.richfaces.BeanValidator";
- public static final String BEAN_VALIDATOR_FACET = "org.richfaces.validator.";
-
+ public static final String BEAN_VALIDATOR_FACET = "org.richfaces.validator.";
- @Override
- public void setParent(UIComponent parent) {
- super.setParent(parent);
+ @Override
+ public void setParent(UIComponent parent) {
+ super.setParent(parent);
- if (null != parent && parent instanceof EditableValueHolder) {
- setParentProperties(parent);
- }
- }
+ if (null != parent && parent instanceof EditableValueHolder) {
+ setParentProperties(parent);
+ }
+ }
- /**
- * @param parent
- * @throws FacesException
- */
- public void setParentProperties(UIComponent parent) throws FacesException {
- if (!(parent instanceof EditableValueHolder)) {
- throw new FacesException("Parent component must be an EditableValueHolder");
- }
+ /**
+ * @param parent
+ * @throws FacesException
+ */
+ public void setParentProperties(UIComponent parent) throws FacesException {
+ if (!(parent instanceof EditableValueHolder)) {
+ throw new FacesException(
+ "Parent component must be an EditableValueHolder");
+ }
- if (null != getEvent()) {
- ValueExpression binding = new EventValueExpression(this);
- parent.setValueExpression(getEvent(), binding);
- }
- }
+ if (null != getEvent()) {
+ ValueExpression binding = new EventValueExpression(this);
+ parent.setValueExpression(getEvent(), binding);
+ }
+ }
- @Override
- public void broadcast(FacesEvent event) throws AbortProcessingException {
- super.broadcast(event);
- if (event.getComponent() == this) {
- FacesContext context = getFacesContext();
- if (event instanceof ValidationEvent) {
- // ByPass UpdateModelValue
- context.renderResponse();
- } else if (event instanceof AjaxEvent) {
- // TODO - find all UIMessages components for a parent UIInput.
- // for an ajaxSingle component, re-render only target message.
- AjaxContext ajaxContext = AjaxContext.getCurrentInstance(context);
- ajaxContext.setSubmittedRegionClientId(this.getClientId(context));
- }
- }
- }
+ @Override
+ public void broadcast(FacesEvent event) throws AbortProcessingException {
+ super.broadcast(event);
+ if (event.getComponent() == this) {
+ FacesContext context = getFacesContext();
+ if (event instanceof ValidationEvent) {
+ // ByPass UpdateModelValue
+ context.renderResponse();
+ } else if (event instanceof AjaxEvent) {
+ // TODO - find all UIMessages components for a parent UIInput.
+ // for an ajaxSingle component, re-render only target message.
+ AjaxContext ajaxContext = AjaxContext
+ .getCurrentInstance(context);
+ ajaxContext.setSubmittedRegionClientId(this
+ .getClientId(context));
+ }
+ }
+ }
- @Override
- public void queueEvent(FacesEvent event) {
- if (event instanceof ValidationEvent && event.getComponent() == this) {
- UIComponent parent = getParent();
- if (parent instanceof UIInput) {
- UIInput input = (UIInput) parent;
- if (input.isImmediate()) {
- event.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
- } else {
- event.setPhaseId(PhaseId.PROCESS_VALIDATIONS);
- }
- }
- }
- super.queueEvent(event);
- }
+ @Override
+ public void queueEvent(FacesEvent event) {
+ if (event instanceof ValidationEvent && event.getComponent() == this) {
+ UIComponent parent = getParent();
+ if (parent instanceof UIInput) {
+ UIInput input = (UIInput) parent;
+ if (input.isImmediate()) {
+ event.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
+ } else {
+ event.setPhaseId(PhaseId.PROCESS_VALIDATIONS);
+ }
+ }
+ }
+ super.queueEvent(event);
+ }
- public String getEventString() {
- StringBuffer buildOnEvent = new StringBuffer();
- String onsubmit = getOnsubmit();
- // Insert script to call before submit ajax request.
- if (null != onsubmit) {
- buildOnEvent.append(onsubmit).append(";");
- }
- buildOnEvent.append(AjaxRendererUtils.buildOnEvent(this, getFacesContext(), getEvent()));
- String script = buildOnEvent.toString();
- return script;
- }
+ public String getEventString() {
+ StringBuffer buildOnEvent = new StringBuffer();
+ String onsubmit = getOnsubmit();
+ // Insert script to call before submit ajax request.
+ if (null != onsubmit) {
+ buildOnEvent.append(onsubmit).append(";");
+ }
+ buildOnEvent.append(AjaxRendererUtils.buildOnEvent(this,
+ getFacesContext(), getEvent()));
+ String script = buildOnEvent.toString();
+ return script;
+ }
- public UIComponent getSingleComponent() {
- return getParent();
- }
+ public UIComponent getSingleComponent() {
+ return getParent();
+ }
- public void encodeAjax(FacesContext context) throws IOException {
- AjaxContext ajaxContext = AjaxContext.getCurrentInstance(context);
- Set<String> renderedAreas = ajaxContext.getAjaxRenderedAreas();
- List<UIComponent> allMessages = getMessages(context);
- for (UIComponent message : allMessages) {
- if (message.isRendered()) {
- message.encodeAll(context);
- renderedAreas.add(message.getClientId(context));
- }
- }
- // Write information about encoded areas after submission.
- AjaxRendererUtils.encodeAreas(context, this);
- }
+ public void encodeAjax(FacesContext context) throws IOException {
+ AjaxContext ajaxContext = AjaxContext.getCurrentInstance(context);
+ Set<String> renderedAreas = ajaxContext.getAjaxRenderedAreas();
+ for (UIComponent message : getMessages(context)) {
+ if (message.isRendered()) {
+ message.encodeAll(context);
+ renderedAreas.add(message.getClientId(context));
+ }
+ }
+ // Write information about encoded areas after submission.
+ AjaxRendererUtils.encodeAreas(context, this);
+ }
- public List<UIComponent> getMessages(FacesContext context) {
- ArrayList<UIComponent> messages = new ArrayList<UIComponent>();
- UIComponent form = AjaxRendererUtils.getNestingForm(this);
- if (null == form) {
- form = context.getViewRoot();
- }
- findMessages(form, messages);
- return messages;
- }
+ public Set<UIComponent> getMessages(FacesContext context) {
+ Set<UIComponent> messages = new HashSet<UIComponent>();
+ findMessages(this.getParent(), this, messages,false);
+ findRichMessages(context, context.getViewRoot(), messages);
+ return messages;
+ }
- /**
- * Recursive search messages for the parent component.
- *
- * @param component
- * @param messages
- */
- protected void findMessages(UIComponent component, ArrayList<UIComponent> messages) {
- // TODO create more intellectual search, symmenric for an UIMessage
- // algoritm.
- Iterator<UIComponent> facetsAndChildren = component.getFacetsAndChildren();
- while (facetsAndChildren.hasNext()) {
- UIComponent child = (UIComponent) facetsAndChildren.next();
- if (child instanceof UIMessage || child instanceof UIMessages) {
- UIComponent message = (UIComponent) child;
- Object messag = message.getAttributes().get("for");
- String messageFor = messag != null ? messag.toString() : null ;
-
- if (null != messageFor && messageFor.equals(getParent().getId())) {
- messages.add(message);
- }
- }
- else {
- findMessages(child, messages);
- }
- }
- }
+
+ /**
+ * Find all instances of the {@link UIRichMessages} and update list of the rendered messages.
+ * @param context
+ * @param component
+ * @param messages
+ */
+ protected void findRichMessages(FacesContext context,UIComponent component, Set<UIComponent> messages) {
+ Iterator<UIComponent> facetsAndChildren = component.getFacetsAndChildren();
+ while (facetsAndChildren.hasNext()) {
+ UIComponent child = (UIComponent) facetsAndChildren.next();
+ if (child instanceof UIRichMessages) {
+ UIRichMessages richMessage = (UIRichMessages) child;
+ if(null == richMessage.getFor()){
+ richMessage.updateMessages(context, this.getParent().getClientId(context));
+ messages.add(richMessage);
+ }
+ } else {
+ findRichMessages(context, child, messages);
+ }
+ }
+ }
- /*
- * (non-Javadoc)
- *
- * @see org.ajax4jsf.event.AjaxSource#addAjaxListener(org.ajax4jsf.event.AjaxListener)
- */
- public void addAjaxListener(AjaxListener listener) {
- addFacesListener(listener);
- }
+ /**
+ * Recursive search messages for the parent component.
+ * @param parent
+ * @param component
+ * @param messages
+ * @return
+ */
+ protected boolean findMessages(UIComponent parent, UIComponent component,
+ Set<UIComponent> messages,boolean found) {
+ Iterator<UIComponent> facetsAndChildren = parent.getFacetsAndChildren();
+ while (facetsAndChildren.hasNext()) {
+ UIComponent child = (UIComponent) facetsAndChildren.next();
+ if (child != component) {
+ if (child instanceof UIMessage || child instanceof UIMessages) {
+ UIComponent message = (UIComponent) child;
+ Object targetId = message.getAttributes().get("for");
+ if (null != targetId
+ && targetId.equals(getParent().getId())) {
+ messages.add(message);
+ found = true;
+ }
+ } else {
+ found |= findMessages(child, null, messages,found);
+ }
+ }
+ }
+ if(!(found && parent instanceof NamingContainer) && component != null){
+ UIComponent newParent = parent.getParent();
+ if(null != newParent){
+ found = findMessages(newParent, parent, messages,found);
+ }
+ }
+ return found;
+ }
- /*
- * (non-Javadoc)
- *
- * @see org.ajax4jsf.event.AjaxSource#getAjaxListeners()
- */
- public AjaxListener[] getAjaxListeners() {
- return (AjaxListener[]) getFacesListeners(AjaxListener.class);
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.ajax4jsf.event.AjaxSource#addAjaxListener(org.ajax4jsf.event.AjaxListener
+ * )
+ */
+ public void addAjaxListener(AjaxListener listener) {
+ addFacesListener(listener);
+ }
- /* (non-Javadoc)
- * @see org.ajax4jsf.event.AjaxSource#removeAjaxListener(org.ajax4jsf.event.AjaxListener)
- */
- public void removeAjaxListener(AjaxListener listener) {
- removeFacesListener(listener);
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.ajax4jsf.event.AjaxSource#getAjaxListeners()
+ */
+ public AjaxListener[] getAjaxListeners() {
+ return (AjaxListener[]) getFacesListeners(AjaxListener.class);
+ }
- public void setAjaxListener(MethodExpression listener) {
- AjaxListener[] ajaxListeners = getAjaxListeners();
- for (int i = 0; i < ajaxListeners.length; i++) {
+ /*
+ * (non-Javadoc)
+ *
+ * @seeorg.ajax4jsf.event.AjaxSource#removeAjaxListener(org.ajax4jsf.event.
+ * AjaxListener)
+ */
+ public void removeAjaxListener(AjaxListener listener) {
+ removeFacesListener(listener);
+ }
+
+ public void setAjaxListener(MethodExpression listener) {
+ AjaxListener[] ajaxListeners = getAjaxListeners();
+ for (int i = 0; i < ajaxListeners.length; i++) {
AjaxListener ajaxListener = ajaxListeners[i];
- if (ajaxListener.getClass().equals(MethodExpressionAjaxListener.class)) {
+ if (ajaxListener.getClass().equals(
+ MethodExpressionAjaxListener.class)) {
MethodExpressionAjaxListener expressionListener = (MethodExpressionAjaxListener) ajaxListener;
if (expressionListener.getExpression() != listener) {
removeAjaxListener(ajaxListener);
@@ -233,21 +269,22 @@
}
}
}
- }
-
- addAjaxListener(new MethodExpressionAjaxListener(listener));
- }
-
- public MethodExpression getAjaxListener() {
- AjaxListener[] ajaxListeners = getAjaxListeners();
- for (int i = 0; i < ajaxListeners.length; i++) {
+ }
+
+ addAjaxListener(new MethodExpressionAjaxListener(listener));
+ }
+
+ public MethodExpression getAjaxListener() {
+ AjaxListener[] ajaxListeners = getAjaxListeners();
+ for (int i = 0; i < ajaxListeners.length; i++) {
AjaxListener ajaxListener = ajaxListeners[i];
- if (ajaxListener.getClass().equals(MethodExpressionAjaxListener.class)) {
+ if (ajaxListener.getClass().equals(
+ MethodExpressionAjaxListener.class)) {
MethodExpressionAjaxListener expressionListener = (MethodExpressionAjaxListener) ajaxListener;
return expressionListener.getExpression();
}
- }
-
- return null;
- }
+ }
+
+ return null;
+ }
}
Modified: trunk/ui/beanValidator/src/main/java/org/richfaces/component/UIGraphValidator.java
===================================================================
--- trunk/ui/beanValidator/src/main/java/org/richfaces/component/UIGraphValidator.java 2008-12-22 18:48:42 UTC (rev 11977)
+++ trunk/ui/beanValidator/src/main/java/org/richfaces/component/UIGraphValidator.java 2008-12-23 01:20:32 UTC (rev 11978)
@@ -53,6 +53,7 @@
import org.richfaces.event.ValidationEvent;
import org.richfaces.validator.BeanValidator;
import org.richfaces.validator.FacesBeanValidator;
+import org.richfaces.validator.GraphValidator;
/**
@@ -106,23 +107,43 @@
* @param newvalue
*/
public abstract void setProfile(Set<String> newvalue);
+
+ /**
+ * Get graph validator Id.
+ * @return
+ */
+ public abstract String getType();
+ /**
+ * Set graph validator Id.
+ * @param newvalue
+ */
+ public abstract void setType(String newvalue);
+
+
+
@Override
public void processUpdates(FacesContext context) {
super.processUpdates(context);
Object value = getValue();
if (null != value) {
- BeanValidator beanValidator = BeanValidator.getInstance(context);
- String[] messages = beanValidator.validateGraph(context, value,getProfile());
- if (null != messages) {
- context.renderResponse();
- // send all validation messages.
- for (String msg : messages) {
- // TODO - create Summary message ?
- String summary = null != getSummary() ? getSummary() + msg : msg;
- context.addMessage(null, new FacesMessage(
- FacesMessage.SEVERITY_ERROR, summary, msg));
+ Validator validator = context.getApplication().createValidator(getType());
+ if (validator instanceof GraphValidator) {
+ GraphValidator graphValidator = (GraphValidator) validator;
+ String[] messages = graphValidator.validateGraph(context,this, value,getProfile());
+ if (null != messages) {
+ context.renderResponse();
+ // send all validation messages.
+ for (String msg : messages) {
+ // TODO - create Summary message ?
+ String summary = null != getSummary() ? getSummary() + msg : msg;
+ context.addMessage(null, new FacesMessage(
+ FacesMessage.SEVERITY_ERROR, summary, msg));
+ }
}
+
+ } else {
+ throw new FacesException("Validator "+FacesBeanValidator.BEAN_VALIDATOR_TYPE+" does not implement GraphValidator");
}
}
@@ -131,7 +152,7 @@
@Override
public void encodeBegin(FacesContext context) throws IOException {
super.encodeBegin(context);
- FacesBeanValidator validator = (FacesBeanValidator)context.getApplication().createValidator(FacesBeanValidator.BEAN_VALIDATOR_TYPE);
+ FacesBeanValidator validator = (FacesBeanValidator)context.getApplication().createValidator(getType());
validator.setSummary(getSummary());
setupValidators(this,validator);
}
Modified: trunk/ui/beanValidator/src/main/java/org/richfaces/validator/FacesBeanValidator.java
===================================================================
--- trunk/ui/beanValidator/src/main/java/org/richfaces/validator/FacesBeanValidator.java 2008-12-22 18:48:42 UTC (rev 11977)
+++ trunk/ui/beanValidator/src/main/java/org/richfaces/validator/FacesBeanValidator.java 2008-12-23 01:20:32 UTC (rev 11978)
@@ -21,6 +21,7 @@
package org.richfaces.validator;
import java.io.Serializable;
+import java.util.Set;
import javax.el.ELException;
import javax.el.ValueExpression;
@@ -39,7 +40,7 @@
* @author asmirnov
*
*/
-public class FacesBeanValidator implements Validator,Serializable {
+public class FacesBeanValidator implements Validator,Serializable, GraphValidator {
/**
*
@@ -112,4 +113,11 @@
}
}
}
+
+ public String[] validateGraph(FacesContext context, UIComponent component,
+ Object value, Set<String> profiles) throws ValidatorException {
+ BeanValidator beanValidator = BeanValidator.getInstance(context);
+ String[] messages = beanValidator.validateGraph(context, value,profiles);
+ return messages;
+ }
}
Added: trunk/ui/beanValidator/src/main/java/org/richfaces/validator/GraphValidator.java
===================================================================
--- trunk/ui/beanValidator/src/main/java/org/richfaces/validator/GraphValidator.java (rev 0)
+++ trunk/ui/beanValidator/src/main/java/org/richfaces/validator/GraphValidator.java 2008-12-23 01:20:32 UTC (rev 11978)
@@ -0,0 +1,24 @@
+/**
+ *
+ */
+package org.richfaces.validator;
+
+import java.util.Set;
+
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.validator.Validator;
+import javax.faces.validator.ValidatorException;
+
+/**
+ * This interface should be implemented by the JSF {@link Validator} which able to
+ * validate entire graph.
+ * @author asmirnov
+ *
+ */
+public interface GraphValidator {
+
+ public String[] validateGraph(FacesContext context, UIComponent component, Object value,
+ Set<String> profiles) throws ValidatorException ;
+
+}
Property changes on: trunk/ui/beanValidator/src/main/java/org/richfaces/validator/GraphValidator.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified: trunk/ui/beanValidator/src/test/java/org/richfaces/component/BeanValidatorComponentTest.java
===================================================================
--- trunk/ui/beanValidator/src/test/java/org/richfaces/component/BeanValidatorComponentTest.java 2008-12-22 18:48:42 UTC (rev 11977)
+++ trunk/ui/beanValidator/src/test/java/org/richfaces/component/BeanValidatorComponentTest.java 2008-12-23 01:20:32 UTC (rev 11978)
@@ -29,6 +29,7 @@
import javax.el.PropertyNotFoundException;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIForm;
+import javax.faces.component.UIMessage;
import javax.faces.component.UIMessages;
import javax.faces.component.html.HtmlForm;
import javax.faces.el.MethodNotFoundException;
@@ -50,6 +51,7 @@
UIBeanValidator validator = null;
HtmlInputText input = null;
UIMessages messages = null;
+ private UIMessage message;
public BeanValidatorComponentTest(String name) {
super(name);
@@ -64,12 +66,17 @@
validator = (UIBeanValidator)application.createComponent(UIBeanValidator.COMPONENT_TYPE);
validator.setId("validator");
+ input.setId("input");
input.getChildren().add(validator);
- input.setId("input");
form.getChildren().add(input);
- messages = (UIMessages)application.createComponent(UIMessages.COMPONENT_TYPE);
+ messages = (UIRichMessages)application.createComponent(UIRichMessages.COMPONENT_TYPE);
messages.setId("messages");
form.getChildren().add(messages);
+ message = (UIMessage)application.createComponent(UIMessage.COMPONENT_TYPE);
+ message.setId("msg");
+ message.setFor("input");
+ form.getChildren().add(message);
+ facesContext.getViewRoot().getChildren().add(form);
}
public void tearDown() throws Exception {
Modified: trunk/ui/message/src/main/java/org/richfaces/component/UIRichMessages.java
===================================================================
--- trunk/ui/message/src/main/java/org/richfaces/component/UIRichMessages.java 2008-12-22 18:48:42 UTC (rev 11977)
+++ trunk/ui/message/src/main/java/org/richfaces/component/UIRichMessages.java 2008-12-23 01:20:32 UTC (rev 11978)
@@ -20,102 +20,297 @@
*/
package org.richfaces.component;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map.Entry;
+
import javax.el.ELException;
import javax.el.ValueExpression;
import javax.faces.FacesException;
+import javax.faces.application.FacesMessage;
+import javax.faces.application.FacesMessage.Severity;
+import javax.faces.component.UIComponent;
import javax.faces.component.UIMessages;
import javax.faces.context.FacesContext;
import org.ajax4jsf.component.AjaxOutput;
+import org.ajax4jsf.renderkit.RendererUtils;
/**
* @author Anton Belevich
- *
+ *
*/
-public abstract class UIRichMessages extends UIMessages implements AjaxOutput{
+public abstract class UIRichMessages extends UIMessages implements AjaxOutput {
- private String forVal = null;
- private boolean isPassed = true;
-
- /**
- * <p>Return the client identifier of the component for which
- * this component represents associated message(s) (if any).</p>
- */
- public String getFor() {
+ public static final String COMPONENT_TYPE = "org.richfaces.component.RichMessages";
- if (this.forVal != null) {
- return (this.forVal);
+ private String forVal = null;
+ private boolean updated = false;
+
+ private List<FacesMessageWithId> renderedMessages;
+
+ /**
+ * <p>
+ * Return the client identifier of the component for which this component
+ * represents associated message(s) (if any).
+ * </p>
+ */
+ public String getFor() {
+
+ if (this.forVal != null) {
+ return (this.forVal);
+ }
+ ValueExpression ve = getValueExpression("for");
+ if (ve != null) {
+ try {
+ return ((String) ve.getValue(getFacesContext().getELContext()));
+ } catch (ELException e) {
+ throw new FacesException(e);
+ }
+ } else {
+ return (null);
+ }
+
}
- ValueExpression ve = getValueExpression("for");
- if (ve != null) {
- try {
- return ((String) ve.getValue(getFacesContext().getELContext()));
- }
- catch (ELException e) {
- throw new FacesException(e);
- }
- } else {
- return (null);
- }
- }
+ /**
+ * <p>
+ * Set the client identifier of the component for which this component
+ * represents associated message(s) (if any). This property must be set
+ * before the message is displayed.
+ * </p>
+ *
+ * @param newFor
+ * The new client id
+ */
+ public void setFor(String newFor) {
+ forVal = newFor;
- /**
- * <p>Set the client identifier of the component for which this
- * component represents associated message(s) (if any). This
- * property must be set before the message is displayed.</p>
- *
- * @param newFor The new client id
- */
- public void setFor(String newFor) {
+ }
- forVal = newFor;
+ public boolean isUpdated() {
+ return updated;
+ }
- }
-
- public boolean isPassed(){
- return isPassed;
- }
-
public boolean isAjaxRendered() {
return true;
}
- public void setAjaxRendered(boolean ajaxRendered){
- if(!ajaxRendered){
+ public Iterator<FacesMessage> getMessages(FacesContext context) {
+ String forInputId = getFor();
+ if (null == this.renderedMessages || null != forInputId) {
+ List<String> severenities = getSeverenities();
+ renderedMessages = new ArrayList<FacesMessageWithId>();
+ if (isGlobalOnly()) {
+ addMessagesForId(context, null, severenities);
+ } else if (null != forInputId) {
+ UIComponent componentFor = RendererUtils.getInstance()
+ .findComponentFor(context, this, forInputId);
+ if (null != componentFor) {
+ addMessagesForId(context,
+ componentFor.getClientId(context), severenities);
+ }
+ } else {
+ Iterator<String> clientIdsWithMessages = context
+ .getClientIdsWithMessages();
+ while (clientIdsWithMessages.hasNext()) {
+ addMessagesForId(context, clientIdsWithMessages.next(),
+ severenities);
+ }
+ }
+ }
+ ;
+ final Iterator<FacesMessageWithId> iterator = this
+ .renderedMessages.iterator();
+ return new Iterator<FacesMessage>() {
+
+ public boolean hasNext() {
+ return iterator.hasNext();
+ }
+
+ public FacesMessage next() {
+ return iterator.next().getMessage();
+ }
+
+ public void remove() {
+ }
+ };
+ }
+
+ public void updateMessages(FacesContext context, String forId) {
+ updated = true;
+ if (null == renderedMessages) {
+ renderedMessages = new ArrayList<FacesMessageWithId>();
+ } else {
+ Iterator<FacesMessageWithId> renderedMessageIterator = renderedMessages
+ .iterator();
+ while (renderedMessageIterator.hasNext()) {
+ String clientId = renderedMessageIterator.next().getClientId();
+ if ((null == forId && null == clientId) || (null != forId && forId.equals(clientId))) {
+ renderedMessageIterator.remove();
+ }
+ }
+ }
+ addMessagesForId(context, forId, getSeverenities());
+ }
+
+ /**
+ * @param context
+ * @param forId
+ * @param severenities
+ * TODO
+ */
+ protected void addMessagesForId(FacesContext context, String forId,
+ List<String> severenities) {
+ Iterator<FacesMessage> messages = context.getMessages(forId);
+ while (messages.hasNext()) {
+ FacesMessage message = messages.next();
+ if (severenities.size() == 0 || severenities.contains("ALL")
+ || isApplicableMessage(severenities, message)) {
+ renderedMessages.add(
+ new FacesMessageWithId(forId, message));
+ }
+ }
+ }
+
+ /**
+ * @param severenities
+ * @param message
+ * @return
+ */
+ protected boolean isApplicableMessage(List<String> severenities,
+ FacesMessage message) {
+ if (severenities.size() == 0 || severenities.contains("ALL")) {
+ return true;
+ }
+ Severity severity = message.getSeverity();
+ for (Object key : FacesMessage.VALUES_MAP.keySet()) {
+ Severity sev = (Severity) FacesMessage.VALUES_MAP.get(key);
+ if(0 == sev.compareTo(severity)){
+ return severenities.contains(key);
+ }
+ }
+ return false;
+ }
+
+ public void setAjaxRendered(boolean ajaxRendered) {
+ if (!ajaxRendered) {
new IllegalArgumentException();
- }
+ }
}
-
- public void decode(FacesContext context){
-// isPassed = true;
- super.decode(context);
+
+ @Override
+ public void encodeBegin(FacesContext context) throws IOException {
+ if (!isUpdated()) {
+ setRenderedMessages(null);
+ }
+ super.encodeBegin(context);
}
public abstract String getLevel();
public abstract void setLevel(String level);
+ public List<String> getSeverenities() {
+ String level = getLevel();
+ List<String> severenities;
+ if (null != level) {
+ String[] levels = level.split(",");
+ severenities = new ArrayList<String>(levels.length);
+ for (int i = 0; i < levels.length; i++) {
+ String levelName = levels[i].toUpperCase().trim();
+ severenities.add(levelName);
+ }
+ } else {
+ severenities = Collections.emptyList();
+ }
+ return severenities;
+ }
+
private Object[] values;
public Object saveState(FacesContext context) {
if (values == null) {
- values = new Object[2];
+ values = new Object[3];
}
values[0] = super.saveState(context);
values[1] = this.forVal;
+ values[2] = saveAttachedState(context, getRenderedMessages());
return (values);
}
+ @SuppressWarnings("unchecked")
public void restoreState(FacesContext context, Object state) {
values = (Object[]) state;
super.restoreState(context, values[0]);
forVal = (String) values[1];
+ setRenderedMessages((List<FacesMessageWithId>) restoreAttachedState(
+ context, values[2]));
}
+ /**
+ * @author asmirnov
+ *
+ */
+ @SuppressWarnings("serial")
+ public static class FacesMessageWithId implements
+ Serializable {
+ private FacesMessage message;
+
+ private final String clientId;
+
+ /**
+ * @param clientId
+ * @param message
+ */
+ public FacesMessageWithId(String clientId, FacesMessage message) {
+ this.message = message;
+ this.clientId = clientId;
+ }
+
+ /**
+ * @return the message
+ */
+ public FacesMessage getMessage() {
+ return message;
+ }
+
+ /**
+ * @return the clientId
+ */
+ public String getClientId() {
+ return clientId;
+ }
+
+
+
+
+ }
+
+ /**
+ * @return the renderedMessages
+ */
+ protected List<FacesMessageWithId> getRenderedMessages() {
+ return renderedMessages;
+ }
+
+ /**
+ * @param renderedMessages
+ * the renderedMessages to set
+ */
+ protected void setRenderedMessages(List<FacesMessageWithId> renderedMessages) {
+ this.renderedMessages = renderedMessages;
+ }
+
}
Modified: trunk/ui/message/src/main/java/org/richfaces/renderkit/html/HtmlRichMessagesRenderer.java
===================================================================
--- trunk/ui/message/src/main/java/org/richfaces/renderkit/html/HtmlRichMessagesRenderer.java 2008-12-22 18:48:42 UTC (rev 11977)
+++ trunk/ui/message/src/main/java/org/richfaces/renderkit/html/HtmlRichMessagesRenderer.java 2008-12-23 01:20:32 UTC (rev 11978)
@@ -57,17 +57,8 @@
throw new FacesException("The rich:messages component must specify only one of 'for' or 'globalOnly' atribbute");
}
}
- Iterator<FacesMessage> messagesIter = getMessageIterator(context, forIds, component);
+ Iterator<FacesMessage> messagesIter = uiMessages.getMessages(context);
boolean isDisplayNone = !messagesIter.hasNext();
- String [] levels = uiMessages.getLevel().split(",");
- List<String> acceptLevels = creatAcceptLevels(levels);
-
- if(!isDisplayNone){
-
- if (!isAcceptableMessagesPresent(acceptLevels, messagesIter)) {
- isDisplayNone = true;
- }
- }
boolean isWroteTable = false;
layout = (String)uiMessages.getAttributes().get("layout");
if((layout != null) && (layout.equals("table"))){
@@ -89,7 +80,7 @@
}
- messagesIter = getMessageIterator(context, forIds, component);
+// messagesIter = getMessageIterator(context, forIds, component);
if(!messagesIter.hasNext()){
renderBody(uiMessages, context, writer, null,isWroteTable);
@@ -97,11 +88,7 @@
while(messagesIter.hasNext()){
FacesMessage facesMsg = (FacesMessage)(messagesIter.next());
-
- if(isAcceptableMessage(facesMsg, acceptLevels)){
-
- renderBody(uiMessages, context, writer, facesMsg,isWroteTable);
- }
+ renderBody(uiMessages, context, writer, facesMsg,isWroteTable);
}
}
@@ -231,7 +218,7 @@
public void renderBody(UIRichMessages uiMessages, FacesContext context, ResponseWriter writer,FacesMessage facesMsg,boolean isWroteTable) throws IOException{
- if(uiMessages.isPassed() || facesMsg != null ){
+ if(facesMsg != null ){
if(isWroteTable){
writer.startElement(HTML.TR_ELEMENT, uiMessages);
writer.startElement(HTML.td_ELEM, uiMessages);
Added: trunk/ui/message/src/test/java/org/richfaces/component/UIMessagesTest.java
===================================================================
--- trunk/ui/message/src/test/java/org/richfaces/component/UIMessagesTest.java (rev 0)
+++ trunk/ui/message/src/test/java/org/richfaces/component/UIMessagesTest.java 2008-12-23 01:20:32 UTC (rev 11978)
@@ -0,0 +1,239 @@
+/**
+ *
+ */
+package org.richfaces.component;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIComponent;
+import javax.faces.component.html.HtmlInputText;
+
+import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
+import org.richfaces.component.UIRichMessages.FacesMessageWithId;
+
+/**
+ * @author asmirnov
+ *
+ */
+public class UIMessagesTest extends AbstractAjax4JsfTestCase {
+
+ private static final class MockRichMessages extends UIRichMessages {
+ private String level = "ALL";
+
+ @Override
+ public String getLevel() {
+ return level;
+ }
+
+ @Override
+ public void setLevel(String level) {
+ this.level = level;
+
+ }
+
+ public boolean isKeepTransient() {
+ return true;
+ }
+
+ public void setKeepTransient(boolean ajaxRendered) {
+ }
+ }
+
+ private static final String INPUT_ID = "input";
+ private static final String MESSAGES_ID = "messages";
+ private UIRichMessages uiMessages;
+
+ /**
+ * @param name
+ */
+ public UIMessagesTest(String name) {
+ super(name);
+ }
+
+ /* (non-Javadoc)
+ * @see org.ajax4jsf.tests.AbstractAjax4JsfTestCase#setUp()
+ */
+ public void setUp() throws Exception {
+ super.setUp();
+ uiMessages = new MockRichMessages();
+ uiMessages.setId(MESSAGES_ID);
+ List<UIComponent> children = facesContext.getViewRoot().getChildren();
+ children.add(uiMessages);
+ HtmlInputText inputText = new HtmlInputText();
+ inputText.setId(INPUT_ID);
+ children.add(inputText);
+ }
+
+ /* (non-Javadoc)
+ * @see org.ajax4jsf.tests.AbstractAjax4JsfTestCase#tearDown()
+ */
+ public void tearDown() throws Exception {
+ this.uiMessages = null;
+ super.tearDown();
+ }
+
+
+ /**
+ * Test method for {@link org.richfaces.component.UIRichMessages#getMessages(javax.faces.context.FacesContext)}.
+ */
+ public void testGetMessages() {
+ prepareMessages();
+ uiMessages.getMessages(facesContext);
+ assertEquals(8, uiMessages.getRenderedMessages().size());
+ }
+
+ /**
+ * Test method for {@link org.richfaces.component.UIRichMessages#getMessages(javax.faces.context.FacesContext)}.
+ */
+ public void testGetMessagesWithFor() {
+ prepareMessages();
+ uiMessages.setFor(INPUT_ID);
+ uiMessages.getMessages(facesContext);
+ assertEquals(4, uiMessages.getRenderedMessages().size());
+ assertEquals(INPUT_ID, uiMessages.getRenderedMessages().get(0).getClientId());
+ }
+
+ /**
+ * Test method for {@link org.richfaces.component.UIRichMessages#getMessages(javax.faces.context.FacesContext)}.
+ */
+ public void testGetMessagesForGlobal() {
+ prepareMessages();
+ uiMessages.setGlobalOnly(true);
+ Iterator<FacesMessage> iterator = uiMessages.getMessages(facesContext);
+ assertEquals(4, uiMessages.getRenderedMessages().size());
+ assertTrue(iterator.hasNext());
+ }
+ /**
+ * Test method for {@link org.richfaces.component.UIRichMessages#updateMessages(javax.faces.context.FacesContext, java.lang.String)}.
+ */
+ public void testUpdateMessages() {
+ prepareMessages();
+ ArrayList<FacesMessageWithId> savedMessages = prepareSavedMessages();
+ uiMessages.setRenderedMessages(savedMessages);
+ uiMessages.updateMessages(facesContext, INPUT_ID);
+ assertTrue(uiMessages.isUpdated());
+ assertSame(savedMessages,uiMessages.getRenderedMessages());
+ assertEquals(6, uiMessages.getRenderedMessages().size());
+ }
+
+ /**
+ * @return
+ */
+ private ArrayList<FacesMessageWithId> prepareSavedMessages() {
+ ArrayList<FacesMessageWithId> savedMessages = new ArrayList<FacesMessageWithId>();
+ FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_WARN,"Warn","Old Warn level message");
+ savedMessages.add(new FacesMessageWithId(INPUT_ID,facesMessage));
+ savedMessages.add(new FacesMessageWithId(null,facesMessage));
+ savedMessages.add(new FacesMessageWithId("bar",facesMessage));
+ return savedMessages;
+ }
+
+ /**
+ * Test method for {@link org.richfaces.component.UIRichMessages#updateMessages(javax.faces.context.FacesContext, java.lang.String)}.
+ */
+ public void testUpdateEmptyMessages() {
+ prepareMessages();
+ uiMessages.updateMessages(facesContext, INPUT_ID);
+ assertTrue(uiMessages.isUpdated());
+ assertNotNull(uiMessages.getRenderedMessages());
+ assertEquals(4, uiMessages.getRenderedMessages().size());
+ }
+ /**
+ * Test method for {@link org.richfaces.component.UIRichMessages#encodeBegin(javax.faces.context.FacesContext)}.
+ * @throws IOException
+ */
+ public void testEncodeBeginFacesContext() throws IOException {
+ prepareMessages();
+ uiMessages.setRenderedMessages(prepareSavedMessages());
+ uiMessages.encodeBegin(facesContext);
+ assertNull(uiMessages.getRenderedMessages());
+ }
+
+ /**
+ * Test method for {@link org.richfaces.component.UIRichMessages#encodeBegin(javax.faces.context.FacesContext)}.
+ * @throws IOException
+ */
+ public void testEncodeBeginAfterUpdate() throws IOException {
+ prepareMessages();
+ ArrayList<FacesMessageWithId> savedMessages = prepareSavedMessages();
+ uiMessages.setRenderedMessages(savedMessages);
+ uiMessages.updateMessages(facesContext, null);
+ uiMessages.encodeBegin(facesContext);
+ assertNotNull(uiMessages.getRenderedMessages());
+ assertSame(savedMessages,uiMessages.getRenderedMessages());
+ assertEquals(6, uiMessages.getRenderedMessages().size());
+ }
+ /**
+ * Test method for {@link org.richfaces.component.UIRichMessages#getSeverenities()}.
+ */
+ public void testGetSeverenities() {
+ uiMessages.setLevel("ALL,ERROR");
+ List<String> severenities = uiMessages.getSeverenities();
+ assertEquals(2, severenities.size());
+ assertTrue(severenities.contains("ALL"));
+ assertTrue(severenities.contains("ERROR"));
+ }
+
+ /**
+ * Test method for {@link org.richfaces.component.UIRichMessages#saveState(javax.faces.context.FacesContext)}.
+ */
+ public void testSaveStateFacesContext() {
+ prepareMessages();
+ uiMessages.getMessages(facesContext);
+ Object state = uiMessages.saveState(facesContext);
+ MockRichMessages newMessages = new MockRichMessages();
+ newMessages.restoreState(facesContext, state);
+ assertNotNull(newMessages.getRenderedMessages());
+ assertEquals(uiMessages.getRenderedMessages().size(), newMessages.getRenderedMessages().size());
+
+ }
+
+
+ public void testAddMessagesForId() throws Exception {
+ List<String> severenities = prepareMessages();
+ severenities.add("ALL");
+ uiMessages.setRenderedMessages(new ArrayList<FacesMessageWithId>());
+ uiMessages.addMessagesForId(facesContext, INPUT_ID, severenities);
+ assertEquals(4, uiMessages.getRenderedMessages().size());
+ }
+
+ public void testAddMessagesForIdNull() throws Exception {
+ List<String> severenities = prepareMessages();
+ uiMessages.setRenderedMessages(new ArrayList<FacesMessageWithId>());
+ uiMessages.addMessagesForId(facesContext, null, severenities);
+ assertEquals(4, uiMessages.getRenderedMessages().size());
+ }
+
+ public void testAddMessagesForIdWithLevels() throws Exception {
+ List<String> severenities = prepareMessages();
+ uiMessages.setRenderedMessages(new ArrayList<FacesMessageWithId>());
+ severenities.add("ERROR");
+ uiMessages.addMessagesForId(facesContext, INPUT_ID, severenities);
+ assertEquals(1, uiMessages.getRenderedMessages().size());
+ }
+ /**
+ * @return
+ */
+ private List<String> prepareMessages() {
+ addMessages(INPUT_ID);
+ addMessages(null);
+ List<String> severenities = new ArrayList<String>();
+ return severenities;
+ }
+
+ /**
+ * @param id TODO
+ *
+ */
+ private void addMessages(String id) {
+ facesContext.addMessage(id, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error","Error level message"));
+ facesContext.addMessage(id, new FacesMessage(FacesMessage.SEVERITY_FATAL,"Fatal","Fatal level message"));
+ facesContext.addMessage(id, new FacesMessage(FacesMessage.SEVERITY_INFO,"Info","Info level message"));
+ facesContext.addMessage(id, new FacesMessage(FacesMessage.SEVERITY_WARN,"Warn","Warn level message"));
+ }
+
+}
Property changes on: trunk/ui/message/src/test/java/org/richfaces/component/UIMessagesTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
More information about the richfaces-svn-commits
mailing list