Use selection-specific sim exporting in context menu

This commit is contained in:
SiboVG 2023-03-14 23:29:10 +01:00
parent 05561b2872
commit d02b8fc431
4 changed files with 133 additions and 83 deletions

View File

@ -536,7 +536,8 @@ simpanel.pop.plot = Plot / Export
simpanel.pop.run = Run
simpanel.pop.delete = Delete
simpanel.pop.duplicate = Duplicate
simpanel.pop.exportToCSV = Export table as CSV file
simpanel.pop.exportSimTableToCSV = Export simulation table as CSV file
simpanel.pop.exportSelectedSimsToCSV = Export simulations as CSV file
simpanel.pop.exportToCSV.save.dialog.title = Save as CSV file
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

View File

@ -87,9 +87,10 @@ public class SimulationTableCSVExport {
* 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
* @param onlySelected If true, only export the selected rows in the table.
* @return The CSV data as one string block.
*/
public String generateCSVDate(String fieldSep, int precision) {
public String generateCSVDate(String fieldSep, int precision, boolean onlySelected) {
int modelColumnCount = simulationTableModel.getColumnCount();
int modelRowCount = simulationTableModel.getRowCount();
populateColumnNameToUnitsHashTable();
@ -114,7 +115,16 @@ public class SimulationTableCSVExport {
StringBuilder fullOutputResult = new StringBuilder(CSVSimResultString);
// Get relevant data and create the comma separated data from it.
int[] iterator;
if (onlySelected) {
iterator = simulationTable.getSelectedRows();
} else {
iterator = new int[modelRowCount];
for (int i = 0; i < modelRowCount; i++) {
iterator[i] = i;
}
}
for (int i : iterator) {
// 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);
@ -173,13 +183,20 @@ public class SimulationTableCSVExport {
return fullOutputResult.toString();
}
public void export(File file, String fieldSep, int precision) {
/**
* Export the simulation table data to a CSV file
* @param file the file to save the results to
* @param fieldSep the CSV separator to use
* @param precision the decimal precision to use in the CSV file
* @param onlySelected if true, only export the selected rows in the table
*/
public void export(File file, String fieldSep, int precision, boolean onlySelected) {
if (file == null) {
log.warn("No file selected for export");
return;
}
String CSVData = generateCSVDate(fieldSep, precision);
String CSVData = generateCSVDate(fieldSep, precision, onlySelected);
this.dumpDataToFile(CSVData, file);
log.info("Simulation table data exported to " + file.getAbsolutePath());
}

View File

@ -88,7 +88,6 @@ import net.sf.openrocket.gui.util.SwingPreferences;
import net.sf.openrocket.gui.util.URLUtil;
import net.sf.openrocket.l10n.Translator;
import net.sf.openrocket.logging.Markers;
import net.sf.openrocket.rocketcomponent.AxialStage;
import net.sf.openrocket.rocketcomponent.ComponentChangeEvent;
import net.sf.openrocket.rocketcomponent.ComponentChangeListener;
import net.sf.openrocket.rocketcomponent.Rocket;
@ -568,7 +567,7 @@ public class BasicFrame extends JFrame {
//// END CREATE and implement File > "Encode 3D" menu and submenu
*/
// export sim table...
AbstractAction simTableExportAction = simulationPanel.getSimulationTableAsCSVExportAction();
AbstractAction simTableExportAction = simulationPanel.getExportSimulationTableAsCSVAction();
JMenuItem exportSimTableToCSVMenuItem = new JMenuItem(simTableExportAction);
menu.add(exportSimTableToCSVMenuItem);

View File

@ -114,6 +114,7 @@ public class SimulationPanel extends JPanel {
private final SimulationAction duplicateSimulationAction;
private final SimulationAction deleteSimulationAction;
private final SimulationAction simTableExportAction;
private final SimulationAction selectedSimsExportAction;
private int[] previousSelection = null;
private JMenuItem exportSimTableToCSVMenuItem;
@ -132,6 +133,7 @@ public class SimulationPanel extends JPanel {
duplicateSimulationAction = new DuplicateSimulationAction();
deleteSimulationAction = new DeleteSimulationAction();
simTableExportAction = new ExportSimulationTableAsCSVAction();
selectedSimsExportAction = new ExportSelectedSimulationsAsCSVAction();
//////// The simulation action buttons ////////
@ -190,7 +192,7 @@ public class SimulationPanel extends JPanel {
pm.addSeparator();
pm.add(runSimulationAction);
pm.add(plotSimulationAction);
pm.add(simTableExportAction);
pm.add(selectedSimsExportAction);
// The normal left/right and tab/shift-tab key action traverses each cell/column of the table instead of going to the next row.
TableRowTraversalPolicy.setTableRowTraversalPolicy(simulationTable);
@ -388,6 +390,79 @@ public class SimulationPanel extends JPanel {
openDialog(false, sims);
}
private void exportSimulationsToCSV(boolean onlySelected) {
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 = setUpSimExportCSVFileChooser();
int selectionStatus = fch.showSaveDialog(tableParent);
if (selectionStatus != JFileChooser.APPROVE_OPTION) {
log.debug("User cancelled CSV export");
return;
}
// Fetch the info from the file chooser
File CSVFile = fch.getSelectedFile();
CSVFile = FileHelper.forceExtension(CSVFile, "csv");
if (!FileHelper.confirmWrite(CSVFile, SimulationPanel.this)) {
log.debug("User cancelled CSV export overwrite");
return;
}
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.export(CSVFile, separator, precision, onlySelected);
}
/**
* Create the file chooser to save the CSV file.
* @return The file chooser.
*/
private JFileChooser setUpSimExportCSVFileChooser() {
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());
fch.setAcceptAllFileFilterUsed(false);
// 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);
// TODO: update this dynamically instead of hard-coded values
// The macOS file chooser has an issue where it does not update its size when the accessory is added.
if (SystemInfo.getPlatform() == SystemInfo.Platform.MAC_OS) {
Dimension currentSize = fch.getPreferredSize();
Dimension newSize = new Dimension((int) (1.5 * currentSize.width), (int) (1.3 * currentSize.height));
fch.setPreferredSize(newSize);
}
return fch;
}
private Simulation[] getSelectedSimulations() {
int[] selection = simulationTable.getSelectedRows();
if (selection.length == 0) {
@ -456,9 +531,9 @@ public class SimulationPanel extends JPanel {
/**
* Return the action for exporting the simulation table data to a CSV file.
* @return
* @return the action for exporting the simulation table data to a CSV file.
*/
public AbstractAction getSimulationTableAsCSVExportAction() {
public AbstractAction getExportSimulationTableAsCSVAction() {
return simTableExportAction;
}
@ -473,6 +548,7 @@ public class SimulationPanel extends JPanel {
plotSimulationAction.updateEnabledState();
duplicateSimulationAction.updateEnabledState();
simTableExportAction.updateEnabledState();
selectedSimsExportAction.updateEnabledState();
}
/// when the simulation tab is selected this run outdated simulated if appropriate.
@ -624,85 +700,19 @@ public class SimulationPanel extends JPanel {
}
}
/**
* Export the entire simulation table as a CSV file.
*/
class ExportSimulationTableAsCSVAction extends SimulationAction {
public ExportSimulationTableAsCSVAction() {
putValue(NAME, trans.get("simpanel.pop.exportToCSV"));
putValue(NAME, trans.get("simpanel.pop.exportSimTableToCSV"));
putValue(SMALL_ICON, Icons.SIM_TABLE_EXPORT);
}
@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.debug("User cancelled CSV export");
return;
}
// Fetch the info from the file chooser
File CSVFile = fch.getSelectedFile();
CSVFile = FileHelper.forceExtension(CSVFile, "csv");
if (!FileHelper.confirmWrite(CSVFile, SimulationPanel.this)) {
log.debug("User cancelled CSV export overwrite");
return;
}
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.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());
fch.setAcceptAllFileFilterUsed(false);
// 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);
// TODO: update this dynamically instead of hard-coded values
// The macOS file chooser has an issue where it does not update its size when the accessory is added.
if (SystemInfo.getPlatform() == SystemInfo.Platform.MAC_OS) {
Dimension currentSize = fch.getPreferredSize();
Dimension newSize = new Dimension((int) (1.5 * currentSize.width), (int) (1.3 * currentSize.height));
fch.setPreferredSize(newSize);
}
return fch;
exportSimulationsToCSV(false);
}
@Override
@ -712,6 +722,29 @@ public class SimulationPanel extends JPanel {
}
/**
* Export only the selected simulations as a CSV file.
*/
class ExportSelectedSimulationsAsCSVAction extends SimulationAction {
public ExportSelectedSimulationsAsCSVAction() {
putValue(NAME, trans.get("simpanel.pop.exportSelectedSimsToCSV"));
putValue(SMALL_ICON, Icons.SIM_TABLE_EXPORT);
}
@Override
public void actionPerformed(ActionEvent arg0) {
exportSimulationsToCSV(true);
}
@Override
public void updateEnabledState() {
setEnabled(simulationTableModel != null && simulationTableModel.getRowCount() > 0 &&
simulationTable.getSelectedRowCount() > 0);
}
}
class PlotSimulationAction extends SimulationAction {
public PlotSimulationAction() {
putValue(NAME, trans.get("simpanel.pop.plot"));