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