[Schmitzm-commits] r2350 - in trunk: schmitzm-core/src/main/java/de/schmitzm/io schmitzm-core/src/main/java/de/schmitzm/swing schmitzm-excelcsv/src/main/java/de/schmitzm/csv schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Fri Jun 21 21:09:05 CEST 2013


Author: mojays
Date: 2013-06-21 21:09:05 +0200 (Fri, 21 Jun 2013)
New Revision: 2350

Added:
   trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/JXLUtil.java
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/io/AbstractProgressUpdater.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/StatusDialog.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
   trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/csv/CsvUtil.java
   trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/ExportUtil.java
Log:
- new utility class JXLUtil
- ExportUtil: append excel file on export; use of ProgressUpdater
- CsvUtil: new method to create empty string converter (for csv export)
- AbstractProgressUpdater: check for minimum and maximum value modified



Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/io/AbstractProgressUpdater.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/AbstractProgressUpdater.java	2013-06-21 14:59:57 UTC (rev 2349)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/AbstractProgressUpdater.java	2013-06-21 19:09:05 UTC (rev 2350)
@@ -93,7 +93,7 @@
    */
   @Override
   public void initProgress(int minValue, int maxValue) {
-    if ( minValue >= maxValue )
+    if ( minValue > maxValue )
       throw new UnsupportedOperationException("Progress minimum value has to be lesser than maximum.");
     this.minValue = minValue;
     this.maxValue = maxValue;

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/StatusDialog.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/StatusDialog.java	2013-06-21 14:59:57 UTC (rev 2349)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/StatusDialog.java	2013-06-21 19:09:05 UTC (rev 2350)
@@ -247,4 +247,15 @@
     return messageLabel;
   }
 
+  /**
+   * Sets the status message of the dialog.
+   */
+  public void setMessage(final String mess) {
+    SwingUtil.invokeLater( new Runnable() {
+      @Override
+      public void run() {
+        getMessageLabel().setText(mess);
+      }
+    });
+  }
 }

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2013-06-21 14:59:57 UTC (rev 2349)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2013-06-21 19:09:05 UTC (rev 2350)
@@ -2588,6 +2588,17 @@
     }
 
     /**
+     * Causes runnable to have its run method called in the dispatch thread of
+     * the event queue.
+     */
+    public static void invokeLater(final Runnable runnable) {
+      if (EventQueue.isDispatchThread())
+        runnable.run();
+      else
+        EventQueue.invokeLater(runnable);
+    }
+
+    /**
      * Sets a {@link JProgressBar} to (in)determinate with the given maximum value.
      * This is done using {@link SwingUtilities#invokeLater(Runnable)} if
      * the method is not called on EDT.
@@ -2622,10 +2633,7 @@
         }
       };
       // do work on adaquate thread 
-      if ( SwingUtilities.isEventDispatchThread() )
-        work.run();
-      else
-        SwingUtilities.invokeLater(work);
+      invokeLater(work);
     }
 
     /**
@@ -2643,12 +2651,8 @@
           bar.setValue(newValue);
         }
       };
-      // do work on adaquate thread 
-      if ( SwingUtilities.isEventDispatchThread() ) {
-        work.run();
-      } else {
-        SwingUtilities.invokeLater(work);
-      }
+      // do work on adaquate thread
+      invokeLater(work);
     }
     
     

Modified: trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/csv/CsvUtil.java
===================================================================
--- trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/csv/CsvUtil.java	2013-06-21 14:59:57 UTC (rev 2349)
+++ trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/csv/CsvUtil.java	2013-06-21 19:09:05 UTC (rev 2350)
@@ -156,7 +156,7 @@
   /**
    * Creates a {@link CellProcessor} which converts empty strings to {@code null}
    * for each column (of the header) of the CSV file
-   * @param inFile CSV file
+   * @param header header columns
    * @param noDataValues additional values (besides empty string) which are
    *                     converted to {@code null}.
    */
@@ -183,6 +183,34 @@
   }
 
   /**
+   * Creates a {@link CellProcessor} which converts {@code null} to empty strings
+   * for each column (of the header) of the CSV file
+   * @param header header columns
+   * @param noDataValues additional values (besides empty string) which are
+   *                     converted to {@code null}.
+   */
+  public static CellProcessor[] createEmptyStringConverterCellProcessors(String[] header, final Object... noDataValues) throws IOException {
+    // CellProessor to convert NULL to blank strings
+    CellProcessor cp = new CellProcessor() {
+      @Override
+      public Object execute(Object value, CSVContext context) {
+        if ( value == null )
+          return "";
+        for (Object noDataValue : noDataValues)
+          if ( value.equals(noDataValue) )
+            return "";
+        return value;
+      }
+    };
+    // Processor needed for each CSV column
+    CellProcessor[] cps = new CellProcessor[header.length];
+    for (int i=0; i<cps.length; i++)
+      cps[i] = cp;
+  
+    return cps;
+  }
+
+  /**
    * Converts a string to double.
    * @return 0 if input string is {@code null}
    */

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	2013-06-21 14:59:57 UTC (rev 2349)
+++ trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/ExportUtil.java	2013-06-21 19:09:05 UTC (rev 2350)
@@ -10,10 +10,12 @@
 import javax.swing.JProgressBar;
 
 import jxl.CellView;
+import jxl.Sheet;
 import jxl.Workbook;
 import jxl.format.Border;
 import jxl.format.BorderLineStyle;
 import jxl.format.Colour;
+import jxl.read.biff.BiffException;
 import jxl.write.Blank;
 import jxl.write.DateTime;
 import jxl.write.Label;
@@ -30,7 +32,9 @@
 import org.apache.log4j.Logger;
 
 import de.schmitzm.io.IOUtil;
