You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by bo...@apache.org on 2007/02/25 14:45:24 UTC

svn commit: r511512 - in /myfaces/tobago/trunk/example/test: ./ src/main/java/org/apache/myfaces/tobago/example/test/ src/main/webapp/ src/main/webapp/WEB-INF/

Author: bommel
Date: Sun Feb 25 05:45:23 2007
New Revision: 511512

URL: http://svn.apache.org/viewvc?view=rev&rev=511512
Log:
added export excel example to test

Added:
    myfaces/tobago/trunk/example/test/src/main/java/org/apache/myfaces/tobago/example/test/ExportUIDataToWorkbookUtil.java
Modified:
    myfaces/tobago/trunk/example/test/pom.xml
    myfaces/tobago/trunk/example/test/src/main/java/org/apache/myfaces/tobago/example/test/TestBean.java
    myfaces/tobago/trunk/example/test/src/main/webapp/WEB-INF/faces-config.xml
    myfaces/tobago/trunk/example/test/src/main/webapp/layout.jsp
    myfaces/tobago/trunk/example/test/src/main/webapp/solarList2.jsp

Modified: myfaces/tobago/trunk/example/test/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/test/pom.xml?view=diff&rev=511512&r1=511511&r2=511512
==============================================================================
--- myfaces/tobago/trunk/example/test/pom.xml (original)
+++ myfaces/tobago/trunk/example/test/pom.xml Sun Feb 25 05:45:23 2007
@@ -115,6 +115,11 @@
       </exclusions>
     </dependency>
     <dependency>
+      <groupId>poi</groupId>
+      <artifactId>poi</artifactId>
+      <version>2.5.1-final-20040804</version>
+    </dependency>
+    <dependency>
       <groupId>taglibs</groupId>
       <artifactId>standard</artifactId>
     </dependency>

