]> git.gag.com Git - fw/altos/blob - altosui/AltosEepromMonitor.java
altosdroid: convert spaces to tabs in strings.xml
[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_1.*;
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(1000);
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 state, int state_block, int block) {
139                 if (state_block > 100)
140                         state_block = 100;
141                 if (state < min_state) state = min_state;
142                 if (state >= max_state) state = max_state - 1;
143                 state -= min_state;
144
145                 int pos = state * 100 + state_block;
146
147                 pbar.setString(String.format("block %d state %s", block, state_name));
148                 pbar.setValue(pos);
149         }
150
151         public void set_value(String in_state_name, int in_state, int in_state_block, int in_block) {
152                 final String state_name = in_state_name;
153                 final int state = in_state;
154                 final int state_block = in_state_block;
155                 final int block = in_block;
156                 Runnable r = new Runnable() {
157                                 public void run() {
158                                         try {
159                                                 set_value_internal(state_name, state, state_block, block);
160                                         } catch (Exception ex) {
161                                         }
162                                 }
163                         };
164                 SwingUtilities.invokeLater(r);
165         }
166
167         private void set_serial_internal(int serial) {
168                 serial_value.setText(String.format("%d", serial));
169         }
170
171         public void set_serial(int in_serial) {
172                 final int serial = in_serial;
173                 Runnable r = new Runnable() {
174                                 public void run() {
175                                         try {
176                                                 set_serial_internal(serial);
177                                         } catch (Exception ex) {
178                                         }
179                                 }
180                         };
181                 SwingUtilities.invokeLater(r);
182         }
183
184         private void set_flight_internal(int flight) {
185                 flight_value.setText(String.format("%d", flight));
186         }
187
188         public void set_flight(int in_flight) {
189                 final int flight = in_flight;
190                 Runnable r = new Runnable() {
191                                 public void run() {
192                                         try {
193                                                 set_flight_internal(flight);
194                                         } catch (Exception ex) {
195                                         }
196                                 }
197                         };
198                 SwingUtilities.invokeLater(r);
199         }
200
201         private void set_file_internal(String file) {
202                 file_value.setText(String.format("%s", file));
203         }
204
205         public void set_file(String in_file) {
206                 final String file = in_file;
207                 Runnable r = new Runnable() {
208                                 public void run() {
209                                         try {
210                                                 set_file_internal(file);
211                                         } catch (Exception ex) {
212                                         }
213                                 }
214                         };
215                 SwingUtilities.invokeLater(r);
216         }
217
218         private void done_internal() {
219                 setVisible(false);
220                 dispose();
221         }
222
223         public void done() {
224                 Runnable r = new Runnable() {
225                                 public void run() {
226                                         try {
227                                                 done_internal();
228                                         } catch (Exception ex) {
229                                         }
230                                 }
231                         };
232                 SwingUtilities.invokeLater(r);
233         }
234
235         private void reset_internal() {
236                 set_value_internal("startup",min_state,0, 0);
237                 set_flight_internal(0);
238                 set_file_internal("");
239         }
240
241         public void reset() {
242                 Runnable r = new Runnable() {
243                                 public void run() {
244                                         try {
245                                                 reset_internal();
246                                         } catch (Exception ex) {
247                                         }
248                                 }
249                         };
250                 SwingUtilities.invokeLater(r);
251         }
252 }