1 package net.sf.openrocket.gui.dialogs;
3 import java.awt.Dialog;
4 import java.awt.Window;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.awt.event.WindowAdapter;
8 import java.awt.event.WindowEvent;
9 import java.util.ArrayList;
10 import java.util.List;
12 import javax.swing.AbstractListModel;
13 import javax.swing.ComboBoxModel;
14 import javax.swing.JButton;
15 import javax.swing.JComboBox;
16 import javax.swing.JDialog;
17 import javax.swing.JLabel;
18 import javax.swing.JPanel;
19 import javax.swing.JTabbedPane;
21 import net.miginfocom.swing.MigLayout;
22 import net.sf.openrocket.gui.components.ResizeLabel;
23 import net.sf.openrocket.unit.Unit;
24 import net.sf.openrocket.unit.UnitGroup;
25 import net.sf.openrocket.util.GUIUtil;
26 import net.sf.openrocket.util.Prefs;
28 public class PreferencesDialog extends JDialog {
30 private final List<DefaultUnitSelector> unitSelectors = new ArrayList<DefaultUnitSelector>();
32 private PreferencesDialog() {
33 super((Window)null, "Preferences", Dialog.ModalityType.APPLICATION_MODAL);
35 JPanel panel = new JPanel(new MigLayout("fill, gap unrel","[grow]","[grow][]"));
37 JTabbedPane tabbedPane = new JTabbedPane();
38 panel.add(tabbedPane,"grow, wrap");
41 tabbedPane.addTab("Units", null, unitsPane(), "Default units");
42 tabbedPane.addTab("Confirmation", null, confirmationPane(), "Confirmation dialog settings");
46 JButton close = new JButton("Close");
47 close.addActionListener(new ActionListener() {
49 public void actionPerformed(ActionEvent arg0) {
50 PreferencesDialog.this.setVisible(false);
51 PreferencesDialog.this.dispose();
54 panel.add(close,"span, right, tag close");
56 this.setContentPane(panel);
59 this.setLocationRelativeTo(null);
61 this.addWindowListener(new WindowAdapter() {
63 public void windowClosed(WindowEvent e) {
64 Prefs.storeDefaultUnits();
68 GUIUtil.setDefaultButton(close);
69 GUIUtil.installEscapeCloseOperation(this);
73 private JPanel confirmationPane() {
74 JPanel panel = new JPanel(new MigLayout("fill"));
76 panel.add(new JLabel("Position to insert new body components:"));
77 panel.add(new JComboBox(new PrefChoiseSelector(Prefs.BODY_COMPONENT_INSERT_POSITION_KEY,
78 "Always ask", "Insert in middle", "Add to end")), "wrap para, sg combos");
80 panel.add(new JLabel("Confirm deletion of simulations:"));
81 panel.add(new JComboBox(new PrefBooleanSelector(Prefs.CONFIRM_DELETE_SIMULATION,
82 "Delete", "Confirm", true)), "wrap para, sg combos");
87 private JPanel unitsPane() {
88 JPanel panel = new JPanel(new MigLayout("", "[][]40lp[][]"));
91 panel.add(new JLabel("Select your preferred units:"), "span, wrap paragraph");
94 public static final UnitGroup UNITS_LENGTH;
95 public static final UnitGroup UNITS_MOTOR_DIMENSIONS;
96 public static final UnitGroup UNITS_DISTANCE;
98 public static final UnitGroup UNITS_VELOCITY;
99 public static final UnitGroup UNITS_ACCELERATION;
100 public static final UnitGroup UNITS_MASS;
101 public static final UnitGroup UNITS_FORCE;
102 public static final UnitGroup UNITS_IMPULSE;
104 public static final UnitGroup UNITS_STABILITY;
105 public static final UnitGroup UNITS_FLIGHT_TIME;
106 public static final UnitGroup UNITS_ROLL;
108 public static final UnitGroup UNITS_AREA;
109 public static final UnitGroup UNITS_DENSITY_LINE;
110 public static final UnitGroup UNITS_DENSITY_SURFACE;
111 public static final UnitGroup UNITS_DENSITY_BULK;
112 public static final UnitGroup UNITS_ROUGHNESS;
114 public static final UnitGroup UNITS_TEMPERATURE;
115 public static final UnitGroup UNITS_PRESSURE;
116 public static final UnitGroup UNITS_ANGLE;
119 panel.add(new JLabel("Rocket dimensions:"));
120 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_LENGTH));
121 panel.add(combo, "sizegroup boxes");
123 panel.add(new JLabel("Line density:"));
124 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_DENSITY_LINE));
125 panel.add(combo, "sizegroup boxes, wrap");
129 panel.add(new JLabel("Motor dimensions:"));
130 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_MOTOR_DIMENSIONS));
131 panel.add(combo, "sizegroup boxes");
133 panel.add(new JLabel("Surface density:"));
134 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_DENSITY_SURFACE));
135 panel.add(combo, "sizegroup boxes, wrap");
139 panel.add(new JLabel("Distance:"));
140 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_DISTANCE));
141 panel.add(combo, "sizegroup boxes");
143 panel.add(new JLabel("Bulk density::"));
144 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_DENSITY_BULK));
145 panel.add(combo, "sizegroup boxes, wrap");
149 panel.add(new JLabel("Velocity:"));
150 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_VELOCITY));
151 panel.add(combo, "sizegroup boxes");
153 panel.add(new JLabel("Surface roughness:"));
154 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_ROUGHNESS));
155 panel.add(combo, "sizegroup boxes, wrap");
159 panel.add(new JLabel("Acceleration:"));
160 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_ACCELERATION));
161 panel.add(combo, "sizegroup boxes");
163 panel.add(new JLabel("Area:"));
164 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_AREA));
165 panel.add(combo, "sizegroup boxes, wrap");
169 panel.add(new JLabel("Mass:"));
170 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_MASS));
171 panel.add(combo, "sizegroup boxes");
173 panel.add(new JLabel("Angle:"));
174 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_ANGLE));
175 panel.add(combo, "sizegroup boxes, wrap");
179 panel.add(new JLabel("Force:"));
180 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_FORCE));
181 panel.add(combo, "sizegroup boxes");
183 panel.add(new JLabel("Roll rate:"));
184 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_ROLL));
185 panel.add(combo, "sizegroup boxes, wrap");
189 panel.add(new JLabel("Total impulse:"));
190 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_IMPULSE));
191 panel.add(combo, "sizegroup boxes");
193 panel.add(new JLabel("Temperature:"));
194 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_TEMPERATURE));
195 panel.add(combo, "sizegroup boxes, wrap");
199 panel.add(new JLabel("Stability:"));
200 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_STABILITY));
201 panel.add(combo, "sizegroup boxes");
203 panel.add(new JLabel("Pressure:"));
204 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_PRESSURE));
205 panel.add(combo, "sizegroup boxes, wrap para");
209 JButton button = new JButton("Default metric");
210 button.addActionListener(new ActionListener() {
212 public void actionPerformed(ActionEvent e) {
213 UnitGroup.setDefaultMetricUnits();
214 for (DefaultUnitSelector s: unitSelectors)
218 panel.add(button, "spanx, split 2, grow");
220 button = new JButton("Default imperial");
221 button.addActionListener(new ActionListener() {
223 public void actionPerformed(ActionEvent e) {
224 UnitGroup.setDefaultImperialUnits();
225 for (DefaultUnitSelector s: unitSelectors)
229 panel.add(button, "grow, wrap para");
232 panel.add(new ResizeLabel("The effects will take place the next time you open a window.",-2),
242 private class DefaultUnitSelector extends AbstractListModel implements ComboBoxModel {
244 private final UnitGroup group;
245 public DefaultUnitSelector(UnitGroup group) {
247 unitSelectors.add(this);
251 public Object getSelectedItem() {
252 return group.getDefaultUnit();
255 public void setSelectedItem(Object item) {
256 if (!(item instanceof Unit)) {
257 throw new IllegalArgumentException("Illegal argument "+item);
259 group.setDefaultUnit(group.getUnitIndex((Unit)item));
262 public Object getElementAt(int index) {
263 return group.getUnit(index);
266 public int getSize() {
267 return group.getUnitCount();
271 public void fireChange() {
272 this.fireContentsChanged(this, 0, this.getSize());
278 private class PrefChoiseSelector extends AbstractListModel implements ComboBoxModel {
279 private final String preference;
280 private final String[] descriptions;
282 public PrefChoiseSelector(String preference, String ... descriptions) {
283 this.preference = preference;
284 this.descriptions = descriptions;
288 public Object getSelectedItem() {
289 return descriptions[Prefs.getChoise(preference, descriptions.length, 0)];
293 public void setSelectedItem(Object item) {
294 if (!(item instanceof String)) {
295 throw new IllegalArgumentException("Illegal argument "+item);
298 for (index = 0; index < descriptions.length; index++) {
299 if (((String)item).equalsIgnoreCase(descriptions[index]))
302 if (index >= descriptions.length) {
303 throw new IllegalArgumentException("Illegal argument "+item);
306 Prefs.putChoise(preference, index);
310 public Object getElementAt(int index) {
311 return descriptions[index];
314 public int getSize() {
315 return descriptions.length;
320 private class PrefBooleanSelector extends AbstractListModel implements ComboBoxModel {
321 private final String preference;
322 private final String trueDesc, falseDesc;
323 private final boolean def;
325 public PrefBooleanSelector(String preference, String falseDescription,
326 String trueDescription, boolean defaultState) {
327 this.preference = preference;
328 this.trueDesc = trueDescription;
329 this.falseDesc = falseDescription;
330 this.def = defaultState;
334 public Object getSelectedItem() {
335 if (Prefs.NODE.getBoolean(preference, def)) {
343 public void setSelectedItem(Object item) {
344 if (!(item instanceof String)) {
345 throw new IllegalArgumentException("Illegal argument "+item);
348 if (trueDesc.equals(item)) {
349 Prefs.NODE.putBoolean(preference, true);
350 } else if (falseDesc.equals(item)) {
351 Prefs.NODE.putBoolean(preference, false);
353 throw new IllegalArgumentException("Illegal argument "+item);
358 public Object getElementAt(int index) {
361 return def ? trueDesc : falseDesc;
364 return def ? falseDesc: trueDesc;
367 throw new IndexOutOfBoundsException("Boolean asked for index="+index);
371 public int getSize() {
378 //////// Singleton implementation ////////
380 private static PreferencesDialog dialog = null;
382 public static void showPreferences() {
383 if (dialog != null) {
386 dialog = new PreferencesDialog();
387 dialog.setVisible(true);