[ 
https://issues.apache.org/jira/browse/TOMAHAWK-980?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12501887
 ] 

Emil Cazacu commented on TOMAHAWK-980:
--------------------------------------

Hi,

I encountered a bug, when dealing with <h:panelGroup .
In order to correct the behavior, I changed (locally - on my computer) the 
method addColumnValue(...):
- it adds in the excel cell the first text (notnull or not empty) that it finds 
in the tree of components, using a recursive call.

I add herein the code, and I hope that it will fit you needs.

--------------------
  /**
   * If it is null, simply adds an empty cell<br>
   * If it is a ValueHolder, adds empty cell with that value<br>
   * If it is a Command (link or button), tries to add the correct displayed
   * text - either on value attribute or in a ValueHolder child<br>
   */
  @SuppressWarnings("unchecked")
  private void addColumnValue(
      HSSFWorkbook workbook,
      HSSFRow rowHeader,
      UIComponent component,
      int index,
      boolean headerCell) {
    HSSFCell cell = rowHeader.createCell((short) index);
    cell.setEncoding(HSSFCell.ENCODING_UTF_16);

    if (headerCell) {
      // we make the line with the header in bold
      HSSFFont font = workbook.createFont();
      font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

      HSSFCellStyle style = workbook.createCellStyle();
      style.setFont(font);
      cell.setCellStyle(style);
    }

    String stringValue = getFirstTextFromUIComponent(component);

    if (stringValue != null) {
      cell.setCellValue(stringValue);
    }
  }



  /**
   * Recursive call<br>
   * This method will return the value of the first encountered ValueHolder.<br>
   * 
   * With this method we try meet the largest variation.<br>
   * It is far from perfect, and far from being any way configurable.
   */
  private String getFirstTextFromUIComponent(UIComponent component) {
    // null verification
    if (component == null) {
      return null;
    }

    // ValueHolder -> UIInput, UIOutput:
    if (component instanceof ValueHolder) {
      String value = 
RendererUtils.getStringValue(FacesContext.getCurrentInstance(),
          component);
      if (value != null && !"".equals(value)) {
        return value;
      }
    }

    // to cope with the fact that the values of the links are not displayed
    // <t:commandLink value="#{dataItem.denumireLunga}" ...
    if (component instanceof UICommand) {
      UICommand cmd = (UICommand) component;
      if (cmd.getValue() != null) {
        // if that command has value attribute
        String value = "" + cmd.getValue();
        if (value != null && !"".equals(value)) {
          return value;
        }
      }
    }

    // we loop through children to find a ValueHolder
    List<UIComponent> children = component.getChildren();
    for (int i = 0; i < children.size(); i++) {
      String value = getFirstTextFromUIComponent(children.get(i));
      if (value != null && !"".equals(value)) {
        // we have found a not null value in one of the children,
        // so we will return it
        return value;
      }
    }

    return null;
  }


