[Schmitzm-commits] r1980 - trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Thu May 3 18:37:06 CEST 2012


Author: mojays
Date: 2012-05-03 18:37:06 +0200 (Thu, 03 May 2012)
New Revision: 1980

Added:
   trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/AbstractExportDataProvider.java
   trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/ExportDataProvider.java
Removed:
   trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/LineExporter.java
Modified:
   trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/CellValueConverter.java
   trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/DefaultCellValueConverter.java
   trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/ExportUtil.java
Log:
LineExporter replaced by ExportDataProvider

Added: trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/AbstractExportDataProvider.java
===================================================================
--- trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/AbstractExportDataProvider.java	                        (rev 0)
+++ trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/AbstractExportDataProvider.java	2012-05-03 16:37:06 UTC (rev 1980)
@@ -0,0 +1,63 @@
+/**
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ * 
+ * This file is part of the SCHMITZM library - a collection of utility 
+ * classes based on Java 1.6, focusing (not only) on Java Swing 
+ * and the Geotools library.
+ * 
+ * The SCHMITZM project is hosted at:
+ * http://wald.intevation.org/projects/schmitzm/
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License (license.txt)
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ * or try this link: http://www.gnu.org/licenses/lgpl.html
+ * 
+ * Contributors:
+ *     Martin O. J. Schmitz - initial API and implementation
+ *     Stefan A. Tzeggai - additional utility classes
+ */
+package de.schmitzm.jxl.export;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Abstract implementation for {@link ExportDataProvider} which does not (yet)
+ * implement cell value conversion and no special cell formats.
+ * @see DefaultCellValueConverter
+ * @author Martin O.J. Schmitz
+ *
+ */
+public abstract class AbstractExportDataProvider<E> extends DefaultCellValueConverter implements ExportDataProvider<E>{
+  /**
+   * Provides putting the export values to the given
+   * map. Simply calls {@link #getData(int)} and {@link #exportTo(Object, Map)}!
+   * @param idx index of data element to put in map
+   */
+  @Override
+  public void exportTo(int idx, Map<String, Object> map) {
+    E data = getData(idx);
+    exportTo(data, map);
+  }
+  
+  /**
+   * Called when the export process does not need a data
+   * element anymore. Provides the possibility to free memory.
+   * This default implementation does NOTHING!
+   */
+  @Override
+  public void disposeData(int idx) {
+  }
+
+}

