From: Keith Packard Date: Fri, 26 Nov 2010 00:23:18 +0000 (-0800) Subject: altosui: Flight data download GUI operations called only from main thread X-Git-Tag: debian/0.7.1+168+gcb08bc2~5 X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=commitdiff_plain;h=7f88520089660845009148b69bfcea6c9dff9672 altosui: Flight data download GUI operations called only from main thread Swing doesn't like UI functions being called from non-dispatch thread, so fix up the eeprom download code to use SwingUtilities.invokeLater to make sure this works right. Signed-off-by: Keith Packard --- diff --git a/altosui/AltosEepromDownload.java b/altosui/AltosEepromDownload.java index 02fc36f2..e5ff766c 100644 --- a/altosui/AltosEepromDownload.java +++ b/altosui/AltosEepromDownload.java @@ -214,6 +214,27 @@ public class AltosEepromDownload implements Runnable { } } + private void show_error_internal(String message, String title) { + JOptionPane.showMessageDialog(frame, + message, + title, + JOptionPane.ERROR_MESSAGE); + } + + private void show_error(String in_message, String in_title) { + final String message = in_message; + final String title = in_title; + Runnable r = new Runnable() { + public void run() { + try { + show_error_internal(message, title); + } catch (Exception ex) { + } + } + }; + SwingUtilities.invokeLater(r); + } + public void run () { if (remote) { serial_line.set_radio(); @@ -221,26 +242,16 @@ public class AltosEepromDownload implements Runnable { serial_line.flush_input(); } - monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed); - monitor.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - eeprom_thread.interrupt(); - } - }); try { CaptureLog(); } catch (IOException ee) { - JOptionPane.showMessageDialog(frame, - device.toShortString(), - ee.getLocalizedMessage(), - JOptionPane.ERROR_MESSAGE); + show_error (device.toShortString(), + ee.getLocalizedMessage()); } catch (InterruptedException ie) { } catch (TimeoutException te) { - JOptionPane.showMessageDialog(frame, - String.format("Connection to \"%s\" failed", - device.toShortString()), - "Connection Failed", - JOptionPane.ERROR_MESSAGE); + show_error (String.format("Connection to \"%s\" failed", + device.toShortString()), + "Connection Failed"); } if (remote) serial_line.printf("~"); @@ -260,6 +271,14 @@ public class AltosEepromDownload implements Runnable { serial_line = new AltosSerial(device); if (!device.matchProduct(AltosDevice.product_telemetrum)) remote = true; + monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed); + monitor.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (eeprom_thread != null) + eeprom_thread.interrupt(); + } + }); + eeprom_thread = new Thread(this); eeprom_thread.start(); } catch (FileNotFoundException ee) { diff --git a/altosui/AltosEepromMonitor.java b/altosui/AltosEepromMonitor.java index 7ff00ead..13a49a95 100644 --- a/altosui/AltosEepromMonitor.java +++ b/altosui/AltosEepromMonitor.java @@ -141,7 +141,7 @@ public class AltosEepromMonitor extends JDialog { cancel.addActionListener(l); } - public void set_value(String state_name, int in_state, int in_block) { + private void set_value_internal(String state_name, int in_state, int in_block) { int block = in_block; int state = in_state; @@ -157,20 +157,86 @@ public class AltosEepromMonitor extends JDialog { pbar.setValue(pos); } - public void set_serial(int serial) { + public void set_value(String in_state_name, int in_state, int in_block) { + final String state_name = in_state_name; + final int state = in_state; + final int block = in_block; + Runnable r = new Runnable() { + public void run() { + try { + set_value_internal(state_name, state, block); + } catch (Exception ex) { + } + } + }; + SwingUtilities.invokeLater(r); + } + + private void set_serial_internal(int serial) { serial_value.setText(String.format("%d", serial)); } - public void set_flight(int flight) { + public void set_serial(int in_serial) { + final int serial = in_serial; + Runnable r = new Runnable() { + public void run() { + try { + set_serial_internal(serial); + } catch (Exception ex) { + } + } + }; + SwingUtilities.invokeLater(r); + } + + private void set_flight_internal(int flight) { flight_value.setText(String.format("%d", flight)); } - public void set_file(String file) { + public void set_flight(int in_flight) { + final int flight = in_flight; + Runnable r = new Runnable() { + public void run() { + try { + set_flight_internal(flight); + } catch (Exception ex) { + } + } + }; + SwingUtilities.invokeLater(r); + } + + private void set_file_internal(String file) { file_value.setText(String.format("%s", file)); } - public void done() { + public void set_file(String in_file) { + final String file = in_file; + Runnable r = new Runnable() { + public void run() { + try { + set_file_internal(file); + } catch (Exception ex) { + } + } + }; + SwingUtilities.invokeLater(r); + } + + private void done_internal() { setVisible(false); dispose(); } + + public void done() { + Runnable r = new Runnable() { + public void run() { + try { + done_internal(); + } catch (Exception ex) { + } + } + }; + SwingUtilities.invokeLater(r); + } }