altoslib: Improve EEprom download
[fw/altos] / altosuilib / AltosEepromMonitorUI.java
index 3427fe0fac5f5a8e675f056a2c5c6e3df3295ffb..fc6c95c6e01b9b5b37057e74df9b8f31c955e736 100644 (file)
 
 package org.altusmetrum.altosuilib_12;
 
+import java.io.*;
+import java.util.*;
+import java.util.concurrent.*;
 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 import org.altusmetrum.altoslib_12.*;
 
+       class result_holder {
+               static int result;
+       }
+
 public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMonitor {
        JFrame          owner;
        Container       pane;
@@ -32,7 +39,6 @@ public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMo
        JLabel          file_label;
        JLabel          serial_value;
        JLabel          flight_value;
-       JLabel          file_value;
        JButton         cancel;
        JProgressBar    pbar;
        ActionListener  listener;
@@ -42,6 +48,8 @@ public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMo
        public AltosEepromMonitorUI(JFrame owner) {
                super (owner, "Download Flight Data", false);
 
+               setMinimumSize(new Dimension(600, 100));
+
                this.owner = owner;
 
                GridBagConstraints c;
@@ -85,30 +93,11 @@ public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMo
                flight_value = new JLabel("");
                pane.add(flight_value, c);
 
-               c = new GridBagConstraints();
-               c.fill = GridBagConstraints.NONE;
-               c.gridx = 0; c.gridy = 2;
-               c.anchor = GridBagConstraints.LINE_START;
-               c.insets = il;
-               file_label = new JLabel("File:");
-               pane.add(file_label, c);
-
-               c = new GridBagConstraints();
-               c.fill = GridBagConstraints.HORIZONTAL;
-               c.weightx = 1;
-               c.gridx = 1; c.gridy = 2;
-               c.anchor = GridBagConstraints.LINE_START;
-               c.insets = ir;
-               file_value = new JLabel("");
-               pane.add(file_value, c);
-
                pbar = new JProgressBar();
                pbar.setMinimum(0);
                pbar.setMaximum(progress_max);
-               pbar.setValue(0);
-               pbar.setString("startup");
                pbar.setStringPainted(true);
-               pbar.setPreferredSize(new Dimension(600, 20));
+               set_block_internal(0);
                c = new GridBagConstraints();
                c.fill = GridBagConstraints.HORIZONTAL;
                c.anchor = GridBagConstraints.CENTER;
@@ -118,7 +107,6 @@ public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMo
                c.insets = ib;
                pane.add(pbar, c);
 
-
                cancel = new JButton("Cancel");
                c = new GridBagConstraints();
                c.fill = GridBagConstraints.NONE;
@@ -141,8 +129,9 @@ public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMo
                final Thread eeprom_thread = in_eeprom_thread;
                cancel.addActionListener(new ActionListener() {
                                public void actionPerformed(ActionEvent e) {
-                                       if (eeprom_thread != null)
+                                       if (eeprom_thread != null) {
                                                eeprom_thread.interrupt();
+                                       }
                                }
                        });
        }
@@ -162,6 +151,7 @@ public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMo
                s = String.format("block %d of %d", block, max_block);
 
                pbar.setString(s);
+               pbar.setStringPainted(true);
                pbar.setValue((int) (pos * progress_max));
        }
 
@@ -216,23 +206,6 @@ public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMo
                SwingUtilities.invokeLater(r);
        }
 
-       private void set_filename_internal(String filename) {
-               file_value.setText(String.format("%s", filename));
-       }
-
-       public void set_filename(String in_filename) {
-               final String filename = in_filename;
-               Runnable r = new Runnable() {
-                               public void run() {
-                                       try {
-                                               set_filename_internal(filename);
-                                       } catch (Exception ex) {
-                                       }
-                               }
-                       };
-               SwingUtilities.invokeLater(r);
-       }
-
        private void done_internal(boolean success) {
                listener.actionPerformed(new ActionEvent(this,
                                                         success ? 1 : 0,
@@ -258,7 +231,6 @@ public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMo
                set_max(1);
                set_block_internal(0);
                set_flight_internal(0);
-               set_filename_internal("");
        }
 
        public void reset() {
@@ -293,6 +265,29 @@ public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMo
                                              joption_message_type);
        }
 
+       public Boolean check_overwrite(File in_file) {
+               final Semaphore check_overwrite_done = new Semaphore(0);
+               final File file = in_file;
+               final result_holder result = new result_holder();
+
+               Runnable r = new Runnable() {
+                               public void run() {
+                                       result_holder.result = JOptionPane.showConfirmDialog(owner,
+                                                                                            String.format("\"%s\" already exists, overwrite?",
+                                                                                                          file.toString()),
+                                                                                            "Overwrite Existing File?",
+                                                                                            JOptionPane.YES_NO_OPTION);
+                                       check_overwrite_done.release();
+                               }
+                       };
+
+               SwingUtilities.invokeLater(r);
+               try {
+                       check_overwrite_done.acquire();
+               } catch (Exception e) {}
+               return result_holder.result == JOptionPane.YES_OPTION;
+       }
+
        public void show_message(String in_message, String in_title, int in_message_type) {
                final String message = in_message;
                final String title = in_title;