From 7b6367b30b0832fd51e86356ad8cbe32049e74e0 Mon Sep 17 00:00:00 2001 From: Daniel_M_Williams Date: Sun, 31 May 2015 12:38:39 -0400 Subject: [PATCH] Bugfix: If configured wrong, booster stage recovery device would not deploy. This patch prevents the UI from showing those options. --- .../sf/openrocket/gui/adaptors/EnumModel.java | 50 ++++++++++++++++--- .../gui/configdialog/ParachuteConfig.java | 12 ++++- .../configdialog/RecoveryDeviceConfig.java | 2 + .../gui/configdialog/StreamerConfig.java | 6 +++ .../DeploymentSelectionDialog.java | 6 ++- 5 files changed, 66 insertions(+), 10 deletions(-) diff --git a/swing/src/net/sf/openrocket/gui/adaptors/EnumModel.java b/swing/src/net/sf/openrocket/gui/adaptors/EnumModel.java index a6757a380..4a7b49f98 100644 --- a/swing/src/net/sf/openrocket/gui/adaptors/EnumModel.java +++ b/swing/src/net/sf/openrocket/gui/adaptors/EnumModel.java @@ -1,9 +1,13 @@ package net.sf.openrocket.gui.adaptors; +import java.util.ArrayList; import java.util.EventObject; import javax.swing.AbstractListModel; import javax.swing.ComboBoxModel; +import javax.swing.MutableComboBoxModel; + +import org.jfree.util.Log; import net.sf.openrocket.util.ChangeSource; import net.sf.openrocket.util.Reflection; @@ -11,7 +15,7 @@ import net.sf.openrocket.util.StateChangeListener; public class EnumModel> extends AbstractListModel - implements ComboBoxModel, StateChangeListener { + implements ComboBoxModel, MutableComboBoxModel, StateChangeListener { private final ChangeSource source; private final String valueName; @@ -20,6 +24,8 @@ public class EnumModel> extends AbstractListModel private final Enum[] values; private Enum currentValue = null; + ArrayList> displayedValues = new ArrayList>(); + private final Reflection.Method getMethod; private final Reflection.Method setMethod; @@ -60,6 +66,9 @@ public class EnumModel> extends AbstractListModel else this.values = enumClass.getEnumConstants(); + for (Enum e : this.values){ + this.displayedValues.add( e ); + } this.nullText = nullText; stateChanged(null); // Update current value @@ -99,20 +108,20 @@ public class EnumModel> extends AbstractListModel @Override public Object getElementAt(int index) { + if( ( index < 0 ) || ( index >= this.displayedValues.size())){ + return nullText; // bad parameter + } + if (values[index] == null) return nullText; - return values[index]; + return displayedValues.get( index); } @Override public int getSize() { - return values.length; + return displayedValues.size(); } - - - - @SuppressWarnings("unchecked") @Override public void stateChanged(EventObject e) { @@ -130,4 +139,31 @@ public class EnumModel> extends AbstractListModel return "EnumModel["+source.getClass().getCanonicalName()+":"+valueName+"]"; } + @Override + public void addElement(Object item) { + // Not actually allowed. The model starts out with all the enums, and only allows hiding some. + } + + @Override + public void removeElement(Object obj) { + if( null == obj ){ + return; + } + this.displayedValues.remove( obj ); + } + + @Override + public void insertElementAt(Object item, int index) { + // Not actually allowed. The model starts out with all the enums, and only allows hiding some. + } + + @Override + public void removeElementAt(int index) { + if( ( index < 0 ) || ( index >= this.displayedValues.size())){ + return; // bad parameter + } + + this.displayedValues.remove( index ); + } + } diff --git a/swing/src/net/sf/openrocket/gui/configdialog/ParachuteConfig.java b/swing/src/net/sf/openrocket/gui/configdialog/ParachuteConfig.java index d20ee770e..107fca930 100644 --- a/swing/src/net/sf/openrocket/gui/configdialog/ParachuteConfig.java +++ b/swing/src/net/sf/openrocket/gui/configdialog/ParachuteConfig.java @@ -4,6 +4,7 @@ package net.sf.openrocket.gui.configdialog; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import javax.swing.ComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JLabel; @@ -25,6 +26,7 @@ import net.sf.openrocket.gui.components.UnitSelector; import net.sf.openrocket.l10n.Translator; import net.sf.openrocket.material.Material; import net.sf.openrocket.rocketcomponent.DeploymentConfiguration; +import net.sf.openrocket.rocketcomponent.DeploymentConfiguration.DeployEvent; import net.sf.openrocket.rocketcomponent.Parachute; import net.sf.openrocket.rocketcomponent.RocketComponent; import net.sf.openrocket.startup.Application; @@ -85,7 +87,7 @@ public class ParachuteConfig extends RecoveryDeviceConfig { //// Material: panel.add(new JLabel(trans.get("ParachuteCfg.lbl.Material"))); - JComboBox combo = new JComboBox(new MaterialModel(panel, component, + JComboBox combo = new JComboBox(new MaterialModel(panel, component, Material.Type.SURFACE)); combo.setToolTipText(trans.get("ParachuteCfg.combo.MaterialModel")); panel.add(combo, "spanx 3, growx, wrap 30lp"); @@ -193,7 +195,13 @@ public class ParachuteConfig extends RecoveryDeviceConfig { panel.add(new JLabel(trans.get("ParachuteCfg.lbl.Deploysat") + " " + CommonStrings.dagger), ""); DeploymentConfiguration deploymentConfig = parachute.getDeploymentConfiguration().getDefault(); - combo = new JComboBox(new EnumModel(deploymentConfig, "DeployEvent")); + // this issues a warning because EnumModel ipmlements ComboBoxModel without a parameter... + ComboBoxModel deployOptionsModel = new EnumModel(deploymentConfig, "DeployEvent"); + combo = new JComboBox( deployOptionsModel ); + if( (component.getStageNumber() + 1 ) == d.getRocket().getStageCount() ){ + // This is the bottom stage: Restrict deployment options. + combo.removeItem( DeployEvent.LOWER_STAGE_SEPARATION ); + } panel.add(combo, "spanx 3, growx, wrap"); // ... and delay diff --git a/swing/src/net/sf/openrocket/gui/configdialog/RecoveryDeviceConfig.java b/swing/src/net/sf/openrocket/gui/configdialog/RecoveryDeviceConfig.java index 2f5f37c30..ea5a71438 100644 --- a/swing/src/net/sf/openrocket/gui/configdialog/RecoveryDeviceConfig.java +++ b/swing/src/net/sf/openrocket/gui/configdialog/RecoveryDeviceConfig.java @@ -13,6 +13,8 @@ import net.sf.openrocket.rocketcomponent.RocketComponent; public abstract class RecoveryDeviceConfig extends RocketComponentConfig { + private static final long serialVersionUID = 7263235700953855062L; + protected final List altitudeComponents = new ArrayList(); public RecoveryDeviceConfig(OpenRocketDocument d, RocketComponent component) { diff --git a/swing/src/net/sf/openrocket/gui/configdialog/StreamerConfig.java b/swing/src/net/sf/openrocket/gui/configdialog/StreamerConfig.java index a9977abb7..68ad60ccf 100644 --- a/swing/src/net/sf/openrocket/gui/configdialog/StreamerConfig.java +++ b/swing/src/net/sf/openrocket/gui/configdialog/StreamerConfig.java @@ -4,6 +4,7 @@ package net.sf.openrocket.gui.configdialog; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import javax.swing.ComboBoxModel; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; @@ -26,6 +27,7 @@ import net.sf.openrocket.material.Material; import net.sf.openrocket.rocketcomponent.DeploymentConfiguration; import net.sf.openrocket.rocketcomponent.RocketComponent; import net.sf.openrocket.rocketcomponent.Streamer; +import net.sf.openrocket.rocketcomponent.DeploymentConfiguration.DeployEvent; import net.sf.openrocket.startup.Application; import net.sf.openrocket.unit.UnitGroup; @@ -196,6 +198,10 @@ public class StreamerConfig extends RecoveryDeviceConfig { DeploymentConfiguration deploymentConfig = streamer.getDeploymentConfiguration().getDefault(); combo = new JComboBox(new EnumModel(deploymentConfig, "DeployEvent")); + if( (component.getStageNumber() + 1 ) == d.getRocket().getStageCount() ){ + // This is the bottom stage. restrict deployment options. + combo.removeItem( DeployEvent.LOWER_STAGE_SEPARATION ); + } panel.add(combo, "spanx 3, growx, wrap"); // ... and delay diff --git a/swing/src/net/sf/openrocket/gui/dialogs/flightconfiguration/DeploymentSelectionDialog.java b/swing/src/net/sf/openrocket/gui/dialogs/flightconfiguration/DeploymentSelectionDialog.java index 6744f07eb..3b8430153 100644 --- a/swing/src/net/sf/openrocket/gui/dialogs/flightconfiguration/DeploymentSelectionDialog.java +++ b/swing/src/net/sf/openrocket/gui/dialogs/flightconfiguration/DeploymentSelectionDialog.java @@ -76,7 +76,11 @@ public class DeploymentSelectionDialog extends JDialog { //// Deploys at: panel.add(new JLabel(trans.get("ParachuteCfg.lbl.Deploysat")), ""); - final JComboBox event = new JComboBox(new EnumModel(newConfiguration, "DeployEvent")); + final JComboBox event = new JComboBox(new EnumModel(newConfiguration, "DeployEvent")); + if( (component.getStageNumber() + 1 ) == rocket.getStageCount() ){ + // This is the bottom stage: Restrict deployment options. + event.removeItem( DeployEvent.LOWER_STAGE_SEPARATION ); + } panel.add(event, "spanx 3, growx, wrap"); // ... and delay