Updates for 0.9.5
[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.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;
36
37 public class MotorConfig extends JPanel {
38
39         private final Rocket rocket;
40         private final MotorMount mount;
41         private final Configuration configuration;
42         private JPanel panel;
43         private JLabel motorLabel;
44         
45         public MotorConfig(MotorMount motorMount) {
46                 super(new MigLayout("fill"));
47
48                 this.rocket = ((RocketComponent)motorMount).getRocket();
49                 this.mount = motorMount;
50                 this.configuration = ((RocketComponent)motorMount).getRocket()
51                         .getDefaultConfiguration();
52                 
53                 BooleanModel model;
54                 
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");
59                 
60                 
61                 panel = new JPanel(new MigLayout("fill"));
62                 this.add(panel,"grow, wrap");
63                 
64
65                 // Motor configuration selector
66                 panel.add(new JLabel("Motor configuration:"), "shrink");
67                 
68                 JComboBox combo = new JComboBox(new MotorConfigurationModel(configuration));
69                 panel.add(combo,"growx");
70                 
71                 configuration.addChangeListener(new ChangeListener() {
72                         @Override
73                         public void stateChanged(ChangeEvent e) {
74                                 updateFields();
75                         }
76                 });
77                 
78                 JButton button = new JButton("New");
79                 button.addActionListener(new ActionListener() {
80                         @Override
81                         public void actionPerformed(ActionEvent e) {
82                                 String id = rocket.newMotorConfigurationID();
83                                 configuration.setMotorConfigurationID(id);
84                         }
85                 });
86                 panel.add(button, "wrap unrel");
87                 
88                 
89                 // Current motor
90                 panel.add(new JLabel("Current motor:"), "shrink");
91                 
92                 motorLabel = new JLabel();
93                 motorLabel.setFont(motorLabel.getFont().deriveFont(Font.BOLD));
94                 updateFields();
95                 panel.add(motorLabel,"wrap unrel");
96
97                 
98                 
99                 //  Overhang
100                 panel.add(new JLabel("Motor overhang:"));
101                 
102                 DoubleModel m = new DoubleModel(motorMount, "MotorOverhang", UnitGroup.UNITS_LENGTH);
103                 
104                 JSpinner spin = new JSpinner(m.getSpinnerModel());
105                 spin.setEditor(new SpinnerEditor(spin));
106                 panel.add(spin,"span, split, width :65lp:");
107                 
108                 panel.add(new UnitSelector(m),"width :30lp:");
109                 panel.add(new BasicSlider(m.getSliderModel(-0.02,0.06)),"w 100lp, wrap unrel");
110
111
112                 
113                 // Select ignition event
114                 panel.add(new JLabel("Ignition at:"),"");
115                 
116                 combo = new JComboBox(new EnumModel<IgnitionEvent>(mount, "IgnitionEvent"));
117                 panel.add(combo,"growx, wrap");
118                 
119                 // ... and delay
120                 panel.add(new JLabel("plus"),"gap indent, skip 1, span, split");
121                 
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");
126                 
127                 panel.add(new JLabel("seconds"),"wrap unrel");
128
129
130                 
131                 // Check stage count
132                 RocketComponent c = (RocketComponent)mount;
133                 c = c.getRocket();
134                 int stages = c.getChildCount();
135                 
136                 if (stages == 1) {
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");
140                 } else {
141                         panel.add(new StyledLabel("The current design has " + stages + " stages.", -1), 
142                                         "skip 1, spanx, wrap para");
143                 }
144                 
145                 
146                 // Select etc. buttons
147                 button = new JButton("Select motor");
148                 button.addActionListener(new ActionListener() {
149                         @Override
150                         public void actionPerformed(ActionEvent e) {
151                                 String id = configuration.getMotorConfigurationID();
152                                 
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();
158                                 
159                                 if (m != null) {
160                                         if (id == null) {
161                                                 id = rocket.newMotorConfigurationID();
162                                                 configuration.setMotorConfigurationID(id);
163                                         }
164                                         mount.setMotor(id, m);
165                                         mount.setMotorDelay(id, d);
166                                 }
167                                 updateFields();
168                         }
169                 });
170                 panel.add(button,"span, split, growx");
171                 
172                 button = new JButton("Remove motor");
173                 button.addActionListener(new ActionListener() {
174                         @Override
175                         public void actionPerformed(ActionEvent e) {
176                                 mount.setMotor(configuration.getMotorConfigurationID(), null);
177                                 updateFields();
178                         }
179                 });
180                 panel.add(button,"growx, wrap");
181                 
182                 
183                 
184                 
185                 
186                 // Set enabled status
187                 
188                 setDeepEnabled(panel, motorMount.isMotorMount());
189                 check.addChangeListener(new ChangeListener() {
190                         @Override
191                         public void stateChanged(ChangeEvent e) {
192                                 setDeepEnabled(panel, mount.isMotorMount());
193                         }
194                 });
195                 
196         }
197         
198         public void updateFields() {
199                 String id = configuration.getMotorConfigurationID();
200                 Motor m = mount.getMotor(id);
201                 if (m == null)
202                         motorLabel.setText("None");
203                 else
204                         motorLabel.setText(m.getManufacturer().getDisplayName() + " " +
205                                         m.getDesignation(mount.getMotorDelay(id)));
206         }
207         
208         
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);
214                         }
215                 }
216         }
217         
218 }