> ExcelExport - corrections to Excel generation - when a commandLink on a column
> ------------------------------------------------------------------------------
>
>                 Key: TOMAHAWK-980
>                 URL: https://issues.apache.org/jira/browse/TOMAHAWK-980
>             Project: MyFaces Tomahawk
>          Issue Type: Bug
>    Affects Versions: 1.1.5
>         Environment: Suse 10.0
> Firefox 1.5.0.10
> apache-tomcat-5.5.20
>            Reporter: Emil Cazacu
>            Assignee: Cagatay Civici
>             Fix For: 1.1.5
>
>
> Bugs:
> 1. - If my datatable has a commandLink, the value on it is not displayed e.g.
> <t:column>
>     <f:facet name="header">
>         <t:outputText value="Product" />
>     </f:facet>
>     <t:commandLink action="navToEditForProdus">
>         <t:outputText value="#{dataItem.den}" />
>         <t:updateActionListener property="#{XxxUIBean.id}" 
> value="#{dataItem.id}" />
>     </t:commandLink>
> </t:column>
> I know that this one can be overcome by using 
>     <t:commandLink action="navToEditForProdus" value="#{dataItem.den}"
> but I have tones of code, I do not want to have this one on my mind too.
> 2. - the header is not bold, (this is less important)
> I have the correction (JSE 5), working ok for me:
> in class ExcelExportPhaseListener.java
>   /**
>    * If it is null, simply adds an empty cell<br>
>    * If it is a ValueHolder, adds empty cell with that value<br>
>    * If it is a Command (link or button), tries to add the correct displayed
>    * text - either on value attribute or in a ValueHolder child<br>
>    */
>   @SuppressWarnings("unchecked")
>   private void addColumnValue(
>       HSSFWorkbook workbook,
>       HSSFRow rowHeader,
>       UIComponent component,
>       int index,
>       boolean headerCell) {
>     HSSFCell cell = rowHeader.createCell((short) index);
>     cell.setEncoding(HSSFCell.ENCODING_UTF_16);
>     if (headerCell) {
>       // we make the line with the header in bold
>       HSSFFont font = workbook.createFont();
>       font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
>       HSSFCellStyle style = workbook.createCellStyle();
>       style.setFont(font);
>       cell.setCellStyle(style);
>     }
>     String stringValue = null;
>     if (component == null) {
>       // null is the returned value
>       stringValue = null;
>     } else if (component instanceof ValueHolder) {
>       stringValue = 
> RendererUtils.getStringValue(FacesContext.getCurrentInstance(),
>           component);
>     } else if (component instanceof UICommand) {
>       // to cope with the fact that the values of the links are not displayed
>       UICommand cmd = (UICommand) component;
>       if (cmd.getValue() != null) {
>         // if that command has value attribute
>         stringValue = "" + cmd.getValue();
>       } else {
>         // value attribute is null, so let's look in the children
>         List<UIComponent> children = cmd.getChildren();
>         for (int i = 0; i < children.size(); i++) {
>           UIComponent child = children.get(i);
>           if (child instanceof ValueHolder) {
>             // we eventually found a component with a value, we use it
>             stringValue = 
> RendererUtils.getStringValue(FacesContext.getCurrentInstance(),
>                 child);
>             break;
>           }
>         }
>       }
>     }
>     if (stringValue != null) {
>       cell.setCellValue(stringValue);
>     }
>   }
>   The other methods modified to cope with the changes int the method 
> addColumnValue(...).
>   Only simple pass of "HSSFWorkbook workbook" or "boolean headerCell" - 
> nothing more:
>   // for header
>   private void addColumnHeaders(HSSFWorkbook workbook, HSSFSheet sheet, List 
> columns) {
>     HSSFRow rowHeader = sheet.createRow(0);
>     for (int i = 0; i < columns.size(); i++) {
>       UIColumn column = (UIColumn) columns.get(i);
>       addColumnValue(workbook, rowHeader, column.getHeader(), i, true);
>     }
>   }
>   // for the other table 
>   private void addColumnValues(
>       HSSFWorkbook workbook,
>       HSSFSheet sheet,
>       List columns,
>       HtmlDataTable dataTable) {
>     for (int i = 0; i < dataTable.getRowCount(); i++) {
>       dataTable.setRowIndex(i);
>       HSSFRow row = sheet.createRow(i + 1);
>       for (int j = 0; j < columns.size(); j++) {
>         UIColumn column = (UIColumn) columns.get(j);
>         addColumnValue(workbook, row, (UIComponent) 
> column.getChildren().get(0), j, false);
>       }
>     }
>   }
>   // the caller
>   private HSSFWorkbook generateExcel(FacesContext facesContext, HtmlDataTable 
> table) {
>     HSSFWorkbook workbook = new HSSFWorkbook();
>     HSSFSheet sheet = workbook.createSheet(table.getId());
>     List columns = getColumns(table);
>     int currentRowIndex = table.getRowIndex();
>     addColumnHeaders(workbook, sheet, columns);
>     addColumnValues(workbook, sheet, columns, table);
>     table.setRowIndex(currentRowIndex);
>     return workbook;
>   }
>   
> I looked on the repository, and the latest sources are with the bug.
> http://svn.apache.org/repos/asf/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportPhaseListener.java
> Excel Exporter is a nice feature, and if there is anything I can do to help, 
> please contact me.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to