From fbd40859a5fa1765bb37db852eaf45e8a3426d95 Mon Sep 17 00:00:00 2001 From: Daniel_M_Williams Date: Sat, 23 Jan 2016 20:03:53 -0500 Subject: [PATCH] [Bugfix] Cleaned up event-handling, esp. with active stages & graphics - added new ComponentChangeEvent type: GRAPHIC - does not change the rocket structure, nor behavior, but requires updates to the UI - removed event handling from FlightConfiguration - caused a circular event loop - also, collected too many responsibilities in the class (bad code smell) - minor refactor of event handling in Rocket. (no change in behavior) - Fixed StageSelector behavior - should now pull selected configuration from rocket - should now correctly notify rocket (and indirectly, the graphics) --- .../rocketcomponent/ComponentChangeEvent.java | 10 ++-- .../rocketcomponent/FlightConfiguration.java | 14 ++---- .../sf/openrocket/rocketcomponent/Rocket.java | 48 +++++++++---------- .../rocketcomponent/RocketComponent.java | 2 +- .../gui/components/StageSelector.java | 37 +++++++------- .../gui/dialogs/ComponentAnalysisDialog.java | 4 +- .../sf/openrocket/gui/main/RocketActions.java | 2 + .../gui/scalefigure/RocketPanel.java | 12 ++++- 8 files changed, 68 insertions(+), 61 deletions(-) diff --git a/core/src/net/sf/openrocket/rocketcomponent/ComponentChangeEvent.java b/core/src/net/sf/openrocket/rocketcomponent/ComponentChangeEvent.java index f5a21cce9..ffeab7f0c 100644 --- a/core/src/net/sf/openrocket/rocketcomponent/ComponentChangeEvent.java +++ b/core/src/net/sf/openrocket/rocketcomponent/ComponentChangeEvent.java @@ -14,7 +14,9 @@ public class ComponentChangeEvent extends EventObject { UNDO( 16, "UNDO"), MOTOR( 32, "Motor"), EVENT( 64, "Event"), - TEXTURE ( 128, "Texture"); + TEXTURE ( 128, "Texture") + , GRAPHIC( 256, "Configuration") + ; protected int value; protected String name; @@ -39,8 +41,7 @@ public class ComponentChangeEvent extends EventObject { /** A change that affects the mass and aerodynamic properties of the rocket */ public static final int AEROMASS_CHANGE = (TYPE.MASS.value | TYPE.AERODYNAMIC.value ); public static final int BOTH_CHANGE = AEROMASS_CHANGE; // syntactic sugar / backward compatibility - /** when a flight configuration fires an event, it is of this type */ - public static final int CONFIG_CHANGE = (TYPE.MASS.value | TYPE.AERODYNAMIC.value | TYPE.TREE.value | TYPE.MOTOR.value | TYPE.EVENT.value); + /** A change that affects the rocket tree structure */ public static final int TREE_CHANGE = TYPE.TREE.value; @@ -52,6 +53,9 @@ public class ComponentChangeEvent extends EventObject { public static final int EVENT_CHANGE = TYPE.EVENT.value; /** A change to the 3D texture assigned to a component*/ public static final int TEXTURE_CHANGE = TYPE.TEXTURE.value; + // when a flight configuration fires an event, it is of this type + // UI-only change, but does not effect the true + public static final int GRAPHIC_CHANGE = TYPE.GRAPHIC.value; //// A bit-field that contains all possible change types. //// Will output as -1. for an explanation, see "twos-complement" representation of signed integers diff --git a/core/src/net/sf/openrocket/rocketcomponent/FlightConfiguration.java b/core/src/net/sf/openrocket/rocketcomponent/FlightConfiguration.java index 984f45de6..9c2bf85ea 100644 --- a/core/src/net/sf/openrocket/rocketcomponent/FlightConfiguration.java +++ b/core/src/net/sf/openrocket/rocketcomponent/FlightConfiguration.java @@ -122,7 +122,7 @@ public class FlightConfiguration implements FlightConfigurableParameterfalse) or active (true) */ - public void setStageActive(final int stageNumber, final boolean _active ) { - this.setStageActive(stageNumber, _active, true ); - } - - private void setStageActive(final int stageNumber, final boolean _active, final boolean updateRequired ) { + private void setStageActive(final int stageNumber, final boolean _active ) { if ((0 <= stageNumber) && (stages.containsKey(stageNumber))) { stages.get(stageNumber).active = _active; - if( updateRequired ){ - update(); - } return; } log.error("error: attempt to retrieve via a bad stage number: " + stageNumber); @@ -396,6 +389,7 @@ public class FlightConfiguration implements FlightConfigurableParameter buttons = new ArrayList(); - public StageSelector(FlightConfiguration configuration) { + public StageSelector(Rocket _rkt) { super(new MigLayout("gap 0!")); - this.configuration = configuration; - - updateButtons(); + this.rocket = _rkt; + updateButtons( this.rocket.getSelectedConfiguration() ); } - private void updateButtons() { + private void updateButtons( final FlightConfiguration configuration ) { int stages = configuration.getStageCount(); if (buttons.size() == stages) return; @@ -44,6 +45,7 @@ public class StageSelector extends JPanel implements StateChangeListener { for(AxialStage stage : configuration.getRocket().getStageList()){ int stageNum = stage.getStageNumber(); JToggleButton button = new JToggleButton(new StageAction(stageNum)); + button.setSelected(true); this.add(button); buttons.add(button); } @@ -51,20 +53,20 @@ public class StageSelector extends JPanel implements StateChangeListener { this.revalidate(); } - @Override - public void stateChanged(EventObject e) { - updateButtons(); + public void stateChanged(EventObject eo) { + Object source = eo.getSource(); + if( source instanceof Rocket ){ + Rocket rkt = (Rocket) eo.getSource(); + updateButtons( rkt.getSelectedConfiguration() ); + } } - - private class StageAction extends AbstractAction implements StateChangeListener { - private static final long serialVersionUID = 7433006728984943763L; + private class StageAction extends AbstractAction { private final int stageNumber; public StageAction(final int stage) { this.stageNumber = stage; - stateChanged(null); } @Override @@ -78,12 +80,9 @@ public class StageSelector extends JPanel implements StateChangeListener { @Override public void actionPerformed(ActionEvent e) { - configuration.toggleStage(stageNumber); + rocket.getSelectedConfiguration().toggleStage(stageNumber); + rocket.fireComponentChangeEvent(ComponentChangeEvent.GRAPHIC_CHANGE); } - @Override - public void stateChanged(EventObject e) { - this.putValue(SELECTED_KEY, configuration.isStageActive(stageNumber)); - } } } diff --git a/swing/src/net/sf/openrocket/gui/dialogs/ComponentAnalysisDialog.java b/swing/src/net/sf/openrocket/gui/dialogs/ComponentAnalysisDialog.java index ea5b1e4d4..8d7133a88 100644 --- a/swing/src/net/sf/openrocket/gui/dialogs/ComponentAnalysisDialog.java +++ b/swing/src/net/sf/openrocket/gui/dialogs/ComponentAnalysisDialog.java @@ -166,11 +166,11 @@ public class ComponentAnalysisDialog extends JDialog implements StateChangeListe panel.add(new BasicSlider(roll.getSliderModel(-20 * 2 * Math.PI, 20 * 2 * Math.PI)), "growx, wrap"); - // Stage and motor selection: //// Active stages: panel.add(new JLabel(trans.get("componentanalysisdlg.lbl.activestages")), "spanx, split, gapafter rel"); - panel.add(new StageSelector(configuration), "gapafter paragraph"); + Rocket rkt = rocketPanel.getDocument().getRocket(); + panel.add(new StageSelector( rkt), "gapafter paragraph"); //// Motor configuration: JLabel label = new JLabel(trans.get("componentanalysisdlg.lbl.motorconf")); diff --git a/swing/src/net/sf/openrocket/gui/main/RocketActions.java b/swing/src/net/sf/openrocket/gui/main/RocketActions.java index 7824a1281..baeab86b1 100644 --- a/swing/src/net/sf/openrocket/gui/main/RocketActions.java +++ b/swing/src/net/sf/openrocket/gui/main/RocketActions.java @@ -664,6 +664,7 @@ public class RocketActions { RocketComponent parent = selected.getParent(); document.addUndoPosition("Move "+selected.getComponentName()); parent.moveChild(selected, parent.getChildPosition(selected)-1); + rocket.fireComponentChangeEvent( ComponentChangeEvent.TREE_CHANGE ); selectionModel.setSelectedComponent(selected); } @@ -709,6 +710,7 @@ public class RocketActions { RocketComponent parent = selected.getParent(); document.addUndoPosition("Move "+selected.getComponentName()); parent.moveChild(selected, parent.getChildPosition(selected)+1); + rocket.fireComponentChangeEvent( ComponentChangeEvent.TREE_CHANGE ); selectionModel.setSelectedComponent(selected); } diff --git a/swing/src/net/sf/openrocket/gui/scalefigure/RocketPanel.java b/swing/src/net/sf/openrocket/gui/scalefigure/RocketPanel.java index 124e6eca8..1e33b4a56 100644 --- a/swing/src/net/sf/openrocket/gui/scalefigure/RocketPanel.java +++ b/swing/src/net/sf/openrocket/gui/scalefigure/RocketPanel.java @@ -146,6 +146,7 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change private List listeners = new ArrayList(); + /** * The executor service used for running the background simulations. * This uses a fixed-sized thread pool for all background simulations @@ -167,6 +168,11 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change }); } + + public OpenRocketDocument getDocument(){ + return this.document; + } + public RocketPanel(OpenRocketDocument document) { this.document = document; Rocket rkt = document.getRocket(); @@ -295,8 +301,9 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change add(scaleSelector); // Stage selector - final FlightConfiguration configuration = document.getDefaultConfiguration(); - StageSelector stageSelector = new StageSelector(configuration); + final Rocket rkt = document.getRocket(); + StageSelector stageSelector = new StageSelector( rkt ); + rkt.addChangeListener(stageSelector); add(stageSelector); // Flight configuration selector @@ -847,4 +854,5 @@ public class RocketPanel extends JPanel implements TreeSelectionListener, Change // putValue(Action.SELECTED_KEY, figure.getType() == type && !is3d); // } // } + }