+ return false;
+ file = hexfile_chooser.getSelectedFile();
+ if (file == null)
+ return false;
+ AltosUIPreferences.set_firmwaredir(file.getParentFile());
+
+ return true;
+ }
+
+ boolean select_device() {
+ int product = Altos.product_any;
+
+ device = AltosDeviceUIDialog.show(frame, Altos.product_any);
+
+ if (device == null)
+ return false;
+ return true;
+ }
+
+ boolean update_rom_config_info(AltosRomconfig existing_config) {
+ AltosRomconfig new_config;
+ new_config = AltosRomconfigUI.show(frame, existing_config);
+ if (new_config == null)
+ return false;
+ rom_config = new_config;
+ set_serial(rom_config.serial_number);
+ setVisible(true);
+ return true;
+ }
+
+ void exception (Exception e) {
+ if (e instanceof FileNotFoundException) {
+ JOptionPane.showMessageDialog(frame,
+ ((FileNotFoundException) e).getMessage(),
+ "Cannot open file",
+ JOptionPane.ERROR_MESSAGE);
+ } else if (e instanceof AltosSerialInUseException) {
+ JOptionPane.showMessageDialog(frame,
+ String.format("Device \"%s\" already in use",
+ device.toShortString()),
+ "Device in use",
+ JOptionPane.ERROR_MESSAGE);
+ } else if (e instanceof IOException) {
+ JOptionPane.showMessageDialog(frame,
+ e.getMessage(),
+ file.toString(),
+ JOptionPane.ERROR_MESSAGE);
+ }
+ }
+
+ class flash_task implements Runnable, AltosFlashListener {
+ AltosFlashUI ui;
+ Thread t;
+ AltosProgrammer programmer;
+
+ 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 {
+ if (ui.is_pair_programmed())
+ programmer = new AltosFlash(ui.file, link, this);
+ else
+ programmer = new AltosSelfFlash(ui.file, link, this);
+
+ final AltosRomconfig current_config = programmer.romconfig();
+
+ final Semaphore await_rom_config = new Semaphore(0);
+ SwingUtilities.invokeLater(new Runnable() {
+ public void run() {
+ ui.programmer = programmer;
+ ui.update_rom_config_info(current_config);
+ await_rom_config.release();
+ }
+ });
+ await_rom_config.acquire();
+
+ if (ui.rom_config != null) {
+ programmer.set_romconfig(ui.rom_config);
+ programmer.flash();
+ }
+ } catch (InterruptedException ee) {
+ final Exception e = ee;
+ 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);
+ }
+ });
+ } finally {
+ if (programmer != null)
+ programmer.close();
+ }
+ }
+
+ public flash_task(AltosFlashUI in_ui) {
+ ui = in_ui;
+ t = new Thread(this);
+ t.start();
+ }
+ }
+
+ flash_task flasher;
+
+ private boolean open_device() throws InterruptedException {
+ try {
+ link = new AltosSerial(device);
+ if (is_pair_programmed())
+ return true;
+
+ if (link == null)
+ throw new IOException(String.format("%s: open failed", device.toShortString()));
+
+ while (!link.is_loader()) {
+ link.to_loader();
+
+ java.util.List<AltosDevice> devices = null;
+
+ for (int tries = 0; tries < 10; tries++) {
+ Thread.sleep(100);
+ devices = AltosUSBDevice.list(AltosLib.product_altusmetrum);
+ if (devices.size() != 0)
+ break;
+ }
+
+ if (devices.size() == 1)
+ device = devices.get(0);
+ else {
+ device = AltosDeviceUIDialog.show(frame, AltosLib.product_altusmetrum);
+ if (device == null)
+ return false;
+ }
+ link = new AltosSerial(device);
+ }
+ return true;
+ } catch (AltosSerialInUseException ee) {
+ exception(ee);
+ } catch (FileNotFoundException fe) {
+ exception(fe);
+ } catch (IOException ie) {
+ exception (ie);
+ }
+ return false;
+ }
+
+ /*
+ * Execute the steps for flashing
+ * a device. Note that this returns immediately;
+ * this dialog is not modal
+ */
+ void showDialog() {
+ if (!select_device())