Generate LED icons on the fly. Include SVG versions.
[fw/altos] / altosuilib / AltosEepromDelete.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 AltosEepromDelete implements Runnable {
28         AltosEepromList         flights;
29         Thread                  eeprom_thread;
30         AltosSerial             serial_line;
31         boolean                 remote;
32         JFrame                  frame;
33         ActionListener          listener;
34         boolean                 success;
35
36         private void DeleteLog (AltosEepromLog log)
37                 throws IOException, InterruptedException, TimeoutException {
38
39                 if (flights.config_data.flight_log_max != 0 || flights.config_data.log_format != 0) {
40
41                         /* Devices with newer firmware can erase the
42                          * flash blocks containing each flight
43                          */
44                         serial_line.flush_input();
45                         serial_line.printf("d %d\n", log.flight);
46                         for (;;) {
47                                 /* It can take a while to erase the flash... */
48                                 String line = serial_line.get_reply(20000);
49                                 if (line == null)
50                                         throw new TimeoutException();
51                                 if (line.equals("Erased"))
52                                         break;
53                                 if (line.startsWith("No such"))
54                                         throw new IOException(line);
55                         }
56                 }
57         }
58
59         private void show_error_internal(String message, String title) {
60                 JOptionPane.showMessageDialog(frame,
61                                               message,
62                                               title,
63                                               JOptionPane.ERROR_MESSAGE);
64         }
65
66         private void show_error(String in_message, String in_title) {
67                 final String message = in_message;
68                 final String title = in_title;
69                 Runnable r = new Runnable() {
70                                 public void run() {
71                                         try {
72                                                 show_error_internal(message, title);
73                                         } catch (Exception ex) {
74                                         }
75                                 }
76                         };
77                 SwingUtilities.invokeLater(r);
78         }
79
80         public void run () {
81                 success = false;
82                 try {
83                         if (remote)
84                                 serial_line.start_remote();
85
86                         for (AltosEepromLog log : flights) {
87                                 if (log.delete_selected) {
88                                         DeleteLog(log);
89                                 }
90                         }
91                         success = true;
92                 } catch (IOException ee) {
93                         show_error (ee.getLocalizedMessage(),
94                                     serial_line.device.toShortString());
95                 } catch (InterruptedException ie) {
96                 } catch (TimeoutException te) {
97                         show_error (String.format("Connection to \"%s\" failed",
98                                                   serial_line.device.toShortString()),
99                                     "Connection Failed");
100                 } finally {
101                         try {
102                                 if (remote)
103                                         serial_line.stop_remote();
104                         } catch (InterruptedException ie) {
105                         } finally {
106                                 serial_line.flush_output();
107                                 serial_line.close();
108                         }
109                 }
110                 if (listener != null) {
111                         Runnable r = new Runnable() {
112                                         public void run() {
113                                                 try {
114                                                         listener.actionPerformed(new ActionEvent(this,
115                                                                                                  success ? 1 : 0,
116                                                                                                  "delete"));
117                                                 } catch (Exception ex) {
118                                                 }
119                                         }
120                                 };
121                         SwingUtilities.invokeLater(r);
122                 }
123         }
124
125         public void start() {
126                 eeprom_thread = new Thread(this);
127                 eeprom_thread.start();
128         }
129
130         public void addActionListener(ActionListener l) {
131                 listener = l;
132         }
133
134         public AltosEepromDelete(JFrame given_frame,
135                                  AltosSerial given_serial_line,
136                                  boolean given_remote,
137                                  AltosEepromList given_flights) {
138                 frame = given_frame;
139                 serial_line = given_serial_line;
140                 remote = given_remote;
141                 flights = given_flights;
142                 success = false;
143         }
144 }