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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altosuilib_14;
21 import java.awt.event.*;
24 import java.util.concurrent.*;
25 import org.altusmetrum.altoslib_14.*;
27 public class AltosEepromManage implements ActionListener {
32 AltosSerial serial_line;
33 AltosEepromList flights;
34 AltosEepromDownload download;
35 AltosEepromDelete delete;
36 AltosEepromGrapher grapher;
38 public void finish() {
39 if (serial_line != null) {
41 serial_line.flush_input();
42 } catch (InterruptedException ie) {
49 private int countDeletedFlights() {
51 for (AltosEepromLog flight : flights) {
52 if (flight.delete_selected)
58 private String showDeletedFlights() {
61 for (AltosEepromLog flight : flights) {
62 if (flight.delete_selected) {
63 if (result.equals(""))
64 result = String.format("%d", flight.flight);
66 result = String.format("%s, %d", result, flight.flight);
72 public boolean delete_start() {
74 boolean any_selected = false;
75 for (AltosEepromLog flight : flights)
76 any_selected = any_selected || flight.delete_selected;
78 delete = new AltosEepromDelete(frame,
82 delete.addActionListener(this);
84 * Start flight log delete
93 public void graph_start() {
94 boolean any_selected = false;
95 for (AltosEepromLog flight : flights) {
96 if (!flight.download_selected)
97 flight.graph_selected = false;
98 any_selected = any_selected || flight.graph_selected;
100 if (any_selected && grapher != null)
101 grapher.graph_flights(flights);
104 public void actionPerformed(ActionEvent e) {
105 String cmd = e.getActionCommand();
106 boolean success = e.getID() != 0;
107 boolean running = false;
109 if (cmd.equals("download")) {
111 running = delete_start();
115 } else if (cmd.equals("delete")) {
117 JOptionPane.showMessageDialog(frame,
118 String.format("%d flights erased: %s",
119 countDeletedFlights(),
120 showDeletedFlights()),
121 serial_line.device.toShortString(),
122 JOptionPane.INFORMATION_MESSAGE);
130 public void got_flights(AltosEepromList in_flights) {
131 boolean running = false;;
133 flights = in_flights;
135 if (flights.size() == 0) {
136 JOptionPane.showMessageDialog(frame,
137 String.format("No flights available on %d",
139 serial_line.device.toShortString(),
140 JOptionPane.INFORMATION_MESSAGE);
142 AltosEepromSelect select = new AltosEepromSelect(frame, flights, grapher != null);
145 boolean any_selected = false;
146 for (AltosEepromLog flight : flights)
147 any_selected = any_selected || flight.download_selected;
149 AltosEepromMonitorUI monitor = new AltosEepromMonitorUI(frame);
150 monitor.addActionListener(this);
151 serial_line.set_frame(frame);
152 download = new AltosEepromDownload(monitor,
157 * Start flight log download
163 running = delete_start();
171 } catch (Exception e) {
176 public void got_exception(Exception e) {
177 if (e instanceof IOException) {
178 IOException ee = (IOException) e;
179 JOptionPane.showMessageDialog(frame,
180 device.toShortString(),
181 ee.getLocalizedMessage(),
182 JOptionPane.ERROR_MESSAGE);
183 } else if (e instanceof TimeoutException) {
184 //TimeoutException te = (TimeoutException) e;
185 JOptionPane.showMessageDialog(frame,
186 String.format("Communications failed with \"%s\"",
187 device.toShortString()),
188 "Cannot open target device",
189 JOptionPane.ERROR_MESSAGE);
194 class EepromGetList implements Runnable {
196 AltosEepromManage manage;
201 flights = new AltosEepromList(serial_line, remote);
204 got_flights(flights);
207 } catch (Exception e) {
208 final Exception f_e = e;
215 SwingUtilities.invokeLater(r);
218 public EepromGetList(AltosEepromManage in_manage) {
223 public AltosEepromManage(JFrame given_frame, AltosEepromGrapher grapher, int product) {
225 //boolean running = false;
228 this.grapher = grapher;
229 device = AltosDeviceUIDialog.show(frame, product);
233 if (device != null) {
235 serial_line = new AltosSerial(device);
236 if (device.matchProduct(AltosLib.product_basestation))
239 serial_line.set_frame(frame);
241 EepromGetList get_list = new EepromGetList(this);
242 Thread t = new Thread(get_list);
244 } catch (FileNotFoundException ee) {
245 JOptionPane.showMessageDialog(frame,
247 "Cannot open target device",
248 JOptionPane.ERROR_MESSAGE);
249 } catch (AltosSerialInUseException si) {
250 JOptionPane.showMessageDialog(frame,
251 String.format("Device \"%s\" already in use",
252 device.toShortString()),
254 JOptionPane.ERROR_MESSAGE);