[seam-commits] Seam SVN: r8820 - trunk/src/excel/org/jboss/seam/excel/ui.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Tue Aug 26 14:27:00 EDT 2008


Author: nickarls
Date: 2008-08-26 14:26:58 -0400 (Tue, 26 Aug 2008)
New Revision: 8820

Modified:
   trunk/src/excel/org/jboss/seam/excel/ui/ExcelComponent.java
   trunk/src/excel/org/jboss/seam/excel/ui/UICell.java
   trunk/src/excel/org/jboss/seam/excel/ui/UIColumn.java
   trunk/src/excel/org/jboss/seam/excel/ui/UIExcelExport.java
   trunk/src/excel/org/jboss/seam/excel/ui/UIWorkbook.java
   trunk/src/excel/org/jboss/seam/excel/ui/UIWorksheet.java
Log:
JBSEAM-3233 and some minor streamlining

Modified: trunk/src/excel/org/jboss/seam/excel/ui/ExcelComponent.java
===================================================================
--- trunk/src/excel/org/jboss/seam/excel/ui/ExcelComponent.java	2008-08-26 18:17:14 UTC (rev 8819)
+++ trunk/src/excel/org/jboss/seam/excel/ui/ExcelComponent.java	2008-08-26 18:26:58 UTC (rev 8820)
@@ -1,16 +1,23 @@
 package org.jboss.seam.excel.ui;
 
+import java.io.IOException;
+import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.List;
 
 import javax.faces.component.UIComponent;
 import javax.faces.component.UIComponentBase;
 import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
+import javax.faces.render.RenderKit;
 
 import org.jboss.seam.excel.Command;
 import org.jboss.seam.excel.ExcelWorkbook;
 import org.jboss.seam.excel.Template;
 import org.jboss.seam.excel.WorksheetItem;
