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.
18 package org.altusmetrum.altosuilib_8;
20 import java.awt.event.*;
23 import java.util.concurrent.*;
24 import org.altusmetrum.altoslib_8.*;
26 public class AltosEepromManage implements ActionListener {
31 AltosSerial serial_line;
32 AltosEepromList flights;
33 AltosEepromDownload download;
34 AltosEepromDelete delete;
36 public void finish() {
37 if (serial_line != null) {
39 serial_line.flush_input();
40 } catch (InterruptedException ie) {
47 private int countDeletedFlights() {
49 for (AltosEepromLog flight : flights) {
56 private String showDeletedFlights() {
59 for (AltosEepromLog flight : flights) {
60 if (flight.selected) {
61 if (result.equals(""))
62 result = String.format("%d", flight.flight);
64 result = String.format("%s, %d", result, flight.flight);
70 public boolean download_done() {
71 AltosEepromSelect select = new AltosEepromSelect(frame, flights, "Delete");
74 boolean any_selected = false;
75 for (AltosEepromLog flight : flights)
76 any_selected = any_selected || flight.selected;
78 delete = new AltosEepromDelete(frame,
82 delete.addActionListener(this);
84 * Start flight log delete
94 public void actionPerformed(ActionEvent e) {
95 String cmd = e.getActionCommand();
96 boolean success = e.getID() != 0;
97 boolean running = false;
99 if (cmd.equals("download")) {
101 running = download_done();
102 } else if (cmd.equals("delete")) {
104 JOptionPane.showMessageDialog(frame,
105 String.format("%d flights erased: %s",
106 countDeletedFlights(),
107 showDeletedFlights()),
108 serial_line.device.toShortString(),
109 JOptionPane.INFORMATION_MESSAGE);
116 public void got_flights(AltosEepromList in_flights) {
117 boolean running = false;;
119 flights = in_flights;
121 if (flights.size() == 0) {
122 JOptionPane.showMessageDialog(frame,
123 String.format("No flights available on %d",
125 serial_line.device.toShortString(),
126 JOptionPane.INFORMATION_MESSAGE);
128 AltosEepromSelect select = new AltosEepromSelect(frame, flights, "Download");
131 boolean any_selected = false;
132 for (AltosEepromLog flight : flights)
133 any_selected = any_selected || flight.selected;
135 AltosEepromMonitorUI monitor = new AltosEepromMonitorUI(frame);
136 monitor.addActionListener(this);
137 serial_line.set_frame(frame);
138 download = new AltosEepromDownload(monitor,
143 * Start flight log download
149 running = download_done();
155 } catch (Exception e) {
160 public void got_exception(Exception e) {
161 if (e instanceof IOException) {
162 IOException ee = (IOException) e;
163 JOptionPane.showMessageDialog(frame,
164 device.toShortString(),
165 ee.getLocalizedMessage(),
166 JOptionPane.ERROR_MESSAGE);
167 } else if (e instanceof TimeoutException) {
168 //TimeoutException te = (TimeoutException) e;
169 JOptionPane.showMessageDialog(frame,
170 String.format("Communications failed with \"%s\"",
171 device.toShortString()),
172 "Cannot open target device",
173 JOptionPane.ERROR_MESSAGE);
178 class EepromGetList implements Runnable {
180 AltosEepromManage manage;
185 flights = new AltosEepromList(serial_line, remote);
188 got_flights(flights);
191 } catch (Exception e) {
192 final Exception f_e = e;
199 SwingUtilities.invokeLater(r);
202 public EepromGetList(AltosEepromManage in_manage) {
207 public AltosEepromManage(JFrame given_frame, int product) {
209 //boolean running = false;
212 device = AltosDeviceUIDialog.show(frame, product);
216 if (device != null) {
218 serial_line = new AltosSerial(device);
219 if (device.matchProduct(AltosLib.product_basestation))
222 serial_line.set_frame(frame);
224 EepromGetList get_list = new EepromGetList(this);
225 Thread t = new Thread(get_list);
227 } catch (FileNotFoundException ee) {
228 JOptionPane.showMessageDialog(frame,
230 "Cannot open target device",
231 JOptionPane.ERROR_MESSAGE);
232 } catch (AltosSerialInUseException si) {
233 JOptionPane.showMessageDialog(frame,
234 String.format("Device \"%s\" already in use",
235 device.toShortString()),
237 JOptionPane.ERROR_MESSAGE);