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