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