[fixes #871] Remember manual setting after saving with auto on

This commit is contained in:
Sibo Van Gool 2021-08-22 00:21:33 +02:00
parent 9035284f89
commit 3a5861f988
6 changed files with 80 additions and 24 deletions

View File

@ -152,7 +152,7 @@ class DocumentConfig {
// BodyTube // BodyTube
setters.put("BodyTube:radius", new DoubleSetter( setters.put("BodyTube:radius", new DoubleSetter(
Reflection.findMethod(BodyTube.class, "setOuterRadius", double.class), Reflection.findMethod(BodyTube.class, "setOuterRadius", double.class),
"auto", "auto", " ",
Reflection.findMethod(BodyTube.class, "setOuterRadiusAutomatic", boolean.class))); Reflection.findMethod(BodyTube.class, "setOuterRadiusAutomatic", boolean.class)));
// Parallel Stage // Parallel Stage
@ -204,11 +204,11 @@ class DocumentConfig {
setters.put("Transition:foreradius", new DoubleSetter( setters.put("Transition:foreradius", new DoubleSetter(
Reflection.findMethod(Transition.class, "setForeRadius", double.class), Reflection.findMethod(Transition.class, "setForeRadius", double.class),
"auto", "auto", " ",
Reflection.findMethod(Transition.class, "setForeRadiusAutomatic", boolean.class))); Reflection.findMethod(Transition.class, "setForeRadiusAutomatic", boolean.class)));
setters.put("Transition:aftradius", new DoubleSetter( setters.put("Transition:aftradius", new DoubleSetter(
Reflection.findMethod(Transition.class, "setAftRadius", double.class), Reflection.findMethod(Transition.class, "setAftRadius", double.class),
"auto", "auto", " ",
Reflection.findMethod(Transition.class, "setAftRadiusAutomatic", boolean.class))); Reflection.findMethod(Transition.class, "setAftRadiusAutomatic", boolean.class)));
setters.put("Transition:foreshoulderradius", new DoubleSetter( setters.put("Transition:foreshoulderradius", new DoubleSetter(

View File

@ -1,6 +1,8 @@
package net.sf.openrocket.file.openrocket.importt; package net.sf.openrocket.file.openrocket.importt;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Objects;
import net.sf.openrocket.aerodynamics.Warning; import net.sf.openrocket.aerodynamics.Warning;
import net.sf.openrocket.aerodynamics.WarningSet; import net.sf.openrocket.aerodynamics.WarningSet;
@ -17,6 +19,7 @@ class DoubleSetter implements Setter {
private final String specialString; private final String specialString;
private final Reflection.Method specialMethod; private final Reflection.Method specialMethod;
private final double multiplier; private final double multiplier;
private String separator;
/** /**
* Set only the double value. * Set only the double value.
@ -59,6 +62,23 @@ class DoubleSetter implements Setter {
this.specialMethod = specialMethod; this.specialMethod = specialMethod;
this.multiplier = 1.0; this.multiplier = 1.0;
} }
/**
* Set the double value, or if the value equals the special string, use the
* special setter and set it to true. If the input string contains more information
* besides the special string, you can specify which separator should be used for
* this extra information. The part before the separator is then the special string
* and the part after the separator is the set value.
*
* @param set double setter.
* @param special special string
* @param specialMethod boolean setter.
*/
public DoubleSetter(Reflection.Method set, String special, String separator,
Reflection.Method specialMethod) {
this(set, special, specialMethod);
this.separator = separator;
}
/** /**
@ -80,26 +100,39 @@ class DoubleSetter implements Setter {
WarningSet warnings) { WarningSet warnings) {
s = s.trim(); s = s.trim();
String special = s;
// Check for special case String data = s;
if (specialMethod != null && s.equalsIgnoreCase(specialString)) { String[] args = null;
specialMethod.invoke(c, true);
return; // Extract special string and data if s contains multiple data elements, separated by separator
if (separator != null) {
args = s.split(this.separator);
if (args.length > 1) {
special = args[0];
data = String.join(separator, Arrays.copyOfRange(args, 1, args.length));
}
} }
// Normal case // Normal case
try { if (!special.equalsIgnoreCase(specialString) || (args != null && args.length > 1)) {
double d = Double.parseDouble(s); try {
double d = Double.parseDouble(data);
if (configGetter == null) {
setMethod.invoke(c, d * multiplier); if (configGetter == null) {
} else { setMethod.invoke(c, d * multiplier);
FlightConfigurableParameterSet<?> config = (FlightConfigurableParameterSet<?>) configGetter.invoke(c); } else {
Object obj = config.getDefault(); FlightConfigurableParameterSet<?> config = (FlightConfigurableParameterSet<?>) configGetter.invoke(c);
setMethod.invoke(obj, d * multiplier); Object obj = config.getDefault();
setMethod.invoke(obj, d * multiplier);
}
} catch (NumberFormatException e) {
warnings.add(Warning.FILE_INVALID_PARAMETER + " data: '" + data + "' - " + c.getName());
} }
} catch (NumberFormatException e) { }
warnings.add(Warning.FILE_INVALID_PARAMETER);
// Check for special case
if (specialMethod != null && special.equalsIgnoreCase(specialString)) {
specialMethod.invoke(c, true);
} }
} }
} }

View File

@ -22,8 +22,9 @@ public class BodyTubeSaver extends SymmetricComponentSaver {
super.addParams(c, elements); super.addParams(c, elements);
net.sf.openrocket.rocketcomponent.BodyTube tube = (net.sf.openrocket.rocketcomponent.BodyTube) c; net.sf.openrocket.rocketcomponent.BodyTube tube = (net.sf.openrocket.rocketcomponent.BodyTube) c;
if (tube.isOuterRadiusAutomatic()) if (tube.isOuterRadiusAutomatic()) {
elements.add("<radius>auto</radius>"); elements.add("<radius>auto " + tube.getOuterRadiusNoAutomatic() + "</radius>");
}
else else
elements.add("<radius>" + tube.getOuterRadius() + "</radius>"); elements.add("<radius>" + tube.getOuterRadius() + "</radius>");

View File

@ -45,13 +45,13 @@ public class TransitionSaver extends SymmetricComponentSaver {
if (!nosecone) { if (!nosecone) {
if (trans.isForeRadiusAutomatic()) if (trans.isForeRadiusAutomatic())
elements.add("<foreradius>auto</foreradius>"); elements.add("<foreradius>auto " + trans.getForeRadiusNoAutomatic() + "</foreradius>");
else else
elements.add("<foreradius>" + trans.getForeRadius() + "</foreradius>"); elements.add("<foreradius>" + trans.getForeRadius() + "</foreradius>");
} }
if (trans.isAftRadiusAutomatic()) if (trans.isAftRadiusAutomatic())
elements.add("<aftradius>auto</aftradius>"); elements.add("<aftradius>auto " + trans.getAftRadiusNoAutomatic() + "</aftradius>");
else else
elements.add("<aftradius>" + trans.getAftRadius() + "</aftradius>"); elements.add("<aftradius>" + trans.getAftRadius() + "</aftradius>");

View File

@ -97,6 +97,14 @@ public class BodyTube extends SymmetricComponent implements BoxBounded, MotorMou
} }
return outerRadius; return outerRadius;
} }
/**
* Return the outer radius that was manually entered, so not the value that the component received from automatic
* outer radius.
*/
public double getOuterRadiusNoAutomatic() {
return outerRadius;
}
/** /**
* Set the outer radius of the body tube. If the radius is less than the wall thickness, * Set the outer radius of the body tube. If the radius is less than the wall thickness,

View File

@ -94,6 +94,14 @@ public class Transition extends SymmetricComponent implements InsideColorCompone
return foreRadius; return foreRadius;
} }
/**
* Return the fore radius that was manually entered, so not the value that the component received from automatic
* fore radius.
*/
public double getForeRadiusNoAutomatic() {
return foreRadius;
}
public void setForeRadius(double radius) { public void setForeRadius(double radius) {
if ((this.foreRadius == radius) && (autoForeRadius == false)) if ((this.foreRadius == radius) && (autoForeRadius == false))
return; return;
@ -142,7 +150,13 @@ public class Transition extends SymmetricComponent implements InsideColorCompone
return aftRadius; return aftRadius;
} }
/**
* Return the aft radius that was manually entered, so not the value that the component received from automatic
* zft radius.
*/
public double getAftRadiusNoAutomatic() {
return aftRadius;
}
public void setAftRadius(double radius) { public void setAftRadius(double radius) {
if ((this.aftRadius == radius) && (autoAftRadius2 == false)) if ((this.aftRadius == radius) && (autoAftRadius2 == false))