1 package net.sf.openrocket.gui;
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;
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;
23 public class StorageOptionChooser extends JPanel {
25 public static final double DEFAULT_SAVE_TIME_SKIP = 0.20;
27 private JRadioButton allButton;
28 private JRadioButton someButton;
29 private JRadioButton noneButton;
31 private JSpinner timeSpinner;
33 private JCheckBox compressButton;
36 private boolean artificialEvent = false;
38 public StorageOptionChooser(StorageOptions opts) {
39 super(new MigLayout());
41 ButtonGroup buttonGroup = new ButtonGroup();
44 this.add(new JLabel("Simulated data to store:"), "spanx, wrap unrel");
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");
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, "");
60 timeSpinner = new JSpinner(new SpinnerNumberModel(0.0, 0.0, 5.0, 0.1));
61 timeSpinner.setToolTipText(tip);
62 timeSpinner.addChangeListener(new ChangeListener() {
64 public void stateChanged(ChangeEvent e) {
67 someButton.setSelected(true);
70 this.add(timeSpinner, "wmin 55lp");
72 JLabel label = new JLabel("seconds");
73 label.setToolTipText(tip);
74 this.add(label, "wrap rel");
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);
82 this.add(noneButton, "spanx, wrap 20lp");
85 compressButton = new JCheckBox("Compress file");
86 compressButton.setToolTipText("Using compression reduces the file size significantly.");
87 this.add(compressButton, "spanx");
90 this.setBorder(BorderFactory.createCompoundBorder(
91 BorderFactory.createEmptyBorder(0, 10, 0, 0),
92 BorderFactory.createTitledBorder("Save options")));
98 public void loadOptions(StorageOptions opts) {
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;
110 someButton.setSelected(true);
114 artificialEvent = true;
115 timeSpinner.setValue(t);
116 artificialEvent = false;
118 // Compression checkbox
119 compressButton.setSelected(opts.isCompressionEnabled());
123 public void storeOptions(StorageOptions opts) {
126 if (allButton.isSelected()) {
127 t = StorageOptions.SIMULATION_DATA_ALL;
128 } else if (noneButton.isSelected()) {
129 t = StorageOptions.SIMULATION_DATA_NONE;
131 t = (Double)timeSpinner.getValue();
134 opts.setSimulationTimeSkip(t);
136 opts.setCompressionEnabled(compressButton.isSelected());
138 opts.setExplicitlySet(true);
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.
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.
151 public static boolean verifyStorageOptions(OpenRocketDocument document, JFrame parent) {
152 StorageOptions options = document.getDefaultStorageOptions();
154 if (options.isExplicitlySet()) {
155 // User has explicitly set the values, save as is
160 boolean hasData = false;
163 for (Simulation s: document.getSimulations()) {
164 if (s.getStatus() == Simulation.Status.NOT_SIMULATED ||
165 s.getStatus() == Simulation.Status.EXTERNAL)
168 FlightData data = s.getSimulatedData();
172 for (int i=0; i < data.getBranchCount(); i++) {
173 FlightDataBranch branch = data.getBranch(i);
176 if (branch.getLength() > 0) {
178 break simulationLoop;
185 // No data to store, do not ask only about compression
190 StorageOptionChooser chooser = new StorageOptionChooser(options);
192 if (JOptionPane.showConfirmDialog(parent, chooser, "Save options",
193 JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) !=
194 JOptionPane.OK_OPTION) {
199 chooser.storeOptions(options);