altos: Add gyro-based orientation tracking
[fw/altos] / altosui / AltosFlashUI.java
index 3874b5004995cb0269a612005b2949783f2747de..6eccface19815f29c00a2ef4ddd366d3545aee0e 100644 (file)
@@ -21,15 +21,13 @@ import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 import javax.swing.filechooser.FileNameExtensionFilter;
-import javax.swing.table.*;
 import java.io.*;
-import java.util.*;
-import java.text.*;
-import java.util.prefs.*;
 import java.util.concurrent.*;
+import org.altusmetrum.altoslib_2.*;
+import org.altusmetrum.altosuilib_1.*;
 
 public class AltosFlashUI
-       extends JDialog
+       extends AltosUIDialog
        implements ActionListener
 {
        Container       pane;
@@ -161,7 +159,7 @@ public class AltosFlashUI
        boolean select_source_file() {
                JFileChooser    hexfile_chooser = new JFileChooser();
 
-               File firmwaredir = AltosPreferences.firmwaredir();
+               File firmwaredir = AltosUIPreferences.firmwaredir();
                if (firmwaredir != null)
                        hexfile_chooser.setCurrentDirectory(firmwaredir);
 
@@ -174,12 +172,12 @@ public class AltosFlashUI
                file = hexfile_chooser.getSelectedFile();
                if (file == null)
                        return false;
-               AltosPreferences.set_firmwaredir(file.getParentFile());
+               AltosUIPreferences.set_firmwaredir(file.getParentFile());
                return true;
        }
 
        boolean select_debug_dongle() {
-               debug_dongle = AltosDeviceDialog.show(frame, Altos.product_any);
+               debug_dongle = AltosDeviceUIDialog.show(frame, Altos.product_any);
 
                if (debug_dongle == null)
                        return false;
@@ -200,8 +198,8 @@ public class AltosFlashUI
        void exception (Exception e) {
                if (e instanceof FileNotFoundException) {
                        JOptionPane.showMessageDialog(frame,
-                                                     "Cannot open image",
-                                                     file.toString(),
+                                                     ((FileNotFoundException) e).getMessage(),
+                                                     "Cannot open file",
                                                      JOptionPane.ERROR_MESSAGE);
                } else if (e instanceof AltosSerialInUseException) {
                        JOptionPane.showMessageDialog(frame,
@@ -217,15 +215,30 @@ public class AltosFlashUI
                }
        }
 
-       class flash_task implements Runnable {
+       class flash_task implements Runnable, AltosFlashListener {
                AltosFlashUI    ui;
                Thread          t;
                AltosFlash      flash;
 
+               public void position(String in_s, int in_percent) {
+                       final String s = in_s;
+                       final int percent = in_percent;
+                       Runnable r = new Runnable() {
+                                       public void run() {
+                                               try {
+                                                       ui.actionPerformed(new ActionEvent(this,
+                                                                                          percent,
+                                                                                          s));
+                                               } catch (Exception ex) {
+                                               }
+                                       }
+                               };
+                       SwingUtilities.invokeLater(r);
+               }
+
                public void run () {
                        try {
-                               flash = new AltosFlash(ui.file, ui.debug_dongle);
-                               flash.addActionListener(ui);
+                               flash = new AltosFlash(ui.file, new AltosSerial(ui.debug_dongle), this);
 
                                final AltosRomconfig    current_config = flash.romconfig();
 
@@ -234,30 +247,40 @@ public class AltosFlashUI
                                                public void run() {
                                                        ui.flash = flash;
                                                        ui.update_rom_config_info(current_config);
-                                                       System.out.printf("Done updating rom config info\n");
                                                        await_rom_config.release();
                                                }
                                        });
-                               System.out.printf("Waiting for rom configuration updates\n");
                                await_rom_config.acquire();
-                               System.out.printf("Got rom config update\n");
 
                                if (ui.rom_config != null) {
-                                       System.out.printf("rom_config not null\n");
                                        flash.set_romconfig(ui.rom_config);
                                        flash.flash();
                                }
-                       } catch (Exception ee) {
+                       } catch (InterruptedException ee) {
                                final Exception e = ee;
-                               System.out.printf("exception %s\n", e.toString());
                                SwingUtilities.invokeLater(new Runnable() {
                                                public void run() {
                                                        ui.exception(e);
                                                }
                                        });
+                       } catch (IOException ee) {
+                               final Exception e = ee;
+                               SwingUtilities.invokeLater(new Runnable() {
+                                               public void run() {
+                                                       ui.exception(e);
+                                               }
+                                       });
+                       } catch (AltosSerialInUseException ee) {
+                               final Exception e = ee;
+                               SwingUtilities.invokeLater(new Runnable() {
+                                               public void run() {
+                                                       ui.exception(e);
+                                               }
+                                       });
+                       } finally {
+                               if (flash != null)
+                                       flash.close();
                        }
-                       if (flash != null)
-                               flash.close();
                }
 
                public flash_task(AltosFlashUI in_ui) {