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.*;
33 public class AltosEepromManage implements ActionListener {
38 AltosSerial serial_line;
39 AltosEepromList flights;
40 AltosEepromDownload download;
41 AltosEepromDelete delete;
43 public void finish() {
44 if (serial_line != null) {
46 serial_line.flush_input();
47 } catch (InterruptedException ie) {
54 private String showDeletedFlights() {
57 for (AltosEepromLog flight : flights) {
58 if (flight.selected) {
59 if (result.equals(""))
60 result = String.format("%d", flight.flight);
62 result = String.format("%s, %d", result, flight.flight);
68 public boolean download_done() {
69 AltosEepromSelect select = new AltosEepromSelect(frame, flights, "Delete");
72 boolean any_selected = false;
73 for (AltosEepromLog flight : flights)
74 any_selected = any_selected || flight.selected;
76 delete = new AltosEepromDelete(frame,
80 delete.addActionListener(this);
82 * Start flight log delete
92 public void actionPerformed(ActionEvent e) {
93 String cmd = e.getActionCommand();
94 boolean success = e.getID() != 0;
95 boolean running = false;
97 if (cmd.equals("download")) {
99 running = download_done();
100 } else if (cmd.equals("delete")) {
102 JOptionPane.showMessageDialog(frame,
103 String.format("Flights erased: %s",
104 showDeletedFlights()),
105 serial_line.device.toShortString(),
106 JOptionPane.INFORMATION_MESSAGE);
113 public void got_flights(AltosEepromList in_flights) {
114 boolean running = false;;
116 flights = in_flights;
118 if (flights.size() == 0) {
119 JOptionPane.showMessageDialog(frame,
120 String.format("No flights available on %d",
122 serial_line.device.toShortString(),
123 JOptionPane.INFORMATION_MESSAGE);
125 AltosEepromSelect select = new AltosEepromSelect(frame, flights, "Download");
128 boolean any_selected = false;
129 for (AltosEepromLog flight : flights)
130 any_selected = any_selected || flight.selected;
132 download = new AltosEepromDownload(frame,
136 download.addActionListener(this);
138 * Start flight log download
144 running = download_done();
150 } catch (Exception e) {
155 public void got_exception(Exception e) {
156 if (e instanceof IOException) {
157 IOException ee = (IOException) e;
158 JOptionPane.showMessageDialog(frame,
159 device.toShortString(),
160 ee.getLocalizedMessage(),
161 JOptionPane.ERROR_MESSAGE);
162 } else if (e instanceof TimeoutException) {
163 TimeoutException te = (TimeoutException) e;
164 JOptionPane.showMessageDialog(frame,
165 String.format("Communications failed with \"%s\"",
166 device.toShortString()),
167 "Cannot open target device",
168 JOptionPane.ERROR_MESSAGE);
173 class EepromGetList implements Runnable {
175 AltosEepromManage manage;
180 flights = new AltosEepromList(serial_line, remote);
183 got_flights(flights);
186 } catch (Exception e) {
187 final Exception f_e = e;
194 SwingUtilities.invokeLater(r);
197 public EepromGetList(AltosEepromManage in_manage) {
202 public AltosEepromManage(JFrame given_frame) {
204 boolean running = false;
207 device = AltosDeviceDialog.show(frame, Altos.product_any);
211 if (device != null) {
213 serial_line = new AltosSerial(device);
214 if (device.matchProduct(Altos.product_basestation))
217 serial_line.set_frame(frame);
219 EepromGetList get_list = new EepromGetList(this);
220 Thread t = new Thread(get_list);
222 } catch (FileNotFoundException ee) {
223 JOptionPane.showMessageDialog(frame,
225 "Cannot open target device",
226 JOptionPane.ERROR_MESSAGE);
227 } catch (AltosSerialInUseException si) {
228 JOptionPane.showMessageDialog(frame,
229 String.format("Device \"%s\" already in use",
230 device.toShortString()),
232 JOptionPane.ERROR_MESSAGE);