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