altos: Clean up serial initialization
[fw/altos] / altosui / AltosEepromMonitor.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 javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
30
31 public class AltosEepromMonitor extends JDialog {
32
33         Container       pane;
34         Box             box;
35         JLabel          serial_label;
36         JLabel          flight_label;
37         JLabel          file_label;
38         JLabel          serial_value;
39         JLabel          flight_value;
40         JLabel          file_value;
41         JButton         cancel;
42         JProgressBar    pbar;
43         int             min_state, max_state;
44
45         public AltosEepromMonitor(JFrame owner, int in_min_state, int in_max_state) {
46                 super (owner, "Download Flight Data", false);
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                 min_state = in_min_state;
107                 max_state = in_max_state;
108                 pbar = new JProgressBar();
109                 pbar.setMinimum(0);
110                 pbar.setMaximum((max_state - min_state) * 100);
111                 pbar.setValue(0);
112                 pbar.setString("startup");
113                 pbar.setStringPainted(true);
114                 pbar.setPreferredSize(new Dimension(600, 20));
115                 c = new GridBagConstraints();
116                 c.fill = GridBagConstraints.HORIZONTAL;
117                 c.anchor = GridBagConstraints.CENTER;
118                 c.gridx = 0; c.gridy = 3;
119                 c.gridwidth = GridBagConstraints.REMAINDER;
120                 Insets ib = new Insets(4,4,4,4);
121                 c.insets = ib;
122                 pane.add(pbar, c);
123
124
125                 cancel = new JButton("Cancel");
126                 c = new GridBagConstraints();
127                 c.fill = GridBagConstraints.NONE;
128                 c.anchor = GridBagConstraints.CENTER;
129                 c.gridx = 0; c.gridy = 4;
130                 c.gridwidth = GridBagConstraints.REMAINDER;
131                 Insets ic = new Insets(4,4,4,4);
132                 c.insets = ic;
133                 pane.add(cancel, c);
134
135                 pack();
136                 setLocationRelativeTo(owner);
137                 setVisible(true);
138         }
139
140         public void addActionListener (ActionListener l) {
141                 cancel.addActionListener(l);
142         }
143
144         private void set_value_internal(String state_name, int in_state, int in_block) {
145                 int block = in_block;
146                 int state = in_state;
147
148                 if (block > 100)
149                         block = 100;
150                 if (state < min_state) state = min_state;
151                 if (state >= max_state) state = max_state - 1;
152                 state -= min_state;
153
154                 int pos = state * 100 + block;
155
156                 pbar.setString(state_name);
157                 pbar.setValue(pos);
158         }
159
160         public void set_value(String in_state_name, int in_state, int in_block) {
161                 final String state_name = in_state_name;
162                 final int state = in_state;
163                 final int block = in_block;
164                 Runnable r = new Runnable() {
165                                 public void run() {
166                                         try {
167                                                 set_value_internal(state_name, state, 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 set_file_internal(String file) {
210                 file_value.setText(String.format("%s", file));
211         }
212
213         public void set_file(String in_file) {
214                 final String file = in_file;
215                 Runnable r = new Runnable() {
216                                 public void run() {
217                                         try {
218                                                 set_file_internal(file);
219                                         } catch (Exception ex) {
220                                         }
221                                 }
222                         };
223                 SwingUtilities.invokeLater(r);
224         }
225
226         private void done_internal() {
227                 setVisible(false);
228                 dispose();
229         }
230
231         public void done() {
232                 Runnable r = new Runnable() {
233                                 public void run() {
234                                         try {
235                                                 done_internal();
236                                         } catch (Exception ex) {
237                                         }
238                                 }
239                         };
240                 SwingUtilities.invokeLater(r);
241         }
242
243         private void reset_internal() {
244                 set_value_internal("startup",min_state,0);
245                 set_flight_internal(0);
246                 set_file_internal("");
247         }
248
249         public void reset() {
250                 Runnable r = new Runnable() {
251                                 public void run() {
252                                         try {
253                                                 reset_internal();
254                                         } catch (Exception ex) {
255                                         }
256                                 }
257                         };
258                 SwingUtilities.invokeLater(r);
259         }
260 }