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; version 2 of the License.
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.
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.
18 package org.altusmetrum.altosuilib_3;
21 import java.awt.event.*;
23 import org.altusmetrum.altoslib_5.*;
25 public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMonitor {
37 int min_state, max_state;
38 ActionListener listener;
40 static final int progress_max = 10000;
42 public AltosEepromMonitorUI(JFrame owner) {
43 super (owner, "Download Flight Data", false);
48 Insets il = new Insets(4,4,4,4);
49 Insets ir = new Insets(4,4,4,4);
51 pane = getContentPane();
52 pane.setLayout(new GridBagLayout());
54 c = new GridBagConstraints();
55 c.gridx = 0; c.gridy = 0;
56 c.fill = GridBagConstraints.NONE;
57 c.anchor = GridBagConstraints.LINE_START;
59 serial_label = new JLabel("Serial:");
60 pane.add(serial_label, c);
62 c = new GridBagConstraints();
63 c.gridx = 1; c.gridy = 0;
64 c.fill = GridBagConstraints.HORIZONTAL;
66 c.anchor = GridBagConstraints.LINE_START;
68 serial_value = new JLabel("");
69 pane.add(serial_value, c);
71 c = new GridBagConstraints();
72 c.fill = GridBagConstraints.NONE;
73 c.gridx = 0; c.gridy = 1;
74 c.anchor = GridBagConstraints.LINE_START;
76 flight_label = new JLabel("Flight:");
77 pane.add(flight_label, c);
79 c = new GridBagConstraints();
80 c.fill = GridBagConstraints.HORIZONTAL;
82 c.gridx = 1; c.gridy = 1;
83 c.anchor = GridBagConstraints.LINE_START;
85 flight_value = new JLabel("");
86 pane.add(flight_value, c);
88 c = new GridBagConstraints();
89 c.fill = GridBagConstraints.NONE;
90 c.gridx = 0; c.gridy = 2;
91 c.anchor = GridBagConstraints.LINE_START;
93 file_label = new JLabel("File:");
94 pane.add(file_label, c);
96 c = new GridBagConstraints();
97 c.fill = GridBagConstraints.HORIZONTAL;
99 c.gridx = 1; c.gridy = 2;
100 c.anchor = GridBagConstraints.LINE_START;
102 file_value = new JLabel("");
103 pane.add(file_value, c);
105 pbar = new JProgressBar();
107 pbar.setMaximum(progress_max);
109 pbar.setString("startup");
110 pbar.setStringPainted(true);
111 pbar.setPreferredSize(new Dimension(600, 20));
112 c = new GridBagConstraints();
113 c.fill = GridBagConstraints.HORIZONTAL;
114 c.anchor = GridBagConstraints.CENTER;
115 c.gridx = 0; c.gridy = 3;
116 c.gridwidth = GridBagConstraints.REMAINDER;
117 Insets ib = new Insets(4,4,4,4);
122 cancel = new JButton("Cancel");
123 c = new GridBagConstraints();
124 c.fill = GridBagConstraints.NONE;
125 c.anchor = GridBagConstraints.CENTER;
126 c.gridx = 0; c.gridy = 4;
127 c.gridwidth = GridBagConstraints.REMAINDER;
128 Insets ic = new Insets(4,4,4,4);
133 setLocationRelativeTo(owner);
136 public void addActionListener(ActionListener l) {
140 public void set_states(int min_state, int max_state) {
141 this.min_state = min_state;
142 this.max_state = max_state;
145 public void set_thread(Thread in_eeprom_thread) {
146 final Thread eeprom_thread = in_eeprom_thread;
147 cancel.addActionListener(new ActionListener() {
148 public void actionPerformed(ActionEvent e) {
149 if (eeprom_thread != null)
150 eeprom_thread.interrupt();
155 public void start() {
159 private void set_value_internal(String state_name, int state, int state_block, int block) {
163 if (min_state == AltosLib.ao_flight_invalid) {
167 pos = lblock / 1000.0;
168 s = String.format("block %d", block);
170 if (state_block > 100)
172 if (state < min_state) state = min_state;
173 if (state >= max_state) state = max_state - 1;
176 int nstate = max_state - min_state;
178 double spos = (double) (state - min_state) / (double) nstate;
179 double bpos = state_block / 100.0;
181 pos = spos + bpos / nstate;
183 s = String.format("block %d state %s", block, state_name);
187 pbar.setValue((int) (pos * progress_max));
190 public void set_value(String in_state_name, int in_state, int in_state_block, int in_block) {
191 final String state_name = in_state_name;
192 final int state = in_state;
193 final int state_block = in_state_block;
194 final int block = in_block;
195 Runnable r = new Runnable() {
198 set_value_internal(state_name, state, state_block, block);
199 } catch (Exception ex) {
203 SwingUtilities.invokeLater(r);
206 private void set_serial_internal(int serial) {
207 serial_value.setText(String.format("%d", serial));
210 public void set_serial(int in_serial) {
211 final int serial = in_serial;
212 Runnable r = new Runnable() {
215 set_serial_internal(serial);
216 } catch (Exception ex) {
220 SwingUtilities.invokeLater(r);
223 private void set_flight_internal(int flight) {
224 flight_value.setText(String.format("%d", flight));
227 public void set_flight(int in_flight) {
228 final int flight = in_flight;
229 Runnable r = new Runnable() {
232 set_flight_internal(flight);
233 } catch (Exception ex) {
237 SwingUtilities.invokeLater(r);
240 private void set_filename_internal(String filename) {
241 file_value.setText(String.format("%s", filename));
244 public void set_filename(String in_filename) {
245 final String filename = in_filename;
246 Runnable r = new Runnable() {
249 set_filename_internal(filename);
250 } catch (Exception ex) {
254 SwingUtilities.invokeLater(r);
257 private void done_internal(boolean success) {
258 listener.actionPerformed(new ActionEvent(this,
265 public void done(boolean in_success) {
266 final boolean success = in_success;
267 Runnable r = new Runnable() {
270 done_internal(success);
271 } catch (Exception ex) {
275 SwingUtilities.invokeLater(r);
278 private void reset_internal() {
279 set_value_internal("startup",min_state,0, 0);
280 set_flight_internal(0);
281 set_filename_internal("");
284 public void reset() {
285 Runnable r = new Runnable() {
289 } catch (Exception ex) {
293 SwingUtilities.invokeLater(r);
296 private void show_message_internal(String message, String title, int message_type) {
297 int joption_message_type = JOptionPane.ERROR_MESSAGE;
299 switch (message_type) {
301 joption_message_type = JOptionPane.INFORMATION_MESSAGE;
303 case WARNING_MESSAGE:
304 joption_message_type = JOptionPane.WARNING_MESSAGE;
307 joption_message_type = JOptionPane.ERROR_MESSAGE;
310 JOptionPane.showMessageDialog(owner,
313 joption_message_type);
316 public void show_message(String in_message, String in_title, int in_message_type) {
317 final String message = in_message;
318 final String title = in_title;
319 final int message_type = in_message_type;
320 Runnable r = new Runnable() {
323 show_message_internal(message, title, message_type);
324 } catch (Exception ex) {
328 SwingUtilities.invokeLater(r);