update changelogs for Debian build
[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_input();
48                         serial_line.close();
49                         serial_line = null;
50                 }
51         }
52
53         private String showDeletedFlights() {
54                 String  result = "";
55
56                 for (AltosEepromLog flight : flights) {
57                         if (flight.delete) {
58                                 if (result.equals(""))
59                                         result = String.format("%d", flight.flight);
60                                 else
61                                         result = String.format("%s, %d", result, flight.flight);
62                         }
63                 }
64                 return result;
65         }
66
67         public void actionPerformed(ActionEvent e) {
68                 String  cmd = e.getActionCommand();
69                 boolean success = e.getID() != 0;
70                 boolean running = false;
71
72                 System.out.printf("Eeprom manager action %s %d\n", cmd, e.getID());
73                 if (cmd.equals("download")) {
74                         if (success) {
75                                 if (any_delete) {
76                                         delete.start();
77                                         running = true;
78                                 }
79                         }
80                 } else if (cmd.equals("delete")) {
81                         if (success) {
82                                 JOptionPane.showMessageDialog(frame,
83                                                               String.format("Flights erased: %s",
84                                                                             showDeletedFlights()),
85                                                               serial_line.device.toShortString(),
86                                                               JOptionPane.INFORMATION_MESSAGE);
87                         }
88                 }
89                 if (!running)
90                         finish();
91         }
92
93         public AltosEepromManage(JFrame given_frame) {
94
95                 boolean running = false;
96
97                 frame = given_frame;
98                 device = AltosDeviceDialog.show(frame, AltosDevice.product_any);
99
100                 remote = false;
101                 any_download = false;
102                 any_delete = false;
103
104                 if (device != null) {
105                         try {
106                                 serial_line = new AltosSerial(device);
107                                 if (!device.matchProduct(AltosDevice.product_telemetrum))
108                                         remote = true;
109
110                                 flights = new AltosEepromList(serial_line, remote);
111
112                                 if (flights.size() == 0) {
113                                         JOptionPane.showMessageDialog(frame,
114                                                                       String.format("No flights available on %d",
115                                                                                     device.getSerial()),
116                                                                       serial_line.device.toShortString(),
117                                                 JOptionPane.INFORMATION_MESSAGE);
118                                 } else {
119                                         AltosEepromSelect       select = new AltosEepromSelect(frame, flights);
120
121                                         if (select.run()) {
122                                                 for (AltosEepromLog flight : flights) {
123                                                         any_download = any_download || flight.download;
124                                                         any_delete = any_delete || flight.delete;
125                                                 }
126                                                 if (any_download) {
127                                                         download = new AltosEepromDownload(frame,
128                                                                                            serial_line,
129                                                                                            remote,
130                                                                                            flights);
131                                                         download.addActionListener(this);
132                                                 }
133
134                                                 if (any_delete) {
135                                                         delete = new AltosEepromDelete(frame,
136                                                                                        serial_line,
137                                                                                        remote,
138                                                                                        flights);
139                                                         delete.addActionListener(this);
140                                                 }
141
142                                                 /*
143                                                  * Start flight log download
144                                                  */
145
146                                                 if (any_download) {
147                                                         download.start();
148                                                         running = true;
149                                                 }
150                                                 else if (any_delete) {
151                                                         delete.start();
152                                                         running = true;
153                                                 }
154                                         }
155                                 }
156                         } catch (FileNotFoundException ee) {
157                                 JOptionPane.showMessageDialog(frame,
158                                                               String.format("Cannot open device \"%s\"",
159                                                                             device.toShortString()),
160                                                               "Cannot open target device",
161                                                               JOptionPane.ERROR_MESSAGE);
162                         } catch (AltosSerialInUseException si) {
163                                 JOptionPane.showMessageDialog(frame,
164                                                               String.format("Device \"%s\" already in use",
165                                                                             device.toShortString()),
166                                                               "Device in use",
167                                                               JOptionPane.ERROR_MESSAGE);
168                         } catch (IOException ee) {
169                                 JOptionPane.showMessageDialog(frame,
170                                                               device.toShortString(),
171                                                               ee.getLocalizedMessage(),
172                                                               JOptionPane.ERROR_MESSAGE);
173                         } catch (TimeoutException te) {
174                                 JOptionPane.showMessageDialog(frame,
175                                                               String.format("Communications failed with \"%s\"",
176                                                                             device.toShortString()),
177                                                               "Cannot open target device",
178                                                               JOptionPane.ERROR_MESSAGE);
179                         } catch (InterruptedException ie) {
180                         }
181                         if (!running)
182                                 finish();
183                 }
184         }
185 }