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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 import java.awt.event.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.*;
30 import org.altusmetrum.AltosLib.*;
34 public class AltosEepromManage implements ActionListener {
39 AltosSerial serial_line;
40 AltosEepromList flights;
41 AltosEepromDownload download;
42 AltosEepromDelete delete;
44 public void finish() {
45 if (serial_line != null) {
47 serial_line.flush_input();
48 } catch (InterruptedException ie) {
55 private String showDeletedFlights() {
58 for (AltosEepromLog flight : flights) {
59 if (flight.selected) {
60 if (result.equals(""))
61 result = String.format("%d", flight.flight);
63 result = String.format("%s, %d", result, flight.flight);
69 public boolean download_done() {
70 AltosEepromSelect select = new AltosEepromSelect(frame, flights, "Delete");
73 boolean any_selected = false;
74 for (AltosEepromLog flight : flights)
75 any_selected = any_selected || flight.selected;
77 delete = new AltosEepromDelete(frame,
81 delete.addActionListener(this);
83 * Start flight log delete
93 public void actionPerformed(ActionEvent e) {
94 String cmd = e.getActionCommand();
95 boolean success = e.getID() != 0;
96 boolean running = false;
98 if (cmd.equals("download")) {
100 running = download_done();
101 } else if (cmd.equals("delete")) {
103 JOptionPane.showMessageDialog(frame,
104 String.format("Flights erased: %s",
105 showDeletedFlights()),
106 serial_line.device.toShortString(),
107 JOptionPane.INFORMATION_MESSAGE);
114 public void got_flights(AltosEepromList in_flights) {
115 boolean running = false;;
117 flights = in_flights;
119 if (flights.size() == 0) {
120 JOptionPane.showMessageDialog(frame,
121 String.format("No flights available on %d",
123 serial_line.device.toShortString(),
124 JOptionPane.INFORMATION_MESSAGE);
126 AltosEepromSelect select = new AltosEepromSelect(frame, flights, "Download");
129 boolean any_selected = false;
130 for (AltosEepromLog flight : flights)
131 any_selected = any_selected || flight.selected;
133 download = new AltosEepromDownload(frame,
137 download.addActionListener(this);
139 * Start flight log download
145 running = download_done();
151 } catch (Exception e) {
156 public void got_exception(Exception e) {
157 if (e instanceof IOException) {
158 IOException ee = (IOException) e;
159 JOptionPane.showMessageDialog(frame,
160 device.toShortString(),
161 ee.getLocalizedMessage(),
162 JOptionPane.ERROR_MESSAGE);
163 } else if (e instanceof TimeoutException) {
164 TimeoutException te = (TimeoutException) e;
165 JOptionPane.showMessageDialog(frame,
166 String.format("Communications failed with \"%s\"",
167 device.toShortString()),
168 "Cannot open target device",
169 JOptionPane.ERROR_MESSAGE);
174 class EepromGetList implements Runnable {
176 AltosEepromManage manage;
181 flights = new AltosEepromList(serial_line, remote);
184 got_flights(flights);
187 } catch (Exception e) {
188 final Exception f_e = e;
195 SwingUtilities.invokeLater(r);
198 public EepromGetList(AltosEepromManage in_manage) {
203 public AltosEepromManage(JFrame given_frame) {
205 boolean running = false;
208 device = AltosDeviceDialog.show(frame, Altos.product_any);
212 if (device != null) {
214 serial_line = new AltosSerial(device);
215 if (device.matchProduct(Altos.product_basestation))
218 serial_line.set_frame(frame);
220 EepromGetList get_list = new EepromGetList(this);
221 Thread t = new Thread(get_list);
223 } catch (FileNotFoundException ee) {
224 JOptionPane.showMessageDialog(frame,
226 "Cannot open target device",
227 JOptionPane.ERROR_MESSAGE);
228 } catch (AltosSerialInUseException si) {
229 JOptionPane.showMessageDialog(frame,
230 String.format("Device \"%s\" already in use",
231 device.toShortString()),
233 JOptionPane.ERROR_MESSAGE);