[seam-commits] Seam SVN: r9146 - trunk/ui/src/main/java/org/jboss/seam/ui/renderkit.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Mon Sep 29 08:36:37 EDT 2008


Author: nickarls
Date: 2008-09-29 08:36:36 -0400 (Mon, 29 Sep 2008)
New Revision: 9146

Modified:
   trunk/ui/src/main/java/org/jboss/seam/ui/renderkit/DecorateRendererBase.java
Log:
JBSEAM-3471

Modified: trunk/ui/src/main/java/org/jboss/seam/ui/renderkit/DecorateRendererBase.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/renderkit/DecorateRendererBase.java	2008-09-26 14:58:00 UTC (rev 9145)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/renderkit/DecorateRendererBase.java	2008-09-29 12:36:36 UTC (rev 9146)
@@ -1,6 +1,8 @@
 package org.jboss.seam.ui.renderkit;
 
 import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
 
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
@@ -14,107 +16,146 @@
 
 public class DecorateRendererBase extends RendererBase
 {
-
+   // Place the attributes you want to store away
+   private Map<String, Object> originalValues = new HashMap();
+   // The list of attributes in the event scope to store away
+   String[] storeOriginals = new String[] {"invalid", "required"}; 
+   
    @Override
    protected Class getComponentClass()
    {
       return UIDecorate.class;
    }
+
+   /**
+    * Store away the attribute from the event context (if it is set)
+    * 
+    * @param names The list of context keys to store away
+    */
+   private void storeOriginalValues(String[] names)
+   {
+      for (String name : names)
+      {
+         if (Contexts.getEventContext().isSet(name))
+         {
+            originalValues.put(name, Contexts.getEventContext().get(name));
+         }
+      }
+   }
    
+   /**
+    * Restores the state of the event context. If the value is stored away, it is restored
+    * It it was not in the map, it was not in the context in the first place so clean
+    * up what we have placed there during this run.
+    * 
+    * @param names The list of context keys to restore
+    */
+   private void restoreOriginalValues(String[] names) {
+      for (String name : names) {
+         if (originalValues.containsKey(name)) {
+            Contexts.getEventContext().set(name, originalValues.get(name));
+         } else {
+            Contexts.getEventContext().remove(name);
+         }
+      }
+   }
+
    @Override
    protected void doEncodeBegin(ResponseWriter writer, FacesContext context, UIComponent component) throws IOException
    {
       UIDecorate decorate = (UIDecorate) component;
-      
+
+      storeOriginalValues(storeOriginals);
+
       Contexts.getEventContext().set("invalid", Decoration.hasMessage(decorate, context));
       Contexts.getEventContext().set("required", Decoration.hasRequired(component, context));
- 
+
       boolean hasMessage = decorate.hasMessage();
-      
+
       writer.startElement("div", decorate);
       if (decorate.getStyleClass() != null)
       {
-          writer.writeAttribute(HTML.CLASS_ATTR, decorate.getStyleClass(), HTML.CLASS_ATTR);
+         writer.writeAttribute(HTML.CLASS_ATTR, decorate.getStyleClass(), HTML.CLASS_ATTR);
       }
       if (decorate.getStyle() != null)
       {
-          writer.writeAttribute(HTML.STYLE_ATTR, decorate.getStyle(), HTML.STYLE_ATTR);
+         writer.writeAttribute(HTML.STYLE_ATTR, decorate.getStyle(), HTML.STYLE_ATTR);
       }
       writer.writeAttribute("id", decorate.getClientId(context), "id");
-      
+
       UIComponent aroundDecoration = decorate.getDecoration("aroundField");
       UIComponent aroundInvalidDecoration = decorate.getDecoration("aroundInvalidField");
-      if (aroundDecoration!=null && !hasMessage)
+      if (aroundDecoration != null && !hasMessage)
       {
          aroundDecoration.setParent(decorate);
          aroundDecoration.encodeBegin(context);
       }
-      if (aroundInvalidDecoration!=null && hasMessage)
+      if (aroundInvalidDecoration != null && hasMessage)
       {
          aroundInvalidDecoration.setParent(decorate);
          aroundInvalidDecoration.encodeBegin(context);
       }
    }
-   
+
    @Override
    protected void doEncodeEnd(ResponseWriter writer, FacesContext context, UIComponent component) throws IOException
    {
       UIDecorate decorate = (UIDecorate) component;
-      
+
       boolean hasMessage = decorate.hasMessage();
       UIComponent aroundDecoration = decorate.getDecoration("aroundField");
       UIComponent aroundInvalidDecoration = decorate.getDecoration("aroundInvalidField");
-      if (aroundDecoration!=null && !hasMessage)
+      if (aroundDecoration != null && !hasMessage)
       {
          aroundDecoration.setParent(decorate);
          aroundDecoration.encodeEnd(context);
       }
-      if (aroundInvalidDecoration!=null && hasMessage)
+      if (aroundInvalidDecoration != null && hasMessage)
       {
          aroundInvalidDecoration.setParent(decorate);
          aroundInvalidDecoration.encodeEnd(context);
       }
       context.getResponseWriter().endElement("div");
 
-      Contexts.getEventContext().remove("invalid");
-      Contexts.getEventContext().remove("required");
+      restoreOriginalValues(storeOriginals);
    }
-   
+
    @Override
    protected void doEncodeChildren(ResponseWriter writer, FacesContext context, UIComponent component) throws IOException
    {
       UIDecorate decorate = (UIDecorate) component;
-      
+
       boolean hasMessage = decorate.hasMessage();
 
       UIComponent beforeDecoration = decorate.getDecoration("beforeField");
       UIComponent beforeInvalidDecoration = decorate.getDecoration("beforeInvalidField");
-      if ( beforeDecoration!=null && !hasMessage )
+      if (beforeDecoration != null && !hasMessage)
       {
          beforeDecoration.setParent(decorate);
          renderChild(context, beforeDecoration);
       }
-      if ( beforeInvalidDecoration!=null && hasMessage )
+      if (beforeInvalidDecoration != null && hasMessage)
       {
          beforeInvalidDecoration.setParent(decorate);
          renderChild(context, beforeInvalidDecoration);
       }
-      
+
       renderChildren(context, decorate);
-      
+
       UIComponent afterDecoration = decorate.getDecoration("afterField");
       UIComponent afterInvalidDecoration = decorate.getDecoration("afterInvalidField");
-      if ( afterDecoration!=null  && !hasMessage )
+      if (afterDecoration != null && !hasMessage)
       {
          afterDecoration.setParent(decorate);
-         renderChild(context, afterDecoration);      }
-      if ( afterInvalidDecoration!=null && hasMessage )
+         renderChild(context, afterDecoration);
+      }
+      if (afterInvalidDecoration != null && hasMessage)
       {
          afterInvalidDecoration.setParent(decorate);
          renderChild(context, afterInvalidDecoration);
       }
    }
-   
+
    @Override
    public boolean getRendersChildren()
    {




More information about the seam-commits mailing list