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