From: Bill Kuker Date: Mon, 8 Nov 2010 11:58:16 +0000 (+0000) Subject: allow chamber type choice X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=c17aa2371f561b6466ce34dea02cc92b4a32263f;p=sw%2Fmotorsim allow chamber type choice --- diff --git a/gui/com/billkuker/rocketry/motorsim/visual/ClassChooser.java b/gui/com/billkuker/rocketry/motorsim/visual/ClassChooser.java new file mode 100644 index 0000000..e37a453 --- /dev/null +++ b/gui/com/billkuker/rocketry/motorsim/visual/ClassChooser.java @@ -0,0 +1,52 @@ +package com.billkuker.rocketry.motorsim.visual; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import javax.swing.DefaultComboBoxModel; +import javax.swing.JComboBox; + +public abstract class ClassChooser extends JComboBox { + private static final long serialVersionUID = 1L; + + private class Element { + private final Class clazz; + Element(Class clazz){ + this.clazz = clazz; + } + @Override + public String toString(){ + return clazz.getSimpleName(); + } + } + + private Map, T> lastVal = new HashMap, T>(); + + private DefaultComboBoxModel model ; + + public ClassChooser(Collection> options, T current){ + super(new DefaultComboBoxModel()); + model = (DefaultComboBoxModel)getModel(); + for ( Class clazz : options){ + Element e = new Element(clazz); + model.addElement(e); + if ( clazz == current.getClass() ) + setSelectedItem(e); + } + lastVal.put( (Class) current.getClass(), current); + + addActionListener(new ActionListener(){ + @Override + public void actionPerformed(ActionEvent e) { + @SuppressWarnings("unchecked") + Class selected = ((Element)getSelectedItem()).clazz; + lastVal.put(selected, classSelected(selected, lastVal.get(selected))); + }}); + + } + + protected abstract T classSelected(Class clazz, T last); +} diff --git a/gui/com/billkuker/rocketry/motorsim/visual/workbench/MotorEditor.java b/gui/com/billkuker/rocketry/motorsim/visual/workbench/MotorEditor.java index 4954ebe..b12815b 100644 --- a/gui/com/billkuker/rocketry/motorsim/visual/workbench/MotorEditor.java +++ b/gui/com/billkuker/rocketry/motorsim/visual/workbench/MotorEditor.java @@ -60,6 +60,7 @@ import com.billkuker.rocketry.motorsim.grain.MultiGrain; import com.billkuker.rocketry.motorsim.grain.RodAndTubeGrain; import com.billkuker.rocketry.motorsim.grain.Star; import com.billkuker.rocketry.motorsim.visual.BurnPanel; +import com.billkuker.rocketry.motorsim.visual.ClassChooser; import com.billkuker.rocketry.motorsim.visual.Editor; import com.billkuker.rocketry.motorsim.visual.GrainPanel; import com.billkuker.rocketry.motorsim.visual.HardwarePanel; @@ -98,6 +99,12 @@ public class MotorEditor extends JPanel implements PropertyChangeListener { grainTypes.add(CSlot.class); grainTypes.add(EndBurner.class); } + + private List> chamberTypes = new Vector>(); + { + chamberTypes.add(CylindricalChamber.class); + chamberTypes.add(Schedule40.class); + } private abstract class Chooser extends JPanel { private static final long serialVersionUID = 1L; @@ -245,8 +252,35 @@ public class MotorEditor extends JPanel implements PropertyChangeListener { private class CaseEditor extends JSplitPane implements ComponentListener { private static final long serialVersionUID = 1L; + + private HardwarePanel hp; + private JPanel casing; + private JPanel nozzle; + private Editor casingEditor; + private Editor nozzleEditor; + + private void setup() { + if (casingEditor != null) + casing.remove(casingEditor); + casing.add(casingEditor = new Editor(motor.getChamber())); + if (nozzleEditor != null) + nozzle.remove(nozzleEditor); + nozzle.add(nozzleEditor = new Editor(motor.getNozzle())); + if (hp != null) + remove(hp); + setBottomComponent(hp = new HardwarePanel(motor.getNozzle(), + motor.getChamber())); + if (motor.getNozzle() instanceof ChangeListening.Subject) { + ((ChangeListening.Subject) motor.getNozzle()) + .addPropertyChangeListener(MotorEditor.this); + } + if (motor.getChamber() instanceof ChangeListening.Subject) { + ((ChangeListening.Subject) motor.getChamber()) + .addPropertyChangeListener(MotorEditor.this); + } + } - public CaseEditor(Nozzle n, Chamber c) { + public CaseEditor() { super(JSplitPane.VERTICAL_SPLIT); setName("General Parameters"); this.addComponentListener(this); @@ -254,7 +288,6 @@ public class MotorEditor extends JPanel implements PropertyChangeListener { JPanel parts = new JPanel(); parts.setLayout(new BoxLayout(parts, BoxLayout.X_AXIS)); setTopComponent(parts); - setBottomComponent(new HardwarePanel(n, c)); JPanel nameAndFuel = new JPanel(); nameAndFuel.setLayout(new BoxLayout(nameAndFuel, BoxLayout.Y_AXIS)); @@ -286,13 +319,12 @@ public class MotorEditor extends JPanel implements PropertyChangeListener { } }); + nameAndFuel.add(new JLabel("Fuel:")); nameAndFuel.add( new JComboBox(availableFuels){ - { - this.setSelectedItem(motor.getFuel()); - } private static final long serialVersionUID = 1L; { + this.setSelectedItem(motor.getFuel()); setMinimumSize(new Dimension(200, 20)); setMaximumSize(new Dimension(Short.MAX_VALUE, 20)); addActionListener(new ActionListener(){ @@ -303,29 +335,58 @@ public class MotorEditor extends JPanel implements PropertyChangeListener { }}); } }); + + nameAndFuel.add(new JLabel("Casing:")); + nameAndFuel.add(new ClassChooser(chamberTypes, motor.getChamber()) { + private static final long serialVersionUID = 1L; + { + setMinimumSize(new Dimension(200, 20)); + setMaximumSize(new Dimension(Short.MAX_VALUE, 20)); + } + @Override + protected Chamber classSelected(Class clazz, Chamber c) { + try { + if ( c != null ){ + motor.setChamber(c); + } else { + motor.setChamber(clazz.newInstance()); + } + return motor.getChamber(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + }); + + nameAndFuel.add(Box.createVerticalGlue()); parts.add(nameAndFuel); - JPanel casing = new JPanel(); + casing = new JPanel(); casing.setLayout(new BoxLayout(casing, BoxLayout.Y_AXIS)); casing.add(new JLabel("Casing:")); - casing.add(new Editor(c)); parts.add(casing); - JPanel nozzle = new JPanel(); + nozzle = new JPanel(); nozzle.setLayout(new BoxLayout(nozzle, BoxLayout.Y_AXIS)); nozzle.add(new JLabel("Nozzle:")); - nozzle.add(new Editor(n)); parts.add(nozzle); - - if (n instanceof ChangeListening.Subject) { - ((ChangeListening.Subject) n) - .addPropertyChangeListener(MotorEditor.this); - } - if (c instanceof ChangeListening.Subject) { - ((ChangeListening.Subject) c) - .addPropertyChangeListener(MotorEditor.this); - } + + motor.addPropertyChangeListener(new PropertyChangeListener() { + @Override + public void propertyChange(PropertyChangeEvent arg0) { + setup(); + setResizeWeight(.5); + setDividerLocation(.5); + } + }); + + setup(); } @Override @@ -376,7 +437,7 @@ public class MotorEditor extends JPanel implements PropertyChangeListener { remove(grainEditor); while (tabs.getTabCount() > 1) tabs.removeTabAt(1); - tabs.add(new CaseEditor(motor.getNozzle(), motor.getChamber()), CASING_TAB); + tabs.add(new CaseEditor(), CASING_TAB); tabs.add(new GrainEditor(motor.getGrain()), GRAIN_TAB); tabs.add(bt = new BurnTab(), BURN_TAB); }