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