Clean up reflashing section, include section on self-flash recovery
[fw/altos] / altosui / AltosEepromMonitorUI.java
1 /*
2  * Copyright © 2010 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 org.altusmetrum.altosuilib_1.*;
24 import org.altusmetrum.altoslib_2.*;
25
26 public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMonitor {
27         JFrame          owner;
28         Container       pane;
29         Box             box;
30         JLabel          serial_label;
31         JLabel          flight_label;
32         JLabel          file_label;
33         JLabel          serial_value;
34         JLabel          flight_value;
35         JLabel          file_value;
36         JButton         cancel;
37         JProgressBar    pbar;
38         int             min_state, max_state;
39         ActionListener  listener;
40
41         public AltosEepromMonitorUI(JFrame owner) {
42                 super (owner, "Download Flight Data", false);
43
44                 this.owner = owner;
45
46                 GridBagConstraints c;
47                 Insets il = new Insets(4,4,4,4);
48                 Insets ir = new Insets(4,4,4,4);
49
50                 pane = getContentPane();
51                 pane.setLayout(new GridBagLayout());
52
53                 c = new GridBagConstraints();
54                 c.gridx = 0; c.gridy = 0;
55                 c.fill = GridBagConstraints.NONE;
56                 c.anchor = GridBagConstraints.LINE_START;
57                 c.insets = il;
58                 serial_label = new JLabel("Serial:");
59                 pane.add(serial_label, c);
60
61                 c = new GridBagConstraints();
62                 c.gridx = 1; c.gridy = 0;
63                 c.fill = GridBagConstraints.HORIZONTAL;
64                 c.weightx = 1;
65                 c.anchor = GridBagConstraints.LINE_START;
66                 c.insets = ir;
67                 serial_value = new JLabel("");
68                 pane.add(serial_value, c);
69
70                 c = new GridBagConstraints();
71                 c.fill = GridBagConstraints.NONE;
72                 c.gridx = 0; c.gridy = 1;
73                 c.anchor = GridBagConstraints.LINE_START;
74                 c.insets = il;
75                 flight_label = new JLabel("Flight:");
76                 pane.add(flight_label, c);
77
78                 c = new GridBagConstraints();
79                 c.fill = GridBagConstraints.HORIZONTAL;
80                 c.weightx = 1;
81                 c.gridx = 1; c.gridy = 1;
82                 c.anchor = GridBagConstraints.LINE_START;
83                 c.insets = ir;
84                 flight_value = new JLabel("");
85                 pane.add(flight_value, c);
86
87                 c = new GridBagConstraints();
88                 c.fill = GridBagConstraints.NONE;
89                 c.gridx = 0; c.gridy = 2;
90                 c.anchor = GridBagConstraints.LINE_START;
91                 c.insets = il;
92                 file_label = new JLabel("File:");
93                 pane.add(file_label, c);
94
95                 c = new GridBagConstraints();
96                 c.fill = GridBagConstraints.HORIZONTAL;
97                 c.weightx = 1;
98                 c.gridx = 1; c.gridy = 2;
99                 c.anchor = GridBagConstraints.LINE_START;
100                 c.insets = ir;
101                 file_value = new JLabel("");
102                 pane.add(file_value, c);
103
104                 pbar = new JProgressBar();
105                 pbar.setMinimum(0);
106                 pbar.setMaximum(1000);
107                 pbar.setValue(0);
108                 pbar.setString("startup");
109                 pbar.setStringPainted(true);
110                 pbar.setPreferredSize(new Dimension(600, 20));
111                 c = new GridBagConstraints();
112                 c.fill = GridBagConstraints.HORIZONTAL;
113                 c.anchor = GridBagConstraints.CENTER;
114                 c.gridx = 0; c.gridy = 3;
115                 c.gridwidth = GridBagConstraints.REMAINDER;
116                 Insets ib = new Insets(4,4,4,4);
117                 c.insets = ib;
118                 pane.add(pbar, c);
119
120
121                 cancel = new JButton("Cancel");
122                 c = new GridBagConstraints();
123                 c.fill = GridBagConstraints.NONE;
124                 c.anchor = GridBagConstraints.CENTER;
125                 c.gridx = 0; c.gridy = 4;
126                 c.gridwidth = GridBagConstraints.REMAINDER;
127                 Insets ic = new Insets(4,4,4,4);
128                 c.insets = ic;
129                 pane.add(cancel, c);
130
131                 pack();
132                 setLocationRelativeTo(owner);
133         }
134
135         public void addActionListener(ActionListener l) {
136                 listener = l;
137         }
138
139         public void set_states(int min_state, int max_state) {
140                 this.min_state = min_state;
141                 this.max_state = max_state;
142         }
143
144         public void set_thread(Thread in_eeprom_thread) {
145                 final Thread eeprom_thread = in_eeprom_thread;
146                 cancel.addActionListener(new ActionListener() {
147                                 public void actionPerformed(ActionEvent e) {
148                                         if (eeprom_thread != null)
149                                                 eeprom_thread.interrupt();
150                                 }
151                         });
152         }
153
154         public void start() {
155                 setVisible(true);
156         }
157
158         private void set_value_internal(String state_name, int state, int state_block, int block) {
159                 if (state_block > 100)
160                         state_block = 100;
161                 if (state < min_state) state = min_state;
162                 if (state >= max_state) state = max_state - 1;
163                 state -= min_state;
164
165                 int pos = state * 100 + state_block;
166
167                 pbar.setString(String.format("block %d state %s", block, state_name));
168                 pbar.setValue(pos);
169         }
170
171         public void set_value(String in_state_name, int in_state, int in_state_block, int in_block) {
172                 final String state_name = in_state_name;
173                 final int state = in_state;
174                 final int state_block = in_state_block;
175                 final int block = in_block;
176                 Runnable r = new Runnable() {
177                                 public void run() {
178                                         try {
179                                                 set_value_internal(state_name, state, state_block, block);
180                                         } catch (Exception ex) {
181                                         }
182                                 }
183                         };
184                 SwingUtilities.invokeLater(r);
185         }
186
187         private void set_serial_internal(int serial) {
188                 serial_value.setText(String.format("%d", serial));
189         }
190
191         public void set_serial(int in_serial) {
192                 final int serial = in_serial;
193                 Runnable r = new Runnable() {
194                                 public void run() {
195                                         try {
196                                                 set_serial_internal(serial);
197                                         } catch (Exception ex) {
198                                         }
199                                 }
200                         };
201                 SwingUtilities.invokeLater(r);
202         }
203
204         private void set_flight_internal(int flight) {
205                 flight_value.setText(String.format("%d", flight));
206         }
207
208         public void set_flight(int in_flight) {
209                 final int flight = in_flight;
210                 Runnable r = new Runnable() {
211                                 public void run() {
212                                         try {
213                                                 set_flight_internal(flight);
214                                         } catch (Exception ex) {
215                                         }
216                                 }
217                         };
218                 SwingUtilities.invokeLater(r);
219         }
220
221         private void set_filename_internal(String filename) {
222                 file_value.setText(String.format("%s", filename));
223         }
224
225         public void set_filename(String in_filename) {
226                 final String filename = in_filename;
227                 Runnable r = new Runnable() {
228                                 public void run() {
229                                         try {
230                                                 set_filename_internal(filename);
231                                         } catch (Exception ex) {
232                                         }
233                                 }
234                         };
235                 SwingUtilities.invokeLater(r);
236         }
237
238         private void done_internal(boolean success) {
239                 listener.actionPerformed(new ActionEvent(this,
240                                                          success ? 1 : 0,
241                                                          "download"));
242                 setVisible(false);
243                 dispose();
244         }
245
246         public void done(boolean in_success) {
247                 final boolean success = in_success;
248                 Runnable r = new Runnable() {
249                                 public void run() {
250                                         try {
251                                                 done_internal(success);
252                                         } catch (Exception ex) {
253                                         }
254                                 }
255                         };
256                 SwingUtilities.invokeLater(r);
257         }
258
259         private void reset_internal() {
260                 set_value_internal("startup",min_state,0, 0);
261                 set_flight_internal(0);
262                 set_filename_internal("");
263         }
264
265         public void reset() {
266                 Runnable r = new Runnable() {
267                                 public void run() {
268                                         try {
269                                                 reset_internal();
270                                         } catch (Exception ex) {
271                                         }
272                                 }
273                         };
274                 SwingUtilities.invokeLater(r);
275         }
276
277         private void show_message_internal(String message, String title, int message_type) {
278                 int joption_message_type = JOptionPane.ERROR_MESSAGE;
279
280                 switch (message_type) {
281                 case INFO_MESSAGE:
282                         joption_message_type = JOptionPane.INFORMATION_MESSAGE;
283                         break;
284                 case WARNING_MESSAGE:
285                         joption_message_type = JOptionPane.WARNING_MESSAGE;
286                         break;
287                 case ERROR_MESSAGE:
288                         joption_message_type = JOptionPane.ERROR_MESSAGE;
289                         break;
290                 }
291                 JOptionPane.showMessageDialog(owner,
292                                               message,
293                                               title,
294                                               joption_message_type);
295         }
296
297         public void show_message(String in_message, String in_title, int in_message_type) {
298                 final String message = in_message;
299                 final String title = in_title;
300                 final int message_type = in_message_type;
301                 Runnable r = new Runnable() {
302                                 public void run() {
303                                         try {
304                                                 show_message_internal(message, title, message_type);
305                                         } catch (Exception ex) {
306                                         }
307                                 }
308                         };
309                 SwingUtilities.invokeLater(r);
310         }
311 }