2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altosuilib_14;
21 import java.awt.event.*;
24 import java.util.concurrent.*;
25 import org.altusmetrum.altoslib_14.*;
27 public class AltosEepromDelete implements Runnable {
28 AltosEepromList flights;
30 AltosSerial serial_line;
33 ActionListener listener;
36 private void DeleteLog (AltosEepromLog log)
37 throws IOException, InterruptedException, TimeoutException {
39 if (flights.config_data.flight_log_max != 0 || flights.config_data.log_format != 0) {
41 /* Devices with newer firmware can erase the
42 * flash blocks containing each flight
44 serial_line.flush_input();
45 serial_line.printf("d %d\n", log.flight);
47 /* It can take a while to erase the flash... */
48 String line = serial_line.get_reply(200000);
50 throw new TimeoutException();
51 if (line.equals("Erased"))
53 if (line.equals("Failed to erase"))
54 throw new IOException(line);
55 if (line.startsWith("No such"))
56 throw new IOException(line);
61 private void show_error_internal(String message, String title) {
62 JOptionPane.showMessageDialog(frame,
65 JOptionPane.ERROR_MESSAGE);
68 private void show_error(String in_message, String in_title) {
69 final String message = in_message;
70 final String title = in_title;
71 Runnable r = new Runnable() {
74 show_error_internal(message, title);
75 } catch (Exception ex) {
79 SwingUtilities.invokeLater(r);
86 serial_line.start_remote();
88 for (AltosEepromLog log : flights) {
89 if (log.delete_selected) {
94 } catch (IOException ee) {
95 show_error (ee.getLocalizedMessage(),
96 serial_line.device.toShortString());
97 } catch (InterruptedException ie) {
98 } catch (TimeoutException te) {
99 show_error (String.format("Connection to \"%s\" failed",
100 serial_line.device.toShortString()),
101 "Connection Failed");
105 serial_line.stop_remote();
106 } catch (InterruptedException ie) {
108 serial_line.flush_output();
112 if (listener != null) {
113 Runnable r = new Runnable() {
116 listener.actionPerformed(new ActionEvent(this,
119 } catch (Exception ex) {
123 SwingUtilities.invokeLater(r);
127 public void start() {
128 eeprom_thread = new Thread(this);
129 eeprom_thread.start();
132 public void addActionListener(ActionListener l) {
136 public AltosEepromDelete(JFrame given_frame,
137 AltosSerial given_serial_line,
138 boolean given_remote,
139 AltosEepromList given_flights) {
141 serial_line = given_serial_line;
142 remote = given_remote;
143 flights = given_flights;