diff --git a/core/src/net/sf/openrocket/unit/UnitGroup.java b/core/src/net/sf/openrocket/unit/UnitGroup.java index 654410d70..03dcf38d1 100644 --- a/core/src/net/sf/openrocket/unit/UnitGroup.java +++ b/core/src/net/sf/openrocket/unit/UnitGroup.java @@ -456,7 +456,7 @@ public class UnitGroup { * @param rocket the rocket from which to calculate the caliber * @return the unit group */ - public static UnitGroup stabilityUnits(Rocket rocket) { + public static StabilityUnitGroup stabilityUnits(Rocket rocket) { return new StabilityUnitGroup(rocket); } @@ -467,7 +467,7 @@ public class UnitGroup { * @param config the rocket configuration from which to calculate the caliber * @return the unit group */ - public static UnitGroup stabilityUnits(FlightConfiguration config) { + public static StabilityUnitGroup stabilityUnits(FlightConfiguration config) { return new StabilityUnitGroup(config); } @@ -717,7 +717,8 @@ public class UnitGroup { * A private class that switches the CaliberUnit to a rocket-specific CaliberUnit. * All other methods are passed through to UNITS_STABILITY. */ - private static class StabilityUnitGroup extends UnitGroup { + public static class StabilityUnitGroup extends UnitGroup { + private final PercentageOfLengthUnit percentageOfLengthUnit; public StabilityUnitGroup(double ref) { this(new CaliberUnit(ref), new PercentageOfLengthUnit(ref)); } @@ -728,6 +729,7 @@ public class UnitGroup { public StabilityUnitGroup(FlightConfiguration config) { this(new CaliberUnit(config), new PercentageOfLengthUnit(config)); } private StabilityUnitGroup(CaliberUnit caliberUnit, PercentageOfLengthUnit percentageOfLengthUnit) { + this.percentageOfLengthUnit = percentageOfLengthUnit; this.units.addAll(UnitGroup.UNITS_STABILITY.units); this.defaultUnit = UnitGroup.UNITS_STABILITY.defaultUnit; for (int i = 0; i < units.size(); i++) { @@ -746,5 +748,13 @@ public class UnitGroup { super.setDefaultUnit(n); UNITS_STABILITY.setDefaultUnit(n); } + + /** + * Returns the percentage of length unit. (Stability in %) + * @return the percentage of length unit. + */ + public Unit getPercentageOfLengthUnit() { + return this.percentageOfLengthUnit; + } } } diff --git a/swing/src/net/sf/openrocket/gui/figureelements/RocketInfo.java b/swing/src/net/sf/openrocket/gui/figureelements/RocketInfo.java index 64c120c19..baf9b831a 100644 --- a/swing/src/net/sf/openrocket/gui/figureelements/RocketInfo.java +++ b/swing/src/net/sf/openrocket/gui/figureelements/RocketInfo.java @@ -41,7 +41,7 @@ public class RocketInfo implements FigureElement { private final Caret cpCaret = new CPCaret(0,0); private final Caret cgCaret = new CGCaret(0,0); - private UnitGroup stabilityUnits; + private UnitGroup.StabilityUnitGroup stabilityUnits; private FlightConfiguration configuration; private double cg = 0, cp = 0; @@ -201,7 +201,7 @@ public class RocketInfo implements FigureElement { GlyphVector cgValue = createText(getCg()); GlyphVector cpValue = createText(getCp()); - GlyphVector stabValue = createText(getStability()); + GlyphVector stabValue = createText(getStabilityCombined()); //// CG: GlyphVector cgText = createText(trans.get("RocketInfo.cgText")); @@ -276,16 +276,47 @@ public class RocketInfo implements FigureElement { public String getMassWithMotors(Unit u) { return u.toStringUnit(massWithMotors); } - + + /** - * Get the stability, in calibers. + * Get the stability in both the selected stability unit and in percentage, e.g. "2.4 cal (14.1 %)". + * If the current unit is already the percentage length unit, only use that. * - * @return the current stability margin + * @return the current stability margin in the currently selected stability unit and in percentage */ - public String getStability () { - return stabilityUnits.getDefaultUnit().toStringUnit(cp-cg); + public String getStabilityCombined() { + Unit defaultUnit = stabilityUnits.getDefaultUnit(); + String stability = getStability(); + + if (Double.isNaN(getStabilityValue()) || defaultUnit == stabilityUnits.getPercentageOfLengthUnit()) { + return stability; + } + + String stabilityPercentage = getStabilityPercentage(); + + return stability + " (" + stabilityPercentage + ")"; } + /** + * Get the stability in the currently selected unit. + * @return the current stability margin in the currently selected stability unit + */ + private String getStability() { + return stabilityUnits.getDefaultUnit().toStringUnit(getStabilityValue()); + } + + /** + * Get the stability in the percentage length unit. + * @return the current stability margin in the percentage length unit + */ + private String getStabilityPercentage() { + return stabilityUnits.getPercentageOfLengthUnit().toStringUnit(getStabilityValue()); + } + + private double getStabilityValue() { + return cp - cg; + } + /** * Get the center of pressure in default length units. * diff --git a/swing/src/net/sf/openrocket/gui/print/DesignReport.java b/swing/src/net/sf/openrocket/gui/print/DesignReport.java index cb281336e..4483ebb1d 100644 --- a/swing/src/net/sf/openrocket/gui/print/DesignReport.java +++ b/swing/src/net/sf/openrocket/gui/print/DesignReport.java @@ -256,7 +256,7 @@ public class DesignReport { canvas.showText(text.getMassWithMotors(UnitGroup.UNITS_MASS.getDefaultUnit())); canvas.newlineShowText(STABILITY); - canvas.showText(text.getStability()); + canvas.showText(text.getStabilityCombined()); canvas.newlineShowText(CG); canvas.showText(text.getCg());