Bug fixes and startup checks
[debian/openrocket] / src / net / sf / openrocket / gui / configdialog / MotorConfig.java
1 package net.sf.openrocket.gui.configdialog;
2
3
4 import java.awt.Component;
5 import java.awt.Container;
6 import java.awt.Font;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9
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;
18
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.main.MotorChooserDialog;
28 import net.sf.openrocket.rocketcomponent.Configuration;
29 import net.sf.openrocket.rocketcomponent.Motor;
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;
35
36 public class MotorConfig extends JPanel {
37
38         private final Rocket rocket;
39         private final MotorMount mount;
40         private final Configuration configuration;
41         private JPanel panel;
42         private JLabel motorLabel;
43         
44         public MotorConfig(MotorMount motorMount) {
45                 super(new MigLayout("fill"));
46
47                 this.rocket = ((RocketComponent)motorMount).getRocket();
48                 this.mount = motorMount;
49                 this.configuration = ((RocketComponent)motorMount).getRocket()
50                         .getDefaultConfiguration();
51                 
52                 BooleanModel model;
53                 
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");
58                 
59                 
60                 panel = new JPanel(new MigLayout("fill"));
61                 this.add(panel,"grow, wrap");
62                 
63
64                 // Motor configuration selector
65                 panel.add(new JLabel("Motor configuration:"), "shrink");
66                 
67                 JComboBox combo = new JComboBox(new MotorConfigurationModel(configuration));
68                 panel.add(combo,"growx");
69                 
70                 configuration.addChangeListener(new ChangeListener() {
71                         @Override
72                         public void stateChanged(ChangeEvent e) {
73                                 updateFields();
74                         }
75                 });
76                 
77                 JButton button = new JButton("New");
78                 button.addActionListener(new ActionListener() {
79                         @Override
80                         public void actionPerformed(ActionEvent e) {
81                                 String id = rocket.newMotorConfigurationID();
82                                 configuration.setMotorConfigurationID(id);
83                         }
84                 });
85                 panel.add(button, "wrap unrel");
86                 
87                 
88                 // Current motor
89                 panel.add(new JLabel("Current motor:"), "shrink");
90                 
91                 motorLabel = new JLabel();
92                 motorLabel.setFont(motorLabel.getFont().deriveFont(Font.BOLD));
93                 updateFields();
94                 panel.add(motorLabel,"wrap unrel");
95
96                 
97                 
98                 //  Overhang
99                 panel.add(new JLabel("Motor overhang:"));
100                 
101                 DoubleModel m = new DoubleModel(motorMount, "MotorOverhang", UnitGroup.UNITS_LENGTH);
102                 
103                 JSpinner spin = new JSpinner(m.getSpinnerModel());
104                 spin.setEditor(new SpinnerEditor(spin));
105                 panel.add(spin,"span, split, width :65lp:");
106                 
107                 panel.add(new UnitSelector(m),"width :30lp:");
108                 panel.add(new BasicSlider(m.getSliderModel(-0.02,0.06)),"w 100lp, wrap unrel");
109
110
111                 
112                 // Select ignition event
113                 panel.add(new JLabel("Ignition at:"),"");
114                 
115                 combo = new JComboBox(new EnumModel<IgnitionEvent>(mount, "IgnitionEvent"));
116                 panel.add(combo,"growx, wrap");
117                 
118                 // ... and delay
119                 panel.add(new JLabel("plus"),"gap indent, skip 1, span, split");
120                 
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");
125                 
126                 panel.add(new JLabel("seconds"),"wrap paragraph");
127
128
129                 
130                 
131                 // Select etc. buttons
132                 button = new JButton("Select motor");
133                 button.addActionListener(new ActionListener() {
134                         @Override
135                         public void actionPerformed(ActionEvent e) {
136                                 String id = configuration.getMotorConfigurationID();
137                                 
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();
143                                 
144                                 if (m != null) {
145                                         if (id == null) {
146                                                 id = rocket.newMotorConfigurationID();
147                                                 configuration.setMotorConfigurationID(id);
148                                         }
149                                         mount.setMotor(id, m);
150                                         mount.setMotorDelay(id, d);
151                                 }
152                                 updateFields();
153                         }
154                 });
155                 panel.add(button,"span, split, grow");
156                 
157                 button = new JButton("Remove motor");
158                 button.addActionListener(new ActionListener() {
159                         @Override
160                         public void actionPerformed(ActionEvent e) {
161                                 mount.setMotor(configuration.getMotorConfigurationID(), null);
162                                 updateFields();
163                         }
164                 });
165                 panel.add(button,"grow, wrap");
166                 
167                 
168                 
169                 
170                 
171                 // Set enabled status
172                 
173                 setDeepEnabled(panel, motorMount.isMotorMount());
174                 check.addChangeListener(new ChangeListener() {
175                         @Override
176                         public void stateChanged(ChangeEvent e) {
177                                 setDeepEnabled(panel, mount.isMotorMount());
178                         }
179                 });
180                 
181         }
182         
183         public void updateFields() {
184                 String id = configuration.getMotorConfigurationID();
185                 Motor m = mount.getMotor(id);
186                 if (m == null)
187                         motorLabel.setText("None");
188                 else
189                         motorLabel.setText(m.getManufacturer() + " " +
190                                         m.getDesignation(mount.getMotorDelay(id)));
191         }
192         
193         
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);
199                         }
200                 }
201         }
202         
203 }