Added validating and some validation
authorBill Kuker <bkuker@billkuker.com>
Sun, 9 Aug 2009 22:15:23 +0000 (22:15 +0000)
committerBill Kuker <bkuker@billkuker.com>
Sun, 9 Aug 2009 22:15:23 +0000 (22:15 +0000)
fixed moon burner offset

12 files changed:
src/com/billkuker/rocketry/motorsim/Burn.java
src/com/billkuker/rocketry/motorsim/ConvergentDivergentNozzle.java
src/com/billkuker/rocketry/motorsim/Motor.java
src/com/billkuker/rocketry/motorsim/Validating.java [new file with mode: 0644]
src/com/billkuker/rocketry/motorsim/grain/CoredCylindricalGrain.java
src/com/billkuker/rocketry/motorsim/grain/Finocyl.java
src/com/billkuker/rocketry/motorsim/grain/Moonburner.java
src/com/billkuker/rocketry/motorsim/grain/MultiGrain.java
src/com/billkuker/rocketry/motorsim/grain/RodAndTubeGrain.java
src/com/billkuker/rocketry/motorsim/visual/workbench/MotorEditor.java
src/com/billkuker/rocketry/motorsim/visual/workbench/WorkbenchTreeCellRenderer.java
src/com/billkuker/rocketry/motorsim/visual/workbench/WorkbenchTreeModel.java

index ef2474b39d0291e82e9d47f7553c2716a66671ab..6be9861fa4f22acec53b41e7b08cbb76bf033d5a 100644 (file)
@@ -25,6 +25,8 @@ import org.apache.log4j.Logger;
 import org.jscience.physics.amount.Amount;\r
 import org.jscience.physics.amount.Constants;\r
 \r
