Updates
[debian/openrocket] / src / net / sf / openrocket / gui / StorageOptionChooser.java
index 9b8f1aa46190899fdf023f69809eb404b779837a..7e4b956c863ed85348673c1f79a8f4719f3d3398 100644 (file)
@@ -1,5 +1,8 @@
 package net.sf.openrocket.gui;
 
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
 import javax.swing.BorderFactory;
 import javax.swing.ButtonGroup;
 import javax.swing.JCheckBox;
@@ -17,6 +20,8 @@ import net.miginfocom.swing.MigLayout;
 import net.sf.openrocket.document.OpenRocketDocument;
 import net.sf.openrocket.document.Simulation;
 import net.sf.openrocket.document.StorageOptions;
+import net.sf.openrocket.file.OpenRocketSaver;
+import net.sf.openrocket.file.RocketSaver;
 import net.sf.openrocket.simulation.FlightData;
 import net.sf.openrocket.simulation.FlightDataBranch;
 
@@ -24,6 +29,8 @@ public class StorageOptionChooser extends JPanel {
        
        public static final double DEFAULT_SAVE_TIME_SKIP = 0.20;
 
+       private final OpenRocketDocument document;
+       
        private JRadioButton allButton;
        private JRadioButton someButton;
        private JRadioButton noneButton;
@@ -32,11 +39,30 @@ public class StorageOptionChooser extends JPanel {
        
        private JCheckBox compressButton;
        
+       private JLabel estimateLabel;
+       
        
        private boolean artificialEvent = false;
        
-       public StorageOptionChooser(StorageOptions opts) {
+       public StorageOptionChooser(OpenRocketDocument doc, StorageOptions opts) {
                super(new MigLayout());
+               
+               this.document = doc;
+               
+               
+               ChangeListener changeUpdater = new ChangeListener() {
+                       @Override
+                       public void stateChanged(ChangeEvent e) {
+                               updateEstimate();
+                       }
+               };
+               ActionListener actionUpdater = new ActionListener() {
+                       @Override
+                       public void actionPerformed(ActionEvent e) {
+                               updateEstimate();
+                       }
+               };
+               
 
                ButtonGroup buttonGroup = new ButtonGroup();
                String tip;
@@ -47,6 +73,7 @@ public class StorageOptionChooser extends JPanel {
                allButton.setToolTipText("<html>Store all simulated data.<br>" +
                                "This can result in very large files!");
                buttonGroup.add(allButton);
+               allButton.addActionListener(actionUpdater);
                this.add(allButton, "spanx, wrap rel");
                
                
@@ -55,6 +82,7 @@ public class StorageOptionChooser extends JPanel {
                                "Larger values result in smaller files.";
                someButton.setToolTipText(tip);
                buttonGroup.add(someButton);
+               someButton.addActionListener(actionUpdater);
                this.add(someButton, "");
                
                timeSpinner = new JSpinner(new SpinnerNumberModel(0.0, 0.0, 5.0, 0.1));
@@ -68,6 +96,7 @@ public class StorageOptionChooser extends JPanel {
                        }
                });
                this.add(timeSpinner, "wmin 55lp");
+               timeSpinner.addChangeListener(changeUpdater);
                
                JLabel label = new JLabel("seconds");
                label.setToolTipText(tip);
@@ -78,13 +107,22 @@ public class StorageOptionChooser extends JPanel {
                noneButton.setToolTipText("<html>Store only the values shown in the summary table.<br>" +
                                "This results in the smallest files.");
                buttonGroup.add(noneButton);
-
+               noneButton.addActionListener(actionUpdater);
                this.add(noneButton, "spanx, wrap 20lp");
                
                
+               
                compressButton = new JCheckBox("Compress file");
                compressButton.setToolTipText("Using compression reduces the file size significantly.");
-               this.add(compressButton, "spanx");
+               compressButton.addActionListener(actionUpdater);
+               this.add(compressButton, "spanx, wrap para");
+               
+               
+               // Estimate is updated in loadOptions(opts)
+               estimateLabel = new JLabel("");
+               estimateLabel.setToolTipText("An estimate on how large the resulting file would " +
+                               "be with the present options.");
+               this.add(estimateLabel, "spanx");
                
                
                this.setBorder(BorderFactory.createCompoundBorder(
@@ -117,6 +155,8 @@ public class StorageOptionChooser extends JPanel {
                
                // Compression checkbox
                compressButton.setSelected(opts.isCompressionEnabled());
+               
+               updateEstimate();
        }
        
        
@@ -140,6 +180,33 @@ public class StorageOptionChooser extends JPanel {
        
        
        
+       // TODO: MEDIUM: The estimation method always uses OpenRocketSaver!
+       private static final RocketSaver ROCKET_SAVER = new OpenRocketSaver();
+       
+       private void updateEstimate() {
+               StorageOptions opts = new StorageOptions();
+               
+               storeOptions(opts);
+               long size = ROCKET_SAVER.estimateFileSize(document, opts);
+               size = Math.max((size+512)/1024, 1);
+
+               String formatted;
+               
+               if (size >= 10000) {
+                       formatted = (size/1000) + " MB";
+               } else if (size >= 1000){
+                       formatted = (size/1000) + "." + ((size/100)%10) + " MB";
+               } else if (size >= 100) {
+                       formatted = ((size/10)*10) + " kB";
+               } else {
+                       formatted = size + " kB";
+               }
+
+               estimateLabel.setText("Estimated file size: " + formatted);
+       }
+       
+       
+       
        /**
         * Asks the user the storage options using a modal dialog window if the document
         * contains simulated data and the user has not explicitly set how to store the data.
@@ -187,7 +254,7 @@ public class StorageOptionChooser extends JPanel {
                }
                
                
-               StorageOptionChooser chooser = new StorageOptionChooser(options);
+               StorageOptionChooser chooser = new StorageOptionChooser(document, options);
                
                if (JOptionPane.showConfirmDialog(parent, chooser, "Save options", 
                                JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) !=
@@ -200,5 +267,4 @@ public class StorageOptionChooser extends JPanel {
                return true;
        }
        
-       
 }