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