Initial commit
[debian/openrocket] / src / net / sf / openrocket / gui / StorageOptionChooser.java
1 package net.sf.openrocket.gui;
2
3 import javax.swing.BorderFactory;
4 import javax.swing.ButtonGroup;
5 import javax.swing.JCheckBox;
6 import javax.swing.JFrame;
7 import javax.swing.JLabel;
8 import javax.swing.JOptionPane;
9 import javax.swing.JPanel;
10 import javax.swing.JRadioButton;
11 import javax.swing.JSpinner;
12 import javax.swing.SpinnerNumberModel;
13 import javax.swing.event.ChangeEvent;
14 import javax.swing.event.ChangeListener;
15
16 import net.miginfocom.swing.MigLayout;
17 import net.sf.openrocket.document.OpenRocketDocument;
18 import net.sf.openrocket.document.Simulation;
19 import net.sf.openrocket.document.StorageOptions;
20 import net.sf.openrocket.simulation.FlightData;
21 import net.sf.openrocket.simulation.FlightDataBranch;
22
23 public class StorageOptionChooser extends JPanel {
24         
25         public static final double DEFAULT_SAVE_TIME_SKIP = 0.20;
26
27         private JRadioButton allButton;
28         private JRadioButton someButton;
29         private JRadioButton noneButton;
30         
31         private JSpinner timeSpinner;
32         
33         private JCheckBox compressButton;
34         
35         
36         private boolean artificialEvent = false;
37         
38         public StorageOptionChooser(StorageOptions opts) {
39                 super(new MigLayout());
40
41                 ButtonGroup buttonGroup = new ButtonGroup();
42                 String tip;
43                 
44                 this.add(new JLabel("Simulated data to store:"), "spanx, wrap unrel");
45
46                 allButton = new JRadioButton("All simulated data");
47                 allButton.setToolTipText("<html>Store all simulated data.<br>" +
48                                 "This can result in very large files!");
49                 buttonGroup.add(allButton);
50                 this.add(allButton, "spanx, wrap rel");
51                 
52                 
53                 someButton = new JRadioButton("Every");
54                 tip = "<html>Store plottable values approximately this far apart.<br>" +
55                                 "Larger values result in smaller files.";
56                 someButton.setToolTipText(tip);
57                 buttonGroup.add(someButton);
58                 this.add(someButton, "");
59                 
60                 timeSpinner = new JSpinner(new SpinnerNumberModel(0.0, 0.0, 5.0, 0.1));
61                 timeSpinner.setToolTipText(tip);
62                 timeSpinner.addChangeListener(new ChangeListener() {
63                         @Override
64                         public void stateChanged(ChangeEvent e) {
65                                 if (artificialEvent)
66                                         return;
67                                 someButton.setSelected(true);
68                         }
69                 });
70                 this.add(timeSpinner, "wmin 55lp");
71                 
72                 JLabel label = new JLabel("seconds");
73                 label.setToolTipText(tip);
74                 this.add(label, "wrap rel");
75                 
76                 
77                 noneButton = new JRadioButton("Only primary figures");
78                 noneButton.setToolTipText("<html>Store only the values shown in the summary table.<br>" +
79                                 "This results in the smallest files.");
80                 buttonGroup.add(noneButton);
81
82                 this.add(noneButton, "spanx, wrap 20lp");
83                 
84                 
85                 compressButton = new JCheckBox("Compress file");
86                 compressButton.setToolTipText("Using compression reduces the file size significantly.");
87                 this.add(compressButton, "spanx");
88                 
89                 
90                 this.setBorder(BorderFactory.createCompoundBorder(
91                                 BorderFactory.createEmptyBorder(0, 10, 0, 0),
92                                 BorderFactory.createTitledBorder("Save options")));
93                 
94                 loadOptions(opts);
95         }
96         
97         
98         public void loadOptions(StorageOptions opts) {
99                 double t;
100                 
101                 // Data storage radio button
102                 t = opts.getSimulationTimeSkip();
103                 if (t == StorageOptions.SIMULATION_DATA_ALL) {
104                         allButton.setSelected(true);
105                         t = DEFAULT_SAVE_TIME_SKIP;
106                 } else if (t == StorageOptions.SIMULATION_DATA_NONE) {
107                         noneButton.setSelected(true);
108                         t = DEFAULT_SAVE_TIME_SKIP;
109                 } else {
110                         someButton.setSelected(true);
111                 }
112                 
113                 // Time skip spinner
114                 artificialEvent = true;
115                 timeSpinner.setValue(t);
116                 artificialEvent = false;
117                 
118                 // Compression checkbox
119                 compressButton.setSelected(opts.isCompressionEnabled());
120         }
121         
122         
123         public void storeOptions(StorageOptions opts) {
124                 double t;
125                 
126                 if (allButton.isSelected()) {
127                         t = StorageOptions.SIMULATION_DATA_ALL;
128                 } else if (noneButton.isSelected()) {
129                         t = StorageOptions.SIMULATION_DATA_NONE;
130                 } else {
131                         t = (Double)timeSpinner.getValue();
132                 }
133                 
134                 opts.setSimulationTimeSkip(t);
135                 
136                 opts.setCompressionEnabled(compressButton.isSelected());
137                 
138                 opts.setExplicitlySet(true);
139         }
140         
141         
142         
143         /**
144          * Asks the user the storage options using a modal dialog window if the document
145          * contains simulated data and the user has not explicitly set how to store the data.
146          * 
147          * @param document      the document to check.
148          * @param parent        the parent frame for the dialog.
149          * @return                      <code>true</code> to continue, <code>false</code> if the user cancelled.
150          */
151         public static boolean verifyStorageOptions(OpenRocketDocument document, JFrame parent) {
152                 StorageOptions options = document.getDefaultStorageOptions();
153                 
154                 if (options.isExplicitlySet()) {
155                         // User has explicitly set the values, save as is
156                         return true;
157                 }
158                 
159                 
160                 boolean hasData = false;
161                 
162                 simulationLoop:
163                         for (Simulation s: document.getSimulations()) {
164                                 if (s.getStatus() == Simulation.Status.NOT_SIMULATED ||
165                                                 s.getStatus() == Simulation.Status.EXTERNAL)
166                                         continue;
167                                 
168                                 FlightData data = s.getSimulatedData();
169                                 if (data == null)
170                                         continue;
171                                 
172                                 for (int i=0; i < data.getBranchCount(); i++) {
173                                         FlightDataBranch branch = data.getBranch(i);
174                                         if (branch == null)
175                                                 continue;
176                                         if (branch.getLength() > 0) {
177                                                 hasData = true;
178                                                 break simulationLoop;
179                                         }
180                                 }
181                         }
182                 
183
184                 if (!hasData) {
185                         // No data to store, do not ask only about compression
186                         return true;
187                 }
188                 
189                 
190                 StorageOptionChooser chooser = new StorageOptionChooser(options);
191                 
192                 if (JOptionPane.showConfirmDialog(parent, chooser, "Save options", 
193                                 JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) !=
194                                         JOptionPane.OK_OPTION) {
195                         // User cancelled
196                         return false;
197                 }
198                 
199                 chooser.storeOptions(options);
200                 return true;
201         }
202         
203         
204 }