2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altosuilib_11;
22 import java.awt.event.*;
24 import org.altusmetrum.altoslib_11.*;
26 public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMonitor {
38 int min_state, max_state;
39 ActionListener listener;
41 static final int progress_max = 10000;
43 public AltosEepromMonitorUI(JFrame owner) {
44 super (owner, "Download Flight Data", false);
49 Insets il = new Insets(4,4,4,4);
50 Insets ir = new Insets(4,4,4,4);
52 pane = getContentPane();
53 pane.setLayout(new GridBagLayout());
55 c = new GridBagConstraints();
56 c.gridx = 0; c.gridy = 0;
57 c.fill = GridBagConstraints.NONE;
58 c.anchor = GridBagConstraints.LINE_START;
60 serial_label = new JLabel("Serial:");
61 pane.add(serial_label, c);
63 c = new GridBagConstraints();
64 c.gridx = 1; c.gridy = 0;
65 c.fill = GridBagConstraints.HORIZONTAL;
67 c.anchor = GridBagConstraints.LINE_START;
69 serial_value = new JLabel("");
70 pane.add(serial_value, c);
72 c = new GridBagConstraints();
73 c.fill = GridBagConstraints.NONE;
74 c.gridx = 0; c.gridy = 1;
75 c.anchor = GridBagConstraints.LINE_START;
77 flight_label = new JLabel("Flight:");
78 pane.add(flight_label, c);
80 c = new GridBagConstraints();
81 c.fill = GridBagConstraints.HORIZONTAL;
83 c.gridx = 1; c.gridy = 1;
84 c.anchor = GridBagConstraints.LINE_START;
86 flight_value = new JLabel("");
87 pane.add(flight_value, c);
89 c = new GridBagConstraints();
90 c.fill = GridBagConstraints.NONE;
91 c.gridx = 0; c.gridy = 2;
92 c.anchor = GridBagConstraints.LINE_START;
94 file_label = new JLabel("File:");
95 pane.add(file_label, c);
97 c = new GridBagConstraints();
98 c.fill = GridBagConstraints.HORIZONTAL;
100 c.gridx = 1; c.gridy = 2;
101 c.anchor = GridBagConstraints.LINE_START;
103 file_value = new JLabel("");
104 pane.add(file_value, c);
106 pbar = new JProgressBar();
108 pbar.setMaximum(progress_max);
110 pbar.setString("startup");
111 pbar.setStringPainted(true);
112 pbar.setPreferredSize(new Dimension(600, 20));
113 c = new GridBagConstraints();
114 c.fill = GridBagConstraints.HORIZONTAL;
115 c.anchor = GridBagConstraints.CENTER;
116 c.gridx = 0; c.gridy = 3;
117 c.gridwidth = GridBagConstraints.REMAINDER;
118 Insets ib = new Insets(4,4,4,4);
123 cancel = new JButton("Cancel");
124 c = new GridBagConstraints();
125 c.fill = GridBagConstraints.NONE;
126 c.anchor = GridBagConstraints.CENTER;
127 c.gridx = 0; c.gridy = 4;
128 c.gridwidth = GridBagConstraints.REMAINDER;
129 Insets ic = new Insets(4,4,4,4);
134 setLocationRelativeTo(owner);
137 public void addActionListener(ActionListener l) {
141 public void set_states(int min_state, int max_state) {
142 this.min_state = min_state;
143 this.max_state = max_state;
146 public void set_thread(Thread in_eeprom_thread) {
147 final Thread eeprom_thread = in_eeprom_thread;
148 cancel.addActionListener(new ActionListener() {
149 public void actionPerformed(ActionEvent e) {
150 if (eeprom_thread != null)
151 eeprom_thread.interrupt();
156 public void start() {
160 private void set_value_internal(String state_name, int state, int state_block, int block) {
164 if (min_state == AltosLib.ao_flight_invalid) {
168 pos = lblock / 1000.0;
169 s = String.format("block %d", block);
171 if (state == AltosLib.ao_flight_invalid)
173 if (state_block > 100)
175 if (state < min_state) state = min_state;
176 if (state > max_state) state = max_state;
178 if (state == max_state)
183 int nstate = max_state - min_state;
185 double spos = (double) state / (double) nstate;
186 double bpos = state_block / 100.0;
188 pos = spos + bpos / nstate;
190 s = String.format("block %d state %s", block, state_name);
194 pbar.setValue((int) (pos * progress_max));
197 public void set_value(String in_state_name, int in_state, int in_state_block, int in_block) {
198 final String state_name = in_state_name;
199 final int state = in_state;
200 final int state_block = in_state_block;
201 final int block = in_block;
202 Runnable r = new Runnable() {
205 set_value_internal(state_name, state, state_block, block);
206 } catch (Exception ex) {
210 SwingUtilities.invokeLater(r);
213 private void set_serial_internal(int serial) {
214 serial_value.setText(String.format("%d", serial));
217 public void set_serial(int in_serial) {
218 final int serial = in_serial;
219 Runnable r = new Runnable() {
222 set_serial_internal(serial);
223 } catch (Exception ex) {
227 SwingUtilities.invokeLater(r);
230 private void set_flight_internal(int flight) {
231 flight_value.setText(String.format("%d", flight));
234 public void set_flight(int in_flight) {
235 final int flight = in_flight;
236 Runnable r = new Runnable() {
239 set_flight_internal(flight);
240 } catch (Exception ex) {
244 SwingUtilities.invokeLater(r);
247 private void set_filename_internal(String filename) {
248 file_value.setText(String.format("%s", filename));
251 public void set_filename(String in_filename) {
252 final String filename = in_filename;
253 Runnable r = new Runnable() {
256 set_filename_internal(filename);
257 } catch (Exception ex) {
261 SwingUtilities.invokeLater(r);
264 private void done_internal(boolean success) {
265 listener.actionPerformed(new ActionEvent(this,
272 public void done(boolean in_success) {
273 final boolean success = in_success;
274 Runnable r = new Runnable() {
277 done_internal(success);
278 } catch (Exception ex) {
282 SwingUtilities.invokeLater(r);
285 private void reset_internal() {
286 set_value_internal("startup",min_state,0, 0);
287 set_flight_internal(0);
288 set_filename_internal("");
291 public void reset() {
292 Runnable r = new Runnable() {
296 } catch (Exception ex) {
300 SwingUtilities.invokeLater(r);
303 private void show_message_internal(String message, String title, int message_type) {
304 int joption_message_type = JOptionPane.ERROR_MESSAGE;
306 switch (message_type) {
308 joption_message_type = JOptionPane.INFORMATION_MESSAGE;
310 case WARNING_MESSAGE:
311 joption_message_type = JOptionPane.WARNING_MESSAGE;
314 joption_message_type = JOptionPane.ERROR_MESSAGE;
317 JOptionPane.showMessageDialog(owner,
320 joption_message_type);
323 public void show_message(String in_message, String in_title, int in_message_type) {
324 final String message = in_message;
325 final String title = in_title;
326 final int message_type = in_message_type;
327 Runnable r = new Runnable() {
330 show_message_internal(message, title, message_type);
331 } catch (Exception ex) {
335 SwingUtilities.invokeLater(r);