From 61f7abe9a10faba40dafbb5faf552edfe777f51a Mon Sep 17 00:00:00 2001 From: SiboVG Date: Mon, 6 Mar 2023 19:25:23 +0100 Subject: [PATCH] Code clean-up --- .../utils/SimulationTableCSVExport.java | 102 +++++++++--------- 1 file changed, 49 insertions(+), 53 deletions(-) diff --git a/swing/src/net/sf/openrocket/utils/SimulationTableCSVExport.java b/swing/src/net/sf/openrocket/utils/SimulationTableCSVExport.java index 6afde728c..90be8dd7e 100644 --- a/swing/src/net/sf/openrocket/utils/SimulationTableCSVExport.java +++ b/swing/src/net/sf/openrocket/utils/SimulationTableCSVExport.java @@ -3,7 +3,6 @@ package net.sf.openrocket.utils; import java.awt.Container; import java.io.BufferedWriter; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; @@ -26,9 +25,10 @@ import net.sf.openrocket.gui.util.FileHelper; import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.l10n.Translator; import net.sf.openrocket.startup.Application; -import net.sf.openrocket.startup.Preferences; import net.sf.openrocket.unit.Value; import net.sf.openrocket.util.TextUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class SimulationTableCSVExport { private final OpenRocketDocument document; @@ -36,15 +36,16 @@ public class SimulationTableCSVExport { private final ColumnTableModel simulationTableModel; private final HashMap valueColumnToUnitString = new HashMap<>(); - private static final Translator trans = Application.getTranslator(); private static final String SPACE = "SPACE"; private static final String TAB = "TAB"; + private static final Translator trans = Application.getTranslator(); + private static final Logger log = LoggerFactory.getLogger(SimulationTableCSVExport.class); + public SimulationTableCSVExport (OpenRocketDocument document, JTable simulationTable, ColumnTableModel simulationTableModel - ) - { + ) { this.document = document; this.simulationTable = simulationTable; this.simulationTableModel = simulationTableModel; @@ -55,7 +56,7 @@ public class SimulationTableCSVExport { * units will be added to the header... */ private void populateColumnNameToUnitsHashTable() { - if (null == simulationTableModel) { + if (simulationTableModel == null) { return; } valueColumnToUnitString.clear(); // necessary if units changed during session @@ -71,18 +72,15 @@ public class SimulationTableCSVExport { } /** * Dump data from sim table to file for run simulations - * @param data The csv data as one string block. - * @param csvFile The file to dump the data to. + * @param data The CSV data as one string block. + * @param CSVFile The file to dump the data to. */ - private void dumpDataToFile(String data, File csvFile) { + private void dumpDataToFile(String data, File CSVFile) { BufferedWriter bufferedWriter = null; try { - csvFile.createNewFile(); - bufferedWriter = new BufferedWriter(new FileWriter(csvFile)); + CSVFile.createNewFile(); + bufferedWriter = new BufferedWriter(new FileWriter(CSVFile)); bufferedWriter.write(data); - } catch (FileNotFoundException e) { - String msg = e.getMessage(); - JOptionPane.showMessageDialog(simulationTable.getParent(), msg); } catch (IOException e) { String msg = e.getMessage(); JOptionPane.showMessageDialog(simulationTable.getParent(), msg); @@ -98,31 +96,28 @@ public class SimulationTableCSVExport { } } + /** + * Create the file chooser to save the CSV file. + * @return The file chooser. + */ private JFileChooser setUpFileChooser() { JFileChooser fch = new JFileChooser(); - String saveDialogTitle = trans.get("simpanel.pop.exportToCSV.save.dialog.title"); - fch.setDialogTitle(saveDialogTitle); - + fch.setDialogTitle(trans.get("simpanel.pop.exportToCSV.save.dialog.title")); fch.setFileFilter(FileHelper.CSV_FILTER); - - // default output csv to same name as the document's rocket name. - String documentFileName = document.getRocket().getName(); - documentFileName += ".csv"; - fch.setSelectedFile(new File(documentFileName)); - CsvOptionPanel csvOptions = new CsvOptionPanel(SimulationTableCSVExport.class /*, - trans.get("GeneralOptimizationDialog.export.header"), trans.get("GeneralOptimizationDialog.export.header.ttip")*/); - fch.setCurrentDirectory(((SwingPreferences) Application.getPreferences()).getDefaultDirectory()); - fch.setAccessory(csvOptions); + + // Default output CSV to same name as the document's rocket name. + String fileName = document.getRocket().getName() + ".csv"; + fch.setSelectedFile(new File(fileName)); + + // Add CSV options to FileChooser + CsvOptionPanel CSVOptions = new CsvOptionPanel(SimulationTableCSVExport.class); + fch.setAccessory(CSVOptions); return fch; } public void performTableDataConversion() { - // one down side is that to change this means you have to export and save at least ONCE! - // there is no entry in the preferences to set this a-priori and it is default to a comma - String fieldSep = Application.getPreferences().getString(Preferences.EXPORT_FIELD_SEPARATOR, ","); - Container tableParent = simulationTable.getParent(); int modelColumnCount = simulationTableModel.getColumnCount(); int modelRowCount = simulationTableModel.getRowCount(); @@ -139,11 +134,12 @@ public class SimulationTableCSVExport { JFileChooser fch = this.setUpFileChooser(); int selectionStatus = fch.showSaveDialog(tableParent); - if (selectionStatus == JFileChooser.CANCEL_OPTION || selectionStatus == JFileChooser.ERROR_OPTION) { - return; // cancel or error... nothing to do here + if (selectionStatus != JFileChooser.APPROVE_OPTION) { + log.info("User cancelled CSV export"); + return; } - fieldSep = ((CsvOptionPanel) fch.getAccessory()).getFieldSeparator(); + String fieldSep = ((CsvOptionPanel) fch.getAccessory()).getFieldSeparator(); int precision = ((CsvOptionPanel) fch.getAccessory()).getDecimalPlaces(); ((CsvOptionPanel) fch.getAccessory()).storePreferences(); @@ -153,12 +149,13 @@ public class SimulationTableCSVExport { fieldSep = "\t"; } - File csvFile = fch.getSelectedFile(); + File CSVFile = fch.getSelectedFile(); + CSVFile = FileHelper.forceExtension(CSVFile, "csv"); - String csvSimResultString = ""; - // obtain the column titles for the first row of the csv + String CSVSimResultString; + // obtain the column titles for the first row of the CSV ArrayList rowColumnElement = new ArrayList<>(); - for (int j=1; j 0! - for (int j=1; j