1 package net.sf.openrocket.gui.configdialog;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.awt.event.FocusEvent;
8 import java.awt.event.FocusListener;
9 import java.util.ArrayList;
10 import java.util.Iterator;
11 import java.util.List;
13 import javax.swing.BorderFactory;
14 import javax.swing.JButton;
15 import javax.swing.JCheckBox;
16 import javax.swing.JColorChooser;
17 import javax.swing.JComboBox;
18 import javax.swing.JLabel;
19 import javax.swing.JPanel;
20 import javax.swing.JScrollPane;
21 import javax.swing.JSpinner;
22 import javax.swing.JTabbedPane;
23 import javax.swing.JTextArea;
24 import javax.swing.JTextField;
26 import net.miginfocom.swing.MigLayout;
27 import net.sf.openrocket.gui.SpinnerEditor;
28 import net.sf.openrocket.gui.adaptors.BooleanModel;
29 import net.sf.openrocket.gui.adaptors.DoubleModel;
30 import net.sf.openrocket.gui.adaptors.EnumModel;
31 import net.sf.openrocket.gui.adaptors.MaterialModel;
32 import net.sf.openrocket.gui.components.BasicSlider;
33 import net.sf.openrocket.gui.components.ColorIcon;
34 import net.sf.openrocket.gui.components.StyledLabel;
35 import net.sf.openrocket.gui.components.StyledLabel.Style;
36 import net.sf.openrocket.gui.components.UnitSelector;
37 import net.sf.openrocket.l10n.Translator;
38 import net.sf.openrocket.material.Material;
39 import net.sf.openrocket.rocketcomponent.ComponentAssembly;
40 import net.sf.openrocket.rocketcomponent.ExternalComponent;
41 import net.sf.openrocket.rocketcomponent.ExternalComponent.Finish;
42 import net.sf.openrocket.rocketcomponent.NoseCone;
43 import net.sf.openrocket.rocketcomponent.Rocket;
44 import net.sf.openrocket.rocketcomponent.RocketComponent;
45 import net.sf.openrocket.startup.Application;
46 import net.sf.openrocket.unit.UnitGroup;
47 import net.sf.openrocket.util.GUIUtil;
48 import net.sf.openrocket.util.Invalidatable;
49 import net.sf.openrocket.util.LineStyle;
50 import net.sf.openrocket.util.Prefs;
52 public class RocketComponentConfig extends JPanel {
54 private static final Translator trans = Application.getTranslator();
56 protected final RocketComponent component;
57 protected final JTabbedPane tabbedPane;
59 private final List<Invalidatable> invalidatables = new ArrayList<Invalidatable>();
62 protected final JTextField componentNameField;
63 protected JTextArea commentTextArea;
64 private final TextFieldListener textFieldListener;
65 private JButton colorButton;
66 private JCheckBox colorDefault;
67 private JPanel buttonPanel;
69 private JLabel massLabel;
72 public RocketComponentConfig(RocketComponent component) {
73 setLayout(new MigLayout("fill", "[grow, fill]"));
74 this.component = component;
77 JLabel label = new JLabel(trans.get("RocketCompCfg.lbl.Componentname"));
78 //// The component name.
79 label.setToolTipText(trans.get("RocketCompCfg.ttip.Thecomponentname"));
80 this.add(label, "split, gapright 10");
82 componentNameField = new JTextField(15);
83 textFieldListener = new TextFieldListener();
84 componentNameField.addActionListener(textFieldListener);
85 componentNameField.addFocusListener(textFieldListener);
86 //// The component name.
87 componentNameField.setToolTipText(trans.get("RocketCompCfg.ttip.Thecomponentname"));
88 this.add(componentNameField, "growx, growy 0, wrap");
91 tabbedPane = new JTabbedPane();
92 this.add(tabbedPane, "growx, growy 1, wrap");
94 //// Override and Mass and CG override options
95 tabbedPane.addTab(trans.get("RocketCompCfg.tab.Override"), null, overrideTab(),
96 trans.get("RocketCompCfg.tab.MassandCGoverride"));
97 if (component.isMassive())
98 //// Figure and Figure style options
99 tabbedPane.addTab(trans.get("RocketCompCfg.tab.Figure"), null, figureTab(),
100 trans.get("RocketCompCfg.tab.Figstyleopt"));
101 //// Comment and Specify a comment for the component
102 tabbedPane.addTab(trans.get("RocketCompCfg.tab.Comment"), null, commentTab(),
103 trans.get("RocketCompCfg.tab.Specifyacomment"));
111 protected void addButtons(JButton... buttons) {
112 if (buttonPanel != null) {
113 this.remove(buttonPanel);
116 buttonPanel = new JPanel(new MigLayout("fill, ins 0"));
119 massLabel = new StyledLabel(trans.get("RocketCompCfg.lbl.Mass") + " ", -1);
120 buttonPanel.add(massLabel, "growx");
122 for (JButton b : buttons) {
123 buttonPanel.add(b, "right, gap para");
127 JButton closeButton = new JButton(trans.get("dlg.but.close"));
128 closeButton.addActionListener(new ActionListener() {
130 public void actionPerformed(ActionEvent arg0) {
131 ComponentConfigDialog.hideDialog();
134 buttonPanel.add(closeButton, "right, gap 30lp");
138 this.add(buttonPanel, "spanx, growx");
143 * Called when a change occurs, so that the fields can be updated if necessary.
144 * When overriding this method, the supermethod must always be called.
146 public void updateFields() {
148 componentNameField.setText(component.getName());
150 // Component color and "Use default color" checkbox
151 if (colorButton != null && colorDefault != null) {
152 colorButton.setIcon(new ColorIcon(getColor()));
154 if ((component.getColor() == null) != colorDefault.isSelected())
155 colorDefault.setSelected(component.getColor() == null);
159 if (component.isMassive()) {
161 String text = trans.get("RocketCompCfg.lbl.Componentmass") + " ";
162 text += UnitGroup.UNITS_MASS.getDefaultUnit().toStringUnit(
163 component.getComponentMass());
165 String overridetext = null;
166 if (component.isMassOverridden()) {
168 overridetext = trans.get("RocketCompCfg.lbl.overriddento")+" " + UnitGroup.UNITS_MASS.getDefaultUnit().
169 toStringUnit(component.getOverrideMass()) + ")";
172 for (RocketComponent c = component.getParent(); c != null; c = c.getParent()) {
173 if (c.isMassOverridden() && c.getOverrideSubcomponents()) {
175 overridetext = trans.get("RocketCompCfg.lbl.overriddenby")+" " + c.getName() + ")";
179 if (overridetext != null)
180 text = text + " " + overridetext;
182 massLabel.setText(text);
184 massLabel.setText("");
189 protected JPanel materialPanel(JPanel panel, Material.Type type) {
190 ////Component material: and Component finish:
191 return materialPanel(panel, type, trans.get("RocketCompCfg.lbl.Componentmaterial"),
192 trans.get("RocketCompCfg.lbl.Componentfinish"));
195 protected JPanel materialPanel(JPanel panel, Material.Type type,
196 String materialString, String finishString) {
197 JLabel label = new JLabel(materialString);
198 //// The component material affects the weight of the component.
199 label.setToolTipText(trans.get("RocketCompCfg.lbl.ttip.componentmaterialaffects"));
200 panel.add(label, "spanx 4, wrap rel");
202 JComboBox combo = new JComboBox(new MaterialModel(panel, component, type));
203 //// The component material affects the weight of the component.
204 combo.setToolTipText(trans.get("RocketCompCfg.combo.ttip.componentmaterialaffects"));
205 panel.add(combo, "spanx 4, growx, wrap paragraph");
208 if (component instanceof ExternalComponent) {
209 label = new JLabel(finishString);
210 ////<html>The component finish affects the aerodynamic drag of the component.<br>
211 String tip = trans.get("RocketCompCfg.lbl.longA1")
212 //// The value indicated is the average roughness height of the surface.
213 + trans.get("RocketCompCfg.lbl.longA2");
214 label.setToolTipText(tip);
215 panel.add(label, "spanx 4, wmin 220lp, wrap rel");
217 combo = new JComboBox(new EnumModel<ExternalComponent.Finish>(component, "Finish"));
218 combo.setToolTipText(tip);
219 panel.add(combo, "spanx 4, growx, split");
222 JButton button = new JButton(trans.get("RocketCompCfg.but.Setforall"));
223 //// Set this finish for all components of the rocket.
224 button.setToolTipText(trans.get("RocketCompCfg.but.ttip.Setforall"));
225 button.addActionListener(new ActionListener() {
227 public void actionPerformed(ActionEvent e) {
228 Finish f = ((ExternalComponent) component).getFinish();
229 Rocket rocket = component.getRocket();
232 // Store previous undo description
233 String desc = ComponentConfigDialog.getUndoDescription();
234 ComponentConfigDialog.addUndoPosition("Set rocket finish");
236 Iterator<RocketComponent> iter = rocket.iterator();
237 while (iter.hasNext()) {
238 RocketComponent c = iter.next();
239 if (c instanceof ExternalComponent) {
240 ((ExternalComponent) c).setFinish(f);
243 // Restore undo description
244 ComponentConfigDialog.addUndoPosition(desc);
250 panel.add(button, "wrap paragraph");
257 private JPanel overrideTab() {
258 JPanel panel = new JPanel(new MigLayout("align 50% 20%, fillx, gap rel unrel",
259 "[][65lp::][30lp::][]", ""));
260 //// Override the mass or center of gravity of the
261 panel.add(new StyledLabel(trans.get("RocketCompCfg.lbl.Overridemassorcenter") + " " +
262 component.getComponentName() + ":", Style.BOLD), "spanx, wrap 20lp");
270 bm = new BooleanModel(component, "MassOverridden");
271 check = new JCheckBox(bm);
273 check.setText(trans.get("RocketCompCfg.checkbox.Overridemass"));
274 panel.add(check, "growx 1, gapright 20lp");
276 DoubleModel m = new DoubleModel(component, "OverrideMass", UnitGroup.UNITS_MASS, 0);
278 JSpinner spin = new JSpinner(m.getSpinnerModel());
279 spin.setEditor(new SpinnerEditor(spin));
280 bm.addEnableComponent(spin, true);
281 panel.add(spin, "growx 1");
283 us = new UnitSelector(m);
284 bm.addEnableComponent(us, true);
285 panel.add(us, "growx 1");
287 bs = new BasicSlider(m.getSliderModel(0, 0.03, 1.0));
288 bm.addEnableComponent(bs);
289 panel.add(bs, "growx 5, w 100lp, wrap");
293 bm = new BooleanModel(component, "CGOverridden");
294 check = new JCheckBox(bm);
295 //// Override center of gravity:"
296 check.setText(trans.get("RocketCompCfg.checkbox.Overridecenterofgrav"));
297 panel.add(check, "growx 1, gapright 20lp");
299 m = new DoubleModel(component, "OverrideCGX", UnitGroup.UNITS_LENGTH, 0);
300 // Calculate suitable length for slider
302 if (component instanceof ComponentAssembly) {
305 Iterator<RocketComponent> iterator = component.iterator(false);
306 while (iterator.hasNext()) {
307 RocketComponent c = iterator.next();
308 if (c.getRelativePosition() == RocketComponent.Position.AFTER)
311 length = new DoubleModel(l);
313 length = new DoubleModel(component, "Length", UnitGroup.UNITS_LENGTH, 0);
316 spin = new JSpinner(m.getSpinnerModel());
317 spin.setEditor(new SpinnerEditor(spin));
318 bm.addEnableComponent(spin, true);
319 panel.add(spin, "growx 1");
321 us = new UnitSelector(m);
322 bm.addEnableComponent(us, true);
323 panel.add(us, "growx 1");
325 bs = new BasicSlider(m.getSliderModel(new DoubleModel(0), length));
326 bm.addEnableComponent(bs);
327 panel.add(bs, "growx 5, w 100lp, wrap 35lp");
330 // Override subcomponents checkbox
331 bm = new BooleanModel(component, "OverrideSubcomponents");
332 check = new JCheckBox(bm);
333 //// Override mass and CG of all subcomponents
334 check.setText(trans.get("RocketCompCfg.checkbox.OverridemassandCG"));
335 panel.add(check, "gap para, spanx, wrap para");
337 //// <html>The overridden mass does not include motors.<br>
338 panel.add(new StyledLabel(trans.get("RocketCompCfg.lbl.longB1") +
339 //// The center of gravity is measured from the front end of the
340 trans.get("RocketCompCfg.lbl.longB2") +" " +
341 component.getComponentName().toLowerCase() + ".", -1),
342 "spanx, wrap, gap para, height 0::30lp");
348 private JPanel commentTab() {
349 JPanel panel = new JPanel(new MigLayout("fill"));
352 panel.add(new StyledLabel(trans.get("RocketCompCfg.lbl.Commentsonthe") +" " + component.getComponentName() + ":",
353 Style.BOLD), "wrap");
355 // TODO: LOW: Changes in comment from other sources not reflected in component
356 commentTextArea = new JTextArea(component.getComment());
357 commentTextArea.setLineWrap(true);
358 commentTextArea.setWrapStyleWord(true);
359 commentTextArea.setEditable(true);
360 GUIUtil.setTabToFocusing(commentTextArea);
361 commentTextArea.addFocusListener(textFieldListener);
363 panel.add(new JScrollPane(commentTextArea), "width 10px, height 10px, growx, growy");
370 private JPanel figureTab() {
371 JPanel panel = new JPanel(new MigLayout("align 20% 20%"));
374 panel.add(new StyledLabel(trans.get("RocketCompCfg.lbl.Figurestyle"), Style.BOLD), "wrap para");
376 //// Component color:
377 panel.add(new JLabel(trans.get("RocketCompCfg.lbl.Componentcolor")), "gapleft para, gapright 10lp");
379 colorButton = new JButton(new ColorIcon(getColor()));
380 colorButton.addActionListener(new ActionListener() {
382 public void actionPerformed(ActionEvent e) {
383 Color c = component.getColor();
385 c = Prefs.getDefaultColor(component.getClass());
389 c = JColorChooser.showDialog(tabbedPane, trans.get("RocketCompCfg.lbl.Choosecolor"), c);
391 component.setColor(c);
395 panel.add(colorButton, "gapright 10lp");
397 //// Use default color
398 colorDefault = new JCheckBox(trans.get("RocketCompCfg.checkbox.Usedefaultcolor"));
399 if (component.getColor() == null)
400 colorDefault.setSelected(true);
401 colorDefault.addActionListener(new ActionListener() {
403 public void actionPerformed(ActionEvent e) {
404 if (colorDefault.isSelected())
405 component.setColor(null);
407 component.setColor(Prefs.getDefaultColor(component.getClass()));
410 panel.add(colorDefault, "wrap para");
412 //// Component line style:
413 panel.add(new JLabel(trans.get("RocketCompCfg.lbl.Complinestyle")), "gapleft para, gapright 10lp");
415 LineStyle[] list = new LineStyle[LineStyle.values().length + 1];
416 System.arraycopy(LineStyle.values(), 0, list, 1, LineStyle.values().length);
418 JComboBox combo = new JComboBox(new EnumModel<LineStyle>(component, "LineStyle",
420 list, trans.get("LineStyle.Defaultstyle")));
421 panel.add(combo, "spanx 2, growx, wrap 50lp");
423 //// Save as default style
424 JButton button = new JButton(trans.get("RocketCompCfg.but.Saveasdefstyle"));
425 button.addActionListener(new ActionListener() {
427 public void actionPerformed(ActionEvent e) {
428 if (component.getColor() != null) {
429 Prefs.setDefaultColor(component.getClass(), component.getColor());
430 component.setColor(null);
432 if (component.getLineStyle() != null) {
433 Prefs.setDefaultLineStyle(component.getClass(), component.getLineStyle());
434 component.setLineStyle(null);
438 panel.add(button, "gapleft para, spanx 3, growx, wrap");
444 private Color getColor() {
445 Color c = component.getColor();
447 c = Prefs.getDefaultColor(component.getClass());
454 protected JPanel shoulderTab() {
455 JPanel panel = new JPanel(new MigLayout("fill"));
458 DoubleModel m0 = new DoubleModel(0);
464 //// Fore shoulder, not for NoseCone
466 if (!(component instanceof NoseCone)) {
467 sub = new JPanel(new MigLayout("gap rel unrel", "[][65lp::][30lp::]", ""));
470 sub.setBorder(BorderFactory.createTitledBorder(trans.get("RocketCompCfg.border.Foreshoulder")));
475 sub.add(new JLabel(trans.get("RocketCompCfg.lbl.Diameter")));
477 m = new DoubleModel(component, "ForeShoulderRadius", 2, UnitGroup.UNITS_LENGTH, 0);
478 m2 = new DoubleModel(component, "ForeRadius", 2, UnitGroup.UNITS_LENGTH);
480 spin = new JSpinner(m.getSpinnerModel());
481 spin.setEditor(new SpinnerEditor(spin));
482 sub.add(spin, "growx");
484 sub.add(new UnitSelector(m), "growx");
485 sub.add(new BasicSlider(m.getSliderModel(m0, m2)), "w 100lp, wrap");
489 sub.add(new JLabel(trans.get("RocketCompCfg.lbl.Length")));
491 m = new DoubleModel(component, "ForeShoulderLength", UnitGroup.UNITS_LENGTH, 0);
493 spin = new JSpinner(m.getSpinnerModel());
494 spin.setEditor(new SpinnerEditor(spin));
495 sub.add(spin, "growx");
497 sub.add(new UnitSelector(m), "growx");
498 sub.add(new BasicSlider(m.getSliderModel(0, 0.02, 0.2)), "w 100lp, wrap");
502 sub.add(new JLabel(trans.get("RocketCompCfg.lbl.Thickness")));
504 m = new DoubleModel(component, "ForeShoulderThickness", UnitGroup.UNITS_LENGTH, 0);
505 m2 = new DoubleModel(component, "ForeShoulderRadius", UnitGroup.UNITS_LENGTH);
507 spin = new JSpinner(m.getSpinnerModel());
508 spin.setEditor(new SpinnerEditor(spin));
509 sub.add(spin, "growx");
511 sub.add(new UnitSelector(m), "growx");
512 sub.add(new BasicSlider(m.getSliderModel(m0, m2)), "w 100lp, wrap");
516 bm = new BooleanModel(component, "ForeShoulderCapped");
517 check = new JCheckBox(bm);
519 check.setText(trans.get("RocketCompCfg.checkbox.Endcapped"));
520 //// Whether the end of the shoulder is capped.
521 check.setToolTipText(trans.get("RocketCompCfg.ttip.Endcapped"));
522 sub.add(check, "spanx");
530 sub = new JPanel(new MigLayout("gap rel unrel", "[][65lp::][30lp::]", ""));
532 if (component instanceof NoseCone)
533 //// Nose cone shoulder
534 sub.setBorder(BorderFactory.createTitledBorder(trans.get("RocketCompCfg.title.Noseconeshoulder")));
537 sub.setBorder(BorderFactory.createTitledBorder(trans.get("RocketCompCfg.title.Aftshoulder")));
542 sub.add(new JLabel(trans.get("RocketCompCfg.lbl.Diameter")));
544 m = new DoubleModel(component, "AftShoulderRadius", 2, UnitGroup.UNITS_LENGTH, 0);
545 m2 = new DoubleModel(component, "AftRadius", 2, UnitGroup.UNITS_LENGTH);
547 spin = new JSpinner(m.getSpinnerModel());
548 spin.setEditor(new SpinnerEditor(spin));
549 sub.add(spin, "growx");
551 sub.add(new UnitSelector(m), "growx");
552 sub.add(new BasicSlider(m.getSliderModel(m0, m2)), "w 100lp, wrap");
556 sub.add(new JLabel(trans.get("RocketCompCfg.lbl.Length")));
558 m = new DoubleModel(component, "AftShoulderLength", UnitGroup.UNITS_LENGTH, 0);
560 spin = new JSpinner(m.getSpinnerModel());
561 spin.setEditor(new SpinnerEditor(spin));
562 sub.add(spin, "growx");
564 sub.add(new UnitSelector(m), "growx");
565 sub.add(new BasicSlider(m.getSliderModel(0, 0.02, 0.2)), "w 100lp, wrap");
569 sub.add(new JLabel(trans.get("RocketCompCfg.lbl.Thickness")));
571 m = new DoubleModel(component, "AftShoulderThickness", UnitGroup.UNITS_LENGTH, 0);
572 m2 = new DoubleModel(component, "AftShoulderRadius", UnitGroup.UNITS_LENGTH);
574 spin = new JSpinner(m.getSpinnerModel());
575 spin.setEditor(new SpinnerEditor(spin));
576 sub.add(spin, "growx");
578 sub.add(new UnitSelector(m), "growx");
579 sub.add(new BasicSlider(m.getSliderModel(m0, m2)), "w 100lp, wrap");
583 bm = new BooleanModel(component, "AftShoulderCapped");
584 check = new JCheckBox(bm);
586 check.setText(trans.get("RocketCompCfg.checkbox.Endcapped"));
587 //// Whether the end of the shoulder is capped.
588 check.setToolTipText(trans.get("RocketCompCfg.ttip.Endcapped"));
589 sub.add(check, "spanx");
602 * Private inner class to handle events in componentNameField.
604 private class TextFieldListener implements ActionListener, FocusListener {
606 public void actionPerformed(ActionEvent e) {
611 public void focusGained(FocusEvent e) {
615 public void focusLost(FocusEvent e) {
619 private void setName() {
620 if (!component.getName().equals(componentNameField.getText())) {
621 component.setName(componentNameField.getText());
623 if (!component.getComment().equals(commentTextArea.getText())) {
624 component.setComment(commentTextArea.getText());
630 protected void register(Invalidatable model) {
631 this.invalidatables.add(model);
634 public void invalidateModels() {
635 for (Invalidatable i : invalidatables) {