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