altos/altosui: Add pad orientation configure option
[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                         for (;;) {
53                                 /* It can take a while to erase the flash... */
54                                 String line = serial_line.get_reply(20000);
55                                 if (line == null)
56                                         throw new TimeoutException();
57                                 if (line.equals("Erased"))
58                                         break;
59                                 if (line.startsWith("No such"))
60                                         throw new IOException(line);
61                         }
62                 }
63         }
64
65         private void show_error_internal(String message, String title) {
66                 JOptionPane.showMessageDialog(frame,
67                                               message,
68                                               title,
69                                               JOptionPane.ERROR_MESSAGE);
70         }
71
72         private void show_error(String in_message, String in_title) {
73                 final String message = in_message;
74                 final String title = in_title;
75                 Runnable r = new Runnable() {
76                                 public void run() {
77                                         try {
78                                                 show_error_internal(message, title);
79                                         } catch (Exception ex) {
80                                         }
81                                 }
82                         };
83                 SwingUtilities.invokeLater(r);
84         }
85
86         public void run () {
87                 if (remote)
88                         serial_line.start_remote();
89
90                 success = false;
91                 try {
92                         for (AltosEepromLog log : flights) {
93                                 if (log.delete) {
94                                         DeleteLog(log);
95                                 }
96                         }
97                         success = true;
98                 } catch (IOException ee) {
99                         show_error (ee.getLocalizedMessage(),
100                                     serial_line.device.toShortString());
101                 } catch (InterruptedException ie) {
102                 } catch (TimeoutException te) {
103                         show_error (String.format("Connection to \"%s\" failed",
104                                                   serial_line.device.toShortString()),
105                                     "Connection Failed");
106                 }
107                 if (remote)
108                         serial_line.stop_remote();
109                 serial_line.flush_output();
110                 serial_line.close();
111                 if (listener != null) {
112                         Runnable r = new Runnable() {
113                                         public void run() {
114                                                 try {
115                                                         listener.actionPerformed(new ActionEvent(this,
116                                                                                                  success ? 1 : 0,
117                                                                                                  "delete"));
118                                                 } catch (Exception ex) {
119                                                 }
120                                         }
121                                 };
122                         SwingUtilities.invokeLater(r);
123                 }
124         }
125
126         public void start() {
127                 eeprom_thread = new Thread(this);
128                 eeprom_thread.start();
129         }
130
131         public void addActionListener(ActionListener l) {
132                 listener = l;
133         }
134
135         public AltosEepromDelete(JFrame given_frame,
136                                  AltosSerial given_serial_line,
137                                  boolean given_remote,
138                                  AltosEepromList given_flights) {
139                 frame = given_frame;
140                 serial_line = given_serial_line;
141                 remote = given_remote;
142                 flights = given_flights;
143                 success = false;
144         }
145 }