From 15829ac22a70c53fb193839cccecaad6fc74986c Mon Sep 17 00:00:00 2001 From: SiboVG Date: Mon, 26 Aug 2024 18:49:33 +0200 Subject: [PATCH] Allow no components selection, but show a warning --- .../main/resources/l10n/messages.properties | 3 + .../componentanalysis/CAExportPanel.java | 85 ++++++++++++------- 2 files changed, 57 insertions(+), 31 deletions(-) diff --git a/core/src/main/resources/l10n/messages.properties b/core/src/main/resources/l10n/messages.properties index 4e0851b09..df8b24556 100644 --- a/core/src/main/resources/l10n/messages.properties +++ b/core/src/main/resources/l10n/messages.properties @@ -970,6 +970,9 @@ CAPlotExportDialog.tab.Export = Export CAExportPanel.Col.Components = Components CAExportPanel.checkbox.Includecadesc = Include component analysis description CAExportPanel.checkbox.ttip.Includecadesc = Include information at the beginning of the file describing the component analysis. +CAExportPanel.dlg.MissingComponents.txt1 = The following data type(s) have no selected components and will be ignored during exporting: +CAExportPanel.dlg.MissingComponents.txt2 = Do you want to continue with the export? +CAExportPanel.dlg.MissingComponents.title = No components selected ! CADataTypeGroup CADataTypeGroup.DOMAIN = Domain Parameter diff --git a/swing/src/main/java/info/openrocket/swing/gui/dialogs/componentanalysis/CAExportPanel.java b/swing/src/main/java/info/openrocket/swing/gui/dialogs/componentanalysis/CAExportPanel.java index fe981c649..2944fabd8 100644 --- a/swing/src/main/java/info/openrocket/swing/gui/dialogs/componentanalysis/CAExportPanel.java +++ b/swing/src/main/java/info/openrocket/swing/gui/dialogs/componentanalysis/CAExportPanel.java @@ -6,6 +6,7 @@ import info.openrocket.core.l10n.Translator; import info.openrocket.core.rocketcomponent.RocketComponent; import info.openrocket.core.startup.Application; import info.openrocket.core.unit.Unit; +import info.openrocket.core.util.StringUtils; import info.openrocket.swing.gui.components.CsvOptionPanel; import info.openrocket.swing.gui.util.FileHelper; import info.openrocket.swing.gui.util.SaveCSVWorker; @@ -20,6 +21,7 @@ import javax.swing.JCheckBox; import javax.swing.JComponent; import javax.swing.JFileChooser; import javax.swing.JLabel; +import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTable; import javax.swing.SwingUtilities; @@ -127,6 +129,39 @@ public class CAExportPanel extends CSVExportPanel { public boolean doExport() { CADataBranch branch = this.parent.runParameterSweep(); + // Check for data types with no selected components + List typesWithNoComponents = new ArrayList<>(); + for (int i = 0; i < selected.length; i++) { + if (selected[i]) { + boolean hasSelectedComponent = selectedComponents.get(i).values().stream().anyMatch(v -> v); + if (!hasSelectedComponent) { + typesWithNoComponents.add(types[i]); + } + } + } + + // Show warning dialog if there are data types with no selected components + if (!typesWithNoComponents.isEmpty()) { + StringBuilder message = new StringBuilder(trans.get("CAExportPanel.dlg.MissingComponents.txt1")); + message.append("\n\n"); + for (CADataType type : typesWithNoComponents) { + message.append("- ").append(StringUtils.removeHTMLTags(type.getName())).append("\n"); + } + message.append("\n").append(trans.get("CAExportPanel.dlg.MissingComponents.txt2")); + + int result = JOptionPane.showConfirmDialog( + this, + message.toString(), + trans.get("CAExportPanel.dlg.MissingComponents.title"), + JOptionPane.YES_NO_OPTION, + JOptionPane.WARNING_MESSAGE + ); + + if (result != JOptionPane.YES_OPTION) { + return false; + } + } + JFileChooser chooser = new SaveFileChooser(); chooser.setFileFilter(FileHelper.CSV_FILTER); chooser.setCurrentDirectory(((SwingPreferences) Application.getPreferences()).getDefaultDirectory()); @@ -161,34 +196,28 @@ public class CAExportPanel extends CSVExportPanel { n++; } - - CADataType[] fieldTypes = new CADataType[n]; - Unit[] fieldUnits = new Unit[n]; - int pos = 0; - for (int i = 0; i < selected.length; i++) { - if (selected[i]) { - fieldTypes[pos] = types[i]; - fieldUnits[pos] = units[i]; - pos++; - } - } - + List fieldTypes = new ArrayList<>(); + List fieldUnits = new ArrayList<>(); Map> components = new HashMap<>(); + // Iterate through the table to get selected items for (int i = 0; i < selected.length; i++) { - if (!selected[i]) { - continue; - } - for (Map.Entry entry : selectedComponents.get(i).entrySet()) { - if (entry.getValue()) { - CADataType type = types[i]; - List typeComponents = components.getOrDefault(type, new ArrayList<>()); - typeComponents.add(entry.getKey()); - components.put(type, typeComponents); + if (selected[i]) { + List selectedComponentsList = new ArrayList<>(); + for (Map.Entry entry : selectedComponents.get(i).entrySet()) { + if (entry.getValue()) { + selectedComponentsList.add(entry.getKey()); + } + } + if (!selectedComponentsList.isEmpty()) { + fieldTypes.add(types[i]); + fieldUnits.add(units[i]); + components.put(types[i], selectedComponentsList); } } } + if (fieldSep.equals(SPACE)) { fieldSep = " "; } else if (fieldSep.equals(TAB)) { @@ -196,8 +225,9 @@ public class CAExportPanel extends CSVExportPanel { } - SaveCSVWorker.exportCAData(file, parent.getParameters(), branch, parent.getSelectedParameter(), fieldTypes, - components, fieldUnits, fieldSep, decimalPlaces, isExponentialNotation, commentChar, analysisComments, + SaveCSVWorker.exportCAData(file, parent.getParameters(), branch, parent.getSelectedParameter(), + fieldTypes.toArray(new CADataType[0]), components, fieldUnits.toArray(new Unit[0]), fieldSep, + decimalPlaces, isExponentialNotation, commentChar, analysisComments, fieldDescriptions, SwingUtilities.getWindowAncestor(this)); return true; @@ -467,14 +497,7 @@ public class CAExportPanel extends CSVExportPanel { private final ItemListener checkBoxListener = e -> { if (updatingState.get()) return; - JCheckBox source = (JCheckBox) e.getSource(); - if (e.getStateChange() == ItemEvent.DESELECTED) { - if (checkBoxMap.values().stream().noneMatch(JCheckBox::isSelected)) { - source.setSelected(true); - } - } - - // Notify the listener of the change + // Remove the check for deselection and forced selection if (listener != null) { listener.onComponentSelectionChanged(getComponentStates()); }