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_14;
23 import java.util.concurrent.*;
25 import java.awt.event.*;
27 import org.altusmetrum.altoslib_14.*;
33 public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMonitor {
44 ActionListener listener;
46 static final int progress_max = 10000;
48 public AltosEepromMonitorUI(JFrame owner) {
49 super (owner, "Download Flight Data", false);
51 setMinimumSize(new Dimension(600, 100));
56 Insets il = new Insets(4,4,4,4);
57 Insets ir = new Insets(4,4,4,4);
59 pane = getContentPane();
60 pane.setLayout(new GridBagLayout());
62 c = new GridBagConstraints();
63 c.gridx = 0; c.gridy = 0;
64 c.fill = GridBagConstraints.NONE;
65 c.anchor = GridBagConstraints.LINE_START;
67 serial_label = new JLabel("Serial:");
68 pane.add(serial_label, c);
70 c = new GridBagConstraints();
71 c.gridx = 1; c.gridy = 0;
72 c.fill = GridBagConstraints.HORIZONTAL;
74 c.anchor = GridBagConstraints.LINE_START;
76 serial_value = new JLabel("");
77 pane.add(serial_value, c);
79 c = new GridBagConstraints();
80 c.fill = GridBagConstraints.NONE;
81 c.gridx = 0; c.gridy = 1;
82 c.anchor = GridBagConstraints.LINE_START;
84 flight_label = new JLabel("Flight:");
85 pane.add(flight_label, c);
87 c = new GridBagConstraints();
88 c.fill = GridBagConstraints.HORIZONTAL;
90 c.gridx = 1; c.gridy = 1;
91 c.anchor = GridBagConstraints.LINE_START;
93 flight_value = new JLabel("");
94 pane.add(flight_value, c);
96 pbar = new JProgressBar();
98 pbar.setMaximum(progress_max);
99 pbar.setStringPainted(true);
100 set_block_internal(0);
101 c = new GridBagConstraints();
102 c.fill = GridBagConstraints.HORIZONTAL;
103 c.anchor = GridBagConstraints.CENTER;
104 c.gridx = 0; c.gridy = 3;
105 c.gridwidth = GridBagConstraints.REMAINDER;
106 Insets ib = new Insets(4,4,4,4);
110 cancel = new JButton("Cancel");
111 c = new GridBagConstraints();
112 c.fill = GridBagConstraints.NONE;
113 c.anchor = GridBagConstraints.CENTER;
114 c.gridx = 0; c.gridy = 4;
115 c.gridwidth = GridBagConstraints.REMAINDER;
116 Insets ic = new Insets(4,4,4,4);
121 setLocationRelativeTo(owner);
124 public void addActionListener(ActionListener l) {
128 public void set_thread(Thread in_eeprom_thread) {
129 final Thread eeprom_thread = in_eeprom_thread;
130 cancel.addActionListener(new ActionListener() {
131 public void actionPerformed(ActionEvent e) {
132 if (eeprom_thread != null) {
133 eeprom_thread.interrupt();
139 public void start() {
145 public void set_block_internal(int block) {
149 pos = (double) block / (double) max_block;
151 s = String.format("block %d of %d", block, max_block);
154 pbar.setStringPainted(true);
155 pbar.setValue((int) (pos * progress_max));
158 public void set_max(int max_block) {
159 this.max_block = max_block;
162 public void set_block(int in_block) {
163 final int block = in_block;
164 Runnable r = new Runnable() {
167 set_block_internal(block);
168 } catch (Exception ex) {
172 SwingUtilities.invokeLater(r);
175 private void set_serial_internal(int serial) {
176 serial_value.setText(String.format("%d", serial));
179 public void set_serial(int in_serial) {
180 final int serial = in_serial;
181 Runnable r = new Runnable() {
184 set_serial_internal(serial);
185 } catch (Exception ex) {
189 SwingUtilities.invokeLater(r);
192 private void set_flight_internal(int flight) {
193 flight_value.setText(String.format("%d", flight));
196 public void set_flight(int in_flight) {
197 final int flight = in_flight;
198 Runnable r = new Runnable() {
201 set_flight_internal(flight);
202 } catch (Exception ex) {
206 SwingUtilities.invokeLater(r);
209 private void done_internal(boolean success) {
210 listener.actionPerformed(new ActionEvent(this,
217 public void done(boolean in_success) {
218 final boolean success = in_success;
219 Runnable r = new Runnable() {
222 done_internal(success);
223 } catch (Exception ex) {
227 SwingUtilities.invokeLater(r);
230 private void reset_internal() {
232 set_block_internal(0);
233 set_flight_internal(0);
236 public void reset() {
237 Runnable r = new Runnable() {
241 } catch (Exception ex) {
245 SwingUtilities.invokeLater(r);
248 private void show_message_internal(String message, String title, int message_type) {
249 int joption_message_type = JOptionPane.ERROR_MESSAGE;
251 switch (message_type) {
253 joption_message_type = JOptionPane.INFORMATION_MESSAGE;
255 case WARNING_MESSAGE:
256 joption_message_type = JOptionPane.WARNING_MESSAGE;
259 joption_message_type = JOptionPane.ERROR_MESSAGE;
262 JOptionPane.showMessageDialog(owner,
265 joption_message_type);
268 public Boolean check_overwrite(File in_file) {
269 final Semaphore check_overwrite_done = new Semaphore(0);
270 final File file = in_file;
271 final result_holder result = new result_holder();
273 Runnable r = new Runnable() {
275 result_holder.result = JOptionPane.showConfirmDialog(owner,
276 String.format("\"%s\" already exists, overwrite?",
278 "Overwrite Existing File?",
279 JOptionPane.YES_NO_OPTION);
280 check_overwrite_done.release();
284 SwingUtilities.invokeLater(r);
286 check_overwrite_done.acquire();
287 } catch (Exception e) {}
288 return result_holder.result == JOptionPane.YES_OPTION;
291 public void show_message(String in_message, String in_title, int in_message_type) {
292 final String message = in_message;
293 final String title = in_title;
294 final int message_type = in_message_type;
295 Runnable r = new Runnable() {
298 show_message_internal(message, title, message_type);
299 } catch (Exception ex) {
303 SwingUtilities.invokeLater(r);