Added: myfaces/tobago/trunk/example/test/src/main/java/org/apache/myfaces/tobago/example/test/ExportUIDataToWorkbookUtil.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/test/src/main/java/org/apache/myfaces/tobago/example/test/ExportUIDataToWorkbookUtil.java?view=auto&rev=511512
==============================================================================
--- myfaces/tobago/trunk/example/test/src/main/java/org/apache/myfaces/tobago/example/test/ExportUIDataToWorkbookUtil.java (added)
+++ myfaces/tobago/trunk/example/test/src/main/java/org/apache/myfaces/tobago/example/test/ExportUIDataToWorkbookUtil.java Sun Feb 25 05:45:23 2007
@@ -0,0 +1,125 @@
+package org.apache.myfaces.tobago.example.test;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFRow;
+import org.apache.poi.hssf.usermodel.HSSFCell;
+import org.apache.myfaces.tobago.component.UIColumnSelector;
+import org.apache.myfaces.tobago.component.UICommand;
+import org.apache.myfaces.tobago.renderkit.RenderUtil;
+import org.apache.commons.lang.StringUtils;
+
+import javax.faces.component.UIComponent;
+import javax.faces.component.UIColumn;
+import javax.faces.component.ValueHolder;
+import javax.faces.component.UIData;
+import javax.faces.context.FacesContext;
+import javax.servlet.http.HttpServletResponse;
+import java.util.List;
+import java.util.ArrayList;
+import java.io.IOException;
+
+
+public class ExportUIDataToWorkbookUtil {
+
+  public static void writeWorkbook(UIData data, String attachmentName, FacesContext context) throws IOException {
+    HSSFWorkbook workbook = createWorkbook(data, context);
+    Object response = context.getExternalContext().getResponse();
+    if (response instanceof HttpServletResponse) {
+      HttpServletResponse servletResponse = (HttpServletResponse) response;
+      servletResponse.setContentType("application/vnd.ms-excel");
+      if (StringUtils.isNotEmpty(attachmentName)) {
+        servletResponse.setHeader("Content-Disposition", "attachment; " + attachmentName);
+      }
+      workbook.write(servletResponse.getOutputStream());
+    } else {
+      //TODO PortletResponse
+    }
+  }
+
+  private static HSSFWorkbook createWorkbook(UIData table, FacesContext context) {
+    HSSFWorkbook workbook = new HSSFWorkbook();
+    HSSFSheet sheet = workbook.createSheet(table.getId());
+    List<UIColumn> columns = getColumns(table);
+    int currentRowIndex = table.getRowIndex();
+    addColumnHeaders(sheet, columns, context);
+    addColumnValues(sheet, columns, table, context);
+    table.setRowIndex(currentRowIndex);
+    return workbook;
+  }
+
+  private static List<UIColumn> getColumns(UIData table) {
+    List<UIColumn> columns = new ArrayList<UIColumn>();
+    for (int i = 0; i < table.getChildCount(); i++) {
+      UIComponent child = (UIComponent) table.getChildren().get(i);
+      if (child instanceof UIColumn && !(child instanceof UIColumnSelector)) {
+        columns.add((UIColumn) child);
+      }
+    }
+    return columns;
+  }
+
+  private static void addColumnValue(HSSFRow rowHeader, UIComponent component, int index, FacesContext context) {
+    HSSFCell cell = rowHeader.createCell((short) index);
+    cell.setEncoding(HSSFCell.ENCODING_UTF_16);
+    if (component instanceof ValueHolder) {
+      String stringValue = RenderUtil.getFormattedValue(context, component);
+      cell.setCellValue(stringValue);
+    } else if (component instanceof org.apache.myfaces.tobago.component.UIColumn
+        || component instanceof UICommand) {
+      String value = component.getAttributes().get("label") != null
+          ? component.getAttributes().get("label").toString() : "";
+      cell.setCellValue(value);
+    }
+  }
+
+  private static void addColumnHeaders(HSSFSheet sheet, List<UIColumn> columns, FacesContext context) {
+    HSSFRow rowHeader = sheet.createRow(0);
+    for (int i = 0; i < columns.size(); i++) {
+      UIColumn column = columns.get(i);
+      addColumnValue(rowHeader, column, i, context);
+    }
+  }
+
+  private static void addColumnValues(HSSFSheet sheet, List<UIColumn> columns, UIData table, FacesContext context) {
+    int rowCount = table.getRowCount();
+    if (rowCount == -1) {
+      int index = 0;
+      table.setRowIndex(index);
+      while (table.isRowAvailable()) {
+        addRow(sheet, index, columns, context);
+        table.setRowIndex(++index);
+      }
+    } else {
+      for (int i = 0; i < table.getRowCount(); i++) {
+        table.setRowIndex(i);
+        addRow(sheet, i, columns, context);
+      }
+    }
+  }
+
+  private static void addRow(HSSFSheet sheet, int index, List<UIColumn> columns, FacesContext context) {
+    HSSFRow row = sheet.createRow(1 + index);
+    for (int j = 0; j < columns.size(); j++) {
+      UIColumn column = columns.get(j);
+      addColumnValue(row, (UIComponent) column.getChildren().get(0), j, context);
+    }
+  }
+}

Modified: myfaces/tobago/trunk/example/test/src/main/java/org/apache/myfaces/tobago/example/test/TestBean.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/test/src/main/java/org/apache/myfaces/tobago/example/test/TestBean.java?view=diff&rev=511512&r1=511511&r2=511512
==============================================================================
--- myfaces/tobago/trunk/example/test/src/main/java/org/apache/myfaces/tobago/example/test/TestBean.java (original)
+++ myfaces/tobago/trunk/example/test/src/main/java/org/apache/myfaces/tobago/example/test/TestBean.java Sun Feb 25 05:45:23 2007
@@ -38,6 +38,8 @@
 import java.sql.ResultSetMetaData;
 import java.sql.Types;
 import java.util.Map;
