Add shipping products to fat_altos target, note that in Releasing
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altosuilib_13;
20
21 import java.awt.event.*;
22 import javax.swing.*;
23 import java.io.*;
24 import java.util.concurrent.*;
25 import org.altusmetrum.altoslib_13.*;
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         AltosEepromGrapher      grapher;
37
38         public void finish() {
39                 if (serial_line != null) {
40                         try {
41                                 serial_line.flush_input();
42                         } catch (InterruptedException ie) {
43                         }
44                         serial_line.close();
45                         serial_line = null;
46                 }
47         }
48
49         private int countDeletedFlights() {
50                 int count = 0;
51                 for (AltosEepromLog flight : flights) {
52                         if (flight.delete_selected)
53                                 count++;
54                 }
55                 return count;
56         }
57
58         private String showDeletedFlights() {
59                 String  result = "";
60
61                 for (AltosEepromLog flight : flights) {
62                         if (flight.delete_selected) {
63                                 if (result.equals(""))
64                                         result = String.format("%d", flight.flight);
65                                 else
66                                         result = String.format("%s, %d", result, flight.flight);
67                         }
68                 }
69                 return result;
70         }
71
72         public boolean delete_start() {
73
74                 boolean any_selected = false;
75                 for (AltosEepromLog flight : flights)
76                         any_selected = any_selected || flight.delete_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                 return false;
91         }
92
93         public void graph_start() {
94                 boolean any_selected = false;
95                 for (AltosEepromLog flight : flights) {
96                         if (!flight.download_selected)
97                                 flight.graph_selected = false;
98                         any_selected = any_selected || flight.graph_selected;
99                 }
100                 if (any_selected && grapher != null)
101                         grapher.graph_flights(flights);
102         }
103
104         public void actionPerformed(ActionEvent e) {
105                 String  cmd = e.getActionCommand();
106                 boolean success = e.getID() != 0;
107                 boolean running = false;
108
109                 if (cmd.equals("download")) {
110                         if (success) {
111                                 running = delete_start();
112                                 if (!running)
113                                         graph_start();
114                         }
115                 } else if (cmd.equals("delete")) {
116                         if (success) {
117                                 JOptionPane.showMessageDialog(frame,
118                                                               String.format("%d flights erased: %s",
119                                                                             countDeletedFlights(),
120                                                                             showDeletedFlights()),
121                                                               serial_line.device.toShortString(),
122                                                               JOptionPane.INFORMATION_MESSAGE);
123                                 graph_start();
124                         }
125                 }
126                 if (!running)
127                         finish();
128         }
129
130         public void got_flights(AltosEepromList in_flights) {
131                 boolean running = false;;
132
133                 flights = in_flights;
134                 try {
135                         if (flights.size() == 0) {
136                                 JOptionPane.showMessageDialog(frame,
137                                                               String.format("No flights available on %d",
138                                                                             device.getSerial()),
139                                                               serial_line.device.toShortString(),
140                                                               JOptionPane.INFORMATION_MESSAGE);
141                         } else {
142                                 AltosEepromSelect       select = new AltosEepromSelect(frame, flights, grapher != null);
143
144                                 if (select.run()) {
145                                         boolean any_selected = false;
146                                         for (AltosEepromLog flight : flights)
147                                                 any_selected = any_selected || flight.download_selected;
148                                         if (any_selected) {
149                                                 AltosEepromMonitorUI monitor = new AltosEepromMonitorUI(frame);
150                                                 monitor.addActionListener(this);
151                                                 serial_line.set_frame(frame);
152                                                 download = new AltosEepromDownload(monitor,
153                                                                                    serial_line,
154                                                                                    remote,
155                                                                                    flights);
156                                                 /*
157                                                  * Start flight log download
158                                                  */
159
160                                                 download.start();
161                                                 running = true;
162                                         } else {
163                                                 running = delete_start();
164                                                 if (!running)
165                                                         graph_start();
166                                         }
167                                 }
168                         }
169                         if (!running)
170                                 finish();
171                 } catch (Exception e) {
172                         got_exception(e);
173                 }
174         }
175
176         public void got_exception(Exception e) {
177                 if (e instanceof IOException) {
178                         IOException     ee = (IOException) e;
179                         JOptionPane.showMessageDialog(frame,
180                                                       device.toShortString(),
181                                                       ee.getLocalizedMessage(),
182                                                       JOptionPane.ERROR_MESSAGE);
183                 } else if (e instanceof TimeoutException) {
184                         //TimeoutException te = (TimeoutException) e;
185                         JOptionPane.showMessageDialog(frame,
186                                                       String.format("Communications failed with \"%s\"",
187                                                                     device.toShortString()),
188                                                       "Cannot open target device",
189                                                       JOptionPane.ERROR_MESSAGE);
190                 }
191                 finish();
192         }
193
194         class EepromGetList implements Runnable {
195
196                 AltosEepromManage       manage;
197
198                 public void run () {
199                         Runnable r;
200                         try {
201                                 flights = new AltosEepromList(serial_line, remote);
202                                 r = new Runnable() {
203                                                 public void run() {
204                                                         got_flights(flights);
205                                                 }
206                                         };
207                         } catch (Exception e) {
208                                 final Exception f_e = e;
209                                 r = new Runnable() {
210                                                 public void run() {
211                                                         got_exception(f_e);
212                                                 }
213                                         };
214                         }
215                         SwingUtilities.invokeLater(r);
216                 }
217
218                 public EepromGetList(AltosEepromManage in_manage) {
219                         manage = in_manage;
220                 }
221         }
222
223         public AltosEepromManage(JFrame given_frame, AltosEepromGrapher grapher, int product) {
224
225                 //boolean       running = false;
226
227                 frame = given_frame;
228                 this.grapher = grapher;
229                 device = AltosDeviceUIDialog.show(frame, product);
230
231                 remote = false;
232
233                 if (device != null) {
234                         try {
235                                 serial_line = new AltosSerial(device);
236                                 if (device.matchProduct(AltosLib.product_basestation))
237                                         remote = true;
238
239                                 serial_line.set_frame(frame);
240
241                                 EepromGetList   get_list = new EepromGetList(this);
242                                 Thread          t = new Thread(get_list);
243                                 t.start();
244                         } catch (FileNotFoundException ee) {
245                                 JOptionPane.showMessageDialog(frame,
246                                                               ee.getMessage(),
247                                                               "Cannot open target device",
248                                                               JOptionPane.ERROR_MESSAGE);
249                         } catch (AltosSerialInUseException si) {
250                                 JOptionPane.showMessageDialog(frame,
251                                                               String.format("Device \"%s\" already in use",
252                                                                             device.toShortString()),
253                                                               "Device in use",
254                                                               JOptionPane.ERROR_MESSAGE);
255                         }
256                 }
257         }
258 }