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.
21 import java.awt.event.*;
23 import org.altusmetrum.altosuilib_1.*;
24 import org.altusmetrum.altoslib_3.*;
26 public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMonitor {
38 int min_state, max_state;
39 ActionListener listener;
41 public AltosEepromMonitorUI(JFrame owner) {
42 super (owner, "Download Flight Data", false);
47 Insets il = new Insets(4,4,4,4);
48 Insets ir = new Insets(4,4,4,4);
50 pane = getContentPane();
51 pane.setLayout(new GridBagLayout());
53 c = new GridBagConstraints();
54 c.gridx = 0; c.gridy = 0;
55 c.fill = GridBagConstraints.NONE;
56 c.anchor = GridBagConstraints.LINE_START;
58 serial_label = new JLabel("Serial:");
59 pane.add(serial_label, c);
61 c = new GridBagConstraints();
62 c.gridx = 1; c.gridy = 0;
63 c.fill = GridBagConstraints.HORIZONTAL;
65 c.anchor = GridBagConstraints.LINE_START;
67 serial_value = new JLabel("");
68 pane.add(serial_value, c);
70 c = new GridBagConstraints();
71 c.fill = GridBagConstraints.NONE;
72 c.gridx = 0; c.gridy = 1;
73 c.anchor = GridBagConstraints.LINE_START;
75 flight_label = new JLabel("Flight:");
76 pane.add(flight_label, c);
78 c = new GridBagConstraints();
79 c.fill = GridBagConstraints.HORIZONTAL;
81 c.gridx = 1; c.gridy = 1;
82 c.anchor = GridBagConstraints.LINE_START;
84 flight_value = new JLabel("");
85 pane.add(flight_value, c);
87 c = new GridBagConstraints();
88 c.fill = GridBagConstraints.NONE;
89 c.gridx = 0; c.gridy = 2;
90 c.anchor = GridBagConstraints.LINE_START;
92 file_label = new JLabel("File:");
93 pane.add(file_label, c);
95 c = new GridBagConstraints();
96 c.fill = GridBagConstraints.HORIZONTAL;
98 c.gridx = 1; c.gridy = 2;
99 c.anchor = GridBagConstraints.LINE_START;
101 file_value = new JLabel("");
102 pane.add(file_value, c);
104 pbar = new JProgressBar();
106 pbar.setMaximum(1000);
108 pbar.setString("startup");
109 pbar.setStringPainted(true);
110 pbar.setPreferredSize(new Dimension(600, 20));
111 c = new GridBagConstraints();
112 c.fill = GridBagConstraints.HORIZONTAL;
113 c.anchor = GridBagConstraints.CENTER;
114 c.gridx = 0; c.gridy = 3;
115 c.gridwidth = GridBagConstraints.REMAINDER;
116 Insets ib = new Insets(4,4,4,4);
121 cancel = new JButton("Cancel");
122 c = new GridBagConstraints();
123 c.fill = GridBagConstraints.NONE;
124 c.anchor = GridBagConstraints.CENTER;
125 c.gridx = 0; c.gridy = 4;
126 c.gridwidth = GridBagConstraints.REMAINDER;
127 Insets ic = new Insets(4,4,4,4);
132 setLocationRelativeTo(owner);
135 public void addActionListener(ActionListener l) {
139 public void set_states(int min_state, int max_state) {
140 this.min_state = min_state;
141 this.max_state = max_state;
144 public void set_thread(Thread in_eeprom_thread) {
145 final Thread eeprom_thread = in_eeprom_thread;
146 cancel.addActionListener(new ActionListener() {
147 public void actionPerformed(ActionEvent e) {
148 if (eeprom_thread != null)
149 eeprom_thread.interrupt();
154 public void start() {
158 private void set_value_internal(String state_name, int state, int state_block, int block) {
159 if (state_block > 100)
161 if (state < min_state) state = min_state;
162 if (state >= max_state) state = max_state - 1;
165 int pos = state * 100 + state_block;
167 pbar.setString(String.format("block %d state %s", block, state_name));
171 public void set_value(String in_state_name, int in_state, int in_state_block, int in_block) {
172 final String state_name = in_state_name;
173 final int state = in_state;
174 final int state_block = in_state_block;
175 final int block = in_block;
176 Runnable r = new Runnable() {
179 set_value_internal(state_name, state, state_block, block);
180 } catch (Exception ex) {
184 SwingUtilities.invokeLater(r);
187 private void set_serial_internal(int serial) {
188 serial_value.setText(String.format("%d", serial));
191 public void set_serial(int in_serial) {
192 final int serial = in_serial;
193 Runnable r = new Runnable() {
196 set_serial_internal(serial);
197 } catch (Exception ex) {
201 SwingUtilities.invokeLater(r);
204 private void set_flight_internal(int flight) {
205 flight_value.setText(String.format("%d", flight));
208 public void set_flight(int in_flight) {
209 final int flight = in_flight;
210 Runnable r = new Runnable() {
213 set_flight_internal(flight);
214 } catch (Exception ex) {
218 SwingUtilities.invokeLater(r);
221 private void set_filename_internal(String filename) {
222 file_value.setText(String.format("%s", filename));
225 public void set_filename(String in_filename) {
226 final String filename = in_filename;
227 Runnable r = new Runnable() {
230 set_filename_internal(filename);
231 } catch (Exception ex) {
235 SwingUtilities.invokeLater(r);
238 private void done_internal(boolean success) {
239 listener.actionPerformed(new ActionEvent(this,
246 public void done(boolean in_success) {
247 final boolean success = in_success;
248 Runnable r = new Runnable() {
251 done_internal(success);
252 } catch (Exception ex) {
256 SwingUtilities.invokeLater(r);
259 private void reset_internal() {
260 set_value_internal("startup",min_state,0, 0);
261 set_flight_internal(0);
262 set_filename_internal("");
265 public void reset() {
266 Runnable r = new Runnable() {
270 } catch (Exception ex) {
274 SwingUtilities.invokeLater(r);
277 private void show_message_internal(String message, String title, int message_type) {
278 int joption_message_type = JOptionPane.ERROR_MESSAGE;
280 switch (message_type) {
282 joption_message_type = JOptionPane.INFORMATION_MESSAGE;
284 case WARNING_MESSAGE:
285 joption_message_type = JOptionPane.WARNING_MESSAGE;
288 joption_message_type = JOptionPane.ERROR_MESSAGE;
291 JOptionPane.showMessageDialog(owner,
294 joption_message_type);
297 public void show_message(String in_message, String in_title, int in_message_type) {
298 final String message = in_message;
299 final String title = in_title;
300 final int message_type = in_message_type;
301 Runnable r = new Runnable() {
304 show_message_internal(message, title, message_type);
305 } catch (Exception ex) {
309 SwingUtilities.invokeLater(r);