From fab076eedf1a8eb9518b894db3e0dc918d02fe00 Mon Sep 17 00:00:00 2001 From: SiboVG Date: Mon, 6 Mar 2023 19:46:50 +0100 Subject: [PATCH] Move file chooser stuff outside of exporter --- core/resources/l10n/messages.properties | 2 +- .../openrocket/gui/main/SimulationPanel.java | 62 ++++++++- .../utils/SimulationTableCSVExport.java | 130 ++++++------------ 3 files changed, 106 insertions(+), 88 deletions(-) diff --git a/core/resources/l10n/messages.properties b/core/resources/l10n/messages.properties index 1b3f67f10..82be3c83b 100644 --- a/core/resources/l10n/messages.properties +++ b/core/resources/l10n/messages.properties @@ -538,7 +538,7 @@ simpanel.pop.delete = Delete simpanel.pop.duplicate = Duplicate simpanel.pop.exportToCSV = Export table as CSV file simpanel.pop.exportToCSV.save.dialog.title = Save as CSV file -simpanel.dlg.no.simulation.table.rows = Simulation table has no entries... run a simulation please +simpanel.dlg.no.simulation.table.rows = Simulation table has no entries\u2026 Please run a simulation first. simpanel.checkbox.donotask = Do not ask me again simpanel.lbl.defpref = You can change the default operation in the preferences. simpanel.dlg.lbl.DeleteSim1 = Delete the selected simulations? diff --git a/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java b/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java index 14932a2eb..08df1be86 100644 --- a/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java +++ b/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java @@ -3,6 +3,7 @@ package net.sf.openrocket.gui.main; import java.awt.Color; import java.awt.Component; +import java.awt.Container; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; @@ -13,6 +14,7 @@ import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.Comparator; @@ -22,6 +24,7 @@ import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComponent; +import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.JMenuItem; import javax.swing.JOptionPane; @@ -36,6 +39,9 @@ import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.table.DefaultTableCellRenderer; +import net.sf.openrocket.gui.components.CsvOptionPanel; +import net.sf.openrocket.gui.util.FileHelper; +import net.sf.openrocket.gui.util.SwingPreferences; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -623,8 +629,62 @@ public class SimulationPanel extends JPanel { @Override public void actionPerformed(ActionEvent arg0) { + Container tableParent = simulationTable.getParent(); + int rowCount = simulationTableModel.getRowCount(); + + // I'm pretty sure with the enablement/disablement of the menu item under the File dropdown, + // that this would no longer be needed because if there is no sim table yet, the context menu + // won't show up. But I'm going to leave this in just in case.... + if (rowCount <= 0) { + log.info("No simulation table rows to export"); + JOptionPane.showMessageDialog(tableParent, trans.get("simpanel.dlg.no.simulation.table.rows")); + return; + } + + JFileChooser fch = this.setUpFileChooser(); + int selectionStatus = fch.showSaveDialog(tableParent); + if (selectionStatus != JFileChooser.APPROVE_OPTION) { + log.info("User cancelled CSV export"); + return; + } + + // Fetch the info from the file chooser + File CSVFile = fch.getSelectedFile(); + CSVFile = FileHelper.forceExtension(CSVFile, "csv"); + String separator = ((CsvOptionPanel) fch.getAccessory()).getFieldSeparator(); + int precision = ((CsvOptionPanel) fch.getAccessory()).getDecimalPlaces(); + ((CsvOptionPanel) fch.getAccessory()).storePreferences(); + + // Handle some special separator options from CsvOptionPanel + if (separator.equals(trans.get("CsvOptionPanel.separator.space"))) { + separator = " "; + } else if (separator.equals(trans.get("CsvOptionPanel.separator.tab"))) { + separator = "\t"; + } + SimulationTableCSVExport exporter = new SimulationTableCSVExport(document, simulationTable, simulationTableModel); - exporter.performTableDataConversion(); + exporter.export(CSVFile, separator, precision); + } + + /** + * Create the file chooser to save the CSV file. + * @return The file chooser. + */ + private JFileChooser setUpFileChooser() { + JFileChooser fch = new JFileChooser(); + fch.setDialogTitle(trans.get("simpanel.pop.exportToCSV.save.dialog.title")); + fch.setFileFilter(FileHelper.CSV_FILTER); + fch.setCurrentDirectory(((SwingPreferences) Application.getPreferences()).getDefaultDirectory()); + + // 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; } @Override diff --git a/swing/src/net/sf/openrocket/utils/SimulationTableCSVExport.java b/swing/src/net/sf/openrocket/utils/SimulationTableCSVExport.java index 90be8dd7e..1e248bd53 100644 --- a/swing/src/net/sf/openrocket/utils/SimulationTableCSVExport.java +++ b/swing/src/net/sf/openrocket/utils/SimulationTableCSVExport.java @@ -36,16 +36,11 @@ public class SimulationTableCSVExport { private final ColumnTableModel simulationTableModel; private final HashMap valueColumnToUnitString = new HashMap<>(); - 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 - ) { + public SimulationTableCSVExport (OpenRocketDocument document, JTable simulationTable, + ColumnTableModel simulationTableModel) { this.document = document; this.simulationTable = simulationTable; this.simulationTableModel = simulationTableModel; @@ -56,14 +51,14 @@ public class SimulationTableCSVExport { * units will be added to the header... */ private void populateColumnNameToUnitsHashTable() { + valueColumnToUnitString.clear(); // Necessary if units changed during session if (simulationTableModel == null) { return; } - valueColumnToUnitString.clear(); // necessary if units changed during session - for (int i=0; i rowColumnElement = new ArrayList<>(); for (int j = 1; j 0! + // Piece together the column data for the index-row, skipping any rows with null counts > 0! for (int j = 1; j < modelColumnCount ; j++) { // skip first column Object o = simulationTableModel.getValueAt(idx, j); if (o != null) { - final String value; + final String valueString; if (o instanceof Value) { - double dvalue = ((Value) o).getValue(); - value = TextUtil.doubleToString(dvalue, precision); + double value = ((Value) o).getValue(); + valueString = TextUtil.doubleToString(value, precision); } else { - value = o.toString(); + valueString = o.toString(); } - rowColumnElement.add(StringEscapeUtils.escapeCsv(value)); + rowColumnElement.add(StringEscapeUtils.escapeCsv(valueString)); } else { rowColumnElement.add(""); nullCnt++; } } - - // current "unstable" will have a populated sim table EXCEPT for the optimum delay column on a restart + + // Current "unstable" will have a populated sim table EXCEPT for the optimum delay column on a restart // after a save. That means any row that WAS simulated will have exactly one null column in it... so we'll // skip row export for the case where there are MORE than one nulls. Either way the user should run sims. - if (nullCnt > 1) { // ignore rows that have null column fields 1 through 8... + if (nullCnt > 1) { // ignore rows that have null column fields 1 through 8... continue; } - - // create the column data comma separated string for the ith row... + + // Create the column data comma separated string for the ith row... CSVSimResultString = StringUtils.join(fieldSep, rowColumnElement); - // piece together all rows into one big ginormous string, adding any warnings to the item - fullOutputResult.append("\n").append(CSVSimResultString).append(fieldSep).append(warningsText); + // Piece together all rows into one big ginormous string, adding any warnings to the item + fullOutputResult.append("\n").append(CSVSimResultString); + fullOutputResult.append(fieldSep).append(warningsText); } - - // dump the string to the file. - this.dumpDataToFile(fullOutputResult.toString(), CSVFile); + + return fullOutputResult.toString(); } + public void export(File file, String fieldSep, int precision) { + if (file == null) { + log.warn("No file selected for export"); + return; + } + + String CSVData = generateCSVDate(fieldSep, precision); + this.dumpDataToFile(CSVData, file); + log.info("Simulation table data exported to " + file.getAbsolutePath()); + } }