+import org.jboss.seam.log.Log;
+import org.jboss.seam.log.Logging;
+import org.jboss.seam.ui.util.JSF;
 
 /**
  * Common superclass for the UI components. Contains helper methods for merging
@@ -23,7 +30,37 @@
 public abstract class ExcelComponent extends UIComponentBase
 {
    public final static String HEADER_FACET = "header";
+   private static final String DEFAULT_CONTENT_TYPE = "text/html";
+   private static final String DEFAULT_CHARACTER_ENCODING = "utf-8";
 
+
+   /**
+    * Helper method for rendering a component (usually on a facescontext with a caching
+    * reponsewriter)
+    * 
+    * @param facesContext The faces context to render to
+    * @param component The component to render
+    * @return The textual representation of the component
+    * @throws IOException If the JSF helper class can't render the component
+    */
+   public static String cmp2String(FacesContext facesContext, UIComponent component) throws IOException
+   {
+      ResponseWriter oldResponseWriter = facesContext.getResponseWriter();
+      String contentType = oldResponseWriter != null ? oldResponseWriter.getContentType() : DEFAULT_CONTENT_TYPE;
+      String characterEncoding = oldResponseWriter != null ? oldResponseWriter.getCharacterEncoding() : DEFAULT_CHARACTER_ENCODING;
+      RenderKit renderKit = facesContext.getRenderKit();
+      StringWriter cacheingWriter = new StringWriter();
+      ResponseWriter newResponseWriter = renderKit.createResponseWriter(cacheingWriter, contentType, characterEncoding);
+      facesContext.setResponseWriter(newResponseWriter);
+      JSF.renderChild(facesContext, component);
+      if (oldResponseWriter != null) {
+         facesContext.setResponseWriter(oldResponseWriter);
+      }
+      cacheingWriter.flush();
+      cacheingWriter.close();
+      return cacheingWriter.toString();
+   }    
+   
    public ExcelComponent()
    {
       super();

Modified: trunk/src/excel/org/jboss/seam/excel/ui/UICell.java
===================================================================
--- trunk/src/excel/org/jboss/seam/excel/ui/UICell.java	2008-08-26 18:17:14 UTC (rev 8819)
+++ trunk/src/excel/org/jboss/seam/excel/ui/UICell.java	2008-08-26 18:26:58 UTC (rev 8820)
@@ -1,9 +1,14 @@
 package org.jboss.seam.excel.ui;
 
+import java.io.IOException;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.util.Date;
 
+import javax.faces.context.FacesContext;
+
+import org.jboss.seam.core.Interpolator;
+import org.jboss.seam.excel.ExcelWorkbookException;
 import org.jboss.seam.excel.WorksheetItem;
 
 public class UICell extends UICellFormat implements WorksheetItem
@@ -50,10 +55,20 @@
    {
       this.row = row;
    }
-
+  
+   
    public Object getValue()
    {
-      return valueOf("value", value);
+      Object theValue = valueOf("value", value);
+      if (theValue == null) {
+         try {
+            theValue = cmp2String(FacesContext.getCurrentInstance(), this);
+         } catch (IOException e) {
+            String message = Interpolator.instance().interpolate("Could not render cell #0", getId());
+            throw new ExcelWorkbookException(message, e);
+         }
+      }
+      return theValue;
    }
 
    public void setValue(Object value)

Modified: trunk/src/excel/org/jboss/seam/excel/ui/UIColumn.java
===================================================================
--- trunk/src/excel/org/jboss/seam/excel/ui/UIColumn.java	2008-08-26 18:17:14 UTC (rev 8819)
+++ trunk/src/excel/org/jboss/seam/excel/ui/UIColumn.java	2008-08-26 18:26:58 UTC (rev 8820)
@@ -1,6 +1,7 @@
 package org.jboss.seam.excel.ui;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 
@@ -71,7 +72,7 @@
 
    @SuppressWarnings("unchecked")
    @Override
-   public void encodeBegin(FacesContext arg0) throws IOException
+   public void encodeBegin(FacesContext facesContext) throws IOException
    {
       /**
        * Get workbook and worksheet
@@ -120,28 +121,36 @@
        */
       for (WorksheetItem item : getItems(getChildren()))
       {
-         if (item != null)
+         Object oldValue = null;
+         Iterator iterator = null;
+         // Store away the old value for the sheet binding var (if there is one)
+         if (sheet.getVar() != null) {
+            oldValue = FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get(sheet.getVar());
+            iterator = sheet.getDataIterator();
+         } else {
+            // No var, no iteration...
+            iterator = new ArrayList().iterator();
+         }
+         while (iterator.hasNext())
          {
-            // Store away the old value for the sheet binding var
-            Object oldValue = FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get(sheet.getVar());
-            Iterator iterator = sheet.getDataIterator();
-            while (iterator.hasNext())
-            {
-               // Store the bound data in the request map and add the cell
-               FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put(sheet.getVar(), iterator.next());
-               excelWorkbook.addItem(item);
-            }
+            // Store the bound data in the request map and add the cell
+            FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put(sheet.getVar(), iterator.next());
+            excelWorkbook.addItem(item);
+         }
 
-            // Restore the previously modified request map
-            if (oldValue == null)
-            {
-               FacesContext.getCurrentInstance().getExternalContext().getRequestMap().remove(sheet.getVar());
-            }
-            else
-            {
-               FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put(sheet.getVar(), oldValue);
-            }
+         //  No iteration, nothing to restore
+         if (sheet.getVar() == null) {
+            continue;
          }
+         // Restore the previously modified request map (if there was a var)
+         if (oldValue == null)
+         {
+            FacesContext.getCurrentInstance().getExternalContext().getRequestMap().remove(sheet.getVar());
+         }
+         else
+         {
+            FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put(sheet.getVar(), oldValue);
+         }
       }
 
       /**

Modified: trunk/src/excel/org/jboss/seam/excel/ui/UIExcelExport.java
===================================================================
--- trunk/src/excel/org/jboss/seam/excel/ui/UIExcelExport.java	2008-08-26 18:17:14 UTC (rev 8819)
+++ trunk/src/excel/org/jboss/seam/excel/ui/UIExcelExport.java	2008-08-26 18:26:58 UTC (rev 8820)
@@ -41,7 +41,7 @@
 
    @SuppressWarnings("unchecked")
    @Override
-   public void encodeBegin(javax.faces.context.FacesContext arg0) throws IOException
+   public void encodeBegin(javax.faces.context.FacesContext facesContext) throws IOException
    {
       UIData dataTable = (UIData) getParentByClass(getParent(), UIData.class);
       if (dataTable == null)

Modified: trunk/src/excel/org/jboss/seam/excel/ui/UIWorkbook.java
===================================================================
--- trunk/src/excel/org/jboss/seam/excel/ui/UIWorkbook.java	2008-08-26 18:17:14 UTC (rev 8819)
+++ trunk/src/excel/org/jboss/seam/excel/ui/UIWorkbook.java	2008-08-26 18:26:58 UTC (rev 8820)
@@ -287,7 +287,7 @@
 
    @SuppressWarnings("unchecked")
    @Override
-   public void encodeBegin(javax.faces.context.FacesContext arg0) throws IOException
+   public void encodeBegin(javax.faces.context.FacesContext facesContext) throws IOException
    {
       timing = new Date().getTime();
       /**

Modified: trunk/src/excel/org/jboss/seam/excel/ui/UIWorksheet.java
===================================================================
--- trunk/src/excel/org/jboss/seam/excel/ui/UIWorksheet.java	2008-08-26 18:17:14 UTC (rev 8819)
+++ trunk/src/excel/org/jboss/seam/excel/ui/UIWorksheet.java	2008-08-26 18:26:58 UTC (rev 8820)
@@ -92,7 +92,7 @@
    }
 
    @Override
-   public void encodeBegin(javax.faces.context.FacesContext arg0) throws java.io.IOException
+   public void encodeBegin(javax.faces.context.FacesContext facesContext) throws java.io.IOException
    {
       /**
        * Get workbook




More information about the seam-commits mailing list