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