[Refine] Copying a FlightConfiguration in the UI now updates correctly.

- involved adjusting clone() -> copy(...) in FlightConfigurableParameters
This commit is contained in:
Daniel_M_Williams 2016-04-10 16:40:20 -04:00
parent d7faf0d273
commit 345d5952c6
18 changed files with 125 additions and 117 deletions

View File

@ -186,23 +186,36 @@ public class MotorConfiguration implements FlightConfigurableParameter<MotorConf
public int hashCode() {
return this.id.hashCode();
}
/**
* Create a new instance of this motor instance. The state of the motor is
* identical to this instance and can be used independently from this one.
*/
@Override
public MotorConfiguration clone( ) {
MotorConfiguration clone = new MotorConfiguration( this.mount, this.fcid);
clone.motor = this.motor;
clone.ejectionDelay = this.ejectionDelay;
clone.ignitionOveride = this.ignitionOveride;
clone.ignitionDelay = this.ignitionDelay;
clone.ignitionEvent = this.ignitionEvent;
return clone;
}
public int getModID() {
/**
* Create a new instance of this motor instance. The state of the motor is
* identical to this instance and can be used independently from this one.
*/
@Override
public MotorConfiguration clone( ) {
MotorConfiguration clone = new MotorConfiguration( this.mount, this.fcid);
clone.motor = this.motor;
clone.ejectionDelay = this.ejectionDelay;
clone.ignitionOveride = this.ignitionOveride;
clone.ignitionDelay = this.ignitionDelay;
clone.ignitionEvent = this.ignitionEvent;
return clone;
}
@Override
public MotorConfiguration copy( final FlightConfigurationId copyId){
MotorConfiguration clone = new MotorConfiguration( this.mount, copyId);
clone.motor = this.motor;
clone.ejectionDelay = this.ejectionDelay;
clone.ignitionOveride = this.ignitionOveride;
clone.ignitionDelay = this.ignitionDelay;
clone.ignitionEvent = this.ignitionEvent;
return clone;
}
public int getModID() {
return modID;
}

View File

@ -18,17 +18,12 @@ public final class MotorConfigurationId {
private final static String ERROR_ID_TEXT = "MotorInstance Error Id".intern();
private final static UUID ERROR_KEY = new UUID( 62274413, 56768908);
public final static MotorConfigurationId ERROR_ID = new MotorConfigurationId();
private MotorConfigurationId( ) {
this.key = MotorConfigurationId.ERROR_KEY;
}
/**
* Sole constructor.
*
* @param componentName the component ID, must not be null
* @param number a positive motor number
* @param _mount the component ID, must not be null
* @param _fcid the key for a
*/
public MotorConfigurationId(final MotorMount _mount, final FlightConfigurationId _fcid) {
if (null == _mount ) {
@ -43,7 +38,6 @@ public final class MotorConfigurationId {
final long lower = _fcid.key.getLeastSignificantBits();
this.key = new UUID( upper, lower);
}
@Override
public boolean equals(Object other) {

View File

@ -72,8 +72,8 @@ public class AxialStage extends ComponentAssembly implements FlightConfigurableC
}
@Override
public void cloneFlightConfiguration(FlightConfigurationId oldConfigId, FlightConfigurationId newConfigId) {
separations.cloneFlightConfiguration(oldConfigId, newConfigId);
public void copyFlightConfiguration(FlightConfigurationId oldConfigId, FlightConfigurationId newConfigId) {
separations.copyFlightConfiguration(oldConfigId, newConfigId);
}
@Override

View File

@ -397,8 +397,8 @@ public class BodyTube extends SymmetricComponent implements MotorMount, Coaxial
}
@Override
public void cloneFlightConfiguration(FlightConfigurationId oldConfigId, FlightConfigurationId newConfigId) {
motors.cloneFlightConfiguration(oldConfigId, newConfigId);
public void copyFlightConfiguration(FlightConfigurationId oldConfigId, FlightConfigurationId newConfigId) {
motors.copyFlightConfiguration(oldConfigId, newConfigId);
}
@Override

View File

@ -141,6 +141,9 @@ public class DeploymentConfiguration implements FlightConfigurableParameter<Depl
@Override
public DeploymentConfiguration clone() {
return copy(null);
}
public DeploymentConfiguration copy( final FlightConfigurationId copyId) {
DeploymentConfiguration that = new DeploymentConfiguration();
that.deployAltitude = this.deployAltitude;
that.deployDelay = this.deployDelay;

View File

@ -14,6 +14,6 @@ public interface FlightConfigurableComponent {
* @param oldConfigId the old configuration ID
* @param newConfigId the new configuration ID
*/
public void cloneFlightConfiguration(FlightConfigurationId oldConfigId, FlightConfigurationId newConfigId);
void copyFlightConfiguration(FlightConfigurationId oldConfigId, FlightConfigurationId newConfigId);
}

View File

@ -10,11 +10,18 @@ package net.sf.openrocket.rocketcomponent;
public interface FlightConfigurableParameter<E> {
/**
* Return a copy of this object. The listeners must not be copied
* to the new object.
* return an exact copy of this object
*/
public E clone();
public void update();
E clone();
/**
* return a copy of this object, corresponding to the specified Id
*
* @param fcid id to attach the new object to
* @return the desired copy
*/
E copy( final FlightConfigurationId fcid );
void update();
}

View File

@ -221,10 +221,11 @@ public class FlightConfigurableParameterSet<E extends FlightConfigurableParamete
setDefault(tempValue);
}
public FlightConfigurationId cloneFlightConfiguration(FlightConfigurationId oldConfigId, FlightConfigurationId newConfigId) {
public FlightConfigurationId copyFlightConfiguration(FlightConfigurationId oldConfigId, FlightConfigurationId newConfigId) {
// clones the ENTRIES for the given fcid's.
E oldValue = this.get(oldConfigId);
this.set(newConfigId, oldValue.clone());
E newValue = oldValue.copy( newConfigId);
this.set(newConfigId, newValue );
update();
return newConfigId;
}

View File

@ -465,25 +465,40 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
// Note the stages are updated in the constructor call.
FlightConfiguration clone = new FlightConfiguration( this.rocket, this.fcid );
clone.setName("clone[#"+clone.instanceNumber+"]"+clone.fcid.toShortKey());
//FlightConfigurationId cloneId = clone.getFlightConfigurationID();
System.err.println(" cloning from: "+this.toDebug());
System.err.println(" cloning to: "+clone.toDebug());
// // clone motor instances.
// for( MotorConfiguration motor : motors.values() ){
// MotorConfiguration cloneMotor = new MotorConfiguration( motor, cloneId);
// clone.addMotor( cloneMotor);
// cloneMotor.getMount().setMotorConfig(cloneMotor, cloneId);
// }
clone.cachedBounds = this.cachedBounds.clone();
clone.cachedBounds = this.cachedBounds.clone();
clone.modID = this.modID;
clone.boundsModID = -1;
clone.refLengthModID = -1;
return clone;
}
/**
* Copy all available information attached to this, and attached copies to the
* new configuration
*
* @param copyId attached the new configuration to this Id
* @return the new configuration
*/
@Override
public FlightConfiguration copy( final FlightConfigurationId copyId ) {
// Note the stages are updated in the constructor call.
FlightConfiguration copy= new FlightConfiguration( this.rocket, copyId );
// copy motor instances.
for( final MotorConfiguration sourceMotor: motors.values() ){
MotorConfiguration cloneMotor = sourceMotor.copy( copyId);
copy.addMotor( cloneMotor);
cloneMotor.getMount().setMotorConfig(cloneMotor, copyId);
}
copy.cachedBounds = this.cachedBounds.clone();
copy.modID = this.modID;
copy.boundsModID = -1;
copy.refLengthModID = -1;
return copy;
}
@Override
public int getModID() {
// TODO: this doesn't seem consistent...
@ -509,10 +524,8 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
@Override
public boolean equals(Object other){
if( other instanceof FlightConfiguration ){
return this.fcid.equals( ((FlightConfiguration)other).fcid);
}
return false;
return (( other instanceof FlightConfiguration ) &&
this.fcid.equals( ((FlightConfiguration)other).fcid));
}
@Override

View File

@ -73,11 +73,7 @@ public final class FlightConfigurationId implements Comparable<FlightConfigurati
public int hashCode() {
return this.key.hashCode();
}
public UUID intern() {
return this.key;
}
public boolean hasError(){
return (ERROR_UUID == this.key);
}

View File

@ -34,13 +34,16 @@ public class IgnitionConfiguration implements FlightConfigurableParameter<Igniti
@Override
public IgnitionConfiguration clone() {
IgnitionConfiguration clone = new IgnitionConfiguration();
return this.copy(null);
}
public IgnitionConfiguration copy( final FlightConfigurationId copyId) {
IgnitionConfiguration clone = new IgnitionConfiguration();
clone.ignitionDelay = this.ignitionDelay;
clone.ignitionEvent = this.ignitionEvent;
clone.ignitionTime = this.ignitionTime;
return clone;
}
}
@Override
public void update(){

View File

@ -304,8 +304,8 @@ public class InnerTube extends ThicknessRingComponent implements Clusterable, Ra
}
@Override
public void cloneFlightConfiguration(FlightConfigurationId oldConfigId, FlightConfigurationId newConfigId) {
motors.cloneFlightConfiguration(oldConfigId, newConfigId);
public void copyFlightConfiguration(FlightConfigurationId oldConfigId, FlightConfigurationId newConfigId) {
motors.copyFlightConfiguration(oldConfigId, newConfigId);
}

View File

@ -81,8 +81,8 @@ public class ParallelStage extends AxialStage implements FlightConfigurableCompo
}
@Override
public void cloneFlightConfiguration(FlightConfigurationId oldConfigId, FlightConfigurationId newConfigId) {
this.separations.cloneFlightConfiguration(oldConfigId, newConfigId);
public void copyFlightConfiguration(FlightConfigurationId oldConfigId, FlightConfigurationId newConfigId) {
this.separations.copyFlightConfiguration(oldConfigId, newConfigId);
}
@Override

View File

@ -90,8 +90,8 @@ public abstract class RecoveryDevice extends MassObject implements FlightConfigu
}
@Override
public void cloneFlightConfiguration(FlightConfigurationId oldConfigId, FlightConfigurationId newConfigId) {
deploymentConfigurations.cloneFlightConfiguration(oldConfigId, newConfigId);
public void copyFlightConfiguration(FlightConfigurationId oldConfigId, FlightConfigurationId newConfigId) {
deploymentConfigurations.copyFlightConfiguration(oldConfigId, newConfigId);
}

View File

@ -135,6 +135,10 @@ public class StageSeparationConfiguration implements FlightConfigurableParameter
@Override
public StageSeparationConfiguration clone() {
return copy(null);
}
public StageSeparationConfiguration copy( final FlightConfigurationId copyId){
StageSeparationConfiguration clone = new StageSeparationConfiguration();
clone.separationEvent = this.separationEvent;
clone.separationDelay = this.separationDelay;

View File

@ -1,31 +0,0 @@
package net.sf.openrocket.rocketvisitors;
import net.sf.openrocket.rocketcomponent.FlightConfigurableComponent;
import net.sf.openrocket.rocketcomponent.FlightConfigurationId;
import net.sf.openrocket.rocketcomponent.RocketComponent;
public class CopyFlightConfigurationVisitor extends DepthFirstRecusiveVisitor<Void> {
private final FlightConfigurationId oldConfigId;
private final FlightConfigurationId newConfigId;
public CopyFlightConfigurationVisitor(FlightConfigurationId oldConfigId, FlightConfigurationId newConfigId) {
super();
this.oldConfigId = oldConfigId;
this.newConfigId = newConfigId;
}
@Override
public void doAction(RocketComponent visitable) {
if (visitable instanceof FlightConfigurableComponent) {
((FlightConfigurableComponent) visitable).cloneFlightConfiguration(oldConfigId, newConfigId);
}
}
@Override
public Void getResult() {
return null;
}
}

View File

@ -51,11 +51,16 @@ public class ParameterSetTest extends BaseTestCase {
public String toString(){
return "tp#:"+id;
}
@Override
public TestParameter clone(){
return new TestParameter();
}
@Override
public TestParameter clone(){
return new TestParameter();
}
@Override
public TestParameter copy( final FlightConfigurationId copyId){
return new TestParameter();
}
};
@Before
@ -230,7 +235,7 @@ public class ParameterSetTest extends BaseTestCase {
assertThat("set stores default value correctly: ", testSet.get(fcid2), equalTo( tp2 ));
FlightConfigurationId fcid3 = new FlightConfigurationId();
testSet.cloneFlightConfiguration(fcid2, fcid3);
testSet.copyFlightConfiguration(fcid2, fcid3);
// fcid <=> tp2 should be stored....
assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 2 ));
assertThat("set stores default value correctly: ", testSet.get(fcid3), not( testSet.getDefault() ));

View File

@ -27,8 +27,6 @@ import net.sf.openrocket.startup.Application;
import net.sf.openrocket.util.StateChangeListener;
public class FlightConfigurationPanel extends JPanel implements StateChangeListener {
private static final long serialVersionUID = -5467500312467789009L;
//private static final Logger log = LoggerFactory.getLogger(FlightConfigurationPanel.class);
private static final Translator trans = Application.getTranslator();
private final OpenRocketDocument document;
@ -123,7 +121,6 @@ public class FlightConfigurationPanel extends JPanel implements StateChangeListe
private void addConfiguration() {
FlightConfigurationId newFCID = new FlightConfigurationId();
FlightConfiguration newConfig = new FlightConfiguration( rocket, newFCID );
rocket.setFlightConfiguration(newFCID, newConfig);
// Create a new simulation for this configuration.
@ -133,14 +130,15 @@ public class FlightConfigurationPanel extends JPanel implements StateChangeListe
}
private void copyConfiguration() {
FlightConfiguration oldConfig = rocket.getSelectedConfiguration();
FlightConfiguration newConfig = oldConfig.clone();
FlightConfigurationId oldId = oldConfig.getFlightConfigurationID();
FlightConfigurationId newId = newConfig.getFlightConfigurationID();
FlightConfiguration oldConfig = rocket.getSelectedConfiguration();
FlightConfigurationId oldId = oldConfig.getFlightConfigurationID();
FlightConfigurationId newId = new FlightConfigurationId();
FlightConfiguration newConfig = oldConfig.copy( newId);
for (RocketComponent c : rocket) {
if (c instanceof FlightConfigurableComponent) {
((FlightConfigurableComponent) c).cloneFlightConfiguration(oldId, newId);
((FlightConfigurableComponent) c).copyFlightConfiguration(oldId, newId);
}
}
rocket.setFlightConfiguration(newId, newConfig);
@ -170,8 +168,10 @@ public class FlightConfigurationPanel extends JPanel implements StateChangeListe
private void createSimulationForNewConfiguration() {
Simulation newSim = new Simulation(rocket);
OpenRocketDocument doc = BasicFrame.findDocument(rocket);
newSim.setName(doc.getNextSimulationName());
doc.addSimulation(newSim);
if (doc != null) {
newSim.setName(doc.getNextSimulationName());
doc.addSimulation(newSim);
}
}
private void configurationChanged() {
@ -192,7 +192,7 @@ public class FlightConfigurationPanel extends JPanel implements StateChangeListe
int motorMountCount = rocket.accept(new ListMotorMounts()).size();
// Count the number of recovery devices
int recoveryDeviceCount = rocket.accept(new ListComponents<RecoveryDevice>(RecoveryDevice.class)).size();
int recoveryDeviceCount = rocket.accept(new ListComponents<>(RecoveryDevice.class)).size();
// Count the number of stages
int stageCount = rocket.getStageCount();