telegps: Add graph display
[fw/altos] / altosuilib / 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 org.altusmetrum.altosuilib_2;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import org.altusmetrum.altoslib_4.*;
24
25 public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMonitor {
26         JFrame          owner;
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         ActionListener  listener;
39
40         public AltosEepromMonitorUI(JFrame owner) {
41                 super (owner, "Download Flight Data", false);
42
43                 this.owner = owner;
44
45                 GridBagConstraints c;
46                 Insets il = new Insets(4,4,4,4);
47                 Insets ir = new Insets(4,4,4,4);
48
49                 pane = getContentPane();
50                 pane.setLayout(new GridBagLayout());
51
52                 c = new GridBagConstraints();
53                 c.gridx = 0; c.gridy = 0;
54                 c.fill = GridBagConstraints.NONE;
55                 c.anchor = GridBagConstraints.LINE_START;
56                 c.insets = il;
57                 serial_label = new JLabel("Serial:");
58                 pane.add(serial_label, c);
59
60                 c = new GridBagConstraints();
61                 c.gridx = 1; c.gridy = 0;
62                 c.fill = GridBagConstraints.HORIZONTAL;
63                 c.weightx = 1;
64                 c.anchor = GridBagConstraints.LINE_START;
65                 c.insets = ir;
66                 serial_value = new JLabel("");
67                 pane.add(serial_value, c);
68
69                 c = new GridBagConstraints();
70                 c.fill = GridBagConstraints.NONE;
71                 c.gridx = 0; c.gridy = 1;
72                 c.anchor = GridBagConstraints.LINE_START;
73                 c.insets = il;
74                 flight_label = new JLabel("Flight:");
75                 pane.add(flight_label, c);
76
77                 c = new GridBagConstraints();
78                 c.fill = GridBagConstraints.HORIZONTAL;
79                 c.weightx = 1;
80                 c.gridx = 1; c.gridy = 1;
81                 c.anchor = GridBagConstraints.LINE_START;
82                 c.insets = ir;
83                 flight_value = new JLabel("");
84                 pane.add(flight_value, c);
85
86                 c = new GridBagConstraints();
87                 c.fill = GridBagConstraints.NONE;
88                 c.gridx = 0; c.gridy = 2;
89                 c.anchor = GridBagConstraints.LINE_START;
90                 c.insets = il;
91                 file_label = new JLabel("File:");
92                 pane.add(file_label, c);
93
94                 c = new GridBagConstraints();
95                 c.fill = GridBagConstraints.HORIZONTAL;
96                 c.weightx = 1;
97                 c.gridx = 1; c.gridy = 2;
98                 c.anchor = GridBagConstraints.LINE_START;
99                 c.insets = ir;
100                 file_value = new JLabel("");
101                 pane.add(file_value, c);
102
103                 pbar = new JProgressBar();
104                 pbar.setMinimum(0);
105                 pbar.setMaximum(1000);
106                 pbar.setValue(0);
107                 pbar.setString("startup");
108                 pbar.setStringPainted(true);
109                 pbar.setPreferredSize(new Dimension(600, 20));
110                 c = new GridBagConstraints();
111                 c.fill = GridBagConstraints.HORIZONTAL;
112                 c.anchor = GridBagConstraints.CENTER;
113                 c.gridx = 0; c.gridy = 3;
114                 c.gridwidth = GridBagConstraints.REMAINDER;
115                 Insets ib = new Insets(4,4,4,4);
116                 c.insets = ib;
117                 pane.add(pbar, c);
118
119
120                 cancel = new JButton("Cancel");
121                 c = new GridBagConstraints();
122                 c.fill = GridBagConstraints.NONE;
123                 c.anchor = GridBagConstraints.CENTER;
124                 c.gridx = 0; c.gridy = 4;
125                 c.gridwidth = GridBagConstraints.REMAINDER;
126                 Insets ic = new Insets(4,4,4,4);
127                 c.insets = ic;
128                 pane.add(cancel, c);
129
130                 pack();
131                 setLocationRelativeTo(owner);
132         }
133
134         public void addActionListener(ActionListener l) {
135                 listener = l;
136         }
137
138         public void set_states(int min_state, int max_state) {
139                 this.min_state = min_state;
140                 this.max_state = max_state;
141         }
142
143         public void set_thread(Thread in_eeprom_thread) {
144                 final Thread eeprom_thread = in_eeprom_thread;
145                 cancel.addActionListener(new ActionListener() {
146                                 public void actionPerformed(ActionEvent e) {
147                                         if (eeprom_thread != null)
148                                                 eeprom_thread.interrupt();
149                                 }
150                         });
151         }
152
153         public void start() {
154                 setVisible(true);
155         }
156
157         private void set_value_internal(String state_name, int state, int state_block, int block) {
158                 if (state_block > 100)
159                         state_block = 100;
160                 if (state < min_state) state = min_state;
161                 if (state >= max_state) state = max_state - 1;
162                 state -= min_state;
163
164                 int pos = state * 100 + state_block;
165
166                 pbar.setString(String.format("block %d state %s", block, state_name));
167                 pbar.setValue(pos);
168         }
169
170         public void set_value(String in_state_name, int in_state, int in_state_block, int in_block) {
171                 final String state_name = in_state_name;
172                 final int state = in_state;
173                 final int state_block = in_state_block;
174                 final int block = in_block;
175                 Runnable r = new Runnable() {
176                                 public void run() {
177                                         try {
178                                                 set_value_internal(state_name, state, state_block, block);
179                                         } catch (Exception ex) {
180                                         }
181                                 }
182                         };
183                 SwingUtilities.invokeLater(r);
184         }
185
186         private void set_serial_internal(int serial) {
187                 serial_value.setText(String.format("%d", serial));
188         }
189
190         public void set_serial(int in_serial) {
191                 final int serial = in_serial;
192                 Runnable r = new Runnable() {
193                                 public void run() {
194                                         try {
195                                                 set_serial_internal(serial);
196                                         } catch (Exception ex) {
197                                         }
198                                 }
199                         };
200                 SwingUtilities.invokeLater(r);
201         }
202
203         private void set_flight_internal(int flight) {
204                 flight_value.setText(String.format("%d", flight));
205         }
206
207         public void set_flight(int in_flight) {
208                 final int flight = in_flight;
209                 Runnable r = new Runnable() {
210                                 public void run() {
211                                         try {
212                                                 set_flight_internal(flight);
213                                         } catch (Exception ex) {
214                                         }
215                                 }
216                         };
217                 SwingUtilities.invokeLater(r);
218         }
219
220         private void set_filename_internal(String filename) {
221                 file_value.setText(String.format("%s", filename));
222         }
223
224         public void set_filename(String in_filename) {
225                 final String filename = in_filename;
226                 Runnable r = new Runnable() {
227                                 public void run() {
228                                         try {
229                                                 set_filename_internal(filename);
230                                         } catch (Exception ex) {
231                                         }
232                                 }
233                         };
234                 SwingUtilities.invokeLater(r);
235         }
236
237         private void done_internal(boolean success) {
238                 listener.actionPerformed(new ActionEvent(this,
239                                                          success ? 1 : 0,
240                                                          "download"));
241                 setVisible(false);
242                 dispose();
243         }
244
245         public void done(boolean in_success) {
246                 final boolean success = in_success;
247                 Runnable r = new Runnable() {
248                                 public void run() {
249                                         try {
250                                                 done_internal(success);
251                                         } catch (Exception ex) {
252                                         }
253                                 }
254                         };
255                 SwingUtilities.invokeLater(r);
256         }
257
258         private void reset_internal() {
259                 set_value_internal("startup",min_state,0, 0);
260                 set_flight_internal(0);
261                 set_filename_internal("");
262         }
263
264         public void reset() {
265                 Runnable r = new Runnable() {
266                                 public void run() {
267                                         try {
268                                                 reset_internal();
269                                         } catch (Exception ex) {
270                                         }
271                                 }
272                         };
273                 SwingUtilities.invokeLater(r);
274         }
275
276         private void show_message_internal(String message, String title, int message_type) {
277                 int joption_message_type = JOptionPane.ERROR_MESSAGE;
278
279                 switch (message_type) {
280                 case INFO_MESSAGE:
281                         joption_message_type = JOptionPane.INFORMATION_MESSAGE;
282                         break;
283                 case WARNING_MESSAGE:
284                         joption_message_type = JOptionPane.WARNING_MESSAGE;
285                         break;
286                 case ERROR_MESSAGE:
287                         joption_message_type = JOptionPane.ERROR_MESSAGE;
288                         break;
289                 }
290                 JOptionPane.showMessageDialog(owner,
291                                               message,
292                                               title,
293                                               joption_message_type);
294         }
295
296         public void show_message(String in_message, String in_title, int in_message_type) {
297                 final String message = in_message;
298                 final String title = in_title;
299                 final int message_type = in_message_type;
300                 Runnable r = new Runnable() {
301                                 public void run() {
302                                         try {
303                                                 show_message_internal(message, title, message_type);
304                                         } catch (Exception ex) {
305                                         }
306                                 }
307                         };
308                 SwingUtilities.invokeLater(r);
309         }
310 }