altosui: Change flight data saving UI to separate download/delete selections
[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
43         public void finish() {
44                 if (serial_line != null) {
45                         try {
46                                 serial_line.flush_input();
47                         } catch (InterruptedException ie) {
48                         }
49                         serial_line.close();
50                         serial_line = null;
51                 }
52         }
53
54         private String showDeletedFlights() {
55                 String  result = "";
56
57                 for (AltosEepromLog flight : flights) {
58                         if (flight.selected) {
59                                 if (result.equals(""))
60                                         result = String.format("%d", flight.flight);
61                                 else
62                                         result = String.format("%s, %d", result, flight.flight);
63                         }
64                 }
65                 return result;
66         }
67
68         public boolean download_done() {
69                 AltosEepromSelect       select = new AltosEepromSelect(frame, flights, "Delete");
70
71                 if (select.run()) {
72                         boolean any_selected = false;
73                         for (AltosEepromLog flight : flights)
74                                 any_selected = any_selected || flight.selected;
75                         if (any_selected) {
76                                 delete = new AltosEepromDelete(frame,
77                                                                serial_line,
78                                                                remote,
79                                                                flights);
80                                 delete.addActionListener(this);
81                                 /*
82                                  * Start flight log delete
83                                  */
84
85                                 delete.start();
86                                 return true;
87                         }
88                 }
89                 return false;
90         }
91
92         public void actionPerformed(ActionEvent e) {
93                 String  cmd = e.getActionCommand();
94                 boolean success = e.getID() != 0;
95                 boolean running = false;
96
97                 if (cmd.equals("download")) {
98                         if (success)
99                                 running = download_done();
100                 } else if (cmd.equals("delete")) {
101                         if (success) {
102                                 JOptionPane.showMessageDialog(frame,
103                                                               String.format("Flights erased: %s",
104                                                                             showDeletedFlights()),
105                                                               serial_line.device.toShortString(),
106                                                               JOptionPane.INFORMATION_MESSAGE);
107                         }
108                 }
109                 if (!running)
110                         finish();
111         }
112
113         public void got_flights(AltosEepromList in_flights) {
114                 boolean running = false;;
115
116                 flights = in_flights;
117                 try {
118                         if (flights.size() == 0) {
119                                 JOptionPane.showMessageDialog(frame,
120                                                               String.format("No flights available on %d",
121                                                                             device.getSerial()),
122                                                               serial_line.device.toShortString(),
123                                                               JOptionPane.INFORMATION_MESSAGE);
124                         } else {
125                                 AltosEepromSelect       select = new AltosEepromSelect(frame, flights, "Download");
126
127                                 if (select.run()) {
128                                         boolean any_selected = false;
129                                         for (AltosEepromLog flight : flights)
130                                                 any_selected = any_selected || flight.selected;
131                                         if (any_selected) {
132                                                 download = new AltosEepromDownload(frame,
133                                                                                    serial_line,
134                                                                                    remote,
135                                                                                    flights);
136                                                 download.addActionListener(this);
137                                                 /*
138                                                  * Start flight log download
139                                                  */
140
141                                                 download.start();
142                                                 running = true;
143                                         } else {
144                                                 running = download_done();
145                                         }
146                                 }
147                         }
148                         if (!running)
149                                 finish();
150                 } catch (Exception e) {
151                         got_exception(e);
152                 }
153         }
154
155         public void got_exception(Exception e) {
156                 if (e instanceof IOException) {
157                         IOException     ee = (IOException) e;
158                         JOptionPane.showMessageDialog(frame,
159                                                       device.toShortString(),
160                                                       ee.getLocalizedMessage(),
161                                                       JOptionPane.ERROR_MESSAGE);
162                 } else if (e instanceof TimeoutException) {
163                         TimeoutException te = (TimeoutException) e;
164                         JOptionPane.showMessageDialog(frame,
165                                                       String.format("Communications failed with \"%s\"",
166                                                                     device.toShortString()),
167                                                       "Cannot open target device",
168                                                       JOptionPane.ERROR_MESSAGE);
169                 }
170                 finish();
171         }
172
173         class EepromGetList implements Runnable {
174
175                 AltosEepromManage       manage;
176
177                 public void run () {
178                         Runnable r;
179                         try {
180                                 flights = new AltosEepromList(serial_line, remote);
181                                 r = new Runnable() {
182                                                 public void run() {
183                                                         got_flights(flights);
184                                                 }
185                                         };
186                         } catch (Exception e) {
187                                 final Exception f_e = e;
188                                 r = new Runnable() {
189                                                 public void run() {
190                                                         got_exception(f_e);
191                                                 }
192                                         };
193                         }
194                         SwingUtilities.invokeLater(r);
195                 }
196
197                 public EepromGetList(AltosEepromManage in_manage) {
198                         manage = in_manage;
199                 }
200         }
201
202         public AltosEepromManage(JFrame given_frame) {
203
204                 boolean running = false;
205
206                 frame = given_frame;
207                 device = AltosDeviceDialog.show(frame, Altos.product_any);
208
209                 remote = false;
210
211                 if (device != null) {
212                         try {
213                                 serial_line = new AltosSerial(device);
214                                 if (device.matchProduct(Altos.product_basestation))
215                                         remote = true;
216
217                                 serial_line.set_frame(frame);
218
219                                 EepromGetList   get_list = new EepromGetList(this);
220                                 Thread          t = new Thread(get_list);
221                                 t.start();
222                         } catch (FileNotFoundException ee) {
223                                 JOptionPane.showMessageDialog(frame,
224                                                               ee.getMessage(),
225                                                               "Cannot open target device",
226                                                               JOptionPane.ERROR_MESSAGE);
227                         } catch (AltosSerialInUseException si) {
228                                 JOptionPane.showMessageDialog(frame,
229                                                               String.format("Device \"%s\" already in use",
230                                                                             device.toShortString()),
231                                                               "Device in use",
232                                                               JOptionPane.ERROR_MESSAGE);
233                         }
234                 }
235         }
236 }