|
From: <de...@us...> - 2016-10-12 14:33:26
|
Revision: 9432
http://sourceforge.net/p/fudaa/svn/9432
Author: deniger
Date: 2016-10-12 14:33:23 +0000 (Wed, 12 Oct 2016)
Log Message:
-----------
CRUE-719 Import-Export des conditions initiales
Modified Paths:
--------------
trunk/soft/fudaa-crue/ui-modelling/src/main/java/org/fudaa/fudaa/crue/modelling/calcul/ModellingListCLimMsProgressRunnable.java
trunk/soft/fudaa-crue/ui-modelling/src/main/java/org/fudaa/fudaa/crue/modelling/calcul/ModellingListCLimMsTopExportImportPopupBuilder.java
trunk/soft/fudaa-crue/ui-modelling/src/main/java/org/fudaa/fudaa/crue/modelling/calcul/ModellingListCiniImportProgressRunnable.java
trunk/soft/fudaa-crue/ui-modelling/src/main/java/org/fudaa/fudaa/crue/modelling/calcul/ModellingListCiniTopComponentPopupFactory.java
trunk/soft/fudaa-crue/ui-modelling/src/test/java/org/fudaa/fudaa/crue/modelling/calcul/ModellingListCLimMsProgressRunnableTest.java
Added Paths:
-----------
trunk/soft/fudaa-crue/ui-common/src/main/java/org/fudaa/fudaa/crue/common/helper/TablerFileReader.java
Added: trunk/soft/fudaa-crue/ui-common/src/main/java/org/fudaa/fudaa/crue/common/helper/TablerFileReader.java
===================================================================
--- trunk/soft/fudaa-crue/ui-common/src/main/java/org/fudaa/fudaa/crue/common/helper/TablerFileReader.java (rev 0)
+++ trunk/soft/fudaa-crue/ui-common/src/main/java/org/fudaa/fudaa/crue/common/helper/TablerFileReader.java 2016-10-12 14:33:23 UTC (rev 9432)
@@ -0,0 +1,126 @@
+/*
+GPL 2
+ */
+package org.fudaa.fudaa.crue.common.helper;
+
+import com.Ostermiller.util.CSVParser;
+import com.memoire.bu.BuFileFilter;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileReader;
+import java.io.InputStream;
+import java.io.Reader;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.swing.JFileChooser;
+import org.apache.commons.lang.StringUtils;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.usermodel.WorkbookFactory;
+import org.fudaa.ctulu.CtuluLibFile;
+import org.fudaa.ctulu.CtuluResource;
+import org.fudaa.ctulu.gui.CtuluFileChooser;
+import org.openide.util.Exceptions;
+
+/**
+ * Lit a un fichier et le traduit en tableau a 2 dimension.
+ *
+ * @author Frederic Deniger
+ */
+public class TablerFileReader {
+
+ public static CtuluFileChooser createTablerFileChooser() {
+ final BuFileFilter ftCsv = new BuFileFilter(new String[]{"csv", "txt"}, CtuluResource.CTULU.getString("Texte CSV"));
+ final BuFileFilter ftXsl = new BuFileFilter(new String[]{"xls"}, CtuluResource.CTULU.getString("Fichier Excel 97-2003"));
+ final BuFileFilter ftXslx = new BuFileFilter(new String[]{"xlsx"}, CtuluResource.CTULU.getString("Fichier Excel"));
+ ftCsv.setExtensionListInDescription(true);
+ ftXsl.setExtensionListInDescription(true);
+ ftXslx.setExtensionListInDescription(true);
+ CtuluFileChooser fileChooser = new CtuluFileChooser(true);
+ fileChooser.addChoosableFileFilter(ftCsv);
+ fileChooser.addChoosableFileFilter(ftXsl);
+ fileChooser.addChoosableFileFilter(ftXslx);
+ fileChooser.setFileFilter(ftXslx);
+ fileChooser.setAcceptAllFileFilterUsed(false);
+ fileChooser.setMultiSelectionEnabled(false);
+ fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
+ return fileChooser;
+ }
+
+ public String[][] readFile(File file) {
+ final String nameToLowerCase = file.getName().toLowerCase();
+ if (nameToLowerCase.endsWith("xlsx") || nameToLowerCase.endsWith("xls")) {
+ List<String[]> rows = new ArrayList<>();
+ InputStream is = null;
+ try {
+ is = new FileInputStream(file);
+ Workbook wb = WorkbookFactory.create(is);
+ Sheet sheetAt = wb.getSheetAt(0);
+ int maxCol = 0;
+ for (Row row : sheetAt) {
+ maxCol = Math.max(maxCol, row.getLastCellNum());
+ }
+ for (Row row : sheetAt) {
+ List<String> cols = new ArrayList<>();
+ for (int i = 0; i < maxCol; i++) {
+ cols.add(getValue(row.getCell(i, Row.CREATE_NULL_AS_BLANK)));
+ }
+ rows.add(cols.toArray(new String[cols.size()]));
+ }
+ } catch (Exception ex) {
+ Exceptions.printStackTrace(ex);
+ } finally {
+ CtuluLibFile.close(is);
+ }
+ final String[][] toArray = rows.toArray(new String[rows.size()][]);
+ return toArray;
+
+ } else {
+ return readCsv(file);
+ }
+ }
+
+ protected String getValue(Cell cell) {
+ switch (cell.getCellType()) {
+ case Cell.CELL_TYPE_BLANK:
+ return "";
+ case Cell.CELL_TYPE_NUMERIC:
+ return Double.toString(cell.getNumericCellValue());
+ case Cell.CELL_TYPE_STRING:
+ return cell.getStringCellValue();
+ default:
+ try {
+ return Double.toString(cell.getNumericCellValue());
+ } catch (Exception ex) {
+ Logger.getLogger(TablerFileReader.class.getName()).log(Level.INFO, "message {0}", ex);
+ try {
+ return cell.getStringCellValue();
+ } catch (Exception ex1) {
+ Logger.getLogger(TablerFileReader.class.getName()).log(Level.INFO, "message {0}", ex1);
+
+ }
+ }
+ }
+ return StringUtils.EMPTY;
+ }
+
+ protected String[][] readCsv(File file) {
+ Reader reader = null;
+ String[][] values = null;
+ try {
+ reader = new FileReader(file);
+ values = new CSVParser(new BufferedReader(reader), ';').getAllValues();
+ } catch (Exception ex) {
+ Exceptions.printStackTrace(ex);
+ } finally {
+ CtuluLibFile.close(reader);
+ }
+ return values;
+ }
+
+}
Modified: trunk/soft/fudaa-crue/ui-modelling/src/main/java/org/fudaa/fudaa/crue/modelling/calcul/ModellingListCLimMsProgressRunnable.java
===================================================================
--- trunk/soft/fudaa-crue/ui-modelling/src/main/java/org/fudaa/fudaa/crue/modelling/calcul/ModellingListCLimMsProgressRunnable.java 2016-10-12 13:18:00 UTC (rev 9431)
+++ trunk/soft/fudaa-crue/ui-modelling/src/main/java/org/fudaa/fudaa/crue/modelling/calcul/ModellingListCLimMsProgressRunnable.java 2016-10-12 14:33:23 UTC (rev 9432)
@@ -4,28 +4,12 @@
package org.fudaa.fudaa.crue.modelling.calcul;
import com.Ostermiller.util.CSVParser;
-import java.io.BufferedReader;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileReader;
-import java.io.InputStream;
-import java.io.Reader;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-import org.apache.commons.lang.StringUtils;
-import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.ss.usermodel.Workbook;
-import org.apache.poi.ss.usermodel.WorkbookFactory;
-import org.fudaa.ctulu.CtuluLibFile;
+import org.fudaa.fudaa.crue.common.helper.TablerFileReader;
import org.fudaa.fudaa.crue.modelling.calcul.importer.CLimMsImporter;
import org.fudaa.fudaa.crue.views.DonClimMTableModel;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressRunnable;
-import org.openide.util.Exceptions;
/**
*
@@ -57,7 +41,7 @@
}
values = CSVParser.parse(initValues, sep);
} else if (file != null) {
- values = readFile();
+ values = new TablerFileReader().readFile(file);
}
if (values == null) {
return null;
@@ -69,74 +53,4 @@
return importer.importData();
}
- protected String getValue(Cell cell) {
- switch (cell.getCellType()) {
- case Cell.CELL_TYPE_BLANK:
- return "";
- case Cell.CELL_TYPE_NUMERIC:
- return Double.toString(cell.getNumericCellValue());
- case Cell.CELL_TYPE_STRING:
- return cell.getStringCellValue();
- default:
- try {
- return Double.toString(cell.getNumericCellValue());
- } catch (Exception ex) {
- Logger.getLogger(ModellingListCLimMsProgressRunnable.class.getName()).log(Level.INFO, "message {0}", ex);
- try {
- return cell.getStringCellValue();
- } catch (Exception ex1) {
- Logger.getLogger(ModellingListCLimMsProgressRunnable.class.getName()).log(Level.INFO, "message {0}", ex1);
-
- }
- }
- }
- return StringUtils.EMPTY;
- }
-
- protected String[][] readFile() {
- final String nameToLowerCase = file.getName().toLowerCase();
- if (nameToLowerCase.endsWith("xlsx") || nameToLowerCase.endsWith("xls")) {
- List<String[]> rows = new ArrayList<String[]>();
- InputStream is = null;
- try {
- is = new FileInputStream(file);
- Workbook wb = WorkbookFactory.create(is);
- Sheet sheetAt = wb.getSheetAt(0);
- int maxCol = 0;
- for (Row row : sheetAt) {
- maxCol = Math.max(maxCol, row.getLastCellNum());
- }
- for (Row row : sheetAt) {
- List<String> cols = new ArrayList<String>();
- for (int i = 0; i < maxCol; i++) {
- cols.add(getValue(row.getCell(i, Row.CREATE_NULL_AS_BLANK)));
- }
- rows.add(cols.toArray(new String[cols.size()]));
- }
- } catch (Exception ex) {
- Exceptions.printStackTrace(ex);
- } finally {
- CtuluLibFile.close(is);
- }
- final String[][] toArray = rows.toArray(new String[rows.size()][]);
- return toArray;
-
- } else {
- return readCsv();
- }
- }
-
- protected String[][] readCsv() {
- Reader reader = null;
- String[][] values = null;
- try {
- reader = new FileReader(file);
- values = new CSVParser(new BufferedReader(reader), ';').getAllValues();
- } catch (Exception ex) {
- Exceptions.printStackTrace(ex);
- } finally {
- CtuluLibFile.close(reader);
- }
- return values;
- }
}
Modified: trunk/soft/fudaa-crue/ui-modelling/src/main/java/org/fudaa/fudaa/crue/modelling/calcul/ModellingListCLimMsTopExportImportPopupBuilder.java
===================================================================
--- trunk/soft/fudaa-crue/ui-modelling/src/main/java/org/fudaa/fudaa/crue/modelling/calcul/ModellingListCLimMsTopExportImportPopupBuilder.java 2016-10-12 13:18:00 UTC (rev 9431)
+++ trunk/soft/fudaa-crue/ui-modelling/src/main/java/org/fudaa/fudaa/crue/modelling/calcul/ModellingListCLimMsTopExportImportPopupBuilder.java 2016-10-12 14:33:23 UTC (rev 9432)
@@ -16,6 +16,7 @@
import org.fudaa.ctulu.gui.CtuluTableSimpleExporter;
import org.fudaa.fudaa.crue.common.helper.CrueProgressUtils;
import org.fudaa.fudaa.crue.common.helper.CtuluUIForNetbeans;
+import org.fudaa.fudaa.crue.common.helper.TablerFileReader;
import org.fudaa.fudaa.crue.views.DonClimMTableModel;
import org.fudaa.fudaa.crue.views.LoiDisplayer;
import org.fudaa.fudaa.crue.views.LoiDisplayerInstaller;
@@ -53,20 +54,7 @@
if (!tc.isEditable()) {
return;
}
- final BuFileFilter ftCsv = new BuFileFilter(new String[]{"csv", "txt"}, CtuluResource.CTULU.getString("Texte CSV"));
- final BuFileFilter ftXsl = new BuFileFilter(new String[]{"xls"}, CtuluResource.CTULU.getString("Fichier Excel 97-2003"));
- final BuFileFilter ftXslx = new BuFileFilter(new String[]{"xlsx"}, CtuluResource.CTULU.getString("Fichier Excel"));
- ftCsv.setExtensionListInDescription(true);
- ftXsl.setExtensionListInDescription(true);
- ftXslx.setExtensionListInDescription(true);
- CtuluFileChooser fileChooser = new CtuluFileChooser(true);
- fileChooser.addChoosableFileFilter(ftCsv);
- fileChooser.addChoosableFileFilter(ftXsl);
- fileChooser.addChoosableFileFilter(ftXslx);
- fileChooser.setFileFilter(ftXslx);
- fileChooser.setAcceptAllFileFilterUsed(false);
- fileChooser.setMultiSelectionEnabled(false);
- fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
+ CtuluFileChooser fileChooser = TablerFileReader.createTablerFileChooser();
final int res = fileChooser.showDialog(CtuluUIForNetbeans.DEFAULT.getParentComponent(), NbBundle.getMessage(
ModellingListCLimMsTopExportImportPopupBuilder.class,
"button.import.name"));
@@ -81,6 +69,7 @@
}
}
+
protected JMenuItem createExportMenuItem() throws MissingResourceException {
final JMenuItem menuItemExport = new JMenuItem(NbBundle.getMessage(ModellingListCLimMsTopExportImportPopupBuilder.class, "button.export.name"));
menuItemExport.addActionListener(new ActionListener() {
Modified: trunk/soft/fudaa-crue/ui-modelling/src/main/java/org/fudaa/fudaa/crue/modelling/calcul/ModellingListCiniImportProgressRunnable.java
===================================================================
--- trunk/soft/fudaa-crue/ui-modelling/src/main/java/org/fudaa/fudaa/crue/modelling/calcul/ModellingListCiniImportProgressRunnable.java 2016-10-12 13:18:00 UTC (rev 9431)
+++ trunk/soft/fudaa-crue/ui-modelling/src/main/java/org/fudaa/fudaa/crue/modelling/calcul/ModellingListCiniImportProgressRunnable.java 2016-10-12 14:33:23 UTC (rev 9432)
@@ -4,17 +4,13 @@
package org.fudaa.fudaa.crue.modelling.calcul;
import com.Ostermiller.util.CSVParser;
-import java.io.BufferedReader;
import java.io.File;
-import java.io.FileReader;
-import java.io.Reader;
-import org.fudaa.ctulu.CtuluLibFile;
import org.fudaa.ctulu.CtuluLog;
+import org.fudaa.fudaa.crue.common.helper.TablerFileReader;
import org.fudaa.fudaa.crue.common.log.LogsDisplayer;
import org.fudaa.fudaa.crue.modelling.calcul.importer.CiniImporter;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressRunnable;
-import org.openide.util.Exceptions;
import org.openide.util.NbBundle;
/**
@@ -22,24 +18,23 @@
* @author Frederic Deniger
*/
public class ModellingListCiniImportProgressRunnable implements ProgressRunnable<Object> {
-
+
private File file;
private final ModellingListCiniTopComponent topComponent;
private String initValues;
-
+
public ModellingListCiniImportProgressRunnable(File file, ModellingListCiniTopComponent topComponent) {
this.file = file;
this.topComponent = topComponent;
}
-
+
public ModellingListCiniImportProgressRunnable(String initValues, ModellingListCiniTopComponent topComponent) {
this.topComponent = topComponent;
this.initValues = initValues;
}
-
+
@Override
public Object run(ProgressHandle handle) {
- Reader reader = null;
String[][] values = null;
if (initValues != null) {
char sep = '\t';
@@ -48,14 +43,7 @@
}
values = CSVParser.parse(initValues, sep);
} else if (file != null) {
- try {
- reader = new FileReader(file);
- values = new CSVParser(new BufferedReader(reader), ';').getAllValues();
- } catch (Exception ex) {
- Exceptions.printStackTrace(ex);
- } finally {
- CtuluLibFile.close(reader);
- }
+ values = new TablerFileReader().readFile(file);
}
if (values == null) {
return null;
@@ -72,8 +60,9 @@
title = NbBundle.getMessage(ModellingListCiniImportProgressRunnable.class, "cini.importClipboard.bilan");
}
LogsDisplayer.displayError(importData, title);
-
+
}
return null;
}
+
}
Modified: trunk/soft/fudaa-crue/ui-modelling/src/main/java/org/fudaa/fudaa/crue/modelling/calcul/ModellingListCiniTopComponentPopupFactory.java
===================================================================
--- trunk/soft/fudaa-crue/ui-modelling/src/main/java/org/fudaa/fudaa/crue/modelling/calcul/ModellingListCiniTopComponentPopupFactory.java 2016-10-12 13:18:00 UTC (rev 9431)
+++ trunk/soft/fudaa-crue/ui-modelling/src/main/java/org/fudaa/fudaa/crue/modelling/calcul/ModellingListCiniTopComponentPopupFactory.java 2016-10-12 14:33:23 UTC (rev 9432)
@@ -3,7 +3,6 @@
*/
package org.fudaa.fudaa.crue.modelling.calcul;
-import com.memoire.bu.BuFileFilter;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@@ -15,13 +14,13 @@
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;
-import org.fudaa.ctulu.CtuluResource;
import org.fudaa.ctulu.gui.CtuluFileChooser;
import org.fudaa.ctulu.gui.CtuluTableSimpleExporter;
import org.fudaa.ctulu.table.CtuluTable;
import org.fudaa.ctulu.table.CtuluTableModelDefault;
import org.fudaa.fudaa.crue.common.helper.CrueProgressUtils;
import org.fudaa.fudaa.crue.common.helper.CtuluUIForNetbeans;
+import org.fudaa.fudaa.crue.common.helper.TablerFileReader;
import org.fudaa.fudaa.crue.modelling.list.ListNodeAddPopupFactory;
import org.fudaa.fudaa.crue.views.export.OutlineViewExportTableModel;
import org.openide.explorer.view.NodePopupFactory;
@@ -120,14 +119,7 @@
if (!tc.isEditable()) {
return;
}
- final BuFileFilter ftCsv = new BuFileFilter(new String[]{"txt", "csv"}, CtuluResource.CTULU.getString("Texte CSV"));
- ftCsv.setExtensionListInDescription(true);
- CtuluFileChooser fileChooser = new CtuluFileChooser(true);
- fileChooser.addChoosableFileFilter(ftCsv);
- fileChooser.setFileFilter(ftCsv);
- fileChooser.setAcceptAllFileFilterUsed(false);
- fileChooser.setMultiSelectionEnabled(false);
- fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
+ CtuluFileChooser fileChooser = TablerFileReader.createTablerFileChooser();
final int res = fileChooser.showDialog(CtuluUIForNetbeans.DEFAULT.getParentComponent(), NbBundle.getMessage(ModellingListCiniTopComponent.class,
"button.import.name"));
if (res == JFileChooser.APPROVE_OPTION) {
Modified: trunk/soft/fudaa-crue/ui-modelling/src/test/java/org/fudaa/fudaa/crue/modelling/calcul/ModellingListCLimMsProgressRunnableTest.java
===================================================================
--- trunk/soft/fudaa-crue/ui-modelling/src/test/java/org/fudaa/fudaa/crue/modelling/calcul/ModellingListCLimMsProgressRunnableTest.java 2016-10-12 13:18:00 UTC (rev 9431)
+++ trunk/soft/fudaa-crue/ui-modelling/src/test/java/org/fudaa/fudaa/crue/modelling/calcul/ModellingListCLimMsProgressRunnableTest.java 2016-10-12 14:33:23 UTC (rev 9432)
@@ -6,6 +6,7 @@
import java.io.File;
import java.io.IOException;
import org.fudaa.ctulu.CtuluLibFile;
+import org.fudaa.fudaa.crue.common.helper.TablerFileReader;
import static org.junit.Assert.*;
import org.junit.Test;
@@ -27,8 +28,7 @@
protected void testContent(File f) {
assertNotNull(f);
- ModellingListCLimMsProgressRunnable runnable = new ModellingListCLimMsProgressRunnable(f, null);
- String[][] values = runnable.readFile();
+ String[][] values = new TablerFileReader().readFile(f);
assertEquals(2, values.length);
assertEquals(5, values[0].length);
assertArrayEquals(new String[]{"A", "", "B", "C", "D"}, values[0]);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|