From 74777df2d6cc075286f852c64c8cf44bbf38c2e8 Mon Sep 17 00:00:00 2001 From: SiboVG Date: Sat, 17 Aug 2024 22:30:31 +0200 Subject: [PATCH] Refactor component analysis parameters to dedicated class --- .../ComponentAnalysisDialog.java | 39 ++++---- .../ComponentAnalysisParameters.java | 96 +++++++++++++++++++ .../openrocket/swing/gui/main/BasicFrame.java | 2 +- 3 files changed, 117 insertions(+), 20 deletions(-) rename swing/src/main/java/info/openrocket/swing/gui/dialogs/{ => componentanalysis}/ComponentAnalysisDialog.java (96%) create mode 100644 swing/src/main/java/info/openrocket/swing/gui/dialogs/componentanalysis/ComponentAnalysisParameters.java diff --git a/swing/src/main/java/info/openrocket/swing/gui/dialogs/ComponentAnalysisDialog.java b/swing/src/main/java/info/openrocket/swing/gui/dialogs/componentanalysis/ComponentAnalysisDialog.java similarity index 96% rename from swing/src/main/java/info/openrocket/swing/gui/dialogs/ComponentAnalysisDialog.java rename to swing/src/main/java/info/openrocket/swing/gui/dialogs/componentanalysis/ComponentAnalysisDialog.java index 5922760d9..953c0cec3 100644 --- a/swing/src/main/java/info/openrocket/swing/gui/dialogs/ComponentAnalysisDialog.java +++ b/swing/src/main/java/info/openrocket/swing/gui/dialogs/componentanalysis/ComponentAnalysisDialog.java @@ -1,11 +1,10 @@ -package info.openrocket.swing.gui.dialogs; +package info.openrocket.swing.gui.dialogs.componentanalysis; import static info.openrocket.core.unit.Unit.NOUNIT; import static info.openrocket.core.util.Chars.ALPHA; import info.openrocket.core.aerodynamics.AerodynamicCalculator; import info.openrocket.core.aerodynamics.AerodynamicForces; import info.openrocket.core.aerodynamics.FlightConditions; -import info.openrocket.core.logging.Warning; import info.openrocket.core.logging.WarningSet; import info.openrocket.core.l10n.Translator; import info.openrocket.core.masscalc.CMAnalysisEntry; @@ -88,7 +87,7 @@ public class ComponentAnalysisDialog extends JDialog implements StateChangeListe private final JToggleButton worstToggle; private boolean fakeChange = false; private AerodynamicCalculator aerodynamicCalculator; - private double initTheta; + private ComponentAnalysisParameters parameters; private final ColumnTableModel longitudeStabilityTableModel; private final ColumnTableModel dragTableModel; @@ -115,18 +114,15 @@ public class ComponentAnalysisDialog extends JDialog implements StateChangeListe rkt = rocketPanel.getDocument().getRocket(); this.aerodynamicCalculator = rocketPanel.getAerodynamicCalculator().newInstance(); - conditions = new FlightConditions(rkt.getSelectedConfiguration()); - rocketPanel.setCPAOA(0); - aoa = new DoubleModel(rocketPanel, "CPAOA", UnitGroup.UNITS_ANGLE, 0, Math.PI); - rocketPanel.setCPMach(Application.getPreferences().getDefaultMach()); - mach = new DoubleModel(rocketPanel, "CPMach", UnitGroup.UNITS_COEFFICIENT, 0); - initTheta = rocketPanel.getFigure().getRotation(); - rocketPanel.setCPTheta(rocketPanel.getFigure().getRotation()); - theta = new DoubleModel(rocketPanel, "CPTheta", UnitGroup.UNITS_ANGLE, 0, 2 * Math.PI); - rocketPanel.setCPRoll(0); - roll = new DoubleModel(rocketPanel, "CPRoll", UnitGroup.UNITS_ROLL); + // Create ComponentAnalysisParameters + parameters = new ComponentAnalysisParameters(rkt, rocketPanel); + + aoa = new DoubleModel(parameters, "AOA", UnitGroup.UNITS_ANGLE, 0, Math.PI); + mach = new DoubleModel(parameters, "Mach", UnitGroup.UNITS_COEFFICIENT, 0); + theta = new DoubleModel(parameters, "Theta", UnitGroup.UNITS_ANGLE, 0, 2 * Math.PI); + roll = new DoubleModel(parameters, "RollRate", UnitGroup.UNITS_ROLL); //// Wind direction: panel.add(new JLabel(trans.get("componentanalysisdlg.lbl.winddir"))); @@ -435,7 +431,7 @@ public class ComponentAnalysisDialog extends JDialog implements StateChangeListe this.addWindowListener(new WindowAdapter() { @Override public void windowClosed(WindowEvent e) { - theta.setValue(initTheta); + theta.setValue(parameters.getInitialTheta()); //System.out.println("Closing method called: " + this); rkt.removeChangeListener(ComponentAnalysisDialog.this); @@ -470,7 +466,13 @@ public class ComponentAnalysisDialog extends JDialog implements StateChangeListe // Buttons - JButton button; + JButton plotExportBtn = new JButton(trans.get("simpanel.but.plotexport")); + plotExportBtn.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + // TODO: open plot/export dialog + } + }); // TODO: LOW: printing // button = new JButton("Print"); @@ -487,16 +489,15 @@ public class ComponentAnalysisDialog extends JDialog implements StateChangeListe // }); // panel.add(button,"tag ok"); - //button = new JButton("Close"); //Close button - button = new JButton(trans.get("dlg.but.close")); - button.addActionListener(new ActionListener() { + JButton closeBtn = new JButton(trans.get("dlg.but.close")); + closeBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ComponentAnalysisDialog.this.dispose(); } }); - panel.add(button, "span, tag cancel"); + panel.add(closeBtn, "span, tag cancel"); this.setLocationByPlatform(true); diff --git a/swing/src/main/java/info/openrocket/swing/gui/dialogs/componentanalysis/ComponentAnalysisParameters.java b/swing/src/main/java/info/openrocket/swing/gui/dialogs/componentanalysis/ComponentAnalysisParameters.java new file mode 100644 index 000000000..ccf910719 --- /dev/null +++ b/swing/src/main/java/info/openrocket/swing/gui/dialogs/componentanalysis/ComponentAnalysisParameters.java @@ -0,0 +1,96 @@ +package info.openrocket.swing.gui.dialogs.componentanalysis; + +import info.openrocket.core.rocketcomponent.FlightConfiguration; +import info.openrocket.core.rocketcomponent.Rocket; +import info.openrocket.core.startup.Application; +import info.openrocket.core.util.Mutable; +import info.openrocket.swing.gui.scalefigure.RocketPanel; + +public class ComponentAnalysisParameters implements Cloneable { + private final Mutable mutable = new Mutable(); + + private final Rocket rocket; + private final RocketPanel rocketPanel; + + private double theta; + private final double initialTheta; + private double aoa; + private double mach; + private double rollRate; + + public ComponentAnalysisParameters(Rocket rocket, RocketPanel rocketPanel) { + this.rocket = rocket; + this.rocketPanel = rocketPanel; + + setTheta(rocketPanel.getFigure().getRotation()); + this.initialTheta = this.theta; + setAOA(0); + setMach(Application.getPreferences().getDefaultMach()); + setRollRate(0); + } + + public double getTheta() { + return theta; + } + + public void setTheta(double theta) { + mutable.check(); + this.theta = theta; + this.rocketPanel.setCPTheta(theta); + } + + public double getInitialTheta() { + return initialTheta; + } + + public double getAOA() { + return aoa; + } + + public void setAOA(double aoa) { + mutable.check(); + this.aoa = aoa; + this.rocketPanel.setCPAOA(aoa); + } + + public double getMach() { + return mach; + } + + public void setMach(double mach) { + mutable.check(); + this.mach = mach; + this.rocketPanel.setCPMach(mach); + } + + public double getRollRate() { + return rollRate; + } + + public void setRollRate(double rollRate) { + mutable.check(); + this.rollRate = rollRate; + this.rocketPanel.setCPRoll(rollRate); + } + + public FlightConfiguration getSelectedConfiguration() { + return rocket.getSelectedConfiguration(); + } + + public void immute() { + mutable.immute(); + } + + public boolean isMutable() { + return mutable.isMutable(); + } + + @Override + public ComponentAnalysisParameters clone() { + try { + return (ComponentAnalysisParameters) super.clone(); + } catch (CloneNotSupportedException e) { + throw new RuntimeException(e); + } + } +} diff --git a/swing/src/main/java/info/openrocket/swing/gui/main/BasicFrame.java b/swing/src/main/java/info/openrocket/swing/gui/main/BasicFrame.java index 6e6550422..cd6c6f02c 100644 --- a/swing/src/main/java/info/openrocket/swing/gui/main/BasicFrame.java +++ b/swing/src/main/java/info/openrocket/swing/gui/main/BasicFrame.java @@ -89,7 +89,7 @@ import info.openrocket.swing.gui.configdialog.ComponentConfigDialog; import info.openrocket.swing.gui.customexpression.CustomExpressionDialog; import info.openrocket.swing.gui.dialogs.AboutDialog; import info.openrocket.swing.gui.dialogs.BugReportDialog; -import info.openrocket.swing.gui.dialogs.ComponentAnalysisDialog; +import info.openrocket.swing.gui.dialogs.componentanalysis.ComponentAnalysisDialog; import info.openrocket.swing.gui.dialogs.DebugLogDialog; import info.openrocket.swing.gui.dialogs.DecalNotFoundDialog; import info.openrocket.swing.gui.dialogs.DetailDialog;