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 public AltosEepromMonitorUI(JFrame owner) {
41 super (owner, "Download Flight Data", false);
46 Insets il = new Insets(4,4,4,4);
47 Insets ir = new Insets(4,4,4,4);
49 pane = getContentPane();
50 pane.setLayout(new GridBagLayout());
52 c = new GridBagConstraints();
53 c.gridx = 0; c.gridy = 0;
54 c.fill = GridBagConstraints.NONE;
55 c.anchor = GridBagConstraints.LINE_START;
57 serial_label = new JLabel("Serial:");
58 pane.add(serial_label, c);
60 c = new GridBagConstraints();
61 c.gridx = 1; c.gridy = 0;
62 c.fill = GridBagConstraints.HORIZONTAL;
64 c.anchor = GridBagConstraints.LINE_START;
66 serial_value = new JLabel("");
67 pane.add(serial_value, c);
69 c = new GridBagConstraints();
70 c.fill = GridBagConstraints.NONE;
71 c.gridx = 0; c.gridy = 1;
72 c.anchor = GridBagConstraints.LINE_START;
74 flight_label = new JLabel("Flight:");
75 pane.add(flight_label, c);
77 c = new GridBagConstraints();
78 c.fill = GridBagConstraints.HORIZONTAL;
80 c.gridx = 1; c.gridy = 1;
81 c.anchor = GridBagConstraints.LINE_START;
83 flight_value = new JLabel("");
84 pane.add(flight_value, c);
86 c = new GridBagConstraints();
87 c.fill = GridBagConstraints.NONE;
88 c.gridx = 0; c.gridy = 2;
89 c.anchor = GridBagConstraints.LINE_START;
91 file_label = new JLabel("File:");
92 pane.add(file_label, c);
94 c = new GridBagConstraints();
95 c.fill = GridBagConstraints.HORIZONTAL;
97 c.gridx = 1; c.gridy = 2;
98 c.anchor = GridBagConstraints.LINE_START;
100 file_value = new JLabel("");
101 pane.add(file_value, c);
103 pbar = new JProgressBar();
105 pbar.setMaximum(1000);
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);
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);
131 setLocationRelativeTo(owner);
134 public void addActionListener(ActionListener l) {
138 public void set_states(int min_state, int max_state) {
139 this.min_state = min_state;
140 this.max_state = max_state;
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();
153 public void start() {
157 private void set_value_internal(String state_name, int state, int state_block, int block) {
158 if (state_block > 100)
160 if (state < min_state) state = min_state;
161 if (state >= max_state) state = max_state - 1;
164 int pos = state * 100 + state_block;
166 pbar.setString(String.format("block %d state %s", block, state_name));
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() {
178 set_value_internal(state_name, state, state_block, block);
179 } catch (Exception ex) {
183 SwingUtilities.invokeLater(r);
186 private void set_serial_internal(int serial) {
187 serial_value.setText(String.format("%d", serial));
190 public void set_serial(int in_serial) {
191 final int serial = in_serial;
192 Runnable r = new Runnable() {
195 set_serial_internal(serial);
196 } catch (Exception ex) {
200 SwingUtilities.invokeLater(r);
203 private void set_flight_internal(int flight) {
204 flight_value.setText(String.format("%d", flight));
207 public void set_flight(int in_flight) {
208 final int flight = in_flight;
209 Runnable r = new Runnable() {
212 set_flight_internal(flight);
213 } catch (Exception ex) {
217 SwingUtilities.invokeLater(r);
220 private void set_filename_internal(String filename) {
221 file_value.setText(String.format("%s", filename));
224 public void set_filename(String in_filename) {
225 final String filename = in_filename;
226 Runnable r = new Runnable() {
229 set_filename_internal(filename);
230 } catch (Exception ex) {
234 SwingUtilities.invokeLater(r);
237 private void done_internal(boolean success) {
238 listener.actionPerformed(new ActionEvent(this,
245 public void done(boolean in_success) {
246 final boolean success = in_success;
247 Runnable r = new Runnable() {
250 done_internal(success);
251 } catch (Exception ex) {
255 SwingUtilities.invokeLater(r);
258 private void reset_internal() {
259 set_value_internal("startup",min_state,0, 0);
260 set_flight_internal(0);
261 set_filename_internal("");
264 public void reset() {
265 Runnable r = new Runnable() {
269 } catch (Exception ex) {
273 SwingUtilities.invokeLater(r);
276 private void show_message_internal(String message, String title, int message_type) {
277 int joption_message_type = JOptionPane.ERROR_MESSAGE;
279 switch (message_type) {
281 joption_message_type = JOptionPane.INFORMATION_MESSAGE;
283 case WARNING_MESSAGE:
284 joption_message_type = JOptionPane.WARNING_MESSAGE;
287 joption_message_type = JOptionPane.ERROR_MESSAGE;
290 JOptionPane.showMessageDialog(owner,
293 joption_message_type);
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() {
303 show_message_internal(message, title, message_type);
304 } catch (Exception ex) {
308 SwingUtilities.invokeLater(r);