Move file chooser stuff outside of exporter
This commit is contained in:
parent
61f7abe9a1
commit
fab076eedf
@ -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?
|
||||
|
@ -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
|
||||
|
@ -36,16 +36,11 @@ public class SimulationTableCSVExport {
|
||||
private final ColumnTableModel simulationTableModel;
|
||||
private final HashMap<String, String> 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<simulationTableModel.getColumnCount(); i++) {
|
||||
for (int i = 0; i < simulationTableModel.getColumnCount(); i++) {
|
||||
Column c = simulationTableModel.getColumn(i);
|
||||
if (c instanceof ValueColumn) {
|
||||
// only value columns seem to have units that are not zero length strings... these are
|
||||
// Only value columns seem to have units that are not zero length strings... These are
|
||||
// the ones we actually want in our lookup table.
|
||||
valueColumnToUnitString.put(c.toString(), c.getUnits().getDefaultUnit().getUnit());
|
||||
}
|
||||
@ -97,69 +92,23 @@ public class SimulationTableCSVExport {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the file chooser to save the CSV file.
|
||||
* @return The file chooser.
|
||||
* Generate the CSV data from the simulation table
|
||||
* @param fieldSep The field separator to use in the CSV file.
|
||||
* @param precision The number of decimal places to use in the CSV file.
|
||||
* @return
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
public void performTableDataConversion() {
|
||||
Container tableParent = simulationTable.getParent();
|
||||
public String generateCSVDate(String fieldSep, int precision) {
|
||||
int modelColumnCount = simulationTableModel.getColumnCount();
|
||||
int modelRowCount = simulationTableModel.getRowCount();
|
||||
populateColumnNameToUnitsHashTable();
|
||||
|
||||
// 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 (modelRowCount <= 0) {
|
||||
String msg = trans.get("simpanel.dlg.no.simulation.table.rows");
|
||||
JOptionPane.showMessageDialog(tableParent, msg);
|
||||
return;
|
||||
}
|
||||
|
||||
JFileChooser fch = this.setUpFileChooser();
|
||||
int selectionStatus = fch.showSaveDialog(tableParent);
|
||||
if (selectionStatus != JFileChooser.APPROVE_OPTION) {
|
||||
log.info("User cancelled CSV export");
|
||||
return;
|
||||
}
|
||||
|
||||
String fieldSep = ((CsvOptionPanel) fch.getAccessory()).getFieldSeparator();
|
||||
int precision = ((CsvOptionPanel) fch.getAccessory()).getDecimalPlaces();
|
||||
((CsvOptionPanel) fch.getAccessory()).storePreferences();
|
||||
|
||||
if (fieldSep.equals(SPACE)) {
|
||||
fieldSep = " ";
|
||||
} else if (fieldSep.equals(TAB)) {
|
||||
fieldSep = "\t";
|
||||
}
|
||||
|
||||
File CSVFile = fch.getSelectedFile();
|
||||
CSVFile = FileHelper.forceExtension(CSVFile, "csv");
|
||||
|
||||
String CSVSimResultString;
|
||||
// obtain the column titles for the first row of the CSV
|
||||
// Obtain the column titles for the first row of the CSV
|
||||
ArrayList<String> rowColumnElement = new ArrayList<>();
|
||||
for (int j = 1; j<modelColumnCount ; j++) {
|
||||
String colName = simulationTable.getColumnName(j);
|
||||
|
||||
// get the unit string and append to column that it applies to.
|
||||
// columns w/o units will remain unchanged.
|
||||
// Get the unit string and append to column that it applies to. Columns w/o units will remain unchanged.
|
||||
if (valueColumnToUnitString.containsKey(colName)) {
|
||||
String unitString = valueColumnToUnitString.get(colName);
|
||||
colName += " (" + unitString + ")";
|
||||
@ -172,16 +121,15 @@ public class SimulationTableCSVExport {
|
||||
|
||||
StringBuilder fullOutputResult = new StringBuilder(CSVSimResultString);
|
||||
|
||||
// get relevant data and create the comma separated data from it.
|
||||
// Get relevant data and create the comma separated data from it.
|
||||
for (int i = 0; i < modelRowCount; i++) {
|
||||
// account for sorting... resulting CSV file will be in the
|
||||
// same order as shown in the table thanks to this gem.
|
||||
// Account for sorting... resulting CSV file will be in the same order as shown in the table thanks to this gem.
|
||||
int idx = simulationTable.convertRowIndexToModel(i);
|
||||
|
||||
int nullCnt = 0;
|
||||
rowColumnElement.clear();
|
||||
|
||||
// get the simulation's warning text if any... this bypasses the need to use
|
||||
// Get the simulation's warning text if any. This bypasses the need to use
|
||||
// the column 0 stuff which is kind of difficult to use!
|
||||
WarningSet ws = document.getSimulation(idx).getSimulatedWarnings();
|
||||
StringBuilder warningsText = new StringBuilder();
|
||||
@ -192,40 +140,50 @@ public class SimulationTableCSVExport {
|
||||
}
|
||||
}
|
||||
|
||||
// piece together the column data for the ith row, skipping any rows with null counts > 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...
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user