create changelog entry
[debian/openrocket] / core / 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.SwingUtilities;
17 import javax.swing.event.ChangeEvent;
18 import javax.swing.event.ChangeListener;
19
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;
40
41 public class MotorConfig extends JPanel {
42         
43         private final Rocket rocket;
44         private final MotorMount mount;
45         private final Configuration configuration;
46         private JPanel panel;
47         private JLabel motorLabel;
48         private static final Translator trans = Application.getTranslator();
49         
50         public MotorConfig(MotorMount motorMount) {
51                 super(new MigLayout("fill"));
52                 
53                 this.rocket = ((RocketComponent) motorMount).getRocket();
54                 this.mount = motorMount;
55                 this.configuration = ((RocketComponent) motorMount).getRocket().getDefaultConfiguration();
56                 
57                 BooleanModel model;
58                 
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");
64                 
65
66                 panel = new JPanel(new MigLayout("fill"));
67                 this.add(panel, "grow, wrap");
68                 
69
70                 // Motor configuration selector
71                 //// Motor configuration:
72                 panel.add(new JLabel(trans.get("MotorCfg.lbl.Motorcfg")), "shrink");
73                 
74                 JComboBox combo = new JComboBox(new MotorConfigurationModel(configuration));
75                 panel.add(combo, "growx");
76                 
77                 configuration.addChangeListener(new ChangeListener() {
78                         @Override
79                         public void stateChanged(ChangeEvent e) {
80                                 updateFields();
81                         }
82                 });
83                 
84                 //// New button
85                 JButton button = new JButton(trans.get("MotorCfg.but.New"));
86                 button.addActionListener(new ActionListener() {
87                         @Override
88                         public void actionPerformed(ActionEvent e) {
89                                 String id = rocket.newMotorConfigurationID();
90                                 configuration.setMotorConfigurationID(id);
91                         }
92                 });
93                 panel.add(button, "wrap unrel");
94                 
95
96                 // Current motor:
97                 panel.add(new JLabel(trans.get("MotorCfg.lbl.Currentmotor")), "shrink");
98                 
99                 motorLabel = new JLabel();
100                 motorLabel.setFont(motorLabel.getFont().deriveFont(Font.BOLD));
101                 updateFields();
102                 panel.add(motorLabel, "wrap unrel");
103                 
104
105
106                 //  Overhang
107                 //// Motor overhang:
108                 panel.add(new JLabel(trans.get("MotorCfg.lbl.Motoroverhang")));
109                 
110                 DoubleModel dm = new DoubleModel(motorMount, "MotorOverhang", UnitGroup.UNITS_LENGTH);
111                 
112                 JSpinner spin = new JSpinner(dm.getSpinnerModel());
113                 spin.setEditor(new SpinnerEditor(spin));
114                 panel.add(spin, "span, split, width :65lp:");
115                 
116                 panel.add(new UnitSelector(dm), "width :30lp:");
117                 panel.add(new BasicSlider(dm.getSliderModel(-0.02, 0.06)), "w 100lp, wrap unrel");
118                 
119
120
121                 // Select ignition event
122                 //// Ignition at:
123                 panel.add(new JLabel(trans.get("MotorCfg.lbl.Ignitionat")), "");
124                 
125                 combo = new JComboBox(new EnumModel<IgnitionEvent>(mount, "IgnitionEvent"));
126                 panel.add(combo, "growx, wrap");
127                 
128                 // ... and delay
129                 //// plus
130                 panel.add(new JLabel(trans.get("MotorCfg.lbl.plus")), "gap indent, skip 1, span, split");
131                 
132                 dm = new DoubleModel(mount, "IgnitionDelay", 0);
133                 spin = new JSpinner(dm.getSpinnerModel());
134                 spin.setEditor(new SpinnerEditor(spin,3));
135                 panel.add(spin, "gap rel rel");
136                 
137                 //// seconds
138                 panel.add(new JLabel(trans.get("MotorCfg.lbl.seconds")), "wrap unrel");
139                 
140
141
142                 // Check stage count
143                 RocketComponent c = (RocketComponent) mount;
144                 c = c.getRocket();
145                 int stages = c.getChildCount();
146                 
147                 if (stages == 1) {
148                         //// The current design has only one stage.
149                         //// Stages can be added by clicking \"New stage\".
150                         
151                         panel.add(new StyledLabel(trans.get("MotorCfg.lbl.longA1") + " " +
152                                         trans.get("MotorCfg.lbl.longA2"), -1),
153                                         "spanx, right, wrap para");
154                 } else {
155                         //// The current design has 
156                         //// stages.
157                         panel.add(new StyledLabel(trans.get("MotorCfg.lbl.longB1") + " " + stages + " " +
158                                         trans.get("MotorCfg.lbl.longB2"), -1),
159                                         "skip 1, spanx, wrap para");
160                 }
161                 
162
163                 // Select etc. buttons
164                 //// Select motor
165                 button = new JButton(trans.get("MotorCfg.but.Selectmotor"));
166                 button.addActionListener(new ActionListener() {
167                         @Override
168                         public void actionPerformed(ActionEvent e) {
169                                 String id = configuration.getMotorConfigurationID();
170                                 
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();
177                                 
178                                 if (m != null) {
179                                         if (id == null) {
180                                                 id = rocket.newMotorConfigurationID();
181                                                 configuration.setMotorConfigurationID(id);
182                                         }
183                                         mount.setMotor(id, m);
184                                         mount.setMotorDelay(id, d);
185                                 }
186                                 updateFields();
187                         }
188                 });
189                 panel.add(button, "span, split, growx");
190                 
191                 //// Remove motor
192                 button = new JButton(trans.get("MotorCfg.but.Removemotor"));
193                 button.addActionListener(new ActionListener() {
194                         @Override
195                         public void actionPerformed(ActionEvent e) {
196                                 mount.setMotor(configuration.getMotorConfigurationID(), null);
197                                 updateFields();
198                         }
199                 });
200                 panel.add(button, "growx, wrap");
201                 
202
203
204
205
206                 // Set enabled status
207                 
208                 setDeepEnabled(panel, motorMount.isMotorMount());
209                 check.addChangeListener(new ChangeListener() {
210                         @Override
211                         public void stateChanged(ChangeEvent e) {
212                                 setDeepEnabled(panel, mount.isMotorMount());
213                         }
214                 });
215                 
216         }
217         
218         public void updateFields() {
219                 String id = configuration.getMotorConfigurationID();
220                 Motor m = mount.getMotor(id);
221                 if (m == null) {
222                         //// None
223                         motorLabel.setText(trans.get("MotorCfg.lbl.motorLabel"));
224                 } else {
225                         String str = "";
226                         if (m instanceof ThrustCurveMotor)
227                                 str = ((ThrustCurveMotor) m).getManufacturer() + " ";
228                         str += m.getDesignation(mount.getMotorDelay(id));
229                         motorLabel.setText(str);
230                 }
231         }
232         
233         
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);
239                         }
240                 }
241         }
242         
243 }