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;
45 public void finish() {
46 if (serial_line != null) {
48 serial_line.flush_input();
49 } catch (InterruptedException ie) {
56 private String showDeletedFlights() {
59 for (AltosEepromLog flight : flights) {
61 if (result.equals(""))
62 result = String.format("%d", flight.flight);
64 result = String.format("%s, %d", result, flight.flight);
70 public void actionPerformed(ActionEvent e) {
71 String cmd = e.getActionCommand();
72 boolean success = e.getID() != 0;
73 boolean running = false;
75 if (cmd.equals("download")) {
82 } else if (cmd.equals("delete")) {
84 JOptionPane.showMessageDialog(frame,
85 String.format("Flights erased: %s",
86 showDeletedFlights()),
87 serial_line.device.toShortString(),
88 JOptionPane.INFORMATION_MESSAGE);
95 public void got_flights(AltosEepromList in_flights) {
96 boolean running = false;;
100 if (flights.size() == 0) {
101 JOptionPane.showMessageDialog(frame,
102 String.format("No flights available on %d",
104 serial_line.device.toShortString(),
105 JOptionPane.INFORMATION_MESSAGE);
107 AltosEepromSelect select = new AltosEepromSelect(frame, flights);
110 for (AltosEepromLog flight : flights) {
111 any_download = any_download || flight.download;
112 any_delete = any_delete || flight.delete;
115 download = new AltosEepromDownload(frame,
119 download.addActionListener(this);
123 delete = new AltosEepromDelete(frame,
127 delete.addActionListener(this);
131 * Start flight log download
138 else if (any_delete) {
146 } catch (Exception e) {
151 public void got_exception(Exception e) {
152 if (e instanceof IOException) {
153 IOException ee = (IOException) e;
154 JOptionPane.showMessageDialog(frame,
155 device.toShortString(),
156 ee.getLocalizedMessage(),
157 JOptionPane.ERROR_MESSAGE);
158 } else if (e instanceof TimeoutException) {
159 TimeoutException te = (TimeoutException) e;
160 JOptionPane.showMessageDialog(frame,
161 String.format("Communications failed with \"%s\"",
162 device.toShortString()),
163 "Cannot open target device",
164 JOptionPane.ERROR_MESSAGE);
169 class EepromGetList implements Runnable {
171 AltosEepromManage manage;
176 flights = new AltosEepromList(serial_line, remote);
179 got_flights(flights);
182 } catch (Exception e) {
183 final Exception f_e = e;
190 SwingUtilities.invokeLater(r);
193 public EepromGetList(AltosEepromManage in_manage) {
198 public AltosEepromManage(JFrame given_frame) {
200 boolean running = false;
203 device = AltosDeviceDialog.show(frame, Altos.product_any);
206 any_download = false;
209 if (device != null) {
211 serial_line = new AltosSerial(device);
212 if (device.matchProduct(Altos.product_basestation))
215 serial_line.set_frame(frame);
217 EepromGetList get_list = new EepromGetList(this);
218 Thread t = new Thread(get_list);
220 } catch (FileNotFoundException ee) {
221 JOptionPane.showMessageDialog(frame,
222 String.format("Cannot open device \"%s\"",
223 device.toShortString()),
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);