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