+import com.billkuker.rocketry.motorsim.Validating.ValidationException;\r
+\r
 public class Burn {\r
        //Some constants to tune adaptive regression step\r
        private static final double regStepIncreaseFactor = 1.01;\r
@@ -70,11 +72,21 @@ public class Burn {
        }\r
        \r
        public Burn(Motor m){\r
+               try {\r
+                       m.validate();\r
+               } catch (ValidationException e) {\r
+                       throw new IllegalArgumentException("Invalid Motor: " + e.getMessage());\r
+               }\r
                motor = m;\r
                burn();\r
        }\r
        \r
        public Burn(Motor m, BurnProgressListener bpl){\r
+               try {\r
+                       m.validate();\r
+               } catch (ValidationException e) {\r
+                       throw new IllegalArgumentException("Invalid Motor: " + e.getMessage());\r
+               }\r
                motor = m;\r
                this.bpl = bpl;\r
                burn();\r
index 16100312d46438fa457a1f8c7e08e0b5770f37ad..2f10cb99d93a2a88f5c2379c93da251ae0b8c0f9 100644 (file)
@@ -14,7 +14,7 @@ import javax.measure.unit.SI;
 \r
 import org.jscience.physics.amount.Amount;\r
 \r
-public class ConvergentDivergentNozzle implements Nozzle {\r
+public class ConvergentDivergentNozzle implements Nozzle, Validating {\r
 \r
        private Amount<Length> throatDiameter;\r
        \r
@@ -38,8 +38,6 @@ public class ConvergentDivergentNozzle implements Nozzle {
 \r
 \r
        public void setThroatDiameter(Amount<Length> throatDiameter) {\r
-               if ( exitDiameter != null && throatDiameter.isGreaterThan(exitDiameter))\r
-                       throw new IllegalArgumentException("Throat > Exit");\r
                this.throatDiameter = throatDiameter;\r
        }\r
        \r
@@ -50,8 +48,6 @@ public class ConvergentDivergentNozzle implements Nozzle {
 \r
 \r
        public void setExitDiameter(Amount<Length> exitDiameter) {\r
-               if ( throatDiameter != null && exitDiameter.isLessThan(throatDiameter))\r
-                       throw new IllegalArgumentException("Throat > Exit");\r
                this.exitDiameter = exitDiameter;\r
        }\r
        \r
@@ -112,4 +108,10 @@ public class ConvergentDivergentNozzle implements Nozzle {
                \r
                return s;\r
        }\r
+\r
+       @Override\r
+       public void validate() throws ValidationException {\r
+               if ( exitDiameter != null && throatDiameter.isGreaterThan(exitDiameter))\r
+                       throw new IllegalArgumentException("Throat > Exit");\r
+       }\r
 }\r
index 267135130ddbbc9710105e3894f62e3945d30403..91c2c1013bfb43b762b1188753ab9777295d5ff3 100644 (file)
@@ -1,12 +1,30 @@
 package com.billkuker.rocketry.motorsim;\r
 \r
-public class Motor {\r
+import javax.measure.unit.SI;\r
+\r
+import org.jscience.physics.amount.Amount;\r
+import com.billkuker.rocketry.motorsim.Validating.ValidationException;\r
+\r
+public class Motor implements Validating{\r
        private Chamber chamber;\r
        private Grain grain;\r
        private Nozzle nozzle;\r
        private Fuel fuel;\r
        private String name;\r
        \r
+       public void validate() throws ValidationException {\r
+               if ( chamber.chamberVolume().isLessThan(grain.volume(Amount.valueOf(0, SI.MILLIMETER)))){\r
+                       throw new ValidationException(this, "Fuel does not fit in chamber");\r
+               }\r
+               if ( chamber instanceof Validating )\r
+                       ((Validating)chamber).validate();\r
+               if ( grain instanceof Validating )\r
+                       ((Validating)grain).validate();\r
+               if ( nozzle instanceof Validating )\r
+                       ((Validating)nozzle).validate();\r
+               if ( fuel instanceof Validating )\r
+                       ((Validating)fuel).validate();\r
+       }\r
        \r
        public Chamber getChamber() {\r
                return chamber;\r
diff --git a/src/com/billkuker/rocketry/motorsim/Validating.java b/src/com/billkuker/rocketry/motorsim/Validating.java
new file mode 100644 (file)
index 0000000..b020c63
--- /dev/null
@@ -0,0 +1,12 @@
+package com.billkuker.rocketry.motorsim;
+
+public interface Validating {
+
+       public class ValidationException extends Exception{
+               public ValidationException(Validating part, String error){
+                       super(error);
+               }
+       }
+       
+       public void validate() throws ValidationException;
+}
index 6071d8a24e073f44f9ef8a279a73c1fecd52de28..5b05536343919d6439092c9e30ff3969a472ca0a 100644 (file)
@@ -12,11 +12,12 @@ import javax.measure.unit.SI;
 \r
 import org.jscience.physics.amount.Amount;\r
 \r
+import com.billkuker.rocketry.motorsim.Validating;\r
 import com.billkuker.rocketry.motorsim.visual.Editor;\r
 import com.billkuker.rocketry.motorsim.visual.GrainPanel;\r
 \r
 \r
-public class CoredCylindricalGrain extends ExtrudedGrain {\r
+public class CoredCylindricalGrain extends ExtrudedGrain implements Validating {\r
 \r
        private Amount<Length> oD, iD;\r
        private boolean outerSurfaceInhibited = true, innerSurfaceInhibited = false;\r
@@ -128,8 +129,8 @@ public class CoredCylindricalGrain extends ExtrudedGrain {
                iD = id;\r
        }\r
        \r
-       /*\r
-       public void checkValidity() throws ValidationException{\r
+       @Override\r
+       public void validate() throws ValidationException{\r
                if ( iD.equals(Amount.ZERO) )\r
                        throw new ValidationException(this, "Invalid iD");\r
                if ( oD.equals(Amount.ZERO) )\r
@@ -143,7 +144,6 @@ public class CoredCylindricalGrain extends ExtrudedGrain {
                        throw new ValidationException(this, "No exposed grain surface");\r
                \r
        }\r
-       */\r
 \r
        public Amount<Length> webThickness() {\r
                if ( innerSurfaceInhibited && outerSurfaceInhibited ){\r
index 140556274dab0648460acd34ef99c170760bdba7..3364e60e5aafea5b04d88fb947c44a753521a436 100644 (file)
@@ -11,12 +11,14 @@ import javax.measure.unit.SI;
 \r
 import org.jscience.physics.amount.Amount;\r
 \r
+import com.billkuker.rocketry.motorsim.Validating;\r
+import com.billkuker.rocketry.motorsim.Validating.ValidationException;\r
 import com.billkuker.rocketry.motorsim.grain.util.BurningShape;\r
 import com.billkuker.rocketry.motorsim.grain.util.ExtrudedShapeGrain;\r
 import com.billkuker.rocketry.motorsim.visual.Editor;\r
 import com.billkuker.rocketry.motorsim.visual.GrainPanel;\r
 \r
-public class Finocyl extends ExtrudedShapeGrain {\r
+public class Finocyl extends ExtrudedShapeGrain implements Validating {\r
        private Amount<Length> oD = Amount.valueOf(30, SI.MILLIMETER);\r
        private Amount<Length> iD = Amount.valueOf(10, SI.MILLIMETER);\r
        private Amount<Length> finWidth = Amount.valueOf(2, SI.MILLIMETER);\r
@@ -113,4 +115,16 @@ public class Finocyl extends ExtrudedShapeGrain {
                new Editor(e).showAsWindow();\r
                new GrainPanel(e).showAsWindow();\r
        }\r
+       \r
+       @Override\r
+       public void validate() throws ValidationException{\r
+               if ( iD.equals(Amount.ZERO) )\r
+                       throw new ValidationException(this, "Invalid iD");\r
+               if ( oD.equals(Amount.ZERO) )\r
+                       throw new ValidationException(this, "Invalid oD");\r
+               if ( getLength().equals(Amount.ZERO) )\r
+                       throw new ValidationException(this, "Invalid Length");\r
+               if ( iD.isGreaterThan(oD) )\r
+                       throw new ValidationException(this, "iD > oD");         \r
+       }\r
 }\r
index 5a56c951a5c58064b6e3f2efafcafa6722534a06..9ffbec89f8ed2eb55bcfad2f26a3458f8b6cf906 100644 (file)
@@ -9,12 +9,14 @@ import javax.measure.unit.SI;
 \r
 import org.jscience.physics.amount.Amount;\r
 \r
+import com.billkuker.rocketry.motorsim.Validating;\r
+import com.billkuker.rocketry.motorsim.Validating.ValidationException;\r
 import com.billkuker.rocketry.motorsim.grain.util.BurningShape;\r
 import com.billkuker.rocketry.motorsim.grain.util.ExtrudedShapeGrain;\r
 import com.billkuker.rocketry.motorsim.visual.Editor;\r
 import com.billkuker.rocketry.motorsim.visual.GrainPanel;\r
 \r
-public class Moonburner extends ExtrudedShapeGrain {\r
+public class Moonburner extends ExtrudedShapeGrain implements Validating {\r
 \r
        private Amount<Length> oD = Amount.valueOf(30, SI.MILLIMETER);\r
        private Amount<Length> iD = Amount.valueOf(10, SI.MILLIMETER);\r
@@ -72,7 +74,7 @@ public class Moonburner extends ExtrudedShapeGrain {
                xsection.add(outside);\r
                xsection.inhibit(outside);\r
 \r
-               xsection.subtract(new Ellipse2D.Double(odmm/2 - idmm/2 + offmm, odmm/2 - idmm/2 + offmm, idmm, idmm));\r
+               xsection.subtract(new Ellipse2D.Double(odmm/2 - idmm/2 + offmm, odmm/2 - idmm/2, idmm, idmm));\r
                webThickness = null;\r
        }\r
        \r
@@ -81,5 +83,18 @@ public class Moonburner extends ExtrudedShapeGrain {
                new Editor(e).showAsWindow();\r
                new GrainPanel(e).showAsWindow();\r
        }\r
+       \r
+       public void validate() throws ValidationException{\r
+               if ( iD.equals(Amount.ZERO) )\r
+                       throw new ValidationException(this, "Invalid iD");\r
+               if ( oD.equals(Amount.ZERO) )\r
+                       throw new ValidationException(this, "Invalid oD");\r
+               if ( getLength().equals(Amount.ZERO) )\r
+                       throw new ValidationException(this, "Invalid Length");\r
+               if ( iD.isGreaterThan(oD) )\r
+                       throw new ValidationException(this, "iD > oD");\r
+               if ( coreOffset.isGreaterThan(iD.plus(oD).divide(2.0)))\r
+                       throw new ValidationException(this, "Core offset too large");\r
+       }\r
 \r
 }\r
index ad04b094af7558fb064706782a9ae2a48cb575c7..7d836d57b8a84f6ab59e413fd9784395022a4923 100644 (file)
@@ -17,8 +17,9 @@ import org.jscience.physics.amount.Amount;
 \r
 import com.billkuker.rocketry.motorsim.ChangeListening;\r
 import com.billkuker.rocketry.motorsim.Grain;\r
+import com.billkuker.rocketry.motorsim.Validating;\r
 \r
-public class MultiGrain implements Grain, Grain.Composite, PropertyChangeListener {\r
+public class MultiGrain implements Grain, Grain.Composite, PropertyChangeListener, Validating {\r
        \r
        private Grain grain = null;\r
        private int count = 1;\r
@@ -28,6 +29,8 @@ public class MultiGrain implements Grain, Grain.Composite, PropertyChangeListene
        }\r
 \r
        public void setCount(int count) {\r
+               if ( count <= 0 )\r
+                       throw new IllegalArgumentException("Must have at least 1 grain");\r
                this.count = count;\r
        }\r
        \r
@@ -107,4 +110,10 @@ public class MultiGrain implements Grain, Grain.Composite, PropertyChangeListene
                firePropertyChange(evt);\r
        }\r
 \r
+       @Override\r
+       public void validate() throws ValidationException {\r
+               if ( grain instanceof Validating )\r
+                       ((Validating)grain).validate();\r
+       }\r
+\r
 }\r
index 1bdb53d94f6c05c09ac8354d59e5925a5e98cf26..3ce4dae36ba4a5883131adc674fcebae3dc7e3f7 100644 (file)
@@ -8,10 +8,11 @@ import javax.measure.unit.SI;
 import org.jscience.physics.amount.Amount;\r
 \r
 import com.billkuker.rocketry.motorsim.Grain;\r
+import com.billkuker.rocketry.motorsim.Validating;\r
 import com.billkuker.rocketry.motorsim.visual.Editor;\r
 import com.billkuker.rocketry.motorsim.visual.GrainPanel;\r
 \r
-public class RodAndTubeGrain extends CompoundGrain {\r
+public class RodAndTubeGrain extends CompoundGrain implements Validating {\r
        CoredCylindricalGrain rod, tube;\r
        \r
        public static RodAndTubeGrain DEFAULT_GRAIN = new RodAndTubeGrain(){\r
@@ -109,4 +110,13 @@ public class RodAndTubeGrain extends CompoundGrain {
                new Editor(g).showAsWindow();\r
                new GrainPanel(g).showAsWindow();\r
        }\r
+\r
+       @Override\r
+       public void validate() throws ValidationException {\r
+               rod.validate();\r
+               tube.validate();\r
+               if ( rod.getOD().isGreaterThan(tube.getID()))\r
+                       throw new ValidationException(this, "Rod does not fit inside tube");\r
+               \r
+       }\r
 }\r
index f6443545ca034059210535da6c05c641666c1a8f..16eae0f4e754d7177e515f438d2b683f5038af53 100644 (file)
@@ -20,6 +20,7 @@ import javax.swing.JPanel;
 import javax.swing.JProgressBar;\r
 import javax.swing.JSplitPane;\r
 import javax.swing.JTabbedPane;\r
+import javax.swing.JTextArea;\r
 import javax.swing.JTextField;\r
 import javax.swing.SwingUtilities;\r
 import javax.swing.UIManager;\r
@@ -117,12 +118,14 @@ public class MotorEditor extends JTabbedPane implements PropertyChangeListener {
                                public void run() {\r
                                        final JProgressBar bar = new JProgressBar(0,100);\r
                                        add(bar);\r
+                                       try{\r
                                        final Burn b = new Burn(motor, new Burn.BurnProgressListener(){\r
                                                @Override\r
                                                public void setProgress(float f){\r
                                                        bar.setValue((int)(f*100));\r
                                                }\r
                                        });\r
+                               \r
                                        final BurnPanel bp = new BurnPanel(b);\r
                                        SwingUtilities.invokeLater(new Thread() {\r
                                                public void run() {\r
@@ -132,6 +135,12 @@ public class MotorEditor extends JTabbedPane implements PropertyChangeListener {
                                                        revalidate();\r
                                                }\r
                                        });\r
+                                       } catch ( Exception e ){\r
+                                               remove(bar);\r
+                                               JTextArea t = new JTextArea(e.getMessage());\r
+                                               t.setEditable(false);\r
+                                               add(t);\r
+                                       }\r
                                }\r
                        }.start();\r
                }\r
index 4157d1bc46238db0c8184f0c54fccccdbc3a3c11..dafddca366c14345e34a57e21f1f23d562f290ee 100644 (file)
@@ -1,5 +1,6 @@
 package com.billkuker.rocketry.motorsim.visual.workbench;
 
+import java.awt.Color;
 import java.awt.Component;
 
 import javax.swing.JTree;
@@ -7,26 +8,50 @@ import javax.swing.tree.DefaultMutableTreeNode;
 import javax.swing.tree.DefaultTreeCellRenderer;
 
 import com.billkuker.rocketry.motorsim.Motor;
+import com.billkuker.rocketry.motorsim.Validating;
+import com.billkuker.rocketry.motorsim.Validating.ValidationException;
 
 public class WorkbenchTreeCellRenderer extends DefaultTreeCellRenderer {
        private static final long serialVersionUID = 1L;
 
        @Override
-       public Component getTreeCellRendererComponent(JTree tree, Object value,
+       public Component getTreeCellRendererComponent(JTree tree, final Object value,
                        boolean sel, boolean expanded, boolean leaf, int row,
                        boolean hasFocus) {
-               super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,
-                               row, hasFocus);
+
+               String tip = null;
+               setTextNonSelectionColor(Color.black);
+               setTextSelectionColor(Color.black);
+               
+               Object part = null;
                if (value instanceof DefaultMutableTreeNode) {
-                       value = ((DefaultMutableTreeNode) value).getUserObject();
+                       part = ((DefaultMutableTreeNode) value).getUserObject();
+               }
+               
+               if ( part instanceof Validating ){
+                               try {
+                                       ((Validating)part).validate();
+                               } catch (ValidationException e) {
+                                       setTextSelectionColor(Color.RED);
+                                       setTextNonSelectionColor(Color.RED);
+                                       setToolTipText(e.getMessage());
+                                       tip = e.getMessage();
+                               }
                }
-               if (value instanceof Motor) {
-                       setText(((Motor) value).getName());
-               } else if ( value == null ) {
+               
+               super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,
+                               row, hasFocus);
+
+               if (part instanceof Motor) {
+                       setText(((Motor) part).getName());
+               } else if ( part == null ) {
                        setText("");
                } else {
-                       setText(value.getClass().getSimpleName());
+                       setText(part.getClass().getSimpleName());
                }
+               setToolTipText(tip);
+               
+
 
                return this;
        }
index b24b0aab0e17e1e486be38987c18f4705efea57d..85a0942cbfd7dc15b2c3818016f38d3b0edfd00d 100644 (file)
@@ -51,13 +51,14 @@ public class WorkbenchTreeModel extends DefaultTreeModel {
        
        }
        
-       public class MotorNode extends DefaultMutableTreeNode implements PropertyChangeListener {
+       public class MotorNode extends PartNode implements PropertyChangeListener {
                private static final long serialVersionUID = 1L;
                Motor motor;
                PartNode cn, nn, gn, fn;
 
                public MotorNode(Motor m) {
                        super(m);
+                       setAllowsChildren(true);
                        motor = m;
                        add( cn = new PartNode(m.getChamber()));
                        add( nn = new PartNode(m.getNozzle()));
@@ -88,6 +89,7 @@ public class WorkbenchTreeModel extends DefaultTreeModel {
                        } else {
                                nodeChanged(this);
                        }
+                       super.propertyChange(e);
                }
 
        }