d2d956780cd9c398a8b4be7dc976593cf97a847f
[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; 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 org.altusmetrum.altosuilib_3;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import org.altusmetrum.altoslib_5.*;
24
25 public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMonitor {
26         JFrame          owner;
27         Container       pane;
28         Box             box;
29         JLabel          serial_label;
30         JLabel          flight_label;
31         JLabel          file_label;
32         JLabel          serial_value;
33         JLabel          flight_value;
34         JLabel          file_value;
35         JButton         cancel;
36         JProgressBar    pbar;
37         int             min_state, max_state;
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_states(int min_state, int max_state) {
141                 this.min_state = min_state;
142                 this.max_state = max_state;
143         }
144
145         public void set_thread(Thread in_eeprom_thread) {
146                 final Thread eeprom_thread = in_eeprom_thread;
147                 cancel.addActionListener(new ActionListener() {
148                                 public void actionPerformed(ActionEvent e) {
149                                         if (eeprom_thread != null)
150                                                 eeprom_thread.interrupt();
151                                 }
152                         });
153         }
154
155         public void start() {
156                 setVisible(true);
157         }
158
159         private void set_value_internal(String state_name, int state, int state_block, int block) {
160                 double  pos;
161                 String  s;
162
163                 if (min_state == AltosLib.ao_flight_invalid) {
164                         int     lblock = block;
165                         if (lblock > 1000)
166                                 lblock = 1000;
167                         pos = lblock / 1000.0;
168                         s = String.format("block %d", block);
169                 } else {
170                         if (state == AltosLib.ao_flight_invalid)
171                                 state = 0;
172                         if (state_block > 100)
173                                 state_block = 100;
174                         if (state < min_state) state = min_state;
175                         if (state > max_state) state = max_state;
176
177                         if (state == max_state)
178                                 state_block = 0;
179
180                         state -= min_state;
181
182                         int     nstate = max_state - min_state;
183
184                         double  spos = (double) state / (double) nstate;
185                         double  bpos = state_block / 100.0;
186
187                         pos = spos + bpos / nstate;
188
189                         s = String.format("block %d state %s", block, state_name);
190                 }
191
192                 pbar.setString(s);
193                 pbar.setValue((int) (pos * progress_max));
194         }
195
196         public void set_value(String in_state_name, int in_state, int in_state_block, int in_block) {
197                 final String state_name = in_state_name;
198                 final int state = in_state;
199                 final int state_block = in_state_block;
200                 final int block = in_block;
201                 Runnable r = new Runnable() {
202                                 public void run() {
203                                         try {
204                                                 set_value_internal(state_name, state, state_block, block);
205                                         } catch (Exception ex) {
206                                         }
207                                 }
208                         };
209                 SwingUtilities.invokeLater(r);
210         }
211
212         private void set_serial_internal(int serial) {
213                 serial_value.setText(String.format("%d", serial));
214         }
215
216         public void set_serial(int in_serial) {
217                 final int serial = in_serial;
218                 Runnable r = new Runnable() {
219                                 public void run() {
220                                         try {
221                                                 set_serial_internal(serial);
222                                         } catch (Exception ex) {
223                                         }
224                                 }
225                         };
226                 SwingUtilities.invokeLater(r);
227         }
228
229         private void set_flight_internal(int flight) {
230                 flight_value.setText(String.format("%d", flight));
231         }
232
233         public void set_flight(int in_flight) {
234                 final int flight = in_flight;
235                 Runnable r = new Runnable() {
236                                 public void run() {
237                                         try {
238                                                 set_flight_internal(flight);
239                                         } catch (Exception ex) {
240                                         }
241                                 }
242                         };
243                 SwingUtilities.invokeLater(r);
244         }
245
246         private void set_filename_internal(String filename) {
247                 file_value.setText(String.format("%s", filename));
248         }
249
250         public void set_filename(String in_filename) {
251                 final String filename = in_filename;
252                 Runnable r = new Runnable() {
253                                 public void run() {
254                                         try {
255                                                 set_filename_internal(filename);
256                                         } catch (Exception ex) {
257                                         }
258                                 }
259                         };
260                 SwingUtilities.invokeLater(r);
261         }
262
263         private void done_internal(boolean success) {
264                 listener.actionPerformed(new ActionEvent(this,
265                                                          success ? 1 : 0,
266                                                          "download"));
267                 setVisible(false);
268                 dispose();
269         }
270
271         public void done(boolean in_success) {
272                 final boolean success = in_success;
273                 Runnable r = new Runnable() {
274                                 public void run() {
275                                         try {
276                                                 done_internal(success);
277                                         } catch (Exception ex) {
278                                         }
279                                 }
280                         };
281                 SwingUtilities.invokeLater(r);
282         }
283
284         private void reset_internal() {
285                 set_value_internal("startup",min_state,0, 0);
286                 set_flight_internal(0);
287                 set_filename_internal("");
288         }
289
290         public void reset() {
291                 Runnable r = new Runnable() {
292                                 public void run() {
293                                         try {
294                                                 reset_internal();
295                                         } catch (Exception ex) {
296                                         }
297                                 }
298                         };
299                 SwingUtilities.invokeLater(r);
300         }
301
302         private void show_message_internal(String message, String title, int message_type) {
303                 int joption_message_type = JOptionPane.ERROR_MESSAGE;
304
305                 switch (message_type) {
306                 case INFO_MESSAGE:
307                         joption_message_type = JOptionPane.INFORMATION_MESSAGE;
308                         break;
309                 case WARNING_MESSAGE:
310                         joption_message_type = JOptionPane.WARNING_MESSAGE;
311                         break;
312                 case ERROR_MESSAGE:
313                         joption_message_type = JOptionPane.ERROR_MESSAGE;
314                         break;
315                 }
316                 JOptionPane.showMessageDialog(owner,
317                                               message,
318                                               title,
319                                               joption_message_type);
320         }
321
322         public void show_message(String in_message, String in_title, int in_message_type) {
323                 final String message = in_message;
324                 final String title = in_title;
325                 final int message_type = in_message_type;
326                 Runnable r = new Runnable() {
327                                 public void run() {
328                                         try {
329                                                 show_message_internal(message, title, message_type);
330                                         } catch (Exception ex) {
331                                         }
332                                 }
333                         };
334                 SwingUtilities.invokeLater(r);
335         }
336 }