diff --git a/core/src/net/sf/openrocket/motor/MotorConfiguration.java b/core/src/net/sf/openrocket/motor/MotorConfiguration.java index 2bee2f72e..96cb07a10 100644 --- a/core/src/net/sf/openrocket/motor/MotorConfiguration.java +++ b/core/src/net/sf/openrocket/motor/MotorConfiguration.java @@ -1,17 +1,12 @@ package net.sf.openrocket.motor; -import java.util.EventObject; -import java.util.List; - import net.sf.openrocket.rocketcomponent.FlightConfigurableParameter; import net.sf.openrocket.rocketcomponent.IgnitionEvent; import net.sf.openrocket.rocketcomponent.MotorMount; import net.sf.openrocket.rocketcomponent.RocketComponent; import net.sf.openrocket.simulation.MotorState; -import net.sf.openrocket.util.ArrayList; import net.sf.openrocket.util.Coordinate; import net.sf.openrocket.util.Inertia; -import net.sf.openrocket.util.StateChangeListener; /** * A single motor configuration. This includes the selected motor @@ -83,7 +78,6 @@ public class MotorConfiguration implements FlightConfigurableParameter flightConfiguration, RocketComponent component) { - super(flightConfiguration, component, DEFAULT_MOTOR_EVENT_TYPE); + public MotorConfigurationSet(FlightConfigurableParameterSet configSet, RocketComponent component) { + super(configSet); } @Override @@ -35,8 +35,7 @@ public class MotorConfigurationSet extends FlightConfigurableParameterSet> motorSet ("+this.size()+ " motors)\n"); + buffer.append("====== Dumping MotorConfigurationSet for mount ("+this.size()+ " motors)\n"); MotorConfiguration emptyInstance = this.getDefault(); buffer.append(" >> (["+emptyInstance.toString()+"]= @ "+ emptyInstance.getIgnitionEvent().name +" +"+emptyInstance.getIgnitionDelay()+"sec )\n"); diff --git a/core/src/net/sf/openrocket/rocketcomponent/AxialStage.java b/core/src/net/sf/openrocket/rocketcomponent/AxialStage.java index 1cc7353cb..3ca7903cf 100644 --- a/core/src/net/sf/openrocket/rocketcomponent/AxialStage.java +++ b/core/src/net/sf/openrocket/rocketcomponent/AxialStage.java @@ -17,8 +17,7 @@ public class AxialStage extends ComponentAssembly implements FlightConfigurableC protected int stageNumber; public AxialStage(){ - this.separations = new FlightConfigurableParameterSet( - this, ComponentChangeEvent.EVENT_CHANGE, new StageSeparationConfiguration()); + this.separations = new FlightConfigurableParameterSet( new StageSeparationConfiguration()); this.relativePosition = Position.AFTER; this.stageNumber = 0; } @@ -80,8 +79,7 @@ public class AxialStage extends ComponentAssembly implements FlightConfigurableC @Override protected RocketComponent copyWithOriginalID() { AxialStage copy = (AxialStage) super.copyWithOriginalID(); - copy.separations = new FlightConfigurableParameterSet(separations, - copy, ComponentChangeEvent.EVENT_CHANGE); + copy.separations = new FlightConfigurableParameterSet(separations); return copy; } diff --git a/core/src/net/sf/openrocket/rocketcomponent/DeploymentConfiguration.java b/core/src/net/sf/openrocket/rocketcomponent/DeploymentConfiguration.java index 51d8fd605..a6296fbb0 100644 --- a/core/src/net/sf/openrocket/rocketcomponent/DeploymentConfiguration.java +++ b/core/src/net/sf/openrocket/rocketcomponent/DeploymentConfiguration.java @@ -1,16 +1,12 @@ package net.sf.openrocket.rocketcomponent; -import java.util.EventObject; -import java.util.List; import net.sf.openrocket.l10n.Translator; import net.sf.openrocket.simulation.FlightEvent; import net.sf.openrocket.startup.Application; import net.sf.openrocket.unit.UnitGroup; -import net.sf.openrocket.util.ArrayList; import net.sf.openrocket.util.MathUtil; import net.sf.openrocket.util.Pair; -import net.sf.openrocket.util.StateChangeListener; public class DeploymentConfiguration implements FlightConfigurableParameter { @@ -106,7 +102,6 @@ public class DeploymentConfiguration implements FlightConfigurableParameter the parameter type */ -public interface FlightConfigurableParameter extends ChangeSource { +public interface FlightConfigurableParameter { /** * Return a copy of this object. The listeners must not be copied diff --git a/core/src/net/sf/openrocket/rocketcomponent/FlightConfigurableParameterSet.java b/core/src/net/sf/openrocket/rocketcomponent/FlightConfigurableParameterSet.java index b7efd5286..c9dc8e2d5 100644 --- a/core/src/net/sf/openrocket/rocketcomponent/FlightConfigurableParameterSet.java +++ b/core/src/net/sf/openrocket/rocketcomponent/FlightConfigurableParameterSet.java @@ -1,14 +1,12 @@ package net.sf.openrocket.rocketcomponent; import java.util.Collections; -import java.util.EventObject; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map.Entry; import net.sf.openrocket.util.ArrayList; -import net.sf.openrocket.util.StateChangeListener; import net.sf.openrocket.util.Utils; /** @@ -24,13 +22,7 @@ public class FlightConfigurableParameterSet map = new HashMap(); - - protected E defaultValue; - protected final RocketComponent component; - protected final int eventType; - - private final Listener listener = new Listener(); - + protected static final FlightConfigurationId defaultValueId = FlightConfigurationId.DEFAULT_VALUE_FCID; /** * Construct a FlightConfiguration that has no overrides. @@ -38,33 +30,25 @@ public class FlightConfigurableParameterSet configSet, RocketComponent component, int eventType) { - this.component = component; - this.eventType = eventType; - - this.defaultValue= configSet.getDefault().clone(); + public FlightConfigurableParameterSet(FlightConfigurableParameterSet configSet ){ for (FlightConfigurationId key : configSet.map.keySet()) { E cloneConfig = configSet.map.get(key).clone(); this.map.put(key, cloneConfig); } } + @Deprecated + // do we want to keep this? it shouldn't actually be called... public boolean containsKey( final FlightConfigurationId fcid ){ return this.map.containsKey(fcid); } @@ -77,7 +61,7 @@ public class FlightConfigurableParameterSet getSortedConfigurationIDs(){ ArrayList toReturn = new ArrayList(); toReturn.addAll( this.map.keySet() ); + toReturn.remove( defaultValueId ); // Java 1.8: //toReturn.sort( null ); @@ -187,7 +170,7 @@ public class FlightConfigurableParameterSet getIDs(){ + public List getIds(){ return this.getSortedConfigurationIDs(); } @@ -201,15 +184,12 @@ public class FlightConfigurableParameterSet> ParameterSet<%s> (%d configurations)\n", this.defaultValue.getClass().getSimpleName(), this.size() )); - - buf.append(String.format(" >> [%s]= %s\n", "DEFAULT", this.getDefault().toString() )); + buf.append(String.format("====== Dumping ConfigurationSet<%s> (%d configurations)\n", this.getDefault().getClass().getSimpleName(), this.size() )); + final String fmt = " [%-12s]: %s\n"; for( FlightConfigurationId loopFCID : this.getSortedConfigurationIDs()){ String shortKey = loopFCID.toShortKey(); - E inst = this.map.get(loopFCID); if( this.isDefault(inst)){ shortKey = "*"+shortKey+"*"; } - buf.append(String.format(" >> [%s]= %s\n", shortKey, inst )); + buf.append(String.format(fmt, shortKey, inst )); } return buf.toString(); } public void update(){ - this.defaultValue.update(); for( E curValue: this.map.values() ){ curValue.update(); } diff --git a/core/src/net/sf/openrocket/rocketcomponent/IgnitionConfiguration.java b/core/src/net/sf/openrocket/rocketcomponent/IgnitionConfiguration.java index eab05f33b..fa8c6b036 100644 --- a/core/src/net/sf/openrocket/rocketcomponent/IgnitionConfiguration.java +++ b/core/src/net/sf/openrocket/rocketcomponent/IgnitionConfiguration.java @@ -1,26 +1,17 @@ package net.sf.openrocket.rocketcomponent; -import java.util.EventObject; -import java.util.List; - -import net.sf.openrocket.util.ArrayList; -import net.sf.openrocket.util.StateChangeListener; - public class IgnitionConfiguration implements FlightConfigurableParameter { protected double ignitionDelay = 0.0; protected IgnitionEvent ignitionEvent = IgnitionEvent.NEVER; protected double ignitionTime = 0.0; - private final List listeners = new ArrayList(); - public double getIgnitionDelay() { return ignitionDelay; } public void setIgnitionDelay(double ignitionDelay) { this.ignitionDelay = ignitionDelay; - fireChangeEvent(); } public IgnitionEvent getIgnitionEvent() { @@ -29,7 +20,6 @@ public class IgnitionConfiguration implements FlightConfigurableParameter deploymentConfigurations; public RecoveryDevice() { - this.deploymentConfigurations = new FlightConfigurableParameterSet(this, ComponentChangeEvent.EVENT_CHANGE, new DeploymentConfiguration()); + this.deploymentConfigurations = new FlightConfigurableParameterSet( new DeploymentConfiguration()); setMaterial(Application.getPreferences().getDefaultComponentMaterial(RecoveryDevice.class, Material.Type.SURFACE)); } @@ -113,8 +113,7 @@ public abstract class RecoveryDevice extends MassObject implements FlightConfigu @Override protected RocketComponent copyWithOriginalID() { RecoveryDevice copy = (RecoveryDevice) super.copyWithOriginalID(); - copy.deploymentConfigurations = new FlightConfigurableParameterSet(deploymentConfigurations, - copy, ComponentChangeEvent.EVENT_CHANGE); + copy.deploymentConfigurations = new FlightConfigurableParameterSet(deploymentConfigurations); return copy; } } diff --git a/core/src/net/sf/openrocket/rocketcomponent/Rocket.java b/core/src/net/sf/openrocket/rocketcomponent/Rocket.java index 87d96dd7d..fe2b70872 100644 --- a/core/src/net/sf/openrocket/rocketcomponent/Rocket.java +++ b/core/src/net/sf/openrocket/rocketcomponent/Rocket.java @@ -85,7 +85,7 @@ public class Rocket extends RocketComponent { functionalModID = modID; FlightConfiguration defaultConfiguration = new FlightConfiguration( this, null); - this.configSet = new FlightConfigurableParameterSet(this, ComponentChangeEvent.CONFIG_CHANGE, defaultConfiguration); + this.configSet = new FlightConfigurableParameterSet( defaultConfiguration); } public String getDesigner() { @@ -301,8 +301,7 @@ public class Rocket extends RocketComponent { @Override public Rocket copyWithOriginalID() { Rocket copy = (Rocket) super.copyWithOriginalID(); - copy.configSet = new FlightConfigurableParameterSet( - this.configSet, copy, ComponentChangeEvent.CONFIG_CHANGE); + copy.configSet = new FlightConfigurableParameterSet( this.configSet); copy.resetListeners(); return copy; @@ -339,7 +338,7 @@ public class Rocket extends RocketComponent { this.refType = r.refType; this.customReferenceLength = r.customReferenceLength; - this.configSet = new FlightConfigurableParameterSet( r.configSet, this, ComponentChangeEvent.CONFIG_CHANGE); + this.configSet = new FlightConfigurableParameterSet( r.configSet ); this.perfectFinish = r.perfectFinish; this.checkComponentStructure(); @@ -440,7 +439,7 @@ public class Rocket extends RocketComponent { } // notify all configurations - this.configSet.update(); + this.update(); // Notify all listeners // Copy the list before iterating to prevent concurrent modification exceptions. @@ -650,10 +649,14 @@ public class Rocket extends RocketComponent { * @return a FlightConfiguration instance */ public FlightConfiguration getFlightConfiguration(final int configIndex) { - checkState(); return this.configSet.get(configIndex); } + public void setDefaultConfiguration(final FlightConfiguration config) { + checkState(); + configSet.setDefault( config); + fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE); + } public void setDefaultConfiguration(final FlightConfigurationId fcid) { checkState(); diff --git a/core/src/net/sf/openrocket/rocketcomponent/RocketComponent.java b/core/src/net/sf/openrocket/rocketcomponent/RocketComponent.java index 428bfb341..7e3df3d2e 100644 --- a/core/src/net/sf/openrocket/rocketcomponent/RocketComponent.java +++ b/core/src/net/sf/openrocket/rocketcomponent/RocketComponent.java @@ -313,7 +313,7 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab protected void componentChanged(ComponentChangeEvent e) { // No-op checkState(); - this.update(); + update(); } @@ -1089,6 +1089,13 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab protected void update() { this.setAxialOffset(this.relativePosition, this.offset); } + + public final void updateChildren(){ + this.update(); + for( RocketComponent rc : children ){ + rc.updateChildren(); + } + } public Coordinate getOffset() { return this.position; diff --git a/core/src/net/sf/openrocket/rocketcomponent/StageSeparationConfiguration.java b/core/src/net/sf/openrocket/rocketcomponent/StageSeparationConfiguration.java index 9d859bd69..6a29e344e 100644 --- a/core/src/net/sf/openrocket/rocketcomponent/StageSeparationConfiguration.java +++ b/core/src/net/sf/openrocket/rocketcomponent/StageSeparationConfiguration.java @@ -146,14 +146,6 @@ public class StageSeparationConfiguration implements FlightConfigurableParameter return clone; } - @Override - public void addChangeListener(StateChangeListener listener) { - } - - @Override - public void removeChangeListener(StateChangeListener listener) { - - } private void fireChangeEvent() { diff --git a/core/test/net/sf/openrocket/rocketcomponent/ParameterSetTest.java b/core/test/net/sf/openrocket/rocketcomponent/ParameterSetTest.java index 918ae7722..aeafed133 100644 --- a/core/test/net/sf/openrocket/rocketcomponent/ParameterSetTest.java +++ b/core/test/net/sf/openrocket/rocketcomponent/ParameterSetTest.java @@ -5,43 +5,242 @@ package net.sf.openrocket.rocketcomponent; //import static org.junit.Assert.assertThat; //import static org.junit.Assert.assertTrue; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.not; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +import java.util.Collections; +import java.util.List; +import java.util.Map.Entry; + +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; -import net.sf.openrocket.util.MathUtil; -import net.sf.openrocket.util.StateChangeListener; +import net.sf.openrocket.util.ArrayList; import net.sf.openrocket.util.BaseTestCase.BaseTestCase; public class ParameterSetTest extends BaseTestCase { - private final static double EPSILON = MathUtil.EPSILON*1E3; + + static int gid=0; + FlightConfigurableParameterSet testSet = null; - private class Parameter implements FlightConfigurableParameter { - - public Parameter(){} + private class TestParameter implements FlightConfigurableParameter { + public final int id; - @Override - public Parameter clone(){ return null; } - + public TestParameter(){ + id = gid++; + } + @Override public void update(){} - + @Override - public void addChangeListener(StateChangeListener listener){} - + public boolean equals( Object other ){ + if( other instanceof TestParameter){ + return (this.id == ((TestParameter)other).id); + } + return false; + } + @Override - public void removeChangeListener(StateChangeListener listener){} - + public int hashCode(){ + return this.id; + } + + @Override + public String toString(){ + return "tp#:"+id; + } + + @Override + public TestParameter clone(){ + return new TestParameter(); + } }; + @Before + public void localSetUp() { + gid=0; + TestParameter defaultParam = new TestParameter(); + testSet = new FlightConfigurableParameterSet( defaultParam ); + } + + // ================ Actual Tests ================ + + @Test + public void testEmptySet() { + assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 )); + TestParameter dtp = new TestParameter(); + testSet.setDefault( dtp); + assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 )); + assertThat("set stores default value correctly: ", testSet.getDefault(), equalTo( dtp )); + } @Test - public void testEmptyRocket() { - //FlightConfigurableParameterSet testSet = new FlightConfigurableParameterSet(); + public void testRetrieveDefault(){ + FlightConfigurationId fcid2 = new FlightConfigurationId(); + // i.e. requesting the value for a non-existent config id should return the default + assertThat("set stores id-value pair correctly : ", testSet.get(fcid2), equalTo( testSet.getDefault() )); + assertThat("set contains wrong number of overrides: ", testSet.size(), equalTo( 0 )); + FlightConfigurationId fcid_def = FlightConfigurationId.DEFAULT_VALUE_FCID; + assertThat("retrieving the via the special default key should produce the default value: ", testSet.get(fcid_def), equalTo( testSet.getDefault() )); + assertThat("set should still contain zero overrides: ", testSet.size(), equalTo( 0 )); + } + @Test + public void testSetGetSecond(){ + assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 )); + + TestParameter tp2 = new TestParameter(); + FlightConfigurationId fcid2 = new FlightConfigurationId(); + testSet.set(fcid2, tp2); + // fcid <=> tp2 should be stored.... + assertThat("set stores default value correctly: ", testSet.get(fcid2), equalTo( tp2 )); + assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 1 )); + } + + @Test(expected=IndexOutOfBoundsException.class) + public void testGetByNegativeIndex() { + assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 )); + + //assertThat + testSet.get(-1); + } + + + @Test(expected=IndexOutOfBoundsException.class) + public void testGetByTooHighIndex() { + assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 )); + TestParameter tp2 = new TestParameter(); + FlightConfigurationId fcid2 = new FlightConfigurationId(); + testSet.set(fcid2, tp2); + assertThat("set should contain one override: ", testSet.size(), equalTo( 1 )); + + //assertThat + testSet.get(1); // this should be off-by-one (too high) + } + + @Test + public void testGetIdsLength(){ + assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 )); + + TestParameter tp2 = new TestParameter(); + FlightConfigurationId fcid2 = new FlightConfigurationId(); + testSet.set(fcid2, tp2); + + TestParameter tp3 = new TestParameter(); + FlightConfigurationId fcid3 = new FlightConfigurationId(); + testSet.set(fcid3, tp3); + + assertThat("set should contain two overrides: ", testSet.size(), equalTo( 2 )); + + // testSet.getSortedConfigurationIDs() + // >> this function should ONLY return ids for the overrides + assertThat("getIds() broken!\n"+testSet.toDebug(), testSet.getIds().size(), equalTo( testSet.size())); + assertThat("getIds() broken!\n"+testSet.toDebug(), testSet.getSortedConfigurationIDs().size(), equalTo( testSet.getIds().size() ) ); + } + + @Test + public void testGetByIndex(){ + assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 )); + + + TestParameter tp1 = new TestParameter(); + FlightConfigurationId fcid1 = new FlightConfigurationId(); + testSet.set(fcid1, tp1); + + TestParameter tp2 = new TestParameter(); + FlightConfigurationId fcid2 = new FlightConfigurationId(); + testSet.set(fcid2, tp2); + + TestParameter tp3 = new TestParameter(); + FlightConfigurationId fcid3 = new FlightConfigurationId(); + testSet.set(fcid3, tp3); + + TestParameter tp4 = new TestParameter(); + FlightConfigurationId fcid4 = new FlightConfigurationId(); + testSet.set(fcid4, tp4); + + assertThat("set should contain two overrides: ", testSet.size(), equalTo( 4 )); + + ArrayList refList = new ArrayList(); + refList.add(fcid1); + refList.add(fcid2); + refList.add(fcid3); + refList.add(fcid4); + Collections.sort(refList); // Java 1.7: + + //assertThat + assertThat("retrieve-by-index broken!\n"+testSet.toDebug(), testSet.get(0), equalTo( testSet.get( refList.get(0)))); + assertThat("retrieve-by-index broken!\n"+testSet.toDebug(), testSet.get(1), equalTo( testSet.get( refList.get(1)))); + assertThat("retrieve-by-index broken!\n"+testSet.toDebug(), testSet.get(2), equalTo( testSet.get( refList.get(2)))); + assertThat("retrieve-by-index broken!\n"+testSet.toDebug(), testSet.get(3), equalTo( testSet.get( refList.get(3)))); + } + + + @Test + public void testRemoveSecond(){ + assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 )); + + TestParameter tp2 = new TestParameter(); + FlightConfigurationId fcid2 = new FlightConfigurationId(); + testSet.set(fcid2, tp2); + // fcid <=> tp2 should be stored.... + assertThat("set stores default value correctly: ", testSet.get(fcid2), equalTo( tp2 )); + assertThat("set should contain one override: ", testSet.size(), equalTo( 1 )); + + testSet.set(fcid2, null); + // fcid <=> tp2 should be stored.... + assertThat("set stores default value correctly: ", testSet.get(fcid2), equalTo( testSet.getDefault() )); + assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 )); } + @Test + public void testGetByValue(){ + assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 )); + assertThat("retrieving the default value should produce the special default key: ", + testSet.getId(testSet.getDefault()), equalTo( FlightConfigurationId.DEFAULT_VALUE_FCID)); + + TestParameter tp2 = new TestParameter(); + FlightConfigurationId fcid2 = new FlightConfigurationId(); + testSet.set(fcid2, tp2); + // fcid <=> tp2 should be stored.... + assertThat("set should contain one override: ", testSet.size(), equalTo( 1 )); + assertThat("set stores default value correctly: ", testSet.get(fcid2), equalTo( tp2 )); + + // now retrieve that same parameter by value + FlightConfigurationId fcid3 = testSet.getId(tp2); + assertThat("set should contain one override: ", testSet.size(), equalTo( 1 )); + assertThat("set stores default value correctly: ", fcid2, equalTo( fcid3 )); + assertThat("set stores default value correctly: ", testSet.get( fcid3), equalTo( tp2 )); + } + + + @Test + public void testCloneSecond(){ + assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 )); + + TestParameter tp2 = new TestParameter(); + FlightConfigurationId fcid2 = new FlightConfigurationId(); + testSet.set(fcid2, tp2); + // fcid <=> tp2 should be stored.... + assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 1 )); + assertThat("set stores default value correctly: ", testSet.get(fcid2), equalTo( tp2 )); + + FlightConfigurationId fcid3 = new FlightConfigurationId(); + testSet.cloneFlightConfiguration(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() )); + } + + } diff --git a/swing/src/net/sf/openrocket/gui/adaptors/Column.java b/swing/src/net/sf/openrocket/gui/adaptors/Column.java index 1c51f98af..92df4b54f 100644 --- a/swing/src/net/sf/openrocket/gui/adaptors/Column.java +++ b/swing/src/net/sf/openrocket/gui/adaptors/Column.java @@ -96,7 +96,7 @@ public abstract class Column { * * @return */ - public Comparator getComparator() { + public Comparator getComparator() { return null; } diff --git a/swing/src/net/sf/openrocket/gui/adaptors/DoubleModel.java b/swing/src/net/sf/openrocket/gui/adaptors/DoubleModel.java index 893cc331c..1b8551259 100644 --- a/swing/src/net/sf/openrocket/gui/adaptors/DoubleModel.java +++ b/swing/src/net/sf/openrocket/gui/adaptors/DoubleModel.java @@ -548,7 +548,7 @@ public class DoubleModel implements StateChangeListener, ChangeSource, Invalidat * The main model handles all values in SI units, i.e. no conversion is made within the model. */ - private final ChangeSource source; + private final Object source; private final String valueName; private final double multiplier; @@ -643,7 +643,7 @@ public class DoubleModel implements StateChangeListener, ChangeSource, Invalidat * @param min Minimum value allowed (in SI units) * @param max Maximum value allowed (in SI units) */ - public DoubleModel(ChangeSource source, String valueName, double multiplier, UnitGroup unit, + public DoubleModel(Object source, String valueName, double multiplier, UnitGroup unit, double min, double max) { this.source = source; this.valueName = valueName; @@ -689,43 +689,42 @@ public class DoubleModel implements StateChangeListener, ChangeSource, Invalidat } - public DoubleModel(ChangeSource source, String valueName, double multiplier, UnitGroup unit, + public DoubleModel(Object source, String valueName, double multiplier, UnitGroup unit, double min) { this(source, valueName, multiplier, unit, min, Double.POSITIVE_INFINITY); } - public DoubleModel(ChangeSource source, String valueName, double multiplier, UnitGroup unit) { + public DoubleModel(Object source, String valueName, double multiplier, UnitGroup unit) { this(source, valueName, multiplier, unit, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); } - public DoubleModel(ChangeSource source, String valueName, UnitGroup unit, + public DoubleModel(Object source, String valueName, UnitGroup unit, double min, double max) { this(source, valueName, 1.0, unit, min, max); } - public DoubleModel(ChangeSource source, String valueName, UnitGroup unit, double min) { + public DoubleModel(Object source, String valueName, UnitGroup unit, double min) { this(source, valueName, 1.0, unit, min, Double.POSITIVE_INFINITY); } - public DoubleModel(ChangeSource source, String valueName, UnitGroup unit) { + public DoubleModel(Object source, String valueName, UnitGroup unit) { this(source, valueName, 1.0, unit, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); } - public DoubleModel(ChangeSource source, String valueName) { + public DoubleModel(Object source, String valueName) { this(source, valueName, 1.0, UnitGroup.UNITS_NONE, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); } - public DoubleModel(ChangeSource source, String valueName, double min) { + public DoubleModel(Object source, String valueName, double min) { this(source, valueName, 1.0, UnitGroup.UNITS_NONE, min, Double.POSITIVE_INFINITY); } - public DoubleModel(ChangeSource source, String valueName, double min, double max) { + public DoubleModel(Object source, String valueName, double min, double max) { this(source, valueName, 1.0, UnitGroup.UNITS_NONE, min, max); } - /** * Returns the value of the variable (in SI units). */ @@ -880,7 +879,6 @@ public class DoubleModel implements StateChangeListener, ChangeSource, Invalidat if (listeners.isEmpty()) { if (source != null) { - source.addChangeListener(this); lastValue = getValue(); lastAutomatic = isAutomatic(); } @@ -909,9 +907,6 @@ public class DoubleModel implements StateChangeListener, ChangeSource, Invalidat checkState(false); listeners.remove(l); - if (listeners.isEmpty() && source != null) { - source.removeChangeListener(this); - } log.trace(this + " removing listener (total " + listeners.size() + "): " + l); } @@ -930,9 +925,6 @@ public class DoubleModel implements StateChangeListener, ChangeSource, Invalidat log.warn("Invalidating " + this + " while still having listeners " + listeners); } listeners.clear(); - if (source != null) { - source.removeChangeListener(this); - } MemoryManagement.collectable(this); } diff --git a/swing/src/net/sf/openrocket/gui/adaptors/EnumModel.java b/swing/src/net/sf/openrocket/gui/adaptors/EnumModel.java index df8ba1f25..3b5fda722 100644 --- a/swing/src/net/sf/openrocket/gui/adaptors/EnumModel.java +++ b/swing/src/net/sf/openrocket/gui/adaptors/EnumModel.java @@ -7,38 +7,37 @@ import javax.swing.AbstractListModel; import javax.swing.ComboBoxModel; import javax.swing.MutableComboBoxModel; -import net.sf.openrocket.util.ChangeSource; import net.sf.openrocket.util.Reflection; import net.sf.openrocket.util.StateChangeListener; -public class EnumModel> extends AbstractListModel - implements ComboBoxModel, MutableComboBoxModel, StateChangeListener { - - private final ChangeSource source; +public class EnumModel> extends AbstractListModel + implements ComboBoxModel, MutableComboBoxModel, StateChangeListener { + private static final long serialVersionUID = 7766446027840316797L; + private final Object source; private final String valueName; private final String nullText; - private final Enum[] values; - private Enum currentValue = null; + private final T[] values; + private T currentValue = null; - ArrayList> displayedValues = new ArrayList>(); + ArrayList displayedValues = new ArrayList(); private final Reflection.Method getMethod; private final Reflection.Method setMethod; - public EnumModel(ChangeSource source, String valueName) { + public EnumModel(Object source, String valueName) { this(source,valueName,null,null); } - public EnumModel(ChangeSource source, String valueName, Enum[] values) { + public EnumModel(Object source, String valueName, T[] values) { this(source, valueName, values, null); } @SuppressWarnings("unchecked") - public EnumModel(ChangeSource source, String valueName, Enum[] values, String nullText) { + public EnumModel(Object source, String valueName, T[] values, String nullText) { Class> enumClass; this.source = source; this.valueName = valueName; @@ -62,15 +61,14 @@ public class EnumModel> extends AbstractListModel if (values != null) this.values = values; else - this.values = enumClass.getEnumConstants(); + this.values = (T[]) enumClass.getEnumConstants(); - for (Enum e : this.values){ + for (T e : this.values){ this.displayedValues.add( e ); } this.nullText = nullText; stateChanged(null); // Update current value - source.addChangeListener(this); } @@ -82,7 +80,7 @@ public class EnumModel> extends AbstractListModel return currentValue; } - + @SuppressWarnings("unchecked") @Override public void setSelectedItem(Object item) { if (item == null) { @@ -102,19 +100,19 @@ public class EnumModel> extends AbstractListModel // Comparison with == ok, since both are enums if (currentValue == item) return; - // @SuppressWarnings("unchecked") - this.currentValue = (Enum) item; + + this.currentValue = (T) item; setMethod.invoke(source, item); } @Override - public Object getElementAt(int index) { + public T getElementAt(int index) { if( ( index < 0 ) || ( index >= this.displayedValues.size())){ - return nullText; // bad parameter + return null; // bad parameter } if (values[index] == null) - return nullText; + return null; return displayedValues.get( index); } @@ -126,7 +124,7 @@ public class EnumModel> extends AbstractListModel @SuppressWarnings("unchecked") @Override public void stateChanged(EventObject e) { - Enum value = (Enum) getMethod.invoke(source); + T value = (T) getMethod.invoke(source); if (value != currentValue) { currentValue = value; this.fireContentsChanged(this, 0, values.length); @@ -139,7 +137,7 @@ public class EnumModel> extends AbstractListModel } @Override - public void addElement(Object item) { + public void addElement(T item) { // Not actually allowed. The model starts out with all the enums, and only allows hiding some. } @@ -152,7 +150,7 @@ public class EnumModel> extends AbstractListModel } @Override - public void insertElementAt(Object item, int index) { + public void insertElementAt(T item, int index) { // Not actually allowed. The model starts out with all the enums, and only allows hiding some. } diff --git a/swing/src/net/sf/openrocket/gui/configdialog/InnerTubeConfig.java b/swing/src/net/sf/openrocket/gui/configdialog/InnerTubeConfig.java index 065f301b8..a77e4e63a 100644 --- a/swing/src/net/sf/openrocket/gui/configdialog/InnerTubeConfig.java +++ b/swing/src/net/sf/openrocket/gui/configdialog/InnerTubeConfig.java @@ -51,6 +51,7 @@ import net.sf.openrocket.util.StateChangeListener; public class InnerTubeConfig extends RocketComponentConfig { + private static final long serialVersionUID = 7900041420864324470L; private static final Translator trans = Application.getTranslator(); @@ -95,11 +96,7 @@ public class InnerTubeConfig extends RocketComponentConfig { panel.add(spin, "growx"); panel.add(new UnitSelector(m), "growx"); - if (od == null) - panel.add(new BasicSlider(m.getSliderModel(0, 0.04, 0.2)), "w 100lp, wrap"); - else - panel.add(new BasicSlider(m.getSliderModel(new DoubleModel(0), od)), - "w 100lp, wrap"); + panel.add(new BasicSlider(m.getSliderModel(new DoubleModel(0), od)), "w 100lp, wrap"); if (m.isAutomaticAvailable()) { JCheckBox check = new JCheckBox(m.getAutomaticAction()); @@ -142,7 +139,7 @@ public class InnerTubeConfig extends RocketComponentConfig { //// Position relative to: panel.add(new JLabel(trans.get("ringcompcfg.Positionrelativeto"))); - JComboBox combo = new JComboBox( + JComboBox combo = new JComboBox( new EnumModel(component, "RelativePosition", new RocketComponent.Position[] { RocketComponent.Position.TOP, @@ -389,6 +386,10 @@ public class InnerTubeConfig extends RocketComponentConfig { class ClusterSelectionPanel extends JPanel { + /** + * + */ + private static final long serialVersionUID = 1804786106133398810L; private static final int BUTTON_SIZE = 50; private static final int MOTOR_DIAMETER = 10; @@ -417,6 +418,10 @@ class ClusterSelectionPanel extends JPanel { private class ClusterButton extends JPanel implements StateChangeListener, MouseListener, Resettable { + /** + * + */ + private static final long serialVersionUID = 3626386642481889629L; private Clusterable component; private ClusterConfiguration config; diff --git a/swing/src/net/sf/openrocket/gui/configdialog/ParachuteConfig.java b/swing/src/net/sf/openrocket/gui/configdialog/ParachuteConfig.java index e0a1eb41a..1db929d97 100644 --- a/swing/src/net/sf/openrocket/gui/configdialog/ParachuteConfig.java +++ b/swing/src/net/sf/openrocket/gui/configdialog/ParachuteConfig.java @@ -138,7 +138,7 @@ public class ParachuteConfig extends RecoveryDeviceConfig { //// Position relative to: panel.add(new JLabel(trans.get("ParachuteCfg.lbl.Posrelativeto"))); - combo = new JComboBox( + combo = new JComboBox( new EnumModel(component, "RelativePosition", new RocketComponent.Position[] { RocketComponent.Position.TOP,