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