Make ComponentPresetDao and interface with implementation in ComponentPresetDatabase...
[debian/openrocket] / core / src / net / sf / openrocket / gui / configdialog / BodyTubeConfig.java
1 package net.sf.openrocket.gui.configdialog;
2
3
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6
7 import javax.swing.JButton;
8 import javax.swing.JCheckBox;
9 import javax.swing.JComboBox;
10 import javax.swing.JLabel;
11 import javax.swing.JPanel;
12 import javax.swing.JSpinner;
13 import javax.swing.SwingUtilities;
14
15 import net.miginfocom.swing.MigLayout;
16 import net.sf.openrocket.database.ComponentPresetDatabase;
17 import net.sf.openrocket.document.OpenRocketDocument;
18 import net.sf.openrocket.gui.SpinnerEditor;
19 import net.sf.openrocket.gui.adaptors.BooleanModel;
20 import net.sf.openrocket.gui.adaptors.DoubleModel;
21 import net.sf.openrocket.gui.adaptors.PresetModel;
22 import net.sf.openrocket.gui.components.BasicSlider;
23 import net.sf.openrocket.gui.components.UnitSelector;
24 import net.sf.openrocket.gui.dialogs.preset.ComponentPresetChooserDialog;
25 import net.sf.openrocket.l10n.Translator;
26 import net.sf.openrocket.material.Material;
27 import net.sf.openrocket.preset.ComponentPreset;
28 import net.sf.openrocket.rocketcomponent.BodyTube;
29 import net.sf.openrocket.rocketcomponent.RocketComponent;
30 import net.sf.openrocket.startup.Application;
31 import net.sf.openrocket.unit.UnitGroup;
32
33 public class BodyTubeConfig extends RocketComponentConfig {
34
35         private MotorConfig motorConfigPane = null;
36         private DoubleModel maxLength;
37         private JComboBox presetComboBox;
38         private PresetModel presetModel;
39         private static final Translator trans = Application.getTranslator();
40
41         public BodyTubeConfig(OpenRocketDocument d, RocketComponent c) {
42                 super(d, c);
43
44                 JPanel panel = new JPanel(new MigLayout("gap rel unrel", "[][65lp::][30lp::][]", ""));
45
46
47
48                 ////  Body tube template
49                 // FIXME: Move to proper location
50                 panel.add(new JLabel(trans.get("PresetModel.lbl.select")));
51                 presetModel = new PresetModel(component);
52                 ((ComponentPresetDatabase)Application.getComponentPresetDao()).addDatabaseListener(presetModel);
53                 presetComboBox = new JComboBox(presetModel);
54                 presetComboBox.setEditable(false);
55                 panel.add(presetComboBox, "wrap para");
56                 //FIXME: temporarily put the select from table button in the config dialog.
57                 {
58                         JButton opendialog = new JButton("o");
59                         opendialog.addActionListener(
60                                         new ActionListener() {
61
62                                                 @Override
63                                                 public void actionPerformed(ActionEvent e) {
64                                                         ComponentPresetChooserDialog dialog = 
65                                                                         new ComponentPresetChooserDialog( SwingUtilities.getWindowAncestor(BodyTubeConfig.this),
66                                                                                         BodyTubeConfig.this.component,
67                                                                                         ComponentPreset.MANUFACTURER,
68                                                                                         ComponentPreset.PARTNO,
69                                                                                         ComponentPreset.OUTER_DIAMETER,
70                                                                                         ComponentPreset.INNER_DIAMETER,
71                                                                                         ComponentPreset.LENGTH);
72                                                         dialog.setVisible(true);
73                                                         ComponentPreset preset = dialog.getSelectedComponentPreset();
74                                                 }
75                                         });
76                         panel.add( opendialog, "wrap" );
77                 }
78
79
80                 ////  Body tube length
81                 panel.add(new JLabel(trans.get("BodyTubecfg.lbl.Bodytubelength")));
82
83                 maxLength = new DoubleModel(2.0);
84                 DoubleModel length = new DoubleModel(component, "Length", UnitGroup.UNITS_LENGTH, 0);
85
86                 JSpinner spin = new JSpinner(length.getSpinnerModel());
87                 spin.setEditor(new SpinnerEditor(spin));
88                 panel.add(spin, "growx");
89
90                 panel.add(new UnitSelector(length), "growx");
91                 panel.add(new BasicSlider(length.getSliderModel(0, 0.5, maxLength)), "w 100lp, wrap");
92
93
94                 //// Body tube diameter
95                 panel.add(new JLabel(trans.get("BodyTubecfg.lbl.Outerdiameter")));
96
97                 DoubleModel od = new DoubleModel(component, "OuterRadius", 2, UnitGroup.UNITS_LENGTH, 0);
98                 // Diameter = 2*Radius
99
100                 spin = new JSpinner(od.getSpinnerModel());
101                 spin.setEditor(new SpinnerEditor(spin));
102                 panel.add(spin, "growx");
103
104                 panel.add(new UnitSelector(od), "growx");
105                 panel.add(new BasicSlider(od.getSliderModel(0, 0.04, 0.2)), "w 100lp, wrap 0px");
106
107                 JCheckBox check = new JCheckBox(od.getAutomaticAction());
108                 //// Automatic
109                 check.setText(trans.get("BodyTubecfg.checkbox.Automatic"));
110                 panel.add(check, "skip, span 2, wrap");
111
112
113                 ////  Inner diameter
114                 panel.add(new JLabel(trans.get("BodyTubecfg.lbl.Innerdiameter")));
115
116                 // Diameter = 2*Radius
117                 DoubleModel m = new DoubleModel(component, "InnerRadius", 2, UnitGroup.UNITS_LENGTH, 0);
118
119
120                 spin = new JSpinner(m.getSpinnerModel());
121                 spin.setEditor(new SpinnerEditor(spin));
122                 panel.add(spin, "growx");
123
124                 panel.add(new UnitSelector(m), "growx");
125                 panel.add(new BasicSlider(m.getSliderModel(new DoubleModel(0), od)), "w 100lp, wrap");
126
127
128                 ////  Wall thickness
129                 panel.add(new JLabel(trans.get("BodyTubecfg.lbl.Wallthickness")));
130
131                 m = new DoubleModel(component, "Thickness", UnitGroup.UNITS_LENGTH, 0);
132
133                 spin = new JSpinner(m.getSpinnerModel());
134                 spin.setEditor(new SpinnerEditor(spin));
135                 panel.add(spin, "growx");
136
137                 panel.add(new UnitSelector(m), "growx");
138                 panel.add(new BasicSlider(m.getSliderModel(0, 0.01)), "w 100lp, wrap 0px");
139
140                 //// Filled
141                 check = new JCheckBox(new BooleanModel(component, "Filled"));
142                 check.setText(trans.get("BodyTubecfg.checkbox.Filled"));
143                 panel.add(check, "skip, span 2, wrap");
144
145
146                 //// Material
147                 panel.add(materialPanel(new JPanel(new MigLayout()), Material.Type.BULK),
148                                 "cell 4 0, gapleft paragraph, aligny 0%, spany");
149
150                 //// General and General properties
151                 tabbedPane.insertTab(trans.get("BodyTubecfg.tab.General"), null, panel,
152                                 trans.get("BodyTubecfg.tab.Generalproperties"), 0);
153                 motorConfigPane = new MotorConfig((BodyTube) c);
154                 //// Motor and Motor mount configuration
155                 tabbedPane.insertTab(trans.get("BodyTubecfg.tab.Motor"), null, motorConfigPane,
156                                 trans.get("BodyTubecfg.tab.Motormountconf"), 1);
157                 tabbedPane.setSelectedIndex(0);
158
159
160         }
161
162         @Override
163         public void updateFields() {
164                 super.updateFields();
165                 if (motorConfigPane != null)
166                         motorConfigPane.updateFields();
167         }
168
169         @Override
170         public void invalidateModels() {
171                 super.invalidateModels();
172                 ((ComponentPresetDatabase)Application.getComponentPresetDao()).removeChangeListener(presetModel);
173         }
174
175
176 }