From 277bb5589a83a868e5e6bff5b9eaf230902c0ea4 Mon Sep 17 00:00:00 2001 From: Bill Kuker Date: Fri, 7 Aug 2009 00:00:18 +0000 Subject: [PATCH] Changed propertylistening and quantitychecking to aspects --- .classpath | 31 ++--- .project | 35 +++--- .../rocketry/motorsim/ChangeListening.aj | 63 ++++++++++ .../motorsim/ConvergentDivergentNozzle.java | 8 +- .../billkuker/rocketry/motorsim/Motor.java | 12 +- .../rocketry/motorsim/MotorPart.java | 117 ------------------ .../rocketry/motorsim/QuantityChecking.aj | 57 +++++++++ .../motorsim/grain/CompoundGrain.java | 12 +- .../motorsim/grain/CoredCylindricalGrain.java | 17 +-- .../motorsim/grain/ExtrudedGrain.java | 12 +- .../rocketry/motorsim/grain/Finocyl.java | 15 --- .../rocketry/motorsim/grain/Moonburner.java | 9 -- .../rocketry/motorsim/grain/MultiGrain.java | 15 ++- .../rocketry/motorsim/io/MotorIO.java | 3 - .../test/CoredCylindricalGrainTest.java | 4 +- .../rocketry/motorsim/visual/GrainPanel.java | 6 +- .../rocketry/motorsim/visual/NozzlePanel.java | 6 +- .../visual/workbench/MotorEditor.java | 48 ++----- .../visual/workbench/WorkbenchTreeModel.java | 10 +- 19 files changed, 199 insertions(+), 281 deletions(-) create mode 100644 src/com/billkuker/rocketry/motorsim/ChangeListening.aj delete mode 100644 src/com/billkuker/rocketry/motorsim/MotorPart.java create mode 100644 src/com/billkuker/rocketry/motorsim/QuantityChecking.aj diff --git a/.classpath b/.classpath index 189b53b..51967f3 100644 --- a/.classpath +++ b/.classpath @@ -1,15 +1,16 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + diff --git a/.project b/.project index 5fc6d6f..311ced9 100644 --- a/.project +++ b/.project @@ -1,17 +1,18 @@ - - - MotorSim - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - + + + MotorSim + + + + + + org.eclipse.ajdt.core.ajbuilder + + + + + + org.eclipse.ajdt.ui.ajnature + org.eclipse.jdt.core.javanature + + diff --git a/src/com/billkuker/rocketry/motorsim/ChangeListening.aj b/src/com/billkuker/rocketry/motorsim/ChangeListening.aj new file mode 100644 index 0000000..86fc38e --- /dev/null +++ b/src/com/billkuker/rocketry/motorsim/ChangeListening.aj @@ -0,0 +1,63 @@ +package com.billkuker.rocketry.motorsim; + +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; +import java.lang.reflect.Method; + +import javax.swing.event.ChangeListener; + +import com.billkuker.rocketry.motorsim.grain.MultiGrain; + +public aspect ChangeListening { + + public interface Subject { + // public void addPropertyChangeListener(PropertyChangeListener l); + } + + private PropertyChangeSupport Subject.pcs; + + public void Subject.addPropertyChangeListener(PropertyChangeListener l) { + //System.out.println("PCS Added"); + pcs.addPropertyChangeListener(l); + } + + public void Subject.removePropertyChangeListener(PropertyChangeListener l) { + //System.out.println("PCS Removed"); + pcs.addPropertyChangeListener(l); + } + + public void Subject.firePropertyChange(PropertyChangeEvent e) { + pcs.firePropertyChange(e); + } + + declare parents: Motor || Grain || Chamber || Nozzle || Fuel implements Subject; + + void around(Subject s, Object newVal): + execution(void Subject+.set*(..)) && target(s) && args(newVal) { + System.out.println(s); + String name = thisJoinPointStaticPart.getSignature().getName(); + name = name.replaceFirst("set", ""); + Object old = null; + try { + String pre = "get"; + if (newVal instanceof Boolean) + pre = "is"; + Method m = s.getClass().getMethod(pre + name); + old = m.invoke(s); + + } catch (Throwable t) { + System.err.print("Error getting old value for " + name); + } + proceed(s, newVal); + if (old != newVal) { + System.out.println(name + " changed from " + old + " to " + + newVal); + s.pcs.firePropertyChange(name, old, newVal); + } + } + + after(Subject s) returning: this(s) && initialization(Subject.new(..)) { + s.pcs = new PropertyChangeSupport(s); + } +} diff --git a/src/com/billkuker/rocketry/motorsim/ConvergentDivergentNozzle.java b/src/com/billkuker/rocketry/motorsim/ConvergentDivergentNozzle.java index 8ce520f..1610031 100644 --- a/src/com/billkuker/rocketry/motorsim/ConvergentDivergentNozzle.java +++ b/src/com/billkuker/rocketry/motorsim/ConvergentDivergentNozzle.java @@ -14,7 +14,7 @@ import javax.measure.unit.SI; import org.jscience.physics.amount.Amount; -public class ConvergentDivergentNozzle extends MotorPart implements Nozzle { +public class ConvergentDivergentNozzle implements Nozzle { private Amount throatDiameter; @@ -40,9 +40,7 @@ public class ConvergentDivergentNozzle extends MotorPart implements Nozzle { public void setThroatDiameter(Amount throatDiameter) { if ( exitDiameter != null && throatDiameter.isGreaterThan(exitDiameter)) throw new IllegalArgumentException("Throat > Exit"); - Amount old = this.throatDiameter; this.throatDiameter = throatDiameter; - firePropertyChange("throatDiameter", old, throatDiameter); } @@ -54,9 +52,7 @@ public class ConvergentDivergentNozzle extends MotorPart implements Nozzle { public void setExitDiameter(Amount exitDiameter) { if ( throatDiameter != null && exitDiameter.isLessThan(throatDiameter)) throw new IllegalArgumentException("Throat > Exit"); - Amount old = this.exitDiameter; this.exitDiameter = exitDiameter; - firePropertyChange("exitDiameter", old, exitDiameter); } public Amount thrust(Amount Po, Amount Pe, Amount Patm, final double k ){ @@ -87,9 +83,7 @@ public class ConvergentDivergentNozzle extends MotorPart implements Nozzle { } public void setEfficiency(double efficiency) { - double old = this.efficiency; this.efficiency = efficiency; - firePropertyChange("efficiency", old, efficiency); } public Shape nozzleShape(Amount chamberDiameter){ diff --git a/src/com/billkuker/rocketry/motorsim/Motor.java b/src/com/billkuker/rocketry/motorsim/Motor.java index a26b0b3..2671351 100644 --- a/src/com/billkuker/rocketry/motorsim/Motor.java +++ b/src/com/billkuker/rocketry/motorsim/Motor.java @@ -1,6 +1,6 @@ package com.billkuker.rocketry.motorsim; -public class Motor extends MotorPart{ +public class Motor { private Chamber chamber; private Grain grain; private Nozzle nozzle; @@ -13,9 +13,7 @@ public class Motor extends MotorPart{ } public void setChamber(Chamber chamber) { - Chamber old = this.chamber; this.chamber = chamber; - firePropertyChange("Chamber", old, chamber); } public Grain getGrain() { @@ -23,9 +21,7 @@ public class Motor extends MotorPart{ } public void setGrain(Grain grain) { - Grain old = this.grain; this.grain = grain; - firePropertyChange("Grain", old, grain); } public Nozzle getNozzle() { @@ -33,9 +29,7 @@ public class Motor extends MotorPart{ } public void setNozzle(Nozzle nozzle) { - Nozzle old = this.nozzle; this.nozzle = nozzle; - firePropertyChange("Nozzle", old, nozzle); } public Fuel getFuel() { @@ -43,9 +37,7 @@ public class Motor extends MotorPart{ } public void setFuel(Fuel fuel) { - Fuel old = this.fuel; this.fuel = fuel; - firePropertyChange("Fuel", old, fuel); } public String getName() { @@ -53,8 +45,6 @@ public class Motor extends MotorPart{ } public void setName(String name) { - String old = this.name; this.name = name; - firePropertyChange("Name", old, name); } } diff --git a/src/com/billkuker/rocketry/motorsim/MotorPart.java b/src/com/billkuker/rocketry/motorsim/MotorPart.java deleted file mode 100644 index eeded4d..0000000 --- a/src/com/billkuker/rocketry/motorsim/MotorPart.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.billkuker.rocketry.motorsim; - -import java.beans.BeanInfo; -import java.beans.Introspector; -import java.beans.PropertyChangeEvent; -import java.beans.PropertyChangeListener; -import java.beans.PropertyChangeSupport; -import java.beans.PropertyDescriptor; -import java.beans.PropertyVetoException; -import java.beans.VetoableChangeListener; -import java.beans.VetoableChangeSupport; -import java.lang.reflect.Field; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; - -import javax.measure.unit.Unit; - -import org.apache.log4j.Logger; -import org.jscience.physics.amount.Amount; - -public class MotorPart { - PropertyChangeSupport pcs; - VetoableChangeSupport vcs; - - private static Logger log = Logger.getLogger(MotorPart.class); - - public interface Validating { - void checkValidity() throws ValidationException; - - public class ValidationException extends Exception { - private static final long serialVersionUID = 1L; - public ValidationException(Validating v, String error) { - super(v.toString() + ": " + error); - } - } - } - - public MotorPart() { - pcs = new PropertyChangeSupport(this); - vcs = new VetoableChangeSupport(this); - - vcs.addVetoableChangeListener(new VetoableChangeListener(){ - @SuppressWarnings("unchecked") - public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException { - if ( evt.getNewValue() instanceof Amount ){ - try { - BeanInfo b = Introspector.getBeanInfo(evt.getSource().getClass()); - PropertyDescriptor ps[] = b.getPropertyDescriptors(); - for ( int i = 0; i < ps.length; i++ ){ - if (ps[i].getName().equals(evt.getPropertyName())){ - Type t = ps[i].getReadMethod().getGenericReturnType(); - ParameterizedType p = (ParameterizedType) t; - Class expected = (Class)p.getActualTypeArguments()[0]; - Field f = expected.getDeclaredField("UNIT"); - Unit u = (Unit) f.get(null); - - Amount a = (Amount)evt.getNewValue(); - if (!a.getUnit().isCompatible(u)) - throw new PropertyVetoException(ps[i].getShortDescription() - + " must be in units of " - + expected.getSimpleName(), evt); - - log.debug("Expected " + expected + " got " + u); - } - } - } catch ( PropertyVetoException e ){ - throw e; - } catch (Exception e) { - e.printStackTrace(); - } - } - } - }); - } - - public void addPropertyChangeListener(PropertyChangeListener listener) { - pcs.addPropertyChangeListener(listener); - } - - public void removePropertyChangeListener(PropertyChangeListener listener) { - pcs.removePropertyChangeListener(listener); - } - - protected void firePropertyChange(PropertyChangeEvent evt) { - pcs.firePropertyChange(evt); - } - - protected void firePropertyChange(String propertyName, boolean oldValue, - boolean newValue) { - pcs.firePropertyChange(propertyName, oldValue, newValue); - } - - protected void firePropertyChange(String propertyName, int oldValue, - int newValue) { - pcs.firePropertyChange(propertyName, oldValue, newValue); - } - - protected void firePropertyChange(String propertyName, Object oldValue, - Object newValue) { - pcs.firePropertyChange(propertyName, oldValue, newValue); - } - - public void fireVetoableChange(String arg0, boolean arg1, boolean arg2) - throws PropertyVetoException { - vcs.fireVetoableChange(arg0, arg1, arg2); - } - - public void fireVetoableChange(String arg0, int arg1, int arg2) - throws PropertyVetoException { - vcs.fireVetoableChange(arg0, arg1, arg2); - } - - public void fireVetoableChange(String arg0, Object arg1, Object arg2) - throws PropertyVetoException { - vcs.fireVetoableChange(arg0, arg1, arg2); - } -} diff --git a/src/com/billkuker/rocketry/motorsim/QuantityChecking.aj b/src/com/billkuker/rocketry/motorsim/QuantityChecking.aj new file mode 100644 index 0000000..544de37 --- /dev/null +++ b/src/com/billkuker/rocketry/motorsim/QuantityChecking.aj @@ -0,0 +1,57 @@ +package com.billkuker.rocketry.motorsim; + +import java.beans.BeanInfo; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.beans.PropertyVetoException; +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + +import javax.measure.unit.Unit; + +import org.jscience.physics.amount.Amount; +import com.billkuker.rocketry.motorsim.ChangeListening.Subject; +import com.billkuker.rocketry.motorsim.grain.CoredCylindricalGrain; + +public aspect QuantityChecking { + + public interface Checked { + }; + + declare parents: Motor || Grain || Chamber || Nozzle || Fuel extends Checked; + + void around(Checked c, Amount amt): + execution(void Checked+.set*(Amount)) && target(c) && args(amt) { + System.out.println(thisJoinPointStaticPart.getSignature().getName() + + " set to " + amt); + try { + BeanInfo b = Introspector.getBeanInfo(c.getClass()); + PropertyDescriptor ps[] = b.getPropertyDescriptors(); + String name = thisJoinPointStaticPart.getSignature().getName(); + name = name.replaceFirst("set", ""); + for (int i = 0; i < ps.length; i++) { + if (ps[i].getName().equals(name)) { + Type t = ps[i].getReadMethod().getGenericReturnType(); + ParameterizedType p = (ParameterizedType) t; + Class expected = (Class) p.getActualTypeArguments()[0]; + Field f = expected.getDeclaredField("UNIT"); + Unit u = (Unit) f.get(null); + + Amount a = amt; + if (!a.getUnit().isCompatible(u)) { + System.err.println("Aspect Expected " + expected + + " got " + u); + + throw new Error(ps[i].getShortDescription() + + " must be in units of " + + expected.getSimpleName()); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + proceed(c, amt); + } +} diff --git a/src/com/billkuker/rocketry/motorsim/grain/CompoundGrain.java b/src/com/billkuker/rocketry/motorsim/grain/CompoundGrain.java index ee1ac61..539beb7 100644 --- a/src/com/billkuker/rocketry/motorsim/grain/CompoundGrain.java +++ b/src/com/billkuker/rocketry/motorsim/grain/CompoundGrain.java @@ -11,10 +11,10 @@ import javax.measure.unit.SI; import org.jscience.physics.amount.Amount; +import com.billkuker.rocketry.motorsim.ChangeListening; import com.billkuker.rocketry.motorsim.Grain; -import com.billkuker.rocketry.motorsim.MotorPart; -public class CompoundGrain extends MotorPart implements Grain { +public class CompoundGrain implements Grain { private Set grains = new HashSet(); @@ -28,14 +28,14 @@ public class CompoundGrain extends MotorPart implements Grain { public void addPropertyChangeListener(PropertyChangeListener listener) { for ( Grain g : grains ) - if ( g instanceof MotorPart ) - ((MotorPart)g).addPropertyChangeListener(listener); + if ( g instanceof ChangeListening.Subject ) + ((ChangeListening.Subject)g).addPropertyChangeListener(listener); } public void removePropertyChangeListener(PropertyChangeListener listener) { for ( Grain g : grains ) - if ( g instanceof MotorPart ) - ((MotorPart)g).removePropertyChangeListener(listener); + if ( g instanceof ChangeListening.Subject ) + ((ChangeListening.Subject)g).removePropertyChangeListener(listener); } public Area getCrossSection(Amount regression) { diff --git a/src/com/billkuker/rocketry/motorsim/grain/CoredCylindricalGrain.java b/src/com/billkuker/rocketry/motorsim/grain/CoredCylindricalGrain.java index a505ef2..6071d8a 100644 --- a/src/com/billkuker/rocketry/motorsim/grain/CoredCylindricalGrain.java +++ b/src/com/billkuker/rocketry/motorsim/grain/CoredCylindricalGrain.java @@ -12,12 +12,11 @@ import javax.measure.unit.SI; import org.jscience.physics.amount.Amount; -import com.billkuker.rocketry.motorsim.MotorPart; import com.billkuker.rocketry.motorsim.visual.Editor; import com.billkuker.rocketry.motorsim.visual.GrainPanel; -public class CoredCylindricalGrain extends ExtrudedGrain implements MotorPart.Validating { +public class CoredCylindricalGrain extends ExtrudedGrain { private Amount oD, iD; private boolean outerSurfaceInhibited = true, innerSurfaceInhibited = false; @@ -122,19 +121,14 @@ public class CoredCylindricalGrain extends ExtrudedGrain implements MotorPart.Va } public void setOD(Amount od) throws PropertyVetoException { - fireVetoableChange("od", this.oD, od); - Amount old = this.oD; this.oD = od; - firePropertyChange("OD", old, oD); } public void setID(Amount id) throws PropertyVetoException { - fireVetoableChange("id", this.iD, id); - Amount old = this.iD; iD = id; - firePropertyChange("ID", old, iD); } + /* public void checkValidity() throws ValidationException{ if ( iD.equals(Amount.ZERO) ) throw new ValidationException(this, "Invalid iD"); @@ -149,6 +143,7 @@ public class CoredCylindricalGrain extends ExtrudedGrain implements MotorPart.Va throw new ValidationException(this, "No exposed grain surface"); } + */ public Amount webThickness() { if ( innerSurfaceInhibited && outerSurfaceInhibited ){ @@ -240,10 +235,7 @@ public class CoredCylindricalGrain extends ExtrudedGrain implements MotorPart.Va public void setOuterSurfaceInhibited(boolean outerSurfaceInhibited) throws PropertyVetoException { - fireVetoableChange("outerSurfaceInhibited", this.outerSurfaceInhibited, outerSurfaceInhibited); - boolean old = this.outerSurfaceInhibited; this.outerSurfaceInhibited = outerSurfaceInhibited; - firePropertyChange("outerSurfaceInhibited", old, outerSurfaceInhibited); } @@ -253,10 +245,7 @@ public class CoredCylindricalGrain extends ExtrudedGrain implements MotorPart.Va public void setInnerSurfaceInhibited(boolean innerSurfaceInhibited) throws PropertyVetoException { - fireVetoableChange("innerSurfaceInhibited", this.innerSurfaceInhibited, innerSurfaceInhibited); - boolean old = this.innerSurfaceInhibited; this.innerSurfaceInhibited = innerSurfaceInhibited; - firePropertyChange("innerSurfaceInhibited", old, innerSurfaceInhibited); } public static void main(String args[]) throws Exception { diff --git a/src/com/billkuker/rocketry/motorsim/grain/ExtrudedGrain.java b/src/com/billkuker/rocketry/motorsim/grain/ExtrudedGrain.java index fb5bd5b..2ce5e0b 100644 --- a/src/com/billkuker/rocketry/motorsim/grain/ExtrudedGrain.java +++ b/src/com/billkuker/rocketry/motorsim/grain/ExtrudedGrain.java @@ -8,9 +8,8 @@ import javax.measure.unit.SI; import org.jscience.physics.amount.Amount; import com.billkuker.rocketry.motorsim.Grain; -import com.billkuker.rocketry.motorsim.MotorPart; -public abstract class ExtrudedGrain extends MotorPart implements Grain { +public abstract class ExtrudedGrain implements Grain { private boolean foreEndInhibited = false; private boolean aftEndInhibited = false; private Amount length = Amount.valueOf(100, SI.MILLIMETER); @@ -33,10 +32,7 @@ public abstract class ExtrudedGrain extends MotorPart implements Grain { } public void setForeEndInhibited(boolean foreEndInhibited)throws PropertyVetoException { - fireVetoableChange("foreEndInhibited", this.foreEndInhibited, foreEndInhibited); - boolean old = this.foreEndInhibited; this.foreEndInhibited = foreEndInhibited; - firePropertyChange("foreEndInhibited", old, foreEndInhibited); } public boolean isAftEndInhibited() { @@ -44,10 +40,7 @@ public abstract class ExtrudedGrain extends MotorPart implements Grain { } public void setAftEndInhibited(boolean aftEndInhibited) throws PropertyVetoException { - fireVetoableChange("aftEndInhibited", this.aftEndInhibited, aftEndInhibited); - boolean old = this.aftEndInhibited; this.aftEndInhibited = aftEndInhibited; - firePropertyChange("aftEndInhibited", old, aftEndInhibited); } public Amount getLength() { @@ -55,9 +48,6 @@ public abstract class ExtrudedGrain extends MotorPart implements Grain { } public void setLength(Amount length) throws PropertyVetoException { - fireVetoableChange("length", this.length, length); - Amount old = this.length; this.length = length; - firePropertyChange("length", old, length); } } diff --git a/src/com/billkuker/rocketry/motorsim/grain/Finocyl.java b/src/com/billkuker/rocketry/motorsim/grain/Finocyl.java index b2295c7..1405562 100644 --- a/src/com/billkuker/rocketry/motorsim/grain/Finocyl.java +++ b/src/com/billkuker/rocketry/motorsim/grain/Finocyl.java @@ -58,11 +58,8 @@ public class Finocyl extends ExtrudedShapeGrain { public void setOD(Amount od) throws PropertyVetoException { if (od.equals(this.oD)) return; - fireVetoableChange("od", this.oD, od); - Amount old = this.oD; this.oD = od; generateGeometry(); - firePropertyChange("OD", old, oD); } public Amount getID() { @@ -72,11 +69,8 @@ public class Finocyl extends ExtrudedShapeGrain { public void setID(Amount id) throws PropertyVetoException { if (id.equals(this.iD)) return; - fireVetoableChange("id", this.iD, id); - Amount old = this.iD; iD = id; generateGeometry(); - firePropertyChange("ID", old, iD); } public Amount getFinWidth() { @@ -86,11 +80,8 @@ public class Finocyl extends ExtrudedShapeGrain { public void setFinWidth(Amount finWidth) throws PropertyVetoException { if (finWidth.equals(this.finWidth)) return; - fireVetoableChange("finWidth", this.finWidth, finWidth); - Amount old = this.finWidth; this.finWidth = finWidth; generateGeometry(); - firePropertyChange("finWidth", old, finWidth); } @@ -101,11 +92,8 @@ public class Finocyl extends ExtrudedShapeGrain { public void setFinDiameter(Amount finDiameter) throws PropertyVetoException { if (finDiameter.equals(this.finDiameter)) return; - fireVetoableChange("finDiameter", this.finDiameter, finDiameter); - Amount old = this.finDiameter; this.finDiameter = finDiameter; generateGeometry(); - firePropertyChange("finDiameter", old, finDiameter); } @@ -116,11 +104,8 @@ public class Finocyl extends ExtrudedShapeGrain { public void setFinCount(int finCount) throws PropertyVetoException { if (finCount==this.finCount) return; - fireVetoableChange("finCount", this.finCount, finCount); - int old = this.finCount; this.finCount = finCount; generateGeometry(); - firePropertyChange("finCount", old, finCount); } public static void main(String args[]) throws Exception { diff --git a/src/com/billkuker/rocketry/motorsim/grain/Moonburner.java b/src/com/billkuker/rocketry/motorsim/grain/Moonburner.java index 66f61ab..5a56c95 100644 --- a/src/com/billkuker/rocketry/motorsim/grain/Moonburner.java +++ b/src/com/billkuker/rocketry/motorsim/grain/Moonburner.java @@ -36,11 +36,8 @@ public class Moonburner extends ExtrudedShapeGrain { public void setOD(Amount od) throws PropertyVetoException { if (od.equals(this.oD)) return; - fireVetoableChange("od", this.oD, od); - Amount old = this.oD; this.oD = od; generateGeometry(); - firePropertyChange("OD", old, oD); } public Amount getID() { @@ -50,11 +47,8 @@ public class Moonburner extends ExtrudedShapeGrain { public void setID(Amount id) throws PropertyVetoException { if (id.equals(this.iD)) return; - fireVetoableChange("id", this.iD, id); - Amount old = this.iD; iD = id; generateGeometry(); - firePropertyChange("ID", old, iD); } public Amount getCoreOffset() { @@ -65,11 +59,8 @@ public class Moonburner extends ExtrudedShapeGrain { throws PropertyVetoException { if (coreOffset.equals(this.coreOffset)) return; - fireVetoableChange("coreOffset", this.coreOffset, coreOffset); - Amount old = this.coreOffset; this.coreOffset = coreOffset; generateGeometry(); - firePropertyChange("coreOffset", old, this.coreOffset); } private void generateGeometry() { diff --git a/src/com/billkuker/rocketry/motorsim/grain/MultiGrain.java b/src/com/billkuker/rocketry/motorsim/grain/MultiGrain.java index c35ec7b..ad04b09 100644 --- a/src/com/billkuker/rocketry/motorsim/grain/MultiGrain.java +++ b/src/com/billkuker/rocketry/motorsim/grain/MultiGrain.java @@ -15,10 +15,10 @@ import javax.measure.unit.SI; import org.jscience.physics.amount.Amount; +import com.billkuker.rocketry.motorsim.ChangeListening; import com.billkuker.rocketry.motorsim.Grain; -import com.billkuker.rocketry.motorsim.MotorPart; -public class MultiGrain extends MotorPart implements Grain, Grain.Composite, PropertyChangeListener { +public class MultiGrain implements Grain, Grain.Composite, PropertyChangeListener { private Grain grain = null; private int count = 1; @@ -28,18 +28,17 @@ public class MultiGrain extends MotorPart implements Grain, Grain.Composite, Pro } public void setCount(int count) { - int old = this.count; this.count = count; - firePropertyChange("Count", old, count ); } public void setGrain(Grain g){ - Grain old = grain; + if ( grain instanceof ChangeListening.Subject ){ + ((ChangeListening.Subject)grain).addPropertyChangeListener(this); + } grain = g; - if ( g instanceof MotorPart ){ - ((MotorPart)g).addPropertyChangeListener(this); + if ( g instanceof ChangeListening.Subject ){ + ((ChangeListening.Subject)g).addPropertyChangeListener(this); } - firePropertyChange("Grain", old, grain); } public Grain getGrain(){ diff --git a/src/com/billkuker/rocketry/motorsim/io/MotorIO.java b/src/com/billkuker/rocketry/motorsim/io/MotorIO.java index 572ad7b..594b365 100644 --- a/src/com/billkuker/rocketry/motorsim/io/MotorIO.java +++ b/src/com/billkuker/rocketry/motorsim/io/MotorIO.java @@ -12,7 +12,6 @@ import java.io.StringWriter; import org.jscience.physics.amount.Amount; import com.billkuker.rocketry.motorsim.Motor; -import com.billkuker.rocketry.motorsim.MotorPart; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.converters.Converter; import com.thoughtworks.xstream.converters.MarshallingContext; @@ -53,8 +52,6 @@ public class MotorIO { private static XStream getXStream(){ XStream xstream = new XStream(); xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES); - xstream.omitField(MotorPart.class, "pcs"); - xstream.omitField(MotorPart.class, "vcs"); xstream.registerConverter(new AmountConverter()); xstream.registerConverter(new JavaBeanConverter(xstream.getClassMapper(), "class"), -20); return xstream; diff --git a/src/com/billkuker/rocketry/motorsim/test/CoredCylindricalGrainTest.java b/src/com/billkuker/rocketry/motorsim/test/CoredCylindricalGrainTest.java index 30324a0..b20291e 100644 --- a/src/com/billkuker/rocketry/motorsim/test/CoredCylindricalGrainTest.java +++ b/src/com/billkuker/rocketry/motorsim/test/CoredCylindricalGrainTest.java @@ -9,7 +9,6 @@ import javax.measure.unit.SI; import org.jscience.physics.amount.Amount; import org.junit.Test; -import com.billkuker.rocketry.motorsim.MotorPart; import com.billkuker.rocketry.motorsim.grain.CoredCylindricalGrain; public class CoredCylindricalGrainTest extends RocketTest { @@ -76,6 +75,7 @@ public class CoredCylindricalGrainTest extends RocketTest { assertApproximate(v, Amount.valueOf(7471, cubeMM), Amount.valueOf(1, cubeMM)); } + /* @Test(expected=MotorPart.Validating.ValidationException.class) public void testCheckValidity() throws MotorPart.Validating.ValidationException, PropertyVetoException{ CoredCylindricalGrain g = new CoredCylindricalGrain(); @@ -86,6 +86,6 @@ public class CoredCylindricalGrainTest extends RocketTest { g.setID(Amount.valueOf(30, SI.MILLIMETER)); g.checkValidity(); - } + }*/ } diff --git a/src/com/billkuker/rocketry/motorsim/visual/GrainPanel.java b/src/com/billkuker/rocketry/motorsim/visual/GrainPanel.java index bd6f9db..35974ec 100644 --- a/src/com/billkuker/rocketry/motorsim/visual/GrainPanel.java +++ b/src/com/billkuker/rocketry/motorsim/visual/GrainPanel.java @@ -28,8 +28,8 @@ import javax.swing.event.ChangeListener; import org.jscience.physics.amount.Amount; +import com.billkuker.rocketry.motorsim.ChangeListening; import com.billkuker.rocketry.motorsim.Grain; -import com.billkuker.rocketry.motorsim.MotorPart; public class GrainPanel extends JPanel { private static final long serialVersionUID = 1L; @@ -45,8 +45,8 @@ public class GrainPanel extends JPanel { grain = g; - if ( g instanceof MotorPart ){ - ((MotorPart)g).addPropertyChangeListener(new PropertyChangeListener(){ + if ( g instanceof ChangeListening.Subject ){ + ((ChangeListening.Subject)g).addPropertyChangeListener(new PropertyChangeListener(){ public void propertyChange(PropertyChangeEvent evt) { repaint(); area.setDomain(area.new IntervalDomain(Amount.valueOf(0, SI.MILLIMETER), grain.webThickness())); diff --git a/src/com/billkuker/rocketry/motorsim/visual/NozzlePanel.java b/src/com/billkuker/rocketry/motorsim/visual/NozzlePanel.java index 2dd29a3..ca88434 100644 --- a/src/com/billkuker/rocketry/motorsim/visual/NozzlePanel.java +++ b/src/com/billkuker/rocketry/motorsim/visual/NozzlePanel.java @@ -16,8 +16,8 @@ import javax.swing.WindowConstants; import org.jscience.physics.amount.Amount; +import com.billkuker.rocketry.motorsim.ChangeListening; import com.billkuker.rocketry.motorsim.ConvergentDivergentNozzle; -import com.billkuker.rocketry.motorsim.MotorPart; import com.billkuker.rocketry.motorsim.Nozzle; public class NozzlePanel extends JPanel { @@ -26,8 +26,8 @@ public class NozzlePanel extends JPanel { public NozzlePanel(Nozzle n){ nozzle = n; - if ( n instanceof MotorPart ){ - ((MotorPart)n).addPropertyChangeListener(new PropertyChangeListener(){ + if ( n instanceof ChangeListening.Subject ){ + ((ChangeListening.Subject)n).addPropertyChangeListener(new PropertyChangeListener(){ public void propertyChange(PropertyChangeEvent evt) { repaint(); } diff --git a/src/com/billkuker/rocketry/motorsim/visual/workbench/MotorEditor.java b/src/com/billkuker/rocketry/motorsim/visual/workbench/MotorEditor.java index 88a045d..c9fdcce 100644 --- a/src/com/billkuker/rocketry/motorsim/visual/workbench/MotorEditor.java +++ b/src/com/billkuker/rocketry/motorsim/visual/workbench/MotorEditor.java @@ -34,12 +34,12 @@ import org.jscience.physics.amount.Amount; import com.billkuker.rocketry.motorsim.Burn; import com.billkuker.rocketry.motorsim.Chamber; +import com.billkuker.rocketry.motorsim.ChangeListening; import com.billkuker.rocketry.motorsim.ConvergentDivergentNozzle; import com.billkuker.rocketry.motorsim.CylindricalChamber; import com.billkuker.rocketry.motorsim.Fuel; import com.billkuker.rocketry.motorsim.Grain; import com.billkuker.rocketry.motorsim.Motor; -import com.billkuker.rocketry.motorsim.MotorPart; import com.billkuker.rocketry.motorsim.Nozzle; import com.billkuker.rocketry.motorsim.fuel.EditableFuel; import com.billkuker.rocketry.motorsim.fuel.KNDX; @@ -59,8 +59,7 @@ import com.billkuker.rocketry.motorsim.visual.GrainPanel; import com.billkuker.rocketry.motorsim.visual.NozzlePanel; import com.billkuker.rocketry.motorsim.visual.Chart.IntervalDomain; -public class MotorEditor extends JTabbedPane implements PropertyChangeListener, - DocumentListener { +public class MotorEditor extends JTabbedPane implements PropertyChangeListener { private static final long serialVersionUID = 1L; RSyntaxTextArea text = new RSyntaxTextArea(); Motor motor; @@ -161,8 +160,8 @@ public class MotorEditor extends JTabbedPane implements PropertyChangeListener, } }); p.add(new Editor(gg)); - if (gg instanceof MotorPart) { - ((MotorPart) gg) + if (gg instanceof ChangeListening.Subject) { + ((ChangeListening.Subject) gg) .addPropertyChangeListener(MotorEditor.this); } } @@ -172,8 +171,8 @@ public class MotorEditor extends JTabbedPane implements PropertyChangeListener, } // setDividerLocation(.25); // setResizeWeight(.25); - if (g instanceof MotorPart) { - ((MotorPart) g).addPropertyChangeListener(MotorEditor.this); + if (g instanceof ChangeListening.Subject) { + ((ChangeListening.Subject) g).addPropertyChangeListener(MotorEditor.this); } } } @@ -219,8 +218,8 @@ public class MotorEditor extends JTabbedPane implements PropertyChangeListener, setRightComponent(burnRate); // setDividerLocation(.25); // setResizeWeight(.25); - if (f instanceof MotorPart) { - ((MotorPart) f).addPropertyChangeListener(MotorEditor.this); + if (f instanceof ChangeListening.Subject) { + ((ChangeListening.Subject) f).addPropertyChangeListener(MotorEditor.this); } } } @@ -264,11 +263,11 @@ public class MotorEditor extends JTabbedPane implements PropertyChangeListener, parts.add(new Editor(c)); parts.add(new Editor(n)); - if (n instanceof MotorPart) { - ((MotorPart) n).addPropertyChangeListener(MotorEditor.this); + if (n instanceof ChangeListening.Subject) { + ((ChangeListening.Subject) n).addPropertyChangeListener(MotorEditor.this); } - if (c instanceof MotorPart) { - ((MotorPart) c).addPropertyChangeListener(MotorEditor.this); + if (c instanceof ChangeListening.Subject) { + ((ChangeListening.Subject) c).addPropertyChangeListener(MotorEditor.this); } } } @@ -280,8 +279,8 @@ public class MotorEditor extends JTabbedPane implements PropertyChangeListener, public MotorEditor(Motor m) { super(JTabbedPane.BOTTOM); - text.getDocument().addDocumentListener(this); text.setName("XML"); + text.setEditable(false); add(text, XML_TAB); setMotor(m, true); } @@ -380,25 +379,4 @@ public class MotorEditor extends JTabbedPane implements PropertyChangeListener, bt.reBurn(); } - public void changedUpdate(DocumentEvent e) { - try { - final Motor m = MotorIO.readMotor(text.getText()); - SwingUtilities.invokeLater(new Runnable() { - public void run() { - setMotor(m, false); - } - }); - System.out.println("Motor Updated"); - } catch (Throwable e1) { - // Leave blank, motor might be broken - } - } - - public void insertUpdate(DocumentEvent e) { - // TODO Auto-generated method stub - } - - public void removeUpdate(DocumentEvent e) { - // TODO Auto-generated method stub - } } diff --git a/src/com/billkuker/rocketry/motorsim/visual/workbench/WorkbenchTreeModel.java b/src/com/billkuker/rocketry/motorsim/visual/workbench/WorkbenchTreeModel.java index 09be53d..f269f79 100644 --- a/src/com/billkuker/rocketry/motorsim/visual/workbench/WorkbenchTreeModel.java +++ b/src/com/billkuker/rocketry/motorsim/visual/workbench/WorkbenchTreeModel.java @@ -8,8 +8,8 @@ import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeNode; +import com.billkuker.rocketry.motorsim.ChangeListening; import com.billkuker.rocketry.motorsim.Motor; -import com.billkuker.rocketry.motorsim.MotorPart; import com.billkuker.rocketry.motorsim.grain.MultiGrain; public class WorkbenchTreeModel extends DefaultTreeModel { @@ -39,8 +39,8 @@ public class WorkbenchTreeModel extends DefaultTreeModel { public PartNode(Object part) { super(part, false); - if (part instanceof MotorPart) { - ((MotorPart) part).addPropertyChangeListener(this); + if (part instanceof ChangeListening.Subject) { + ((ChangeListening.Subject) part).addPropertyChangeListener(this); } } @@ -68,8 +68,8 @@ public class WorkbenchTreeModel extends DefaultTreeModel { } add(gn); add( fn = new PartNode(m.getFuel())); - if (m instanceof MotorPart) { - ((MotorPart) m).addPropertyChangeListener(this); + if (m instanceof ChangeListening.Subject) { + ((ChangeListening.Subject) m).addPropertyChangeListener(this); } } -- 2.47.2