+import de.schmitzm.io.ProgressUpdater;
 import de.schmitzm.lang.LangUtil;
+import de.schmitzm.swing.ProgressBarUpdater;
 import de.schmitzm.swing.SwingUtil;
 import de.schmitzm.temp.BaseTypeUtil;
 
@@ -164,6 +168,36 @@
      * @return number of exported records
      */
     public static <E> int exportToExcelFile(File exportFile, String sheetName, ExportDataProvider<E> dataProvider, JProgressBar progressBar) throws IOException {
+      return exportToExcelFile(exportFile,
+                               sheetName,
+                               dataProvider,
+                               new ProgressBarUpdater(progressBar));
+    }
+    
+    /**
+     * Exports data of the list an Excel file.
+     * @param exportFile destination file
+     * @param dataList list of data to export
+     * @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, ExportDataProvider<E> dataProvider, ProgressUpdater progressUpdater) throws IOException {
+      return exportToExcelFile(exportFile, true, sheetName, dataProvider, progressUpdater);
+    }
+    /**
+     * Exports data of the list an Excel file.
+     * @param exportFile destination file
+     * @param createNewWorkbook indicates whether a complete new excel file is created or
+     *                          an existing workbook is extended
+     * @param dataList list of data to export
+     * @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, boolean createNewWorkbook, String sheetName, ExportDataProvider<E> dataProvider, ProgressUpdater progressUpdater) throws IOException {
+      if ( progressUpdater == null )
+        progressUpdater = ProgressUpdater.DUMMY;
       // 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 );
@@ -171,7 +205,11 @@
       
       if ( !exportFile.exists() && StringUtils.isBlank(IOUtil.getFileExt(exportFile)) )
         exportFile = IOUtil.appendFileExt(exportFile, "xls");
-      SwingUtil.setProgressBarDeterminateOnEDT(progressBar, dataProvider.getDataCount());
+      if ( !exportFile.exists() )
+        createNewWorkbook = true;
+//      SwingUtil.setProgressBarDeterminateOnEDT(progressBar, dataProvider.getDataCount());
+      if ( !progressUpdater.isInitialized() )
+        progressUpdater.initProgress(0, dataProvider.getDataCount());
       int expCount = 0;
       WritableWorkbook workbook = null;
       WritableSheet sheet = null;
@@ -187,8 +225,21 @@
 //        headerCellFormat.setShrinkToFit(true);
         
         // Create workbook
-        workbook = Workbook.createWorkbook(exportFile);
-        sheet = workbook.createSheet(sheetName,0);
+        if ( createNewWorkbook ) {
+          workbook = Workbook.createWorkbook(exportFile);
+          sheet = workbook.createSheet(sheetName,0);
+        } else {
+          workbook = Workbook.createWorkbook(exportFile,Workbook.getWorkbook(exportFile));
+          int  sheetIdx = JXLUtil.findSheetIdx(workbook,sheetName);
+          if ( sheetIdx < 0 )
+            // if sheet does not exist, add sheet at the end
+            sheet = workbook.createSheet(sheetName,9999);
+          else {
+            // replace existing sheet with new one
+            workbook.removeSheet(sheetIdx);
+            sheet = workbook.createSheet(sheetName,sheetIdx);
+          }
+        }
         sheet.getSettings().setVerticalFreeze(1);
         sheet.getSettings().setPrintTitlesRow(0, 0);
         // Create header line
@@ -217,13 +268,16 @@
           }
           dataProvider.disposeData(i);
           expCount++;
-          SwingUtil.updateProgressBarValueOnEDT(progressBar, expCount);
+//          SwingUtil.updateProgressBarValueOnEDT(progressBar, expCount);
+          progressUpdater.incProgress();
         }
         // for "small" sheets, adjust the column width
         if ( sheet.getColumns() <= 20 )
           autoSizeColumns(sheet);
       } catch (RowsExceededException err) {
         throw new IOException(err);
+      } catch (BiffException err) {
+        throw new IOException(err);
       } catch (WriteException err) {
         throw new IOException(err);
       } finally {
@@ -313,4 +367,5 @@
         sheet.setColumnView(c, view);
       }
     }
+    
 }

Added: trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/JXLUtil.java
===================================================================
--- trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/JXLUtil.java	                        (rev 0)
+++ trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/JXLUtil.java	2013-06-21 19:09:05 UTC (rev 2350)
@@ -0,0 +1,67 @@
+/**
+ * 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 jxl.Workbook;
+import jxl.write.WritableWorkbook;
+
+/**
+ * Utility methods for JXL. 
+ * @author Martin O.J. Schmitz
+ */
+public class JXLUtil {
+  /**
+   * Determines the index of a sheet in a workbook.
+   * @param workbook a workbook
+   * @param sheetName sheet name to determine the index for
+   * @return
+   */
+  public static int findSheetIdx(Workbook workbook, String sheetName) {
+    String[] sheetNames = workbook.getSheetNames(); 
+    for (int i=0; i<sheetNames.length; i++ )
+      if ( sheetName.equalsIgnoreCase(sheetNames[i]) )
+        return i;
+    return -1;
+  }
+
+  /**
+   * Determines the index of a sheet in a workbook.
+   * @param workbook a workbook
+   * @param sheetName sheet name to determine the index for
+   * @return
+   */
+  public static int findSheetIdx(WritableWorkbook workbook, String sheetName) {
+    String[] sheetNames = workbook.getSheetNames(); 
+    for (int i=0; i<sheetNames.length; i++ )
+      if ( sheetName.equalsIgnoreCase(sheetNames[i]) )
+        return i;
+    return -1;
+  }
+}



More information about the Schmitzm-commits mailing list