1 package com.billkuker.rocketry.motorsim.visual.workbench;
\r
3 import java.awt.BorderLayout;
\r
4 import java.awt.Dimension;
\r
5 import java.awt.event.ActionEvent;
\r
6 import java.awt.event.ActionListener;
\r
7 import java.awt.event.ComponentEvent;
\r
8 import java.awt.event.ComponentListener;
\r
9 import java.awt.event.FocusEvent;
\r
10 import java.awt.event.FocusListener;
\r
11 import java.beans.PropertyChangeEvent;
\r
12 import java.beans.PropertyChangeListener;
\r
13 import java.beans.PropertyVetoException;
\r
14 import java.util.Collection;
\r
15 import java.util.List;
\r
16 import java.util.Vector;
\r
18 import javax.measure.unit.SI;
\r
19 import javax.swing.Box;
\r
20 import javax.swing.BoxLayout;
\r
21 import javax.swing.DefaultComboBoxModel;
\r
22 import javax.swing.JComboBox;
\r
23 import javax.swing.JFrame;
\r
24 import javax.swing.JLabel;
\r
25 import javax.swing.JPanel;
\r
26 import javax.swing.JSplitPane;
\r
27 import javax.swing.JTabbedPane;
\r
28 import javax.swing.JTextArea;
\r
29 import javax.swing.JTextField;
\r
30 import javax.swing.SwingUtilities;
\r
31 import javax.swing.UIManager;
\r
32 import javax.swing.WindowConstants;
\r
34 import org.apache.log4j.Logger;
\r
35 import org.jscience.physics.amount.Amount;
\r
37 import com.billkuker.rocketry.motorsim.Burn;
\r
38 import com.billkuker.rocketry.motorsim.Chamber;
\r
39 import com.billkuker.rocketry.motorsim.ChangeListening;
\r
40 import com.billkuker.rocketry.motorsim.ConvergentDivergentNozzle;
\r
41 import com.billkuker.rocketry.motorsim.CylindricalChamber;
\r
42 import com.billkuker.rocketry.motorsim.Fuel;
\r
43 import com.billkuker.rocketry.motorsim.Grain;
\r
44 import com.billkuker.rocketry.motorsim.Motor;
\r
45 import com.billkuker.rocketry.motorsim.Nozzle;
\r
46 import com.billkuker.rocketry.motorsim.cases.Schedule40;
\r
47 import com.billkuker.rocketry.motorsim.fuel.KNSU;
\r
48 import com.billkuker.rocketry.motorsim.grain.CSlot;
\r
49 import com.billkuker.rocketry.motorsim.grain.CoredCylindricalGrain;
\r
50 import com.billkuker.rocketry.motorsim.grain.EndBurner;
\r
51 import com.billkuker.rocketry.motorsim.grain.Finocyl;
\r
52 import com.billkuker.rocketry.motorsim.grain.Moonburner;
\r
53 import com.billkuker.rocketry.motorsim.grain.MultiGrain;
\r
54 import com.billkuker.rocketry.motorsim.grain.RodAndTubeGrain;
\r
55 import com.billkuker.rocketry.motorsim.grain.Star;
\r
56 import com.billkuker.rocketry.motorsim.visual.BurnPanel;
\r
57 import com.billkuker.rocketry.motorsim.visual.ClassChooser;
\r
58 import com.billkuker.rocketry.motorsim.visual.Editor;
\r
59 import com.billkuker.rocketry.motorsim.visual.GrainPanel;
\r
60 import com.billkuker.rocketry.motorsim.visual.HardwarePanel;
\r
61 import com.billkuker.rocketry.motorsim.visual.SummaryPanel;
\r
63 public class MotorEditor extends JPanel implements PropertyChangeListener {
\r
64 private static final long serialVersionUID = 1L;
\r
65 private static Logger log = Logger.getLogger(MotorEditor.class);
\r
67 GrainEditor grainEditor;
\r
74 private Vector<BurnWatcher> burnWatchers = new Vector<BurnWatcher>();
\r
75 private DefaultComboBoxModel availableFuels = new DefaultComboBoxModel();
\r
77 public void addFuel(Fuel f){
\r
78 availableFuels.addElement(f);
\r
81 //private static final int XML_TAB = 0;
\r
82 private static final int CASING_TAB = 0;
\r
83 private static final int GRAIN_TAB = 1;
\r
84 private static final int BURN_TAB = 2;
\r
86 private List<Class<? extends Grain>> grainTypes = new Vector<Class<? extends Grain>>();
\r
88 grainTypes.add(CoredCylindricalGrain.class);
\r
89 grainTypes.add(Finocyl.class);
\r
90 grainTypes.add(Star.class);
\r
91 grainTypes.add(Moonburner.class);
\r
92 grainTypes.add(RodAndTubeGrain.class);
\r
93 grainTypes.add(CSlot.class);
\r
94 grainTypes.add(EndBurner.class);
\r
97 private List<Class<? extends Chamber>> chamberTypes = new Vector<Class<? extends Chamber>>();
\r
99 chamberTypes.add(CylindricalChamber.class);
\r
100 chamberTypes.add(Schedule40.class);
\r
103 private class BurnTab extends JPanel {
\r
104 private static final long serialVersionUID = 1L;
\r
105 private Thread currentThread;
\r
108 setLayout(new BorderLayout());
\r
109 setName("Simulation Results");
\r
113 private class BurnCanceled extends RuntimeException{
\r
114 private static final long serialVersionUID = 1L;
\r
117 public void reBurn() {
\r
119 currentThread = new Thread() {
\r
120 public void run() {
\r
121 final Thread me = this;
\r
123 final Burn b = new Burn(motor);
\r
124 b.addBurnProgressListener(
\r
125 new Burn.BurnProgressListener() {
\r
127 public void burnComplete(){};
\r
129 public void setProgress(float f) {
\r
130 if ( currentThread != me ){
\r
131 throw new BurnCanceled();
\r
136 MotorEditor.this.remove(sp);
\r
137 MotorEditor.this.add(sp = new SummaryPanel(b), BorderLayout.NORTH);
\r
141 final BurnPanel bp = new BurnPanel(b);
\r
142 SwingUtilities.invokeLater(new Thread() {
\r
143 public void run() {
\r
144 add(bp, BorderLayout.CENTER);
\r
145 for (BurnWatcher bw : burnWatchers)
\r
146 bw.replace(burn, b);
\r
151 } catch (BurnCanceled c){
\r
152 log.info("Burn Canceled!");
\r
153 } catch (Exception e) {
\r
154 JTextArea t = new JTextArea(e.getMessage());
\r
155 t.setEditable(false);
\r
160 currentThread.start();
\r
164 private class GrainEditor extends JSplitPane {
\r
165 private static final long serialVersionUID = 1L;
\r
167 public GrainEditor(final Grain g) {
\r
168 super(JSplitPane.HORIZONTAL_SPLIT);
\r
169 setName("Grain Geometry");
\r
170 setRightComponent(new GrainPanel(g));
\r
171 if (g instanceof Grain.Composite) {
\r
172 final JPanel p = new JPanel();
\r
173 p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
\r
175 Editor grainEditor = new Editor(g);
\r
176 grainEditor.setAlignmentX(LEFT_ALIGNMENT);
\r
177 p.add(grainEditor);
\r
179 for (Grain gg : ((Grain.Composite) g).getGrains()) {
\r
180 final int grainEditorIndex = p.getComponentCount() + 2;
\r
182 JLabel l = new JLabel("Grain Type:");
\r
183 l.setAlignmentX(LEFT_ALIGNMENT);
\r
186 p.add(new ClassChooser<Grain>(grainTypes, gg) {
\r
187 private static final long serialVersionUID = 1L;
\r
188 {setAlignmentX(LEFT_ALIGNMENT);}
\r
190 protected Grain classSelected(
\r
191 Class<? extends Grain> clazz, Grain ng) {
\r
194 ng = clazz.newInstance();
\r
195 } catch (InstantiationException e) {
\r
196 e.printStackTrace();
\r
197 } catch (IllegalAccessException e) {
\r
198 e.printStackTrace();
\r
201 if (g instanceof MultiGrain) {
\r
202 ((MultiGrain) g).setGrain(ng);
\r
203 p.remove(grainEditorIndex);
\r
204 p.add(new Editor(ng), grainEditorIndex);
\r
206 p.add(new Editor(g), 0);
\r
213 Editor ggEditor = new Editor(gg);
\r
214 ggEditor.setAlignmentX(LEFT_ALIGNMENT);
\r
217 if (gg instanceof ChangeListening.Subject) {
\r
218 ((ChangeListening.Subject) gg)
\r
219 .addPropertyChangeListener(MotorEditor.this);
\r
222 setLeftComponent(p);
\r
224 setLeftComponent(new Editor(g));
\r
226 // setDividerLocation(.25);
\r
227 // setResizeWeight(.25);
\r
228 if (g instanceof ChangeListening.Subject) {
\r
229 ((ChangeListening.Subject) g)
\r
230 .addPropertyChangeListener(MotorEditor.this);
\r
235 private class CaseEditor extends JSplitPane implements ComponentListener {
\r
236 private static final long serialVersionUID = 1L;
\r
238 private HardwarePanel hp;
\r
239 private JPanel casing;
\r
240 private JPanel nozzle;
\r
241 private Editor casingEditor;
\r
242 private Editor nozzleEditor;
\r
244 private void setup() {
\r
245 if (casingEditor != null)
\r
246 casing.remove(casingEditor);
\r
247 casingEditor = new Editor(motor.getChamber());
\r
248 casingEditor.setAlignmentX(LEFT_ALIGNMENT);
\r
249 casing.add(casingEditor);
\r
251 if (nozzleEditor != null)
\r
252 nozzle.remove(nozzleEditor);
\r
253 nozzleEditor = new Editor(motor.getNozzle());
\r
254 nozzleEditor.setAlignmentX(LEFT_ALIGNMENT);
\r
255 nozzle.add(nozzleEditor);
\r
259 setBottomComponent(hp = new HardwarePanel(motor.getNozzle(),
\r
260 motor.getChamber()));
\r
261 if (motor.getNozzle() instanceof ChangeListening.Subject) {
\r
262 ((ChangeListening.Subject) motor.getNozzle())
\r
263 .addPropertyChangeListener(MotorEditor.this);
\r
265 if (motor.getChamber() instanceof ChangeListening.Subject) {
\r
266 ((ChangeListening.Subject) motor.getChamber())
\r
267 .addPropertyChangeListener(MotorEditor.this);
\r
271 public CaseEditor() {
\r
272 super(JSplitPane.VERTICAL_SPLIT);
\r
273 setName("General Parameters");
\r
274 this.addComponentListener(this);
\r
276 JPanel parts = new JPanel();
\r
277 parts.setLayout(new BoxLayout(parts, BoxLayout.X_AXIS));
\r
278 setTopComponent(parts);
\r
280 JPanel nameAndFuel = new JPanel();
\r
281 nameAndFuel.setLayout(new BoxLayout(nameAndFuel, BoxLayout.Y_AXIS));
\r
283 JLabel l = new JLabel("Name:");
\r
284 l.setAlignmentX(LEFT_ALIGNMENT);
\r
285 nameAndFuel.add(l);
\r
286 nameAndFuel.add(new JTextField(motor.getName()) {
\r
287 private static final long serialVersionUID = 1L;
\r
289 setAlignmentX(LEFT_ALIGNMENT);
\r
290 setMinimumSize(new Dimension(200, 20));
\r
291 setMaximumSize(new Dimension(Short.MAX_VALUE, 20));
\r
292 final JTextField t = this;
\r
293 addFocusListener(new FocusListener() {
\r
296 public void focusLost(FocusEvent e) {
\r
297 String n = t.getText();
\r
298 if (!"".equals(n) && !n.equals(motor.getName())) {
\r
301 t.setText(motor.getName());
\r
306 public void focusGained(FocusEvent e) {
\r
314 l = new JLabel("Fuel:");
\r
315 l.setAlignmentX(LEFT_ALIGNMENT);
\r
316 nameAndFuel.add(l);
\r
318 nameAndFuel.add( new JComboBox(availableFuels){
\r
319 private static final long serialVersionUID = 1L;
\r
321 setAlignmentX(LEFT_ALIGNMENT);
\r
322 this.setSelectedItem(motor.getFuel());
\r
323 setMinimumSize(new Dimension(200, 20));
\r
324 setMaximumSize(new Dimension(Short.MAX_VALUE, 20));
\r
325 addActionListener(new ActionListener(){
\r
327 public void actionPerformed(ActionEvent e) {
\r
328 motor.setFuel((Fuel)getSelectedItem());
\r
329 System.out.println("FUEL CHANGED");
\r
334 l = new JLabel("Casing:");
\r
335 l.setAlignmentX(LEFT_ALIGNMENT);
\r
336 nameAndFuel.add(l);
\r
338 nameAndFuel.add(new ClassChooser<Chamber>(chamberTypes, motor.getChamber()) {
\r
339 private static final long serialVersionUID = 1L;
\r
341 setAlignmentX(LEFT_ALIGNMENT);
\r
342 setMinimumSize(new Dimension(200, 20));
\r
343 setMaximumSize(new Dimension(Short.MAX_VALUE, 20));
\r
346 protected Chamber classSelected(Class<? extends Chamber> clazz, Chamber c) {
\r
349 motor.setChamber(c);
\r
351 motor.setChamber(clazz.newInstance());
\r
353 return motor.getChamber();
\r
354 } catch (InstantiationException e) {
\r
355 // TODO Auto-generated catch block
\r
356 e.printStackTrace();
\r
357 } catch (IllegalAccessException e) {
\r
358 // TODO Auto-generated catch block
\r
359 e.printStackTrace();
\r
366 nameAndFuel.add(Box.createVerticalGlue());
\r
367 parts.add(nameAndFuel);
\r
369 casing = new JPanel();
\r
370 casing.setLayout(new BoxLayout(casing, BoxLayout.Y_AXIS));
\r
371 l = new JLabel("Casing:");
\r
372 l.setAlignmentX(LEFT_ALIGNMENT);
\r
376 nozzle = new JPanel();
\r
377 nozzle.setLayout(new BoxLayout(nozzle, BoxLayout.Y_AXIS));
\r
378 l = new JLabel("Nozzle:");
\r
379 l.setAlignmentX(LEFT_ALIGNMENT);
\r
383 motor.addPropertyChangeListener(new PropertyChangeListener() {
\r
385 public void propertyChange(PropertyChangeEvent arg0) {
\r
387 setResizeWeight(.5);
\r
388 setDividerLocation(.5);
\r
396 public void componentHidden(ComponentEvent arg0) {
\r
401 public void componentMoved(ComponentEvent arg0) {
\r
406 public void componentResized(ComponentEvent arg0) {
\r
407 setResizeWeight(.5);
\r
408 setDividerLocation(.5);
\r
412 public void componentShown(ComponentEvent arg0) {
\r
418 public MotorEditor(Motor m, Collection<Fuel> fuels) {
\r
420 setLayout( new BorderLayout());
\r
421 tabs = new JTabbedPane(JTabbedPane.BOTTOM);
\r
422 add(tabs, BorderLayout.CENTER);
\r
424 for ( Fuel f : fuels )
\r
429 public Motor getMotor() {
\r
434 private void setMotor(Motor m) {
\r
436 motor.removePropertyChangeListener(this);
\r
438 motor.addPropertyChangeListener(this);
\r
439 if (grainEditor != null)
\r
440 remove(grainEditor);
\r
441 while (tabs.getTabCount() > 1)
\r
442 tabs.removeTabAt(1);
\r
443 tabs.add(new CaseEditor(), CASING_TAB);
\r
444 tabs.add(new GrainEditor(motor.getGrain()), GRAIN_TAB);
\r
445 tabs.add(bt = new BurnTab(), BURN_TAB);
\r
448 public static Motor defaultMotor() {
\r
449 Motor m = new Motor();
\r
450 m.setName("Example Motor");
\r
451 m.setFuel(new KNSU());
\r
453 CylindricalChamber c = new CylindricalChamber();
\r
454 c.setLength(Amount.valueOf(200, SI.MILLIMETER));
\r
455 c.setID(Amount.valueOf(30, SI.MILLIMETER));
\r
458 Schedule40 pvc = new Schedule40();
\r
459 pvc.setLength(Amount.valueOf(200, SI.MILLIMETER));
\r
462 CoredCylindricalGrain g = new CoredCylindricalGrain();
\r
464 g.setLength(Amount.valueOf(70, SI.MILLIMETER));
\r
465 g.setOD(Amount.valueOf(30, SI.MILLIMETER));
\r
466 g.setID(Amount.valueOf(10, SI.MILLIMETER));
\r
467 } catch (PropertyVetoException v) {
\r
468 throw new Error(v);
\r
472 m.setGrain(new MultiGrain(g, 2));
\r
474 ConvergentDivergentNozzle n = new ConvergentDivergentNozzle();
\r
475 n.setThroatDiameter(Amount.valueOf(7.962, SI.MILLIMETER));
\r
476 n.setExitDiameter(Amount.valueOf(13.79, SI.MILLIMETER));
\r
477 n.setEfficiency(.85);
\r
483 public void focusOnObject(Object o) {
\r
484 if (o instanceof Grain)
\r
485 tabs.setSelectedIndex(GRAIN_TAB);
\r
486 if (o instanceof Chamber || o instanceof Nozzle)
\r
487 tabs.setSelectedIndex(CASING_TAB);
\r
490 public void addBurnWatcher(BurnWatcher bw) {
\r
491 burnWatchers.add(bw);
\r
495 public void showAsWindow() {
\r
496 JFrame f = new JFrame();
\r
497 f.setSize(1024, 768);
\r
498 f.setContentPane(this);
\r
499 f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
\r
500 f.setVisible(true);
\r
503 public static void main(String args[]) throws Exception {
\r
505 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
\r
506 } catch (Exception e1) {
\r
507 e1.printStackTrace();
\r
509 Vector<Fuel> ff = new Vector<Fuel>();
\r
510 ff.add(new KNSU());
\r
511 //new MotorEditor(defaultMotor(), ff).showAsWindow();
\r
514 public void propertyChange(PropertyChangeEvent evt) {
\r
515 // Dont re-burn for a name change!
\r
516 if (!evt.getPropertyName().equals("Name")){
\r
519 for (BurnWatcher bw : burnWatchers)
\r
520 bw.replace(burn, burn);
\r