altosui: Clean up flash code to ensure swing gets called from right thread
[fw/altos] / altosui / AltosFlashUI.java
index f63097ac408ad1f71356311365b487203ce8788f..0302ccd38d9231fd25e8166d2547422efe5c1d01 100644 (file)
@@ -30,7 +30,7 @@ import java.util.concurrent.LinkedBlockingQueue;
 
 public class AltosFlashUI
        extends JDialog
-       implements Runnable, ActionListener
+       implements ActionListener
 {
        Container       pane;
        Box             box;
@@ -49,13 +49,21 @@ public class AltosFlashUI
 
        public void actionPerformed(ActionEvent e) {
                if (e.getSource() == cancel) {
-                       abort();
+                       setVisible(false);
                        dispose();
                } else {
                        String  cmd = e.getActionCommand();
-                       if (cmd.equals("done"))
-                               ;
-                       else if (cmd.equals("start")) {
+                       if (e.getID() == -1) {
+                               JOptionPane.showMessageDialog(frame,
+                                                             e.getActionCommand(),
+                                                             file.toString(),
+                                                             JOptionPane.ERROR_MESSAGE);
+                               setVisible(false);
+                               dispose();
+                       } else if (cmd.equals("done")) {
+                               setVisible(false);
+                               dispose();
+                       } else if (cmd.equals("start")) {
                                setVisible(true);
                        } else {
                                pbar.setValue(e.getID());
@@ -64,52 +72,6 @@ public class AltosFlashUI
                }
        }
 
-       public void run() {
-               try {
-                       flash = new AltosFlash(file, debug_dongle);
-                       flash.addActionListener(this);
-                       AltosRomconfigUI romconfig_ui = new AltosRomconfigUI (frame);
-
-                       romconfig_ui.set(flash.romconfig());
-                       AltosRomconfig romconfig = romconfig_ui.showDialog();
-
-                       if (romconfig != null && romconfig.valid()) {
-                               flash.set_romconfig(romconfig);
-                               serial_value.setText(String.format("%d",
-                                                                  flash.romconfig().serial_number));
-                               file_value.setText(file.toString());
-                               setVisible(true);
-                               flash.flash();
-                               flash = null;
-                       }
-               } catch (FileNotFoundException ee) {
-                       JOptionPane.showMessageDialog(frame,
-                                                     "Cannot open image",
-                                                     file.toString(),
-                                                     JOptionPane.ERROR_MESSAGE);
-               } catch (AltosSerialInUseException si) {
-                       JOptionPane.showMessageDialog(frame,
-                                                     String.format("Device \"%s\" already in use",
-                                                                   debug_dongle.toShortString()),
-                                                     "Device in use",
-                                                     JOptionPane.ERROR_MESSAGE);
-               } catch (IOException e) {
-                       JOptionPane.showMessageDialog(frame,
-                                                     e.getMessage(),
-                                                     file.toString(),
-                                                     JOptionPane.ERROR_MESSAGE);
-               } catch (InterruptedException ie) {
-               } finally {
-                       abort();
-               }
-               dispose();
-       }
-
-       public void abort() {
-               if (flash != null)
-                       flash.abort();
-       }
-
        public void build_dialog() {
                GridBagConstraints c;
                Insets il = new Insets(4,4,4,4);
@@ -212,7 +174,39 @@ public class AltosFlashUI
                if (file != null)
                        AltosPreferences.set_firmwaredir(file.getParentFile());
 
-               thread = new Thread(this);
-               thread.start();
+               try {
+                       flash = new AltosFlash(file, debug_dongle);
+                       flash.addActionListener(this);
+                       AltosRomconfigUI romconfig_ui = new AltosRomconfigUI (frame);
+
+                       romconfig_ui.set(flash.romconfig());
+                       AltosRomconfig romconfig = romconfig_ui.showDialog();
+
+                       if (romconfig != null && romconfig.valid()) {
+                               flash.set_romconfig(romconfig);
+                               serial_value.setText(String.format("%d",
+                                                                  flash.romconfig().serial_number));
+                               file_value.setText(file.toString());
+                               setVisible(true);
+                               flash.flash();
+                       }
+               } catch (FileNotFoundException ee) {
+                       JOptionPane.showMessageDialog(frame,
+                                                     "Cannot open image",
+                                                     file.toString(),
+                                                     JOptionPane.ERROR_MESSAGE);
+               } catch (AltosSerialInUseException si) {
+                       JOptionPane.showMessageDialog(frame,
+                                                     String.format("Device \"%s\" already in use",
+                                                                   debug_dongle.toShortString()),
+                                                     "Device in use",
+                                                     JOptionPane.ERROR_MESSAGE);
+               } catch (IOException e) {
+                       JOptionPane.showMessageDialog(frame,
+                                                     e.getMessage(),
+                                                     file.toString(),
+                                                     JOptionPane.ERROR_MESSAGE);
+               } catch (InterruptedException ie) {
+               }
        }
 }
\ No newline at end of file