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