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.UnitSelector;
27 import net.sf.openrocket.gui.dialogs.MotorChooserDialog;
28 import net.sf.openrocket.motor.Motor;
29 import net.sf.openrocket.rocketcomponent.Configuration;
30 import net.sf.openrocket.rocketcomponent.MotorMount;
31 import net.sf.openrocket.rocketcomponent.Rocket;
32 import net.sf.openrocket.rocketcomponent.RocketComponent;
33 import net.sf.openrocket.rocketcomponent.MotorMount.IgnitionEvent;
34 import net.sf.openrocket.unit.UnitGroup;
36 public class MotorConfig extends JPanel {
38 private final Rocket rocket;
39 private final MotorMount mount;
40 private final Configuration configuration;
42 private JLabel motorLabel;
44 public MotorConfig(MotorMount motorMount) {
45 super(new MigLayout("fill"));
47 this.rocket = ((RocketComponent)motorMount).getRocket();
48 this.mount = motorMount;
49 this.configuration = ((RocketComponent)motorMount).getRocket()
50 .getDefaultConfiguration();
54 model = new BooleanModel(motorMount, "MotorMount");
55 JCheckBox check = new JCheckBox(model);
56 check.setText("This component is a motor mount");
57 this.add(check,"wrap");
60 panel = new JPanel(new MigLayout("fill"));
61 this.add(panel,"grow, wrap");
64 // Motor configuration selector
65 panel.add(new JLabel("Motor configuration:"), "shrink");
67 JComboBox combo = new JComboBox(new MotorConfigurationModel(configuration));
68 panel.add(combo,"growx");
70 configuration.addChangeListener(new ChangeListener() {
72 public void stateChanged(ChangeEvent e) {
77 JButton button = new JButton("New");
78 button.addActionListener(new ActionListener() {
80 public void actionPerformed(ActionEvent e) {
81 String id = rocket.newMotorConfigurationID();
82 configuration.setMotorConfigurationID(id);
85 panel.add(button, "wrap unrel");
89 panel.add(new JLabel("Current motor:"), "shrink");
91 motorLabel = new JLabel();
92 motorLabel.setFont(motorLabel.getFont().deriveFont(Font.BOLD));
94 panel.add(motorLabel,"wrap unrel");
99 panel.add(new JLabel("Motor overhang:"));
101 DoubleModel m = new DoubleModel(motorMount, "MotorOverhang", UnitGroup.UNITS_LENGTH);
103 JSpinner spin = new JSpinner(m.getSpinnerModel());
104 spin.setEditor(new SpinnerEditor(spin));
105 panel.add(spin,"span, split, width :65lp:");
107 panel.add(new UnitSelector(m),"width :30lp:");
108 panel.add(new BasicSlider(m.getSliderModel(-0.02,0.06)),"w 100lp, wrap unrel");
112 // Select ignition event
113 panel.add(new JLabel("Ignition at:"),"");
115 combo = new JComboBox(new EnumModel<IgnitionEvent>(mount, "IgnitionEvent"));
116 panel.add(combo,"growx, wrap");
119 panel.add(new JLabel("plus"),"gap indent, skip 1, span, split");
121 m = new DoubleModel(mount,"IgnitionDelay",0);
122 spin = new JSpinner(m.getSpinnerModel());
123 spin.setEditor(new SpinnerEditor(spin));
124 panel.add(spin,"gap rel rel");
126 panel.add(new JLabel("seconds"),"wrap paragraph");
131 // Select etc. buttons
132 button = new JButton("Select motor");
133 button.addActionListener(new ActionListener() {
135 public void actionPerformed(ActionEvent e) {
136 String id = configuration.getMotorConfigurationID();
138 MotorChooserDialog dialog = new MotorChooserDialog(mount.getMotor(id),
139 mount.getMotorDelay(id), mount.getMotorMountDiameter());
140 dialog.setVisible(true);
141 Motor m = dialog.getSelectedMotor();
142 double d = dialog.getSelectedDelay();
146 id = rocket.newMotorConfigurationID();
147 configuration.setMotorConfigurationID(id);
149 mount.setMotor(id, m);
150 mount.setMotorDelay(id, d);
155 panel.add(button,"span, split, grow");
157 button = new JButton("Remove motor");
158 button.addActionListener(new ActionListener() {
160 public void actionPerformed(ActionEvent e) {
161 mount.setMotor(configuration.getMotorConfigurationID(), null);
165 panel.add(button,"grow, wrap");
171 // Set enabled status
173 setDeepEnabled(panel, motorMount.isMotorMount());
174 check.addChangeListener(new ChangeListener() {
176 public void stateChanged(ChangeEvent e) {
177 setDeepEnabled(panel, mount.isMotorMount());
183 public void updateFields() {
184 String id = configuration.getMotorConfigurationID();
185 Motor m = mount.getMotor(id);
187 motorLabel.setText("None");
189 motorLabel.setText(m.getManufacturer().getDisplayName() + " " +
190 m.getDesignation(mount.getMotorDelay(id)));
194 private static void setDeepEnabled(Component component, boolean enabled) {
195 component.setEnabled(enabled);
196 if (component instanceof Container) {
197 for (Component c: ((Container) component).getComponents()) {
198 setDeepEnabled(c,enabled);