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