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