+import java.util.Date;
+import java.io.IOException;
 
 /*
  * Created by IntelliJ IDEA.
@@ -62,6 +64,45 @@
   private String discoverYear;
   private FileItem file;
   private UIData table;
+  private String value;
+  private Date date;
+  private Date date1;
+
+  public String layout() {
+    this.date1 = date;
+    return "layout";
+  }
+
+  public String export() throws IOException {
+    FacesContext context =  FacesContext.getCurrentInstance();
+    ExportUIDataToWorkbookUtil.writeWorkbook(table, "workbook.xls", context);
+    context.responseComplete();
+    return null;
+  }
+
+  public Date getDate() {
+    return date;
+  }
+
+  public void setDate(Date date) {
+    this.date = date;
+  }
+
+  public Date getDate1() {
+    return date1;
+  }
+
+  public void setDate1(Date date1) {
+    this.date1 = date1;
+  }
+
+  public String getValue() {
+    return value;
+  }
+
+  public void setValue(String value) {
+    this.value = value;
+  }
 
   public TestBean() {
 

Modified: myfaces/tobago/trunk/example/test/src/main/webapp/WEB-INF/faces-config.xml
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/test/src/main/webapp/WEB-INF/faces-config.xml?view=diff&rev=511512&r1=511511&r2=511512
==============================================================================
--- myfaces/tobago/trunk/example/test/src/main/webapp/WEB-INF/faces-config.xml (original)
+++ myfaces/tobago/trunk/example/test/src/main/webapp/WEB-INF/faces-config.xml Sun Feb 25 05:45:23 2007
@@ -48,6 +48,13 @@
       <from-outcome>solarDetail</from-outcome>
       <to-view-id>solarDetail.jsp</to-view-id>
     </navigation-case>
- </navigation-rule>
+  </navigation-rule>
+
+  <navigation-rule>
+    <navigation-case>
+      <from-outcome>layout</from-outcome>
+      <to-view-id>/layout.jsp</to-view-id>
+    </navigation-case>
+  </navigation-rule>
 
 </faces-config>

Modified: myfaces/tobago/trunk/example/test/src/main/webapp/layout.jsp
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/test/src/main/webapp/layout.jsp?view=diff&rev=511512&r1=511511&r2=511512
==============================================================================
--- myfaces/tobago/trunk/example/test/src/main/webapp/layout.jsp (original)
+++ myfaces/tobago/trunk/example/test/src/main/webapp/layout.jsp Sun Feb 25 05:45:23 2007
@@ -19,7 +19,7 @@
 <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 <f:view>
-  <tc:page>
+  <tc:page width="400" height="300">
     <tc:panel>
       <f:facet name="layout">
         <tc:gridLayout margin="10px" rows="1*;fixed;1*" />
@@ -28,12 +28,20 @@
       <tc:box label="the outer box">
 
         <f:facet name="layout">
-          <tc:gridLayout margin="10px" rows="fixed;fixed;fixed" />
+          <tc:gridLayout margin="10px" rows="fixed;fixed;fixed;fixed" />
         </f:facet>
 
         <tc:messages />
 
-        <tx:in label="SerachCriteria" value="" />
+        <tx:date label="SearchCriteria" value="#{test.date}" >
+          <f:convertDateTime pattern="dd/MM/yyyy" />
+           <%--f:facet name="change">
+            <tc:command action="#{test.layout}" />
+          </f:facet --%>
+        </tx:date>
+        <tx:in value="#{test.date1}" >
+          <f:convertDateTime pattern="dd/MM/yyyy" />
+        </tx:in>
 
         <tc:panel>
           <f:facet name="layout">
@@ -41,7 +49,7 @@
           </f:facet>
 
           <tc:cell />
-          <tc:button action="" label="Search" defaultCommand="true" />
+          <tc:button id="test" action="#{test.layout}" label="Search"  />
 
         </tc:panel>
 

Modified: myfaces/tobago/trunk/example/test/src/main/webapp/solarList2.jsp
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/example/test/src/main/webapp/solarList2.jsp?view=diff&rev=511512&r1=511511&r2=511512
==============================================================================
--- myfaces/tobago/trunk/example/test/src/main/webapp/solarList2.jsp (original)
+++ myfaces/tobago/trunk/example/test/src/main/webapp/solarList2.jsp Sun Feb 25 05:45:23 2007
@@ -18,7 +18,7 @@
 <f:view>
   <tc:page width="750px" height="300px">
     <f:facet name="layout">
-      <tc:gridLayout/>
+      <tc:gridLayout rows="1*;20px"/>
     </f:facet>
     <tc:box >
        <tc:sheet binding="#{test.table}" value="#{test.solarObjects}"
@@ -26,5 +26,14 @@
             showHeader="true"  showPageRange="center" rows="10" >
         </tc:sheet>
     </tc:box>
+    <tc:panel>
+      <f:facet name="layout">
+        <tc:gridLayout columns="1*;fixed;1*"/>
+      </f:facet>
+      <tc:cell/>
+      <tc:button action="#{test.export}" label="Export" transition="false" />
+      <tc:cell/>
+    </tc:panel>
+
   </tc:page>
 </f:view>