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