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.
20 import java.awt.event.*;
23 import java.util.concurrent.*;
24 import org.altusmetrum.altoslib_1.*;
25 import org.altusmetrum.altosuilib_1.*;
27 public class AltosEepromManage implements ActionListener {
32 AltosSerial serial_line;
33 AltosEepromList flights;
34 AltosEepromDownload download;
35 AltosEepromDelete delete;
37 public void finish() {
38 if (serial_line != null) {
40 serial_line.flush_input();
41 } catch (InterruptedException ie) {
48 private String showDeletedFlights() {
51 for (AltosEepromLog flight : flights) {
52 if (flight.selected) {
53 if (result.equals(""))
54 result = String.format("%d", flight.flight);
56 result = String.format("%s, %d", result, flight.flight);
62 public boolean download_done() {
63 AltosEepromSelect select = new AltosEepromSelect(frame, flights, "Delete");
66 boolean any_selected = false;
67 for (AltosEepromLog flight : flights)
68 any_selected = any_selected || flight.selected;
70 delete = new AltosEepromDelete(frame,
74 delete.addActionListener(this);
76 * Start flight log delete
86 public void actionPerformed(ActionEvent e) {
87 String cmd = e.getActionCommand();
88 boolean success = e.getID() != 0;
89 boolean running = false;
91 if (cmd.equals("download")) {
93 running = download_done();
94 } else if (cmd.equals("delete")) {
96 JOptionPane.showMessageDialog(frame,
97 String.format("Flights erased: %s",
98 showDeletedFlights()),
99 serial_line.device.toShortString(),
100 JOptionPane.INFORMATION_MESSAGE);
107 public void got_flights(AltosEepromList in_flights) {
108 boolean running = false;;
110 flights = in_flights;
112 if (flights.size() == 0) {
113 JOptionPane.showMessageDialog(frame,
114 String.format("No flights available on %d",
116 serial_line.device.toShortString(),
117 JOptionPane.INFORMATION_MESSAGE);
119 AltosEepromSelect select = new AltosEepromSelect(frame, flights, "Download");
122 boolean any_selected = false;
123 for (AltosEepromLog flight : flights)
124 any_selected = any_selected || flight.selected;
126 download = new AltosEepromDownload(frame,
130 download.addActionListener(this);
132 * Start flight log download
138 running = download_done();
144 } catch (Exception e) {
149 public void got_exception(Exception e) {
150 if (e instanceof IOException) {
151 IOException ee = (IOException) e;
152 JOptionPane.showMessageDialog(frame,
153 device.toShortString(),
154 ee.getLocalizedMessage(),
155 JOptionPane.ERROR_MESSAGE);
156 } else if (e instanceof TimeoutException) {
157 //TimeoutException te = (TimeoutException) e;
158 JOptionPane.showMessageDialog(frame,
159 String.format("Communications failed with \"%s\"",
160 device.toShortString()),
161 "Cannot open target device",
162 JOptionPane.ERROR_MESSAGE);
167 class EepromGetList implements Runnable {
169 AltosEepromManage manage;
174 flights = new AltosEepromList(serial_line, remote);
177 got_flights(flights);
180 } catch (Exception e) {
181 final Exception f_e = e;
188 SwingUtilities.invokeLater(r);
191 public EepromGetList(AltosEepromManage in_manage) {
196 public AltosEepromManage(JFrame given_frame) {
198 //boolean running = false;
201 device = AltosDeviceUIDialog.show(frame, Altos.product_any);
205 if (device != null) {
207 serial_line = new AltosSerial(device);
208 if (device.matchProduct(Altos.product_basestation))
211 serial_line.set_frame(frame);
213 EepromGetList get_list = new EepromGetList(this);
214 Thread t = new Thread(get_list);
216 } catch (FileNotFoundException ee) {
217 JOptionPane.showMessageDialog(frame,
219 "Cannot open target device",
220 JOptionPane.ERROR_MESSAGE);
221 } catch (AltosSerialInUseException si) {
222 JOptionPane.showMessageDialog(frame,
223 String.format("Device \"%s\" already in use",
224 device.toShortString()),
226 JOptionPane.ERROR_MESSAGE);