5fb70a8413cba0e1e5b9be39f4421b46cc3467a8
[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                 if (cmd.equals("download")) {
73                         if (success) {
74                                 if (any_delete) {
75                                         delete.start();
76                                         running = true;
77                                 }
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                 }
88                 if (!running)
89                         finish();
90         }
91
92         public void got_flights(AltosEepromList in_flights) {
93                 boolean running = false;;
94
95                 flights = in_flights;
96                 try {
97                         if (flights.size() == 0) {
98                                 JOptionPane.showMessageDialog(frame,
99                                                               String.format("No flights available on %d",
100                                                                             device.getSerial()),
101                                                               serial_line.device.toShortString(),
102                                                               JOptionPane.INFORMATION_MESSAGE);
103                         } else {
104                                 AltosEepromSelect       select = new AltosEepromSelect(frame, flights);
105
106                                 if (select.run()) {
107                                         for (AltosEepromLog flight : flights) {
108                                                 any_download = any_download || flight.download;
109                                                 any_delete = any_delete || flight.delete;
110                                         }
111                                         if (any_download) {
112                                                 download = new AltosEepromDownload(frame,
113                                                                                    serial_line,
114                                                                                    remote,
115                                                                                    flights);
116                                                 download.addActionListener(this);
117                                         }
118
119                                         if (any_delete) {
120                                                 delete = new AltosEepromDelete(frame,
121                                                                                serial_line,
122                                                                                remote,
123                                                                                flights);
124                                                 delete.addActionListener(this);
125                                         }
126
127                                         /*
128                                          * Start flight log download
129                                          */
130
131                                         if (any_download) {
132                                                 download.start();
133                                                 running = true;
134                                         }
135                                         else if (any_delete) {
136                                                 delete.start();
137                                                 running = true;
138                                         }
139                                 }
140                         }
141                         if (!running)
142                                 finish();
143                 } catch (Exception e) {
144                         got_exception(e);
145                 }
146         }
147
148         public void got_exception(Exception e) {
149                 if (e instanceof IOException) {
150                         IOException     ee = (IOException) e;
151                         JOptionPane.showMessageDialog(frame,
152                                                       device.toShortString(),
153                                                       ee.getLocalizedMessage(),
154                                                       JOptionPane.ERROR_MESSAGE);
155                 } else if (e instanceof TimeoutException) {
156                         TimeoutException te = (TimeoutException) e;
157                         JOptionPane.showMessageDialog(frame,
158                                                       String.format("Communications failed with \"%s\"",
159                                                                     device.toShortString()),
160                                                       "Cannot open target device",
161                                                       JOptionPane.ERROR_MESSAGE);
162                 }
163                 finish();
164         }
165
166         class EepromGetList implements Runnable {
167
168                 AltosEepromManage       manage;
169
170                 public void run () {
171                         try {
172                                 flights = new AltosEepromList(serial_line, remote);
173                                 Runnable r = new Runnable() {
174                                                 public void run() {
175                                                         manage.got_flights(flights);
176                                                 }
177                                         };
178                                 SwingUtilities.invokeLater(r);
179                         } catch (Exception e) {
180                                 manage.got_exception(e);
181                         }
182                 }
183
184                 public EepromGetList(AltosEepromManage in_manage) {
185                         manage = in_manage;
186                 }
187         }
188
189         public AltosEepromManage(JFrame given_frame) {
190
191                 boolean running = false;
192
193                 frame = given_frame;
194                 device = AltosDeviceDialog.show(frame, AltosDevice.product_any);
195
196                 remote = false;
197                 any_download = false;
198                 any_delete = false;
199
200                 if (device != null) {
201                         try {
202                                 serial_line = new AltosSerial(device);
203                                 if (!device.matchProduct(AltosDevice.product_telemetrum))
204                                         remote = true;
205
206                                 serial_line.set_frame(frame);
207
208                                 EepromGetList   get_list = new EepromGetList(this);
209                                 Thread          t = new Thread(get_list);
210                                 t.start();
211                         } catch (FileNotFoundException ee) {
212                                 JOptionPane.showMessageDialog(frame,
213                                                               String.format("Cannot open device \"%s\"",
214                                                                             device.toShortString()),
215                                                               "Cannot open target device",
216                                                               JOptionPane.ERROR_MESSAGE);
217                         } catch (AltosSerialInUseException si) {
218                                 JOptionPane.showMessageDialog(frame,
219                                                               String.format("Device \"%s\" already in use",
220                                                                             device.toShortString()),
221                                                               "Device in use",
222                                                               JOptionPane.ERROR_MESSAGE);
223                         }
224                 }
225         }
226 }