1 package net.sf.openrocket.gui.configdialog;
4 import java.awt.Component;
5 import java.awt.Container;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
10 import javax.swing.JButton;
11 import javax.swing.JCheckBox;
12 import javax.swing.JComboBox;
13 import javax.swing.JLabel;
14 import javax.swing.JPanel;
15 import javax.swing.JSpinner;
16 import javax.swing.event.ChangeEvent;
17 import javax.swing.event.ChangeListener;
19 import net.miginfocom.swing.MigLayout;
20 import net.sf.openrocket.gui.SpinnerEditor;
21 import net.sf.openrocket.gui.adaptors.BooleanModel;
22 import net.sf.openrocket.gui.adaptors.DoubleModel;
23 import net.sf.openrocket.gui.adaptors.EnumModel;
24 import net.sf.openrocket.gui.adaptors.MotorConfigurationModel;
25 import net.sf.openrocket.gui.components.BasicSlider;
26 import net.sf.openrocket.gui.components.StyledLabel;
27 import net.sf.openrocket.gui.components.UnitSelector;
28 import net.sf.openrocket.gui.dialogs.MotorChooserDialog;
29 import net.sf.openrocket.motor.Motor;
30 import net.sf.openrocket.rocketcomponent.Configuration;
31 import net.sf.openrocket.rocketcomponent.MotorMount;
32 import net.sf.openrocket.rocketcomponent.Rocket;
33 import net.sf.openrocket.rocketcomponent.RocketComponent;
34 import net.sf.openrocket.rocketcomponent.MotorMount.IgnitionEvent;
35 import net.sf.openrocket.unit.UnitGroup;
37 public class MotorConfig extends JPanel {
39 private final Rocket rocket;
40 private final MotorMount mount;
41 private final Configuration configuration;
43 private JLabel motorLabel;
45 public MotorConfig(MotorMount motorMount) {
46 super(new MigLayout("fill"));
48 this.rocket = ((RocketComponent)motorMount).getRocket();
49 this.mount = motorMount;
50 this.configuration = ((RocketComponent)motorMount).getRocket()
51 .getDefaultConfiguration();
55 model = new BooleanModel(motorMount, "MotorMount");
56 JCheckBox check = new JCheckBox(model);
57 check.setText("This component is a motor mount");
58 this.add(check,"wrap");
61 panel = new JPanel(new MigLayout("fill"));
62 this.add(panel,"grow, wrap");
65 // Motor configuration selector
66 panel.add(new JLabel("Motor configuration:"), "shrink");
68 JComboBox combo = new JComboBox(new MotorConfigurationModel(configuration));
69 panel.add(combo,"growx");
71 configuration.addChangeListener(new ChangeListener() {
73 public void stateChanged(ChangeEvent e) {
78 JButton button = new JButton("New");
79 button.addActionListener(new ActionListener() {
81 public void actionPerformed(ActionEvent e) {
82 String id = rocket.newMotorConfigurationID();
83 configuration.setMotorConfigurationID(id);
86 panel.add(button, "wrap unrel");
90 panel.add(new JLabel("Current motor:"), "shrink");
92 motorLabel = new JLabel();
93 motorLabel.setFont(motorLabel.getFont().deriveFont(Font.BOLD));
95 panel.add(motorLabel,"wrap unrel");
100 panel.add(new JLabel("Motor overhang:"));
102 DoubleModel m = new DoubleModel(motorMount, "MotorOverhang", UnitGroup.UNITS_LENGTH);
104 JSpinner spin = new JSpinner(m.getSpinnerModel());
105 spin.setEditor(new SpinnerEditor(spin));
106 panel.add(spin,"span, split, width :65lp:");
108 panel.add(new UnitSelector(m),"width :30lp:");
109 panel.add(new BasicSlider(m.getSliderModel(-0.02,0.06)),"w 100lp, wrap unrel");
113 // Select ignition event
114 panel.add(new JLabel("Ignition at:"),"");
116 combo = new JComboBox(new EnumModel<IgnitionEvent>(mount, "IgnitionEvent"));
117 panel.add(combo,"growx, wrap");
120 panel.add(new JLabel("plus"),"gap indent, skip 1, span, split");
122 m = new DoubleModel(mount,"IgnitionDelay",0);
123 spin = new JSpinner(m.getSpinnerModel());
124 spin.setEditor(new SpinnerEditor(spin));
125 panel.add(spin,"gap rel rel");
127 panel.add(new JLabel("seconds"),"wrap unrel");
132 RocketComponent c = (RocketComponent)mount;
134 int stages = c.getChildCount();
137 panel.add(new StyledLabel("The current design has only one stage. " +
138 "Stages can be added by clicking \"New stage\".", -1),
139 "spanx, right, wrap para");
141 panel.add(new StyledLabel("The current design has " + stages + " stages.", -1),
142 "skip 1, spanx, wrap para");
146 // Select etc. buttons
147 button = new JButton("Select motor");
148 button.addActionListener(new ActionListener() {
150 public void actionPerformed(ActionEvent e) {
151 String id = configuration.getMotorConfigurationID();
153 MotorChooserDialog dialog = new MotorChooserDialog(mount.getMotor(id),
154 mount.getMotorDelay(id), mount.getMotorMountDiameter());
155 dialog.setVisible(true);
156 Motor m = dialog.getSelectedMotor();
157 double d = dialog.getSelectedDelay();
161 id = rocket.newMotorConfigurationID();
162 configuration.setMotorConfigurationID(id);
164 mount.setMotor(id, m);
165 mount.setMotorDelay(id, d);
170 panel.add(button,"span, split, growx");
172 button = new JButton("Remove motor");
173 button.addActionListener(new ActionListener() {
175 public void actionPerformed(ActionEvent e) {
176 mount.setMotor(configuration.getMotorConfigurationID(), null);
180 panel.add(button,"growx, wrap");
186 // Set enabled status
188 setDeepEnabled(panel, motorMount.isMotorMount());
189 check.addChangeListener(new ChangeListener() {
191 public void stateChanged(ChangeEvent e) {
192 setDeepEnabled(panel, mount.isMotorMount());
198 public void updateFields() {
199 String id = configuration.getMotorConfigurationID();
200 Motor m = mount.getMotor(id);
202 motorLabel.setText("None");
204 motorLabel.setText(m.getManufacturer().getDisplayName() + " " +
205 m.getDesignation(mount.getMotorDelay(id)));
209 private static void setDeepEnabled(Component component, boolean enabled) {
210 component.setEnabled(enabled);
211 if (component instanceof Container) {
212 for (Component c: ((Container) component).getComponents()) {
213 setDeepEnabled(c,enabled);