8eae5eb841345c0202144e2c15af58d8d177af83
[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 org.altusmetrum.altosuilib.*;
24
25 public class AltosEepromMonitor extends AltosUIDialog {
26
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
39         public AltosEepromMonitor(JFrame owner, int in_min_state, int in_max_state) {
40                 super (owner, "Download Flight Data", false);
41
42                 GridBagConstraints c;
43                 Insets il = new Insets(4,4,4,4);
44                 Insets ir = new Insets(4,4,4,4);
45
46                 pane = getContentPane();
47                 pane.setLayout(new GridBagLayout());
48
49                 c = new GridBagConstraints();
50                 c.gridx = 0; c.gridy = 0;
51                 c.fill = GridBagConstraints.NONE;
52                 c.anchor = GridBagConstraints.LINE_START;
53                 c.insets = il;
54                 serial_label = new JLabel("Serial:");
55                 pane.add(serial_label, c);
56
57                 c = new GridBagConstraints();
58                 c.gridx = 1; c.gridy = 0;
59                 c.fill = GridBagConstraints.HORIZONTAL;
60                 c.weightx = 1;
61                 c.anchor = GridBagConstraints.LINE_START;
62                 c.insets = ir;
63                 serial_value = new JLabel("");
64                 pane.add(serial_value, c);
65
66                 c = new GridBagConstraints();
67                 c.fill = GridBagConstraints.NONE;
68                 c.gridx = 0; c.gridy = 1;
69                 c.anchor = GridBagConstraints.LINE_START;
70                 c.insets = il;
71                 flight_label = new JLabel("Flight:");
72                 pane.add(flight_label, c);
73
74                 c = new GridBagConstraints();
75                 c.fill = GridBagConstraints.HORIZONTAL;
76                 c.weightx = 1;
77                 c.gridx = 1; c.gridy = 1;
78                 c.anchor = GridBagConstraints.LINE_START;
79                 c.insets = ir;
80                 flight_value = new JLabel("");
81                 pane.add(flight_value, c);
82
83                 c = new GridBagConstraints();
84                 c.fill = GridBagConstraints.NONE;
85                 c.gridx = 0; c.gridy = 2;
86                 c.anchor = GridBagConstraints.LINE_START;
87                 c.insets = il;
88                 file_label = new JLabel("File:");
89                 pane.add(file_label, c);
90
91                 c = new GridBagConstraints();
92                 c.fill = GridBagConstraints.HORIZONTAL;
93                 c.weightx = 1;
94                 c.gridx = 1; c.gridy = 2;
95                 c.anchor = GridBagConstraints.LINE_START;
96                 c.insets = ir;
97                 file_value = new JLabel("");
98                 pane.add(file_value, c);
99
100                 min_state = in_min_state;
101                 max_state = in_max_state;
102                 pbar = new JProgressBar();
103                 pbar.setMinimum(0);
104                 pbar.setMaximum((max_state - min_state) * 100);
105                 pbar.setValue(0);
106                 pbar.setString("startup");
107                 pbar.setStringPainted(true);
108                 pbar.setPreferredSize(new Dimension(600, 20));
109                 c = new GridBagConstraints();
110                 c.fill = GridBagConstraints.HORIZONTAL;
111                 c.anchor = GridBagConstraints.CENTER;
112                 c.gridx = 0; c.gridy = 3;
113                 c.gridwidth = GridBagConstraints.REMAINDER;
114                 Insets ib = new Insets(4,4,4,4);
115                 c.insets = ib;
116                 pane.add(pbar, c);
117
118
119                 cancel = new JButton("Cancel");
120                 c = new GridBagConstraints();
121                 c.fill = GridBagConstraints.NONE;
122                 c.anchor = GridBagConstraints.CENTER;
123                 c.gridx = 0; c.gridy = 4;
124                 c.gridwidth = GridBagConstraints.REMAINDER;
125                 Insets ic = new Insets(4,4,4,4);
126                 c.insets = ic;
127                 pane.add(cancel, c);
128
129                 pack();
130                 setLocationRelativeTo(owner);
131                 setVisible(true);
132         }
133
134         public void addActionListener (ActionListener l) {
135                 cancel.addActionListener(l);
136         }
137
138         private void set_value_internal(String state_name, int in_state, int in_block) {
139                 int block = in_block;
140                 int state = in_state;
141
142                 if (block > 100)
143                         block = 100;
144                 if (state < min_state) state = min_state;
145                 if (state >= max_state) state = max_state - 1;
146                 state -= min_state;
147
148                 int pos = state * 100 + block;
149
150                 pbar.setString(state_name);
151                 pbar.setValue(pos);
152         }
153
154         public void set_value(String in_state_name, int in_state, int in_block) {
155                 final String state_name = in_state_name;
156                 final int state = in_state;
157                 final int block = in_block;
158                 Runnable r = new Runnable() {
159                                 public void run() {
160                                         try {
161                                                 set_value_internal(state_name, state, block);
162                                         } catch (Exception ex) {
163                                         }
164                                 }
165                         };
166                 SwingUtilities.invokeLater(r);
167         }
168
169         private void set_serial_internal(int serial) {
170                 serial_value.setText(String.format("%d", serial));
171         }
172
173         public void set_serial(int in_serial) {
174                 final int serial = in_serial;
175                 Runnable r = new Runnable() {
176                                 public void run() {
177                                         try {
178                                                 set_serial_internal(serial);
179                                         } catch (Exception ex) {
180                                         }
181                                 }
182                         };
183                 SwingUtilities.invokeLater(r);
184         }
185
186         private void set_flight_internal(int flight) {
187                 flight_value.setText(String.format("%d", flight));
188         }
189
190         public void set_flight(int in_flight) {
191                 final int flight = in_flight;
192                 Runnable r = new Runnable() {
193                                 public void run() {
194                                         try {
195                                                 set_flight_internal(flight);
196                                         } catch (Exception ex) {
197                                         }
198                                 }
199                         };
200                 SwingUtilities.invokeLater(r);
201         }
202
203         private void set_file_internal(String file) {
204                 file_value.setText(String.format("%s", file));
205         }
206
207         public void set_file(String in_file) {
208                 final String file = in_file;
209                 Runnable r = new Runnable() {
210                                 public void run() {
211                                         try {
212                                                 set_file_internal(file);
213                                         } catch (Exception ex) {
214                                         }
215                                 }
216                         };
217                 SwingUtilities.invokeLater(r);
218         }
219
220         private void done_internal() {
221                 setVisible(false);
222                 dispose();
223         }
224
225         public void done() {
226                 Runnable r = new Runnable() {
227                                 public void run() {
228                                         try {
229                                                 done_internal();
230                                         } catch (Exception ex) {
231                                         }
232                                 }
233                         };
234                 SwingUtilities.invokeLater(r);
235         }
236
237         private void reset_internal() {
238                 set_value_internal("startup",min_state,0);
239                 set_flight_internal(0);
240                 set_file_internal("");
241         }
242
243         public void reset() {
244                 Runnable r = new Runnable() {
245                                 public void run() {
246                                         try {
247                                                 reset_internal();
248                                         } catch (Exception ex) {
249                                         }
250                                 }
251                         };
252                 SwingUtilities.invokeLater(r);
253         }
254 }