1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
/*
* Copyright © 2011 Keith Packard <keithp@keithp.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
package altosui;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.concurrent.*;
import org.altusmetrum.altoslib_2.*;
public class AltosEepromDelete implements Runnable {
AltosEepromList flights;
Thread eeprom_thread;
AltosSerial serial_line;
boolean remote;
JFrame frame;
ActionListener listener;
boolean success;
private void DeleteLog (AltosEepromLog log)
throws IOException, InterruptedException, TimeoutException {
if (flights.config_data.flight_log_max != 0 || flights.config_data.log_format != 0) {
/* Devices with newer firmware can erase the
* flash blocks containing each flight
*/
serial_line.flush_input();
serial_line.printf("d %d\n", log.flight);
for (;;) {
/* It can take a while to erase the flash... */
String line = serial_line.get_reply(20000);
if (line == null)
throw new TimeoutException();
if (line.equals("Erased"))
break;
if (line.startsWith("No such"))
throw new IOException(line);
}
}
}
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 () {
success = false;
try {
if (remote)
serial_line.start_remote();
for (AltosEepromLog log : flights) {
if (log.selected) {
DeleteLog(log);
}
}
success = true;
} catch (IOException ee) {
show_error (ee.getLocalizedMessage(),
serial_line.device.toShortString());
} catch (InterruptedException ie) {
} catch (TimeoutException te) {
show_error (String.format("Connection to \"%s\" failed",
serial_line.device.toShortString()),
"Connection Failed");
} finally {
try {
if (remote)
serial_line.stop_remote();
} catch (InterruptedException ie) {
} finally {
serial_line.flush_output();
serial_line.close();
}
}
if (listener != null) {
Runnable r = new Runnable() {
public void run() {
try {
listener.actionPerformed(new ActionEvent(this,
success ? 1 : 0,
"delete"));
} catch (Exception ex) {
}
}
};
SwingUtilities.invokeLater(r);
}
}
public void start() {
eeprom_thread = new Thread(this);
eeprom_thread.start();
}
public void addActionListener(ActionListener l) {
listener = l;
}
public AltosEepromDelete(JFrame given_frame,
AltosSerial given_serial_line,
boolean given_remote,
AltosEepromList given_flights) {
frame = given_frame;
serial_line = given_serial_line;
remote = given_remote;
flights = given_flights;
success = false;
}
}
|