altosui: Add eeprom 'manage' ui to download and delete multiple flights
[fw/altos] / altosui / AltosEepromManage.java
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.*;
30
31 import libaltosJNI.*;
32
33 public class AltosEepromManage implements ActionListener {
34
35         JFrame                  frame;
36         boolean                 remote;
37         AltosDevice             device;
38         AltosSerial             serial_line;
39         AltosEepromList         flights;
40         AltosEepromDownload     download;
41         AltosEepromDelete       delete;
42         boolean                 any_download;
43         boolean                 any_delete;
44
45         public void finish() {
46                 if (serial_line != null) {
47                         serial_line.flush_output();
48                         serial_line.close();
49                         serial_line = null;
50                 }
51         }
52
53         public void actionPerformed(ActionEvent e) {
54                 String  cmd = e.getActionCommand();
55                 boolean success = e.getID() != 0;
56
57                 System.out.printf("Eeprom manager action %s %d\n", cmd, e.getID());
58                 if (cmd.equals("download")) {
59                         if (success) {
60                                 System.out.printf("Download succeeded\n");
61                                 if (any_delete)
62                                         delete.start();
63                                 else
64                                         finish();
65                         }
66                 } else if (cmd.equals("delete")) {
67                         if (success)
68                                 System.out.printf("Delete succeeded\n");
69                         finish();
70                 }
71         }
72
73         public AltosEepromManage(JFrame given_frame) {
74
75                 frame = given_frame;
76                 device = AltosDeviceDialog.show(frame, AltosDevice.product_any);
77
78                 remote = false;
79                 any_download = false;
80                 any_delete = false;
81
82                 if (device != null) {
83                         try {
84                                 serial_line = new AltosSerial(device);
85                                 if (!device.matchProduct(AltosDevice.product_telemetrum))
86                                         remote = true;
87
88                                 flights = new AltosEepromList(serial_line, remote);
89
90                                 if (flights.size() == 0) {
91                                 } else {
92                                         AltosEepromSelect       select = new AltosEepromSelect(frame, flights);
93
94                                         if (select.run()) {
95                                                 for (AltosEepromLog flight : flights) {
96                                                         any_download = any_download || flight.download;
97                                                         any_delete = any_delete || flight.delete;
98                                                 }
99                                                 if (any_download) {
100                                                         download = new AltosEepromDownload(frame,
101                                                                                            serial_line,
102                                                                                            remote,
103                                                                                            flights);
104                                                         download.addActionListener(this);
105                                                 }
106
107                                                 if (any_delete) {
108                                                         delete = new AltosEepromDelete(frame,
109                                                                                        serial_line,
110                                                                                        remote,
111                                                                                        flights);
112                                                         delete.addActionListener(this);
113                                                 }
114
115                                                 /*
116                                                  * Start flight log download
117                                                  */
118
119                                                 if (any_download)
120                                                         download.start();
121                                                 else if (any_delete)
122                                                         delete.start();
123                                                 else
124                                                         finish();
125                                         }
126                                 }
127                         } catch (FileNotFoundException ee) {
128                                 JOptionPane.showMessageDialog(frame,
129                                                               String.format("Cannot open device \"%s\"",
130                                                                             device.toShortString()),
131                                                               "Cannot open target device",
132                                                               JOptionPane.ERROR_MESSAGE);
133                         } catch (AltosSerialInUseException si) {
134                                 JOptionPane.showMessageDialog(frame,
135                                                               String.format("Device \"%s\" already in use",
136                                                                             device.toShortString()),
137                                                               "Device in use",
138                                                               JOptionPane.ERROR_MESSAGE);
139                         } catch (IOException ee) {
140                                 JOptionPane.showMessageDialog(frame,
141                                                               device.toShortString(),
142                                                               ee.getLocalizedMessage(),
143                                                               JOptionPane.ERROR_MESSAGE);
144                                 finish();
145                         } catch (TimeoutException te) {
146                                 JOptionPane.showMessageDialog(frame,
147                                                               String.format("Communications failed with \"%s\"",
148                                                                             device.toShortString()),
149                                                               "Cannot open target device",
150                                                               JOptionPane.ERROR_MESSAGE);
151                                 finish();
152                         } catch (InterruptedException ie) {
153                                 finish();
154                         }
155                 }
156         }
157 }