From 887e38b6373cbcf8fcbdc72241415724d8a6a1bb Mon Sep 17 00:00:00 2001 From: SiboVG Date: Mon, 6 Mar 2023 20:35:47 +0100 Subject: [PATCH] Refactor to better use export Action --- core/resources/pix/icons/sim_table_export.png | Bin 0 -> 723 bytes .../sf/openrocket/gui/main/BasicFrame.java | 17 +---- .../openrocket/gui/main/SimulationPanel.java | 70 ++++++------------ .../src/net/sf/openrocket/gui/util/Icons.java | 1 + 4 files changed, 27 insertions(+), 61 deletions(-) create mode 100644 core/resources/pix/icons/sim_table_export.png diff --git a/core/resources/pix/icons/sim_table_export.png b/core/resources/pix/icons/sim_table_export.png new file mode 100644 index 0000000000000000000000000000000000000000..25b74d18f740ff208435f72333a1633569c0e833 GIT binary patch literal 723 zcmV;^0xbQBP)9=EBdhkBh&jKg_`&J(Mr{5K9?YB z7%NS3^ZC#w7o(tnOANaU&GbDgva8U-z`c)j6qd7VU7r32pE$mM#UEBK-6|qt9$Fs- z6>t{%pB71ynwG1c@8bc0>raNK6Dt8qRVe;QF-EXH2&zgZfzI{KWSyaNeKXb?5(OWH zv)$_uQKBdbLe2@*YL(JNiVIR}Vqy(9UXB8Al#S?e6$ef`OJ z*^@Dj?#>2+q<{qlHA2?aai;eJ-%1m>7t)ru4w<;#O*nKUzaX~*8Diz*A!^1TLcz "Encode 3D" menu and submenu */ // export sim table... - JMenuItem exportSimTableToCSVMenuItem = new JMenuItem(trans.get("main.menu.file.table.exportToCSV")); - exportSimTableToCSVMenuItem.setIcon(Icons.FILE_EXPORT_AS); + AbstractAction simTableExportAction = simulationPanel.getSimulationTableAsCSVExportAction(); + JMenuItem exportSimTableToCSVMenuItem = new JMenuItem(simTableExportAction); menu.add(exportSimTableToCSVMenuItem); - exportSimTableToCSVMenuItem.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - exportSimulationTableToCSVAction(e); - } - }); - exportSimTableToCSVMenuItem.setEnabled(simulationPanel.isReadyToExportSimTableToCSV(exportSimTableToCSVMenuItem)); + menu.addSeparator(); //// Close @@ -1441,10 +1436,6 @@ public class BasicFrame extends JFrame { } // END ROCKSIM Export Action - public boolean exportSimulationTableToCSVAction(ActionEvent e) { - simulationPanel.runExportSimTableToCSVAction(e); - return false; - } /** * Perform the writing of the design to the given file in RockSim format. * diff --git a/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java b/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java index cd88a003b..eec5f286b 100644 --- a/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java +++ b/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java @@ -18,7 +18,6 @@ import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.Comparator; -import java.util.HashMap; import javax.swing.AbstractAction; import javax.swing.JButton; @@ -104,6 +103,7 @@ public class SimulationPanel extends JPanel { private final JButton runButton; private final JButton deleteButton; private final JButton plotButton; + private final JButton simTableExportButton; private final JPopupMenu pm; private final SimulationAction editSimulationAction; @@ -111,9 +111,8 @@ public class SimulationPanel extends JPanel { private final SimulationAction plotSimulationAction; private final SimulationAction duplicateSimulationAction; private final SimulationAction deleteSimulationAction; - private final SimulationAction dumpSimulationTableAction; + private final SimulationAction simTableExportAction; - private final HashMap valueColumnToUnitString = new HashMap<>(); private int[] previousSelection = null; private JMenuItem exportSimTableToCSVMenuItem; @@ -130,7 +129,7 @@ public class SimulationPanel extends JPanel { plotSimulationAction = new PlotSimulationAction(); duplicateSimulationAction = new DuplicateSimulationAction(); deleteSimulationAction = new DeleteSimulationAction(); - dumpSimulationTableAction = new DumpSimulationToCSVAction(); + simTableExportAction = new ExportSimulationTableAsCSVAction(); //////// The simulation action buttons //////// @@ -152,11 +151,6 @@ public class SimulationPanel extends JPanel { runButton.setToolTipText(trans.get("simpanel.but.ttip.runsimu")); this.add(runButton, "gapright para"); - //// Run then Dump simulations - JButton dumpButton = new IconButton(); - RocketActions.tieActionToButton(dumpButton, dumpSimulationTableAction, trans.get("simpanel.but.runsimulations")); - - //// Delete simulations button deleteButton = new IconButton(); RocketActions.tieActionToButton(deleteButton, deleteSimulationAction, trans.get("simpanel.but.deletesimulations")); @@ -168,6 +162,9 @@ public class SimulationPanel extends JPanel { RocketActions.tieActionToButton(plotButton, plotSimulationAction, trans.get("simpanel.but.plotexport")); this.add(plotButton, "wrap para"); + //// Run then Dump simulations + simTableExportButton = new IconButton(); + RocketActions.tieActionToButton(simTableExportButton, simTableExportAction, trans.get("simpanel.but.runsimulations")); //////// The simulation table @@ -191,7 +188,7 @@ public class SimulationPanel extends JPanel { pm.addSeparator(); pm.add(runSimulationAction); pm.add(plotSimulationAction); - pm.add(dumpSimulationTableAction); + pm.add(simTableExportAction); // 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); @@ -247,7 +244,6 @@ public class SimulationPanel extends JPanel { if ((simulationTable.getSelectedRow() != previousSelectedRow) || (simulationTable.getSelectedRowCount() != previousSelectedRowCount)) { updateButtonStates(); - updateMenuStates(); previousSelectedRow = simulationTable.getSelectedRow(); previousSelectedRowCount = simulationTable.getSelectedRowCount(); } @@ -456,6 +452,14 @@ public class SimulationPanel extends JPanel { } + /** + * Return the action for exporting the simulation table data to a CSV file. + * @return + */ + public AbstractAction getSimulationTableAsCSVExportAction() { + return simTableExportAction; + } + protected void doPopup(MouseEvent e) { pm.show(e.getComponent(), e.getX(), e.getY()); } @@ -466,6 +470,7 @@ public class SimulationPanel extends JPanel { runSimulationAction.updateEnabledState(); plotSimulationAction.updateEnabledState(); duplicateSimulationAction.updateEnabledState(); + simTableExportAction.updateEnabledState(); } /// when the simulation tab is selected this run outdated simulated if appropriate. @@ -617,11 +622,11 @@ public class SimulationPanel extends JPanel { } } - class DumpSimulationToCSVAction extends SimulationAction { + class ExportSimulationTableAsCSVAction extends SimulationAction { - public DumpSimulationToCSVAction() { + public ExportSimulationTableAsCSVAction() { putValue(NAME, trans.get("simpanel.pop.exportToCSV")); - putValue(SMALL_ICON, Icons.FILE_EXPORT_AS); + putValue(SMALL_ICON, Icons.SIM_TABLE_EXPORT); } @Override @@ -681,12 +686,14 @@ public class SimulationPanel extends JPanel { CsvOptionPanel CSVOptions = new CsvOptionPanel(SimulationTableCSVExport.class); fch.setAccessory(CSVOptions); + // TODO: update the file chooser dimensions, it's cropped because of the accessory panel + return fch; } @Override public void updateEnabledState() { - setEnabled(true); + setEnabled(simulationTableModel != null && simulationTableModel.getRowCount() > 0); } } @@ -1111,37 +1118,4 @@ public class SimulationPanel extends JPanel { } } } - - public boolean isReadyToExportSimTableToCSV(JMenuItem exportSimTableToCSVMenuItem) { - // This is the cheapest, dirtiest way I could think of to allow the activation/deactivation - // of the export to csv action under the file menu. With this, we can get activation of the - // menu based upon the exitence of the table model PLUS one or more row in the table. - if (this.exportSimTableToCSVMenuItem == null && exportSimTableToCSVMenuItem != null) { - this.exportSimTableToCSVMenuItem = exportSimTableToCSVMenuItem; - } - - // an example being loaded will have file == null and simulationTableModel != null... likewise a new rocket - // will probably be the same... so this is probably belt-and-suspenders - if (simulationTableModel != null && simulationTableModel.getRowCount() > 0) { - return true; - } - return false; - } - - /** - * A menu state updater that is an analog to the button state updater. - */ - private void updateMenuStates() { - // update the File->Export simulations table... menu entry - this.exportSimTableToCSVMenuItem.setEnabled(simulationTableModel != null && simulationTableModel.getRowCount() > 0); - } - - /** - * Run the csv export action... - * @param e - */ - public void runExportSimTableToCSVAction(ActionEvent e) { - - dumpSimulationTableAction.actionPerformed(e); - } } diff --git a/swing/src/net/sf/openrocket/gui/util/Icons.java b/swing/src/net/sf/openrocket/gui/util/Icons.java index bbbe1b052..4fc17a319 100644 --- a/swing/src/net/sf/openrocket/gui/util/Icons.java +++ b/swing/src/net/sf/openrocket/gui/util/Icons.java @@ -56,6 +56,7 @@ public class Icons { public static final Icon FILE_PRINT = loadImageIcon("pix/icons/print-design.specs.png", "Print specifications"); // public static final Icon FILE_IMPORT = loadImageIcon("pix/icons/model_import.png", "Import"); public static final Icon FILE_EXPORT_AS = loadImageIcon("pix/icons/model_export.png", "Export model as"); + public static final Icon SIM_TABLE_EXPORT = loadImageIcon("pix/icons/sim_table_export.png", "Export simulation table"); public static final Icon ENCODE_3D = loadImageIcon("pix/icons/model_encode3d.png", "Encode 3D"); public static final Icon FILE_CLOSE = loadImageIcon("pix/icons/document-close.png", "Close document"); public static final Icon FILE_QUIT = loadImageIcon("pix/icons/application-exit.png", "Quit OpenRocket");