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