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.SwingUtilities;
17 import javax.swing.event.ChangeEvent;
18 import javax.swing.event.ChangeListener;
20 import net.miginfocom.swing.MigLayout;
21 import net.sf.openrocket.gui.SpinnerEditor;
22 import net.sf.openrocket.gui.adaptors.BooleanModel;
23 import net.sf.openrocket.gui.adaptors.DoubleModel;
24 import net.sf.openrocket.gui.adaptors.EnumModel;
25 import net.sf.openrocket.gui.adaptors.MotorConfigurationModel;
26 import net.sf.openrocket.gui.components.BasicSlider;
27 import net.sf.openrocket.gui.components.StyledLabel;
28 import net.sf.openrocket.gui.components.UnitSelector;
29 import net.sf.openrocket.gui.dialogs.motor.MotorChooserDialog;
30 import net.sf.openrocket.l10n.Translator;
31 import net.sf.openrocket.motor.Motor;
32 import net.sf.openrocket.motor.ThrustCurveMotor;
33 import net.sf.openrocket.rocketcomponent.Configuration;
34 import net.sf.openrocket.rocketcomponent.MotorMount;
35 import net.sf.openrocket.rocketcomponent.MotorMount.IgnitionEvent;
36 import net.sf.openrocket.rocketcomponent.Rocket;
37 import net.sf.openrocket.rocketcomponent.RocketComponent;
38 import net.sf.openrocket.startup.Application;
39 import net.sf.openrocket.unit.UnitGroup;
41 public class MotorConfig extends JPanel {
43 private final Rocket rocket;
44 private final MotorMount mount;
45 private final Configuration configuration;
47 private JLabel motorLabel;
48 private static final Translator trans = Application.getTranslator();
50 public MotorConfig(MotorMount motorMount) {
51 super(new MigLayout("fill"));
53 this.rocket = ((RocketComponent) motorMount).getRocket();
54 this.mount = motorMount;
55 this.configuration = ((RocketComponent) motorMount).getRocket().getDefaultConfiguration();
59 model = new BooleanModel(motorMount, "MotorMount");
60 JCheckBox check = new JCheckBox(model);
61 ////This component is a motor mount
62 check.setText(trans.get("MotorCfg.checkbox.compmotormount"));
63 this.add(check, "wrap");
66 panel = new JPanel(new MigLayout("fill"));
67 this.add(panel, "grow, wrap");
70 // Motor configuration selector
71 //// Motor configuration:
72 panel.add(new JLabel(trans.get("MotorCfg.lbl.Motorcfg")), "shrink");
74 JComboBox combo = new JComboBox(new MotorConfigurationModel(configuration));
75 panel.add(combo, "growx");
77 configuration.addChangeListener(new ChangeListener() {
79 public void stateChanged(ChangeEvent e) {
85 JButton button = new JButton(trans.get("MotorCfg.but.New"));
86 button.addActionListener(new ActionListener() {
88 public void actionPerformed(ActionEvent e) {
89 String id = rocket.newMotorConfigurationID();
90 configuration.setMotorConfigurationID(id);
93 panel.add(button, "wrap unrel");
97 panel.add(new JLabel(trans.get("MotorCfg.lbl.Currentmotor")), "shrink");
99 motorLabel = new JLabel();
100 motorLabel.setFont(motorLabel.getFont().deriveFont(Font.BOLD));
102 panel.add(motorLabel, "wrap unrel");
108 panel.add(new JLabel(trans.get("MotorCfg.lbl.Motoroverhang")));
110 DoubleModel dm = new DoubleModel(motorMount, "MotorOverhang", UnitGroup.UNITS_LENGTH);
112 JSpinner spin = new JSpinner(dm.getSpinnerModel());
113 spin.setEditor(new SpinnerEditor(spin));
114 panel.add(spin, "span, split, width :65lp:");
116 panel.add(new UnitSelector(dm), "width :30lp:");
117 panel.add(new BasicSlider(dm.getSliderModel(-0.02, 0.06)), "w 100lp, wrap unrel");
121 // Select ignition event
123 panel.add(new JLabel(trans.get("MotorCfg.lbl.Ignitionat")), "");
125 combo = new JComboBox(new EnumModel<IgnitionEvent>(mount, "IgnitionEvent"));
126 panel.add(combo, "growx, wrap");
130 panel.add(new JLabel(trans.get("MotorCfg.lbl.plus")), "gap indent, skip 1, span, split");
132 dm = new DoubleModel(mount, "IgnitionDelay", 0);
133 spin = new JSpinner(dm.getSpinnerModel());
134 spin.setEditor(new SpinnerEditor(spin));
135 panel.add(spin, "gap rel rel");
138 panel.add(new JLabel(trans.get("MotorCfg.lbl.seconds")), "wrap unrel");
143 RocketComponent c = (RocketComponent) mount;
145 int stages = c.getChildCount();
148 //// The current design has only one stage.
149 //// Stages can be added by clicking \"New stage\".
151 panel.add(new StyledLabel(trans.get("MotorCfg.lbl.longA1") + " " +
152 trans.get("MotorCfg.lbl.longA2"), -1),
153 "spanx, right, wrap para");
155 //// The current design has
157 panel.add(new StyledLabel(trans.get("MotorCfg.lbl.longB1") + " " + stages + " " +
158 trans.get("MotorCfg.lbl.longB2"), -1),
159 "skip 1, spanx, wrap para");
163 // Select etc. buttons
165 button = new JButton(trans.get("MotorCfg.but.Selectmotor"));
166 button.addActionListener(new ActionListener() {
168 public void actionPerformed(ActionEvent e) {
169 String id = configuration.getMotorConfigurationID();
171 MotorChooserDialog dialog = new MotorChooserDialog(mount.getMotor(id),
172 mount.getMotorDelay(id), mount.getMotorMountDiameter(),
173 SwingUtilities.getWindowAncestor(MotorConfig.this));
174 dialog.setVisible(true);
175 Motor m = dialog.getSelectedMotor();
176 double d = dialog.getSelectedDelay();
180 id = rocket.newMotorConfigurationID();
181 configuration.setMotorConfigurationID(id);
183 mount.setMotor(id, m);
184 mount.setMotorDelay(id, d);
189 panel.add(button, "span, split, growx");
192 button = new JButton(trans.get("MotorCfg.but.Removemotor"));
193 button.addActionListener(new ActionListener() {
195 public void actionPerformed(ActionEvent e) {
196 mount.setMotor(configuration.getMotorConfigurationID(), null);
200 panel.add(button, "growx, wrap");
206 // Set enabled status
208 setDeepEnabled(panel, motorMount.isMotorMount());
209 check.addChangeListener(new ChangeListener() {
211 public void stateChanged(ChangeEvent e) {
212 setDeepEnabled(panel, mount.isMotorMount());
218 public void updateFields() {
219 String id = configuration.getMotorConfigurationID();
220 Motor m = mount.getMotor(id);
223 motorLabel.setText(trans.get("MotorCfg.lbl.motorLabel"));
226 if (m instanceof ThrustCurveMotor)
227 str = ((ThrustCurveMotor) m).getManufacturer() + " ";
228 str += m.getDesignation(mount.getMotorDelay(id));
229 motorLabel.setText(str);
234 private static void setDeepEnabled(Component component, boolean enabled) {
235 component.setEnabled(enabled);
236 if (component instanceof Container) {
237 for (Component c : ((Container) component).getComponents()) {
238 setDeepEnabled(c, enabled);