c06cbbc667c23266076801a7eece076046d7b3f8
[debian/openrocket] / src / net / sf / openrocket / gui / dialogs / PreferencesDialog.java
1 package net.sf.openrocket.gui.dialogs;
2
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;
11
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;
20
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;
27
28 public class PreferencesDialog extends JDialog {
29         
30         private final List<DefaultUnitSelector> unitSelectors = new ArrayList<DefaultUnitSelector>();
31
32         private PreferencesDialog() {
33                 super((Window)null, "Preferences", Dialog.ModalityType.APPLICATION_MODAL);
34                 
35                 JPanel panel = new JPanel(new MigLayout("fill, gap unrel","[grow]","[grow][]"));
36                                 
37                 JTabbedPane tabbedPane = new JTabbedPane();
38                 panel.add(tabbedPane,"grow, wrap");
39                 
40
41                 tabbedPane.addTab("Units", null, unitsPane(), "Default units");
42                 tabbedPane.addTab("Confirmation", null, confirmationPane(), "Confirmation dialog settings");
43                 
44                 
45                 
46                 JButton close = new JButton("Close");
47                 close.addActionListener(new ActionListener() {
48                         @Override
49                         public void actionPerformed(ActionEvent arg0) {
50                                 PreferencesDialog.this.setVisible(false);
51                                 PreferencesDialog.this.dispose();
52                         }
53                 });
54                 panel.add(close,"span, right, tag close");
55                 
56                 this.setContentPane(panel);
57                 pack();
58                 setAlwaysOnTop(true);
59                 this.setLocationRelativeTo(null);
60                 
61                 this.addWindowListener(new WindowAdapter() {
62                         @Override
63                         public void windowClosed(WindowEvent e) {
64                                 Prefs.storeDefaultUnits();
65                         }
66                 });
67
68                 GUIUtil.setDefaultButton(close);
69                 GUIUtil.installEscapeCloseOperation(this);
70         }
71         
72         
73         private JPanel confirmationPane() {
74                 JPanel panel = new JPanel(new MigLayout("fill"));
75                 
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");
79                 
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");
83                 
84                 return panel;
85         }
86         
87         private JPanel unitsPane() {
88                 JPanel panel = new JPanel(new MigLayout("", "[][]40lp[][]"));
89                 JComboBox combo;
90                 
91                 panel.add(new JLabel("Select your preferred units:"), "span, wrap paragraph");
92                 
93 /*
94                 public static final UnitGroup UNITS_LENGTH;
95                 public static final UnitGroup UNITS_MOTOR_DIMENSIONS;
96                 public static final UnitGroup UNITS_DISTANCE;
97                 
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;
103
104                 public static final UnitGroup UNITS_STABILITY;
105                 public static final UnitGroup UNITS_FLIGHT_TIME;
106                 public static final UnitGroup UNITS_ROLL;
107                 
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;
113                 
114                 public static final UnitGroup UNITS_TEMPERATURE;
115                 public static final UnitGroup UNITS_PRESSURE;
116                 public static final UnitGroup UNITS_ANGLE;
117 */
118                 
119                 panel.add(new JLabel("Rocket dimensions:"));
120                 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_LENGTH));
121                 panel.add(combo, "sizegroup boxes");
122                 
123                 panel.add(new JLabel("Line density:"));
124                 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_DENSITY_LINE));
125                 panel.add(combo, "sizegroup boxes, wrap");
126                 
127                 
128                 
129                 panel.add(new JLabel("Motor dimensions:"));
130                 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_MOTOR_DIMENSIONS));
131                 panel.add(combo, "sizegroup boxes");
132                 
133                 panel.add(new JLabel("Surface density:"));
134                 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_DENSITY_SURFACE));
135                 panel.add(combo, "sizegroup boxes, wrap");
136                 
137
138                 
139                 panel.add(new JLabel("Distance:"));
140                 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_DISTANCE));
141                 panel.add(combo, "sizegroup boxes");
142                 
143                 panel.add(new JLabel("Bulk density::"));
144                 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_DENSITY_BULK));
145                 panel.add(combo, "sizegroup boxes, wrap");
146                 
147
148                 
149                 panel.add(new JLabel("Velocity:"));
150                 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_VELOCITY));
151                 panel.add(combo, "sizegroup boxes");
152
153                 panel.add(new JLabel("Surface roughness:"));
154                 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_ROUGHNESS));
155                 panel.add(combo, "sizegroup boxes, wrap");
156                 
157                 
158                 
159                 panel.add(new JLabel("Acceleration:"));
160                 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_ACCELERATION));
161                 panel.add(combo, "sizegroup boxes");
162
163                 panel.add(new JLabel("Area:"));
164                 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_AREA));
165                 panel.add(combo, "sizegroup boxes, wrap");
166                 
167                 
168
169                 panel.add(new JLabel("Mass:"));
170                 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_MASS));
171                 panel.add(combo, "sizegroup boxes");
172                 
173                 panel.add(new JLabel("Angle:"));
174                 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_ANGLE));
175                 panel.add(combo, "sizegroup boxes, wrap");
176                 
177
178                 
179                 panel.add(new JLabel("Force:"));
180                 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_FORCE));
181                 panel.add(combo, "sizegroup boxes");
182                 
183                 panel.add(new JLabel("Roll rate:"));
184                 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_ROLL));
185                 panel.add(combo, "sizegroup boxes, wrap");
186                 
187
188                 
189                 panel.add(new JLabel("Total impulse:"));
190                 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_IMPULSE));
191                 panel.add(combo, "sizegroup boxes");
192                 
193                 panel.add(new JLabel("Temperature:"));
194                 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_TEMPERATURE));
195                 panel.add(combo, "sizegroup boxes, wrap");
196                 
197
198                 
199                 panel.add(new JLabel("Stability:"));
200                 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_STABILITY));
201                 panel.add(combo, "sizegroup boxes");
202
203                 panel.add(new JLabel("Pressure:"));
204                 combo = new JComboBox(new DefaultUnitSelector(UnitGroup.UNITS_PRESSURE));
205                 panel.add(combo, "sizegroup boxes, wrap para");
206                 
207                 
208                 
209                 JButton button = new JButton("Default metric");
210                 button.addActionListener(new ActionListener() {
211                         @Override
212                         public void actionPerformed(ActionEvent e) {
213                                 UnitGroup.setDefaultMetricUnits();
214                                 for (DefaultUnitSelector s: unitSelectors)
215                                         s.fireChange();
216                         }
217                 });
218                 panel.add(button, "spanx, split 2, grow");
219                 
220                 button = new JButton("Default imperial");
221                 button.addActionListener(new ActionListener() {
222                         @Override
223                         public void actionPerformed(ActionEvent e) {
224                                 UnitGroup.setDefaultImperialUnits();
225                                 for (DefaultUnitSelector s: unitSelectors)
226                                         s.fireChange();
227                         }
228                 });
229                 panel.add(button, "grow, wrap para");
230                 
231                 
232                 panel.add(new ResizeLabel("The effects will take place the next time you open a window.",-2),
233                                 "spanx, wrap");
234                 
235
236                 return panel;
237         }
238         
239         
240         
241         
242         private class DefaultUnitSelector extends AbstractListModel implements ComboBoxModel {
243                 
244                 private final UnitGroup group;
245                 public DefaultUnitSelector(UnitGroup group) {
246                         this.group = group;
247                         unitSelectors.add(this);
248                 }
249                 
250                 @Override
251                 public Object getSelectedItem() {
252                         return group.getDefaultUnit();
253                 }
254                 @Override
255                 public void setSelectedItem(Object item) {
256                         if (!(item instanceof Unit)) {
257                                 throw new IllegalArgumentException("Illegal argument "+item);
258                         }
259                         group.setDefaultUnit(group.getUnitIndex((Unit)item));
260                 }
261                 @Override
262                 public Object getElementAt(int index) {
263                         return group.getUnit(index);
264                 }
265                 @Override
266                 public int getSize() {
267                         return group.getUnitCount();
268                 }
269                 
270                 
271                 public void fireChange() {
272                         this.fireContentsChanged(this, 0, this.getSize());
273                 }
274         }
275         
276
277         
278         private class PrefChoiseSelector extends AbstractListModel implements ComboBoxModel {
279                 private final String preference;
280                 private final String[] descriptions;
281                 
282                 public PrefChoiseSelector(String preference, String ... descriptions) {
283                         this.preference = preference;
284                         this.descriptions = descriptions;
285                 }
286                 
287                 @Override
288                 public Object getSelectedItem() {
289                         return descriptions[Prefs.getChoise(preference, descriptions.length, 0)];
290                 }
291                 
292                 @Override
293                 public void setSelectedItem(Object item) {
294                         if (!(item instanceof String)) {
295                                 throw new IllegalArgumentException("Illegal argument "+item);
296                         }
297                         int index;
298                         for (index = 0; index < descriptions.length; index++) {
299                                 if (((String)item).equalsIgnoreCase(descriptions[index]))
300                                         break;
301                         }
302                         if (index >= descriptions.length) {
303                                 throw new IllegalArgumentException("Illegal argument "+item);
304                         }
305                         
306                         Prefs.putChoise(preference, index);
307                 }
308                 
309                 @Override
310                 public Object getElementAt(int index) {
311                         return descriptions[index];
312                 }
313                 @Override
314                 public int getSize() {
315                         return descriptions.length;
316                 }
317         }
318         
319
320         private class PrefBooleanSelector extends AbstractListModel implements ComboBoxModel {
321                 private final String preference;
322                 private final String trueDesc, falseDesc;
323                 private final boolean def;
324                 
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;
331                 }
332                 
333                 @Override
334                 public Object getSelectedItem() {
335                         if (Prefs.NODE.getBoolean(preference, def)) {
336                                 return trueDesc;
337                         } else {
338                                 return falseDesc;
339                         }
340                 }
341                 
342                 @Override
343                 public void setSelectedItem(Object item) {
344                         if (!(item instanceof String)) {
345                                 throw new IllegalArgumentException("Illegal argument "+item);
346                         }
347                         
348                         if (trueDesc.equals(item)) {
349                                 Prefs.NODE.putBoolean(preference, true);
350                         } else if (falseDesc.equals(item)) {
351                                 Prefs.NODE.putBoolean(preference, false);
352                         } else {
353                                 throw new IllegalArgumentException("Illegal argument "+item);
354                         }
355                 }
356                 
357                 @Override
358                 public Object getElementAt(int index) {
359                         switch (index) {
360                         case 0:
361                                 return def ? trueDesc : falseDesc;
362
363                         case 1:
364                                 return def ? falseDesc: trueDesc;
365                                 
366                         default:
367                                 throw new IndexOutOfBoundsException("Boolean asked for index="+index);
368                         }
369                 }
370                 @Override
371                 public int getSize() {
372                         return 2;
373                 }
374         }
375         
376         
377         
378         ////////  Singleton implementation  ////////
379         
380         private static PreferencesDialog dialog = null;
381         
382         public static void showPreferences() {
383                 if (dialog != null) {
384                         dialog.dispose();
385                 }
386                 dialog = new PreferencesDialog();
387                 dialog.setVisible(true);
388         }
389         
390         
391 }