Fix parachute length resizing when loading preset parachute
This commit is contained in:
		
							parent
							
								
									2f81d26216
								
							
						
					
					
						commit
						f66788e618
					
				@ -14,6 +14,7 @@ import net.sf.openrocket.rocketcomponent.RecoveryDevice;
 | 
			
		||||
import net.sf.openrocket.rocketcomponent.Rocket;
 | 
			
		||||
import net.sf.openrocket.rocketcomponent.RocketComponent;
 | 
			
		||||
import net.sf.openrocket.rocketcomponent.AxialStage;
 | 
			
		||||
import org.xml.sax.SAXException;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * A handler that populates the parameters of a previously constructed rocket component.
 | 
			
		||||
@ -27,6 +28,10 @@ class ComponentParameterHandler extends AbstractElementHandler {
 | 
			
		||||
	public ComponentParameterHandler(RocketComponent c, DocumentLoadingContext context) {
 | 
			
		||||
		this.component = c;
 | 
			
		||||
		this.context = context;
 | 
			
		||||
 | 
			
		||||
		// Sometimes setting certain component parameters will clear the preset. We don't want that to happen, so
 | 
			
		||||
		// ignore preset clearing.
 | 
			
		||||
		this.component.setIgnorePresetClearing(true);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	@Override
 | 
			
		||||
@ -124,4 +129,12 @@ class ComponentParameterHandler extends AbstractElementHandler {
 | 
			
		||||
					+ component.getComponentName() + ", ignoring."));
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void endHandler(String element, HashMap<String, String> attributes, String content, WarningSet warnings) throws SAXException {
 | 
			
		||||
		super.endHandler(element, attributes, content, warnings);
 | 
			
		||||
 | 
			
		||||
		// Restore the preset clearing behavior
 | 
			
		||||
		this.component.setIgnorePresetClearing(false);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@ -13,11 +13,17 @@ import net.sf.openrocket.util.Reflection;
 | 
			
		||||
////ComponentPresetSetter  -  sets a ComponentPreset value
 | 
			
		||||
class ComponentPresetSetter implements Setter {
 | 
			
		||||
	private final Reflection.Method setMethod;
 | 
			
		||||
	private Object[] extraParameters = null;
 | 
			
		||||
	
 | 
			
		||||
	public ComponentPresetSetter(Reflection.Method set) {
 | 
			
		||||
		this.setMethod = set;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public ComponentPresetSetter(Reflection.Method set, Object... parameters) {
 | 
			
		||||
		this.setMethod = set;
 | 
			
		||||
		this.extraParameters = parameters;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	@Override
 | 
			
		||||
	public void set(RocketComponent c, String name, HashMap<String, String> attributes,
 | 
			
		||||
			WarningSet warnings) {
 | 
			
		||||
@ -71,7 +77,11 @@ class ComponentPresetSetter implements Setter {
 | 
			
		||||
 | 
			
		||||
		// The preset loader can override the component name, so first store it and then apply it again
 | 
			
		||||
		String componentName = c.getName();
 | 
			
		||||
		if (extraParameters != null) {
 | 
			
		||||
			setMethod.invoke(c, matchingPreset, extraParameters);
 | 
			
		||||
		} else {
 | 
			
		||||
			setMethod.invoke(c, matchingPreset);
 | 
			
		||||
		}
 | 
			
		||||
		c.setName(componentName);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -452,6 +452,9 @@ class DocumentConfig {
 | 
			
		||||
		setters.put("Parachute:linematerial", new MaterialSetter(
 | 
			
		||||
				Reflection.findMethod(Parachute.class, "setLineMaterial", Material.class),
 | 
			
		||||
				Material.Type.LINE));
 | 
			
		||||
		setters.put("Parachute:preset", new ComponentPresetSetter(
 | 
			
		||||
				Reflection.findMethod(Parachute.class, "loadPreset", ComponentPreset.class, Object[].class),
 | 
			
		||||
				false));
 | 
			
		||||
		
 | 
			
		||||
		// PodSet
 | 
			
		||||
		setters.put("PodSet:instancecount", new IntSetter(
 | 
			
		||||
 | 
			
		||||
@ -153,8 +153,18 @@ public class Parachute extends RecoveryDevice {
 | 
			
		||||
		return false;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Load the component from a preset.
 | 
			
		||||
	 * @param preset	the preset to load from
 | 
			
		||||
	 * @param params    extra parameters to be used in the preset loading
 | 
			
		||||
	 *                  	params[0] = boolean allowAutoRadius: true = allow auto radius to be set during preset loading, false = do not allow auto radius
 | 
			
		||||
	 */
 | 
			
		||||
	@Override
 | 
			
		||||
	protected void loadFromPreset(ComponentPreset preset) {
 | 
			
		||||
	protected void loadFromPreset(ComponentPreset preset, Object...params) {
 | 
			
		||||
		boolean allowAutoRadius = true;
 | 
			
		||||
		if (params != null && params.length > 0) {
 | 
			
		||||
			allowAutoRadius = (boolean) params[0];
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// SUBSTITUTE preset parachute values for existing component values
 | 
			
		||||
		//	//	Set preset parachute description
 | 
			
		||||
@ -219,7 +229,8 @@ public class Parachute extends RecoveryDevice {
 | 
			
		||||
		}
 | 
			
		||||
		//	// Size parachute packed diameter within parent inner diameter
 | 
			
		||||
		if (preset.has(ComponentPreset.PACKED_LENGTH) && (getLength() > 0) &&
 | 
			
		||||
			preset.has(ComponentPreset.PACKED_DIAMETER) && (getRadius() > 0)) {
 | 
			
		||||
				preset.has(ComponentPreset.PACKED_DIAMETER) && (getRadius() > 0) &&
 | 
			
		||||
				allowAutoRadius) {
 | 
			
		||||
			setRadiusAutomatic(true);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@ -232,7 +243,12 @@ public class Parachute extends RecoveryDevice {
 | 
			
		||||
			massOverridden = false;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		super.loadFromPreset(preset);
 | 
			
		||||
		super.loadFromPreset(preset, params);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	protected void loadFromPreset(ComponentPreset preset) {
 | 
			
		||||
		loadFromPreset(preset, true);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
 | 
			
		||||
@ -137,6 +137,9 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
 | 
			
		||||
	// Preset component this component is based upon
 | 
			
		||||
	private ComponentPreset presetComponent = null;
 | 
			
		||||
 | 
			
		||||
	// If set to true, presets will not be cleared
 | 
			
		||||
	private boolean ignorePresetClearing = false;
 | 
			
		||||
	
 | 
			
		||||
	// The realistic appearance of this component
 | 
			
		||||
	private Appearance appearance = null;
 | 
			
		||||
 | 
			
		||||
@ -1176,10 +1179,11 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
 | 
			
		||||
	 * preset values.
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param preset	the preset component to load, or <code>null</code> to clear the preset.
 | 
			
		||||
	 * @param params    extra parameters to be used in the preset loading
 | 
			
		||||
	 */
 | 
			
		||||
	public final void loadPreset(ComponentPreset preset) {
 | 
			
		||||
	public final void loadPreset(ComponentPreset preset, Object...params) {
 | 
			
		||||
		for (RocketComponent listener : configListeners) {
 | 
			
		||||
			listener.loadPreset(preset);
 | 
			
		||||
			listener.loadPreset(preset, params);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (presetComponent == preset) {
 | 
			
		||||
@ -1212,7 +1216,10 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
 | 
			
		||||
				rocket.freeze();
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if (params == null || params.length == 0)
 | 
			
		||||
				loadFromPreset(preset);
 | 
			
		||||
			else
 | 
			
		||||
				loadFromPreset(preset, params);
 | 
			
		||||
			
 | 
			
		||||
			this.presetComponent = preset;
 | 
			
		||||
			
 | 
			
		||||
@ -1225,6 +1232,9 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
 | 
			
		||||
		fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public final void loadPreset(ComponentPreset preset) {
 | 
			
		||||
		loadPreset(preset, (Object[]) null);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Load component properties from the specified preset.  The preset is guaranteed
 | 
			
		||||
@ -1237,13 +1247,18 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
 | 
			
		||||
	 * This is because mass setting requires the dimensions to be set beforehand.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param preset	the preset to load from
 | 
			
		||||
	 * @param params    extra parameters to be used in the preset loading
 | 
			
		||||
	 */
 | 
			
		||||
	protected void loadFromPreset(ComponentPreset preset) {
 | 
			
		||||
	protected void loadFromPreset(ComponentPreset preset, Object...params) {
 | 
			
		||||
		if (preset.has(ComponentPreset.LENGTH)) {
 | 
			
		||||
			this.length = preset.get(ComponentPreset.LENGTH);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	protected void loadFromPreset(ComponentPreset preset) {
 | 
			
		||||
		loadFromPreset(preset, (Object[]) null);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	
 | 
			
		||||
	/**
 | 
			
		||||
	 * Clear the current component preset.  This does not affect the component properties
 | 
			
		||||
@ -1254,13 +1269,15 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
 | 
			
		||||
			listener.clearPreset();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (presetComponent == null)
 | 
			
		||||
		if (presetComponent == null || ignorePresetClearing)
 | 
			
		||||
			return;
 | 
			
		||||
		presetComponent = null;
 | 
			
		||||
		fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
	public void setIgnorePresetClearing(boolean ignorePresetClearing) {
 | 
			
		||||
		this.ignorePresetClearing = ignorePresetClearing;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Returns the unique ID of the component.
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user