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_4.*;
25 import org.altusmetrum.altosuilib_2.*;
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 int countDeletedFlights() {
50 for (AltosEepromLog flight : flights) {
57 private String showDeletedFlights() {
60 for (AltosEepromLog flight : flights) {
61 if (flight.selected) {
62 if (result.equals(""))
63 result = String.format("%d", flight.flight);
65 result = String.format("%s, %d", result, flight.flight);
71 public boolean download_done() {
72 AltosEepromSelect select = new AltosEepromSelect(frame, flights, "Delete");
75 boolean any_selected = false;
76 for (AltosEepromLog flight : flights)
77 any_selected = any_selected || flight.selected;
79 delete = new AltosEepromDelete(frame,
83 delete.addActionListener(this);
85 * Start flight log delete
95 public void actionPerformed(ActionEvent e) {
96 String cmd = e.getActionCommand();
97 boolean success = e.getID() != 0;
98 boolean running = false;
100 if (cmd.equals("download")) {
102 running = download_done();
103 } else if (cmd.equals("delete")) {
105 JOptionPane.showMessageDialog(frame,
106 String.format("%d flights erased: %s",
107 countDeletedFlights(),
108 showDeletedFlights()),
109 serial_line.device.toShortString(),
110 JOptionPane.INFORMATION_MESSAGE);
117 public void got_flights(AltosEepromList in_flights) {
118 boolean running = false;;
120 flights = in_flights;
122 if (flights.size() == 0) {
123 JOptionPane.showMessageDialog(frame,
124 String.format("No flights available on %d",
126 serial_line.device.toShortString(),
127 JOptionPane.INFORMATION_MESSAGE);
129 AltosEepromSelect select = new AltosEepromSelect(frame, flights, "Download");
132 boolean any_selected = false;
133 for (AltosEepromLog flight : flights)
134 any_selected = any_selected || flight.selected;
136 AltosEepromMonitorUI monitor = new AltosEepromMonitorUI(frame);
137 monitor.addActionListener(this);
138 serial_line.set_frame(frame);
139 download = new AltosEepromDownload(monitor,
144 * Start flight log download
150 running = download_done();
156 } catch (Exception e) {
161 public void got_exception(Exception e) {
162 if (e instanceof IOException) {
163 IOException ee = (IOException) e;
164 JOptionPane.showMessageDialog(frame,
165 device.toShortString(),
166 ee.getLocalizedMessage(),
167 JOptionPane.ERROR_MESSAGE);
168 } else if (e instanceof TimeoutException) {
169 //TimeoutException te = (TimeoutException) e;
170 JOptionPane.showMessageDialog(frame,
171 String.format("Communications failed with \"%s\"",
172 device.toShortString()),
173 "Cannot open target device",
174 JOptionPane.ERROR_MESSAGE);
179 class EepromGetList implements Runnable {
181 AltosEepromManage manage;
186 flights = new AltosEepromList(serial_line, remote);
189 got_flights(flights);
192 } catch (Exception e) {
193 final Exception f_e = e;
200 SwingUtilities.invokeLater(r);
203 public EepromGetList(AltosEepromManage in_manage) {
208 public AltosEepromManage(JFrame given_frame) {
210 //boolean running = false;
213 device = AltosDeviceUIDialog.show(frame, Altos.product_any);
217 if (device != null) {
219 serial_line = new AltosSerial(device);
220 if (device.matchProduct(Altos.product_basestation))
223 serial_line.set_frame(frame);
225 EepromGetList get_list = new EepromGetList(this);
226 Thread t = new Thread(get_list);
228 } catch (FileNotFoundException ee) {
229 JOptionPane.showMessageDialog(frame,
231 "Cannot open target device",
232 JOptionPane.ERROR_MESSAGE);
233 } catch (AltosSerialInUseException si) {
234 JOptionPane.showMessageDialog(frame,
235 String.format("Device \"%s\" already in use",
236 device.toShortString()),
238 JOptionPane.ERROR_MESSAGE);