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;
46 public void finish() {
47 if (serial_line != null) {
49 serial_line.flush_input();
50 } catch (InterruptedException ie) {
57 private String showDeletedFlights() {
60 for (AltosEepromLog flight : flights) {
62 if (result.equals(""))
63 result = String.format("%d", flight.flight);
65 result = String.format("%s, %d", result, flight.flight);
71 public void actionPerformed(ActionEvent e) {
72 String cmd = e.getActionCommand();
73 boolean success = e.getID() != 0;
74 boolean running = false;
76 if (cmd.equals("download")) {
83 } else if (cmd.equals("delete")) {
85 JOptionPane.showMessageDialog(frame,
86 String.format("Flights erased: %s",
87 showDeletedFlights()),
88 serial_line.device.toShortString(),
89 JOptionPane.INFORMATION_MESSAGE);
96 public void got_flights(AltosEepromList in_flights) {
97 boolean running = false;;
101 if (flights.size() == 0) {
102 JOptionPane.showMessageDialog(frame,
103 String.format("No flights available on %d",
105 serial_line.device.toShortString(),
106 JOptionPane.INFORMATION_MESSAGE);
108 AltosEepromSelect select = new AltosEepromSelect(frame, flights);
111 for (AltosEepromLog flight : flights) {
112 any_download = any_download || flight.download;
113 any_delete = any_delete || flight.delete;
116 download = new AltosEepromDownload(frame,
120 download.addActionListener(this);
124 delete = new AltosEepromDelete(frame,
128 delete.addActionListener(this);
132 * Start flight log download
139 else if (any_delete) {
147 } catch (Exception e) {
152 public void got_exception(Exception e) {
153 if (e instanceof IOException) {
154 IOException ee = (IOException) e;
155 JOptionPane.showMessageDialog(frame,
156 device.toShortString(),
157 ee.getLocalizedMessage(),
158 JOptionPane.ERROR_MESSAGE);
159 } else if (e instanceof TimeoutException) {
160 TimeoutException te = (TimeoutException) e;
161 JOptionPane.showMessageDialog(frame,
162 String.format("Communications failed with \"%s\"",
163 device.toShortString()),
164 "Cannot open target device",
165 JOptionPane.ERROR_MESSAGE);
170 class EepromGetList implements Runnable {
172 AltosEepromManage manage;
177 flights = new AltosEepromList(serial_line, remote);
180 got_flights(flights);
183 } catch (Exception e) {
184 final Exception f_e = e;
191 SwingUtilities.invokeLater(r);
194 public EepromGetList(AltosEepromManage in_manage) {
199 public AltosEepromManage(JFrame given_frame) {
201 boolean running = false;
204 device = AltosDeviceDialog.show(frame, Altos.product_any);
207 any_download = false;
210 if (device != null) {
212 serial_line = new AltosSerial(device);
213 if (device.matchProduct(Altos.product_basestation))
216 serial_line.set_frame(frame);
218 EepromGetList get_list = new EepromGetList(this);
219 Thread t = new Thread(get_list);
221 } catch (FileNotFoundException ee) {
222 JOptionPane.showMessageDialog(frame,
224 "Cannot open target device",
225 JOptionPane.ERROR_MESSAGE);
226 } catch (AltosSerialInUseException si) {
227 JOptionPane.showMessageDialog(frame,
228 String.format("Device \"%s\" already in use",
229 device.toShortString()),
231 JOptionPane.ERROR_MESSAGE);