ddd1a564d1ef3acd6370f6a0523754d715fc6792
[fw/altos] / altosui / AltosEepromMonitorUI.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 import org.altusmetrum.altoslib_2.*;
25
26 public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMonitor {
27
28         Container       pane;
29         Box             box;
30         JLabel          serial_label;
31         JLabel          flight_label;
32         JLabel          file_label;
33         JLabel          serial_value;
34         JLabel          flight_value;
35         JLabel          file_value;
36         JButton         cancel;
37         JProgressBar    pbar;
38         int             min_state, max_state;
39
40         public AltosEepromMonitorUI(JFrame owner) {
41                 super (owner, "Download Flight Data", false);
42
43                 GridBagConstraints c;
44                 Insets il = new Insets(4,4,4,4);
45                 Insets ir = new Insets(4,4,4,4);
46
47                 pane = getContentPane();
48                 pane.setLayout(new GridBagLayout());
49
50                 c = new GridBagConstraints();
51                 c.gridx = 0; c.gridy = 0;
52                 c.fill = GridBagConstraints.NONE;
53                 c.anchor = GridBagConstraints.LINE_START;
54                 c.insets = il;
55                 serial_label = new JLabel("Serial:");
56                 pane.add(serial_label, c);
57
58                 c = new GridBagConstraints();
59                 c.gridx = 1; c.gridy = 0;
60                 c.fill = GridBagConstraints.HORIZONTAL;
61                 c.weightx = 1;
62                 c.anchor = GridBagConstraints.LINE_START;
63                 c.insets = ir;
64                 serial_value = new JLabel("");
65                 pane.add(serial_value, c);
66
67                 c = new GridBagConstraints();
68                 c.fill = GridBagConstraints.NONE;
69                 c.gridx = 0; c.gridy = 1;
70                 c.anchor = GridBagConstraints.LINE_START;
71                 c.insets = il;
72                 flight_label = new JLabel("Flight:");
73                 pane.add(flight_label, c);
74
75                 c = new GridBagConstraints();
76                 c.fill = GridBagConstraints.HORIZONTAL;
77                 c.weightx = 1;
78                 c.gridx = 1; c.gridy = 1;
79                 c.anchor = GridBagConstraints.LINE_START;
80                 c.insets = ir;
81                 flight_value = new JLabel("");
82                 pane.add(flight_value, c);
83
84                 c = new GridBagConstraints();
85                 c.fill = GridBagConstraints.NONE;
86                 c.gridx = 0; c.gridy = 2;
87                 c.anchor = GridBagConstraints.LINE_START;
88                 c.insets = il;
89                 file_label = new JLabel("File:");
90                 pane.add(file_label, c);
91
92                 c = new GridBagConstraints();
93                 c.fill = GridBagConstraints.HORIZONTAL;
94                 c.weightx = 1;
95                 c.gridx = 1; c.gridy = 2;
96                 c.anchor = GridBagConstraints.LINE_START;
97                 c.insets = ir;
98                 file_value = new JLabel("");
99                 pane.add(file_value, c);
100
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         }
131
132         public void set_states(int min_state, int max_state) {
133                 this.min_state = min_state;
134                 this.max_state = max_state;
135         }
136
137         public void set_thread(Thread in_eeprom_thread) {
138                 final Thread eeprom_thread = in_eeprom_thread;
139                 addActionListener(new ActionListener() {
140                                 public void actionPerformed(ActionEvent e) {
141                                         if (eeprom_thread != null)
142                                                 eeprom_thread.interrupt();
143                                 }
144                         });
145         }
146
147         public void start() {
148                 setVisible(true);
149         }
150
151         public void addActionListener (ActionListener l) {
152                 cancel.addActionListener(l);
153         }
154
155         private void set_value_internal(String state_name, int state, int state_block, int block) {
156                 if (state_block > 100)
157                         state_block = 100;
158                 if (state < min_state) state = min_state;
159                 if (state >= max_state) state = max_state - 1;
160                 state -= min_state;
161
162                 int pos = state * 100 + state_block;
163
164                 pbar.setString(String.format("block %d state %s", block, state_name));
165                 pbar.setValue(pos);
166         }
167
168         public void set_value(String in_state_name, int in_state, int in_state_block, int in_block) {
169                 final String state_name = in_state_name;
170                 final int state = in_state;
171                 final int state_block = in_state_block;
172                 final int block = in_block;
173                 Runnable r = new Runnable() {
174                                 public void run() {
175                                         try {
176                                                 set_value_internal(state_name, state, state_block, block);
177                                         } catch (Exception ex) {
178                                         }
179                                 }
180                         };
181                 SwingUtilities.invokeLater(r);
182         }
183
184         private void set_serial_internal(int serial) {
185                 serial_value.setText(String.format("%d", serial));
186         }
187
188         public void set_serial(int in_serial) {
189                 final int serial = in_serial;
190                 Runnable r = new Runnable() {
191                                 public void run() {
192                                         try {
193                                                 set_serial_internal(serial);
194                                         } catch (Exception ex) {
195                                         }
196                                 }
197                         };
198                 SwingUtilities.invokeLater(r);
199         }
200
201         private void set_flight_internal(int flight) {
202                 flight_value.setText(String.format("%d", flight));
203         }
204
205         public void set_flight(int in_flight) {
206                 final int flight = in_flight;
207                 Runnable r = new Runnable() {
208                                 public void run() {
209                                         try {
210                                                 set_flight_internal(flight);
211                                         } catch (Exception ex) {
212                                         }
213                                 }
214                         };
215                 SwingUtilities.invokeLater(r);
216         }
217
218         private void set_filename_internal(String filename) {
219                 file_value.setText(String.format("%s", filename));
220         }
221
222         public void set_filename(String in_filename) {
223                 final String filename = in_filename;
224                 Runnable r = new Runnable() {
225                                 public void run() {
226                                         try {
227                                                 set_filename_internal(filename);
228                                         } catch (Exception ex) {
229                                         }
230                                 }
231                         };
232                 SwingUtilities.invokeLater(r);
233         }
234
235         private void done_internal() {
236                 setVisible(false);
237                 dispose();
238         }
239
240         public void done() {
241                 Runnable r = new Runnable() {
242                                 public void run() {
243                                         try {
244                                                 done_internal();
245                                         } catch (Exception ex) {
246                                         }
247                                 }
248                         };
249                 SwingUtilities.invokeLater(r);
250         }
251
252         private void reset_internal() {
253                 set_value_internal("startup",min_state,0, 0);
254                 set_flight_internal(0);
255                 set_filename_internal("");
256         }
257
258         public void reset() {
259                 Runnable r = new Runnable() {
260                                 public void run() {
261                                         try {
262                                                 reset_internal();
263                                         } catch (Exception ex) {
264                                         }
265                                 }
266                         };
267                 SwingUtilities.invokeLater(r);
268         }
269 }