Modified: trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/CellValueConverter.java
===================================================================
--- trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/CellValueConverter.java	2012-05-03 08:37:11 UTC (rev 1979)
+++ trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/CellValueConverter.java	2012-05-03 16:37:06 UTC (rev 1980)
@@ -5,7 +5,6 @@
 /**
  * Interface to convert a cell value before export to excel cell.
  * @see ExportUtil#createExcelCell(int, int, Object, String)
- * @see ExportUtil#exportToExcel(java.io.File, String, java.util.List, LineExporter, CellValueConverter, javax.swing.JProgressBar)
  */
 public interface CellValueConverter {
   /**

Modified: trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/DefaultCellValueConverter.java
===================================================================
--- trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/DefaultCellValueConverter.java	2012-05-03 08:37:11 UTC (rev 1979)
+++ trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/DefaultCellValueConverter.java	2012-05-03 16:37:06 UTC (rev 1980)
@@ -5,7 +5,6 @@
 /**
  * Default implementation of {@link CellValueConverter}.
  * @see ExportUtil#createExcelCell(int, int, Object, String)
- * @see ExportUtil#exportToExcel(java.io.File, String, java.util.List, LineExporter, CellValueConverter, javax.swing.JProgressBar)
  */
 public class DefaultCellValueConverter {
   /**

Added: trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/ExportDataProvider.java
===================================================================
--- trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/ExportDataProvider.java	                        (rev 0)
+++ trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/ExportDataProvider.java	2012-05-03 16:37:06 UTC (rev 1980)
@@ -0,0 +1,76 @@
+/**
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ * 
+ * This file is part of the SCHMITZM library - a collection of utility 
+ * classes based on Java 1.6, focusing (not only) on Java Swing 
+ * and the Geotools library.
+ * 
+ * The SCHMITZM project is hosted at:
+ * http://wald.intevation.org/projects/schmitzm/
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License (license.txt)
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ * or try this link: http://www.gnu.org/licenses/lgpl.html
+ * 
+ * Contributors:
+ *     Martin O. J. Schmitz - initial API and implementation
+ *     Stefan A. Tzeggai - additional utility classes
+ */
+package de.schmitzm.jxl.export;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Interface to generically provide informations to
+ * {@link ExportUtil#exportToExcelFile(java.io.File, String, ExportDataProvider, javax.swing.JProgressBar)}.
+ * @author Martin O.J. Schmitz
+ *
+ */
+public interface ExportDataProvider<E> extends CellValueConverter {
+  /**
+   * Returns the columns to export.
+   */
+  public List<String> getHeaderColumns();
+
+  /**
+   * Returns the number of data elements to export
+   */
+  public int getDataCount();
+  
+  /**
+   * Returns a data element.
+   */
+  public E getData(int idx);
+  
+  /**
+   * Provides putting the export values to the given
+   * map.
+   */
+  public void exportTo(E data, Map<String,Object> map);
+
+  /**
+   * Provides putting the export values to the given
+   * map.
+   * @param idx index of data element to put in map
+   */
+  public void exportTo(int idx, Map<String,Object> map);
+  
+  /**
+   * Called when the export process does not need a data
+   * element anymore. Provides the possibility to free memory.
+   */
+  public void disposeData(int idx);
+
+}

Modified: trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/ExportUtil.java
===================================================================
--- trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/ExportUtil.java	2012-05-03 08:37:11 UTC (rev 1979)
+++ trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/ExportUtil.java	2012-05-03 16:37:06 UTC (rev 1980)
@@ -62,29 +62,116 @@
     //////////////  Helper methods for EXCEL export  ///////////////////
     ////////////////////////////////////////////////////////////////////
 
+//    /**
+//     * Exports data of the list an Excel file.
+//     * @param exportFile destination file
+//     * @param dataList list of data to export
+//     * @param exporter handles the export of one item of {@code dataList}
+//     * @param cellConverter converts a cell value before creating the
+//     *                      excel cell (can be {@code null})
+////     * @param evictAfterExport indicates whether contact is released from database
+////     *                         after line is exported 
+//     * @return number of exported records
+//     */
+//    public static <E> int exportToExcelFile(File exportFile, String sheetName, List<E> dataList, LineExporter<E> exporter, CellValueConverter cellConverter/*, boolean evictAfterExport*/, JProgressBar progressBar) throws IOException {
+//      // Initialisieren der JXL Formate (irgendwie gibt es Probleme, wenn diese nicht fuer jeden
+//      // Export neu erzeugt werden?!)
+//      CELL_FORMAT_DECIMAL  = new WritableCellFormat( DEFAULT_CELL_FORMAT_DECIMAL );
+//      CELL_FORMAT_INT      = new WritableCellFormat( DEFAULT_CELL_FORMAT_INT );
+////      CELL_FORMAT_MITGL_NR = new WritableCellFormat( DEFAULT_CELL_FORMAT_MITGL_NR );    	
+////      CELL_FORMAT_PLZ      = new WritableCellFormat( DEFAULT_CELL_FORMAT_PLZ );
+//      
+//      if ( !exportFile.exists() && StringUtils.isBlank(IOUtil.getFileExt(exportFile)) )
+//        exportFile = IOUtil.appendFileExt(exportFile, "xls");
+//      SwingUtil.setProgressBarDeterminateOnEDT(progressBar, dataList.size());
+////      Session session = HibernateSessionFactory.getSession();
+//      int expCount = 0;
+//      WritableWorkbook workbook = null;
+//      WritableSheet sheet = null;
+//      try {
+//        // Create cell format for header cells (derive from
+//        // default label cell)
+//        WritableCellFormat headerCellFormat = new WritableCellFormat(new Blank(0,0).getCellFormat());
+//        WritableFont       headerFont       = new WritableFont(headerCellFormat.getFont());
+//        headerFont.setBoldStyle(WritableFont.BOLD);
+//        headerCellFormat.setFont(headerFont);
+//        headerCellFormat.setBackground(Colour.SKY_BLUE);
+//        headerCellFormat.setBorder(Border.BOTTOM, BorderLineStyle.THIN);
+////        headerCellFormat.setShrinkToFit(true);
+//        
+//        // Create workbook
+//        workbook = Workbook.createWorkbook(exportFile);
+//        sheet = workbook.createSheet(sheetName,0);
+//        sheet.getSettings().setVerticalFreeze(1);
+//        sheet.getSettings().setPrintTitlesRow(0, 0);
+//        // Create header line
+//        List<String> header = exporter.getHeaderColumns();
+//        for (int col=0; col<header.size(); col++) {
+//          Label cell = new Label(col,0,header.get(col));
+//          cell.setCellFormat(headerCellFormat);
+//          sheet.addCell(cell);
+//        }
+//        // Write a line for each contact
+//        Map<String,Object> map = new HashMap<String, Object>();
+//        int row = 0;
+//        for (E data : dataList) {
+//          row++;
+//          // Let contact instance fill prepared map
+//          map.clear();
+////          session.refresh(data);
+//          exporter.exportTo(data,map);
+//          // put values in excel cells
+//          for (int col=0; col<header.size(); col++) {
+//            String key = header.get(col);
+//			Object value = map.get(key);
+//            if ( value == null )
+//              continue;
+//            WritableCell cell = createExcelCell(col, row, value, key, cellConverter);
+//            sheet.addCell(cell);
+//          }
+////          // release contact
+////          if ( evictAfterExport )
+////            session.evict(data);
+//
+//          expCount++;
+//          SwingUtil.updateProgressBarValueOnEDT(progressBar, expCount);
+//        }
+//        // for "small" sheets, adjust the column width
+//        if ( sheet.getColumns() <= 20 )
+//          autoSizeColumns(sheet);
+//      } catch (RowsExceededException err) {
+//        throw new IOException(err);
+//      } catch (WriteException err) {
+//        throw new IOException(err);
+//      } finally {
+//        if ( workbook != null )
+//          try {
+//            workbook.write();
+//            workbook.close();
+//          } catch (WriteException err) {
+//            throw new IOException(err);
+//          }
+//      }
+//      return expCount;
+//    }
+
     /**
      * Exports data of the list an Excel file.
      * @param exportFile destination file
      * @param dataList list of data to export
-     * @param exporter handles the export of one item of {@code dataList}
-     * @param cellConverter converts a cell value before creating the
-     *                      excel cell (can be {@code null})
-//     * @param evictAfterExport indicates whether contact is released from database
-//     *                         after line is exported 
-     * @return number of exported contacts
+     * @param dataProvider provides the data to export, handles the export of one item to map and
+     *                     special cell value conversion or cell format
+     * @return number of exported records
      */
-    public static <E> int exportToExcelFile(File exportFile, String sheetName, List<E> dataList, LineExporter<E> exporter, CellValueConverter cellConverter/*, boolean evictAfterExport*/, JProgressBar progressBar) throws IOException {
+    public static <E> int exportToExcelFile(File exportFile, String sheetName, ExportDataProvider<E> dataProvider, JProgressBar progressBar) throws IOException {
       // Initialisieren der JXL Formate (irgendwie gibt es Probleme, wenn diese nicht fuer jeden
       // Export neu erzeugt werden?!)
       CELL_FORMAT_DECIMAL  = new WritableCellFormat( DEFAULT_CELL_FORMAT_DECIMAL );
       CELL_FORMAT_INT      = new WritableCellFormat( DEFAULT_CELL_FORMAT_INT );
-//      CELL_FORMAT_MITGL_NR = new WritableCellFormat( DEFAULT_CELL_FORMAT_MITGL_NR );    	
-//      CELL_FORMAT_PLZ      = new WritableCellFormat( DEFAULT_CELL_FORMAT_PLZ );
       
       if ( !exportFile.exists() && StringUtils.isBlank(IOUtil.getFileExt(exportFile)) )
         exportFile = IOUtil.appendFileExt(exportFile, "xls");
-      SwingUtil.setProgressBarDeterminateOnEDT(progressBar, dataList.size());
-//      Session session = HibernateSessionFactory.getSession();
+      SwingUtil.setProgressBarDeterminateOnEDT(progressBar, dataProvider.getDataCount());
       int expCount = 0;
       WritableWorkbook workbook = null;
       WritableSheet sheet = null;
@@ -105,7 +192,7 @@
         sheet.getSettings().setVerticalFreeze(1);
         sheet.getSettings().setPrintTitlesRow(0, 0);
         // Create header line
-        List<String> header = exporter.getHeaderColumns();
+        List<String> header = dataProvider.getHeaderColumns();
         for (int col=0; col<header.size(); col++) {
           Label cell = new Label(col,0,header.get(col));
           cell.setCellFormat(headerCellFormat);
@@ -114,25 +201,21 @@
         // Write a line for each contact
         Map<String,Object> map = new HashMap<String, Object>();
         int row = 0;
-        for (E data : dataList) {
+        for (int i=0; i<dataProvider.getDataCount(); i++) {
           row++;
           // Let contact instance fill prepared map
           map.clear();
-//          session.refresh(data);
-          exporter.exportTo(data,map);
+          dataProvider.exportTo(i,map);
           // put values in excel cells
           for (int col=0; col<header.size(); col++) {
             String key = header.get(col);
-			Object value = map.get(key);
+            Object value = map.get(key);
             if ( value == null )
               continue;
-            WritableCell cell = createExcelCell(col, row, value, key, cellConverter);
+            WritableCell cell = createExcelCell(col, row, value, key, dataProvider);
             sheet.addCell(cell);
           }
-//          // release contact
-//          if ( evictAfterExport )
-//            session.evict(data);
-
+          dataProvider.disposeData(i);
           expCount++;
           SwingUtil.updateProgressBarValueOnEDT(progressBar, expCount);
         }

Deleted: trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/LineExporter.java
===================================================================
--- trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/LineExporter.java	2012-05-03 08:37:11 UTC (rev 1979)
+++ trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/LineExporter.java	2012-05-03 16:37:06 UTC (rev 1980)
@@ -1,23 +0,0 @@
-package de.schmitzm.jxl.export;
-
-import java.io.File;
-import java.util.List;
-import java.util.Map;
-
-import javax.swing.JProgressBar;
-
-/**
- * Interface to generically provide informations to
- * {@link ExportUtil#exportToExcel(File, String, List, LineExporter, boolean, JProgressBar)}.
- */
-public interface LineExporter<E> {
-  /**
-   * Returns the columns to export.
-   */
-  public List<String> getHeaderColumns();
-  /**
-   * Provides putting the export values to the given
-   * map.
-   */
-  public void exportTo(E data, Map<String,Object> map);
-}
\ No newline at end of file



More information about the Schmitzm